2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "xbeltree.h"
|
|
|
|
|
2023-06-12 09:48:21 +00:00
|
|
|
#include <QHeaderView>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#if QT_CONFIG(clipboard) && QT_CONFIG(contextmenu)
|
|
|
|
# include <QClipboard>
|
|
|
|
# include <QContextMenuEvent>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2016-10-31 10:09:00 +00:00
|
|
|
enum { DomElementRole = Qt::UserRole + 1 };
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(QDomElement)
|
|
|
|
|
2023-06-12 09:48:21 +00:00
|
|
|
static const auto titleElement = u"title"_s;
|
|
|
|
static const auto folderElement = u"folder"_s;
|
|
|
|
static const auto bookmarkElement = u"bookmark"_s;
|
2016-10-31 10:09:00 +00:00
|
|
|
|
2023-06-12 09:48:21 +00:00
|
|
|
static const auto versionAttribute = u"version"_s;
|
|
|
|
static const auto hrefAttribute = u"href"_s;
|
|
|
|
static const auto foldedAttribute = u"folded"_s;
|
2016-10-31 10:09:00 +00:00
|
|
|
|
2023-04-12 14:22:24 +00:00
|
|
|
//! [0]
|
2011-04-27 10:05:43 +00:00
|
|
|
XbelTree::XbelTree(QWidget *parent)
|
|
|
|
: QTreeWidget(parent)
|
|
|
|
{
|
2012-05-06 10:54:24 +00:00
|
|
|
header()->setSectionResizeMode(QHeaderView::Stretch);
|
2023-06-12 09:48:21 +00:00
|
|
|
setHeaderLabels({tr("Title"), tr("Location")});
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
folderIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon),
|
|
|
|
QIcon::Normal, QIcon::Off);
|
|
|
|
folderIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon),
|
|
|
|
QIcon::Normal, QIcon::On);
|
|
|
|
bookmarkIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon));
|
|
|
|
}
|
2023-04-12 14:22:24 +00:00
|
|
|
//! [0]
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2023-06-12 09:48:21 +00:00
|
|
|
#if QT_CONFIG(clipboard) && QT_CONFIG(contextmenu)
|
2016-10-31 10:09:00 +00:00
|
|
|
void XbelTree::contextMenuEvent(QContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
const QTreeWidgetItem *item = itemAt(event->pos());
|
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
const QString url = item->text(1);
|
|
|
|
QMenu contextMenu;
|
|
|
|
QAction *copyAction = contextMenu.addAction(tr("Copy Link to Clipboard"));
|
|
|
|
QAction *openAction = contextMenu.addAction(tr("Open"));
|
|
|
|
QAction *action = contextMenu.exec(event->globalPos());
|
|
|
|
if (action == copyAction)
|
|
|
|
QGuiApplication::clipboard()->setText(url);
|
|
|
|
else if (action == openAction)
|
|
|
|
QDesktopServices::openUrl(QUrl(url));
|
|
|
|
}
|
2023-06-12 09:48:21 +00:00
|
|
|
#endif // QT_CONFIG(clipboard) && QT_CONFIG(contextmenu)
|
2016-10-31 10:09:00 +00:00
|
|
|
|
2023-04-12 14:22:24 +00:00
|
|
|
//! [1]
|
2011-04-27 10:05:43 +00:00
|
|
|
bool XbelTree::read(QIODevice *device)
|
|
|
|
{
|
2022-06-30 11:34:44 +00:00
|
|
|
QDomDocument::ParseResult result =
|
|
|
|
domDocument.setContent(device, QDomDocument::ParseOption::UseNamespaceProcessing);
|
|
|
|
if (!result) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QMessageBox::information(window(), tr("DOM Bookmarks"),
|
|
|
|
tr("Parse error at line %1, column %2:\n%3")
|
2022-06-30 11:34:44 +00:00
|
|
|
.arg(result.errorLine)
|
|
|
|
.arg(result.errorColumn)
|
|
|
|
.arg(result.errorMessage));
|
2011-04-27 10:05:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDomElement root = domDocument.documentElement();
|
|
|
|
if (root.tagName() != "xbel") {
|
|
|
|
QMessageBox::information(window(), tr("DOM Bookmarks"),
|
|
|
|
tr("The file is not an XBEL file."));
|
|
|
|
return false;
|
2023-06-12 09:48:21 +00:00
|
|
|
} else if (root.hasAttribute(versionAttribute)
|
|
|
|
&& root.attribute(versionAttribute) != "1.0"_L1) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QMessageBox::information(window(), tr("DOM Bookmarks"),
|
|
|
|
tr("The file is not an XBEL version 1.0 "
|
|
|
|
"file."));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
clear();
|
|
|
|
|
2016-10-31 10:09:00 +00:00
|
|
|
disconnect(this, &QTreeWidget::itemChanged, this, &XbelTree::updateDomElement);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2023-06-12 09:48:21 +00:00
|
|
|
QDomElement child = root.firstChildElement(folderElement);
|
2011-04-27 10:05:43 +00:00
|
|
|
while (!child.isNull()) {
|
|
|
|
parseFolderElement(child);
|
2023-06-12 09:48:21 +00:00
|
|
|
child = child.nextSiblingElement(folderElement);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2016-10-31 10:09:00 +00:00
|
|
|
connect(this, &QTreeWidget::itemChanged, this, &XbelTree::updateDomElement);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-04-12 14:22:24 +00:00
|
|
|
//! [1]
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2023-04-12 14:22:24 +00:00
|
|
|
//! [2]
|
2016-10-31 10:09:00 +00:00
|
|
|
bool XbelTree::write(QIODevice *device) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
const int IndentSize = 4;
|
|
|
|
|
|
|
|
QTextStream out(device);
|
|
|
|
domDocument.save(out, IndentSize);
|
|
|
|
return true;
|
|
|
|
}
|
2023-04-12 14:22:24 +00:00
|
|
|
//! [2]
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-10-31 10:09:00 +00:00
|
|
|
void XbelTree::updateDomElement(const QTreeWidgetItem *item, int column)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2019-12-06 12:19:37 +00:00
|
|
|
QDomElement element = qvariant_cast<QDomElement>(item->data(0, DomElementRole));
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!element.isNull()) {
|
|
|
|
if (column == 0) {
|
2023-06-12 09:48:21 +00:00
|
|
|
QDomElement oldTitleElement = element.firstChildElement(titleElement);
|
|
|
|
QDomElement newTitleElement = domDocument.createElement(titleElement);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QDomText newTitleText = domDocument.createTextNode(item->text(0));
|
|
|
|
newTitleElement.appendChild(newTitleText);
|
|
|
|
|
|
|
|
element.replaceChild(newTitleElement, oldTitleElement);
|
|
|
|
} else {
|
2023-06-12 09:48:21 +00:00
|
|
|
if (element.tagName() == bookmarkElement)
|
|
|
|
element.setAttribute(hrefAttribute, item->text(1));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:22:24 +00:00
|
|
|
//! [3]
|
2011-04-27 10:05:43 +00:00
|
|
|
void XbelTree::parseFolderElement(const QDomElement &element,
|
|
|
|
QTreeWidgetItem *parentItem)
|
|
|
|
{
|
|
|
|
QTreeWidgetItem *item = createItem(element, parentItem);
|
|
|
|
|
2023-06-12 09:48:21 +00:00
|
|
|
QString title = element.firstChildElement(titleElement).text();
|
2011-04-27 10:05:43 +00:00
|
|
|
if (title.isEmpty())
|
2023-06-12 09:48:21 +00:00
|
|
|
title = tr("Folder");
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
|
|
|
item->setIcon(0, folderIcon);
|
|
|
|
item->setText(0, title);
|
|
|
|
|
2023-06-12 09:48:21 +00:00
|
|
|
bool folded = (element.attribute(foldedAttribute) != "no"_L1);
|
Fix some deprecation warnings in examples
googlesuggest.cpp:163:36: warning: ‘void QTreeWidgetItem::setTextColor(int, const QColor&)’ is deprecated: Use QTreeWidgetItem::setForeground() instead [-Wdeprecated-declarations]
xbeltree.cpp:187:34: warning: ‘void QTreeWidget::setItemExpanded(const QTreeWidgetItem*, bool)’ is deprecated: Use QTreeWidgetItem::setExpanded() instead [-Wdeprecated-declarations]
imageitem.cpp:114:21: warning: ‘void QGraphicsItem::setMatrix(const QMatrix&, bool)’ is deprecated: Use setTransform() instead [-Wdeprecated-declarations]
xbelreader.cpp:143:48: warning: ‘void QTreeWidget::setItemExpanded(const QTreeWidgetItem*, bool)’ is deprecated: Use QTreeWidgetItem::setExpanded() instead [-Wdeprecated-declarations]
xbelgenerator.cpp:103:55: warning: ‘bool QTreeWidget::isItemExpanded(const QTreeWidgetItem*) const’ is deprecated: Use QTreeWidgetItem::isExpanded() instead [-Wdeprecated-declarations]
xbelwriter.cpp:90:55: warning: ‘bool QTreeWidget::isItemExpanded(const QTreeWidgetItem*) const’ is deprecated: Use QTreeWidgetItem::isExpanded() instead [-Wdeprecated-declarations]
xbelhandler.cpp:97:50: warning: ‘void QTreeWidget::setItemExpanded(const QTreeWidgetItem*, bool)’ is deprecated: Use QTreeWidgetItem::setExpanded() instead [-Wdeprecated-declarations]
node.cpp:180:60: warning: ‘QColor QColor::light(int) const’ is deprecated: Use QColor::lighter() instead [-Wdeprecated-declarations]
node.cpp:181:64: warning: ‘QColor QColor::light(int) const’ is deprecated: Use QColor::lighter() instead [-Wdeprecated-declarations]
chip.cpp:82:81: warning: ‘QColor QColor::dark(int) const’ is deprecated: Use QColor::darker() instead [-Wdeprecated-declarations]
chip.cpp:84:40: warning: ‘QColor QColor::light(int) const’ is deprecated: Use QColor::lighter() instead [-Wdeprecated-declarations]
chip.cpp:108:93: warning: ‘QColor QColor::dark(int) const’ is deprecated: Use QColor::darker() instead [-Wdeprecated-declarations]
roundrectitem.cpp:65:42: warning: ‘QColor QColor::dark(int) const’ is deprecated: Use QColor::darker() instead [-Wdeprecated-declarations]
roundrectitem.cpp:97:51: warning: ‘void QPainter::drawRoundRect(const QRectF&, int, int)’ is deprecated: Use drawRoundedRect(..., Qt::RelativeSize) instead [-Wdeprecated-declarations]
roundrectitem.cpp:105:34: warning: ‘void QPainter::drawRoundRect(const QRectF&, int, int)’ is deprecated: Use drawRoundedRect(..., Qt::RelativeSize) instead [-Wdeprecated-declarations]
splashitem.cpp:82:57: warning: ‘void QPainter::drawRoundRect(int, int, int, int, int, int)’ is deprecated: Use drawRoundedRect(..., Qt::RelativeSize) instead [-Wdeprecated-declarations]
robot.cpp:116:53: warning: ‘QColor QColor::light(int) const’ is deprecated: Use QColor::lighter() instead [-Wdeprecated-declarations]
robot.cpp:176:49: warning: ‘QColor QColor::light(int) const’ is deprecated: Use QColor::lighter() instead [-Wdeprecated-declarations]
robot.cpp:200:49: warning: ‘QColor QColor::light(int) const’ is deprecated: Use QColor::lighter() instead [-Wdeprecated-declarations]
mandelbrotwidget.cpp:120:41: warning: ‘const QMatrix& QPainter::matrix() const’ is deprecated: Use transform() instead [-Wdeprecated-declarations]
composition.cpp:344:47: warning: ‘QColor QColor::light(int) const’ is deprecated: Use QColor::lighter() instead [-Wdeprecated-declarations]
composition.cpp:346:46: warning: ‘QColor QColor::dark(int) const’ is deprecated: Use QColor::darker() instead [-Wdeprecated-declarations]
colorswatch.cpp:89:34: warning: ‘QColor QColor::light(int) const’ is deprecated: Use QColor::lighter() instead [-Wdeprecated-declarations]
mainwindow.cpp:81:62: warning: ‘void QTreeWidget::setItemSelected(const QTreeWidgetItem*, bool)’ is deprecated: Use QTreeWidgetItem::setSelected() instead [-Wdeprecated-declarations]
puzzlewidget.cpp:172:35: warning: ‘Qt::DropAction QDrag::start(Qt::DropActions)’ is deprecated: Use QDrag::exec() instead [-Wdeprecated-declarations]
spreadsheet.cpp:191:37: warning: ‘QColor QTableWidgetItem::backgroundColor() const’ is deprecated: Use QTableWidgetItem::background() instead [-Wdeprecated-declarations]
spreadsheet.cpp:198:32: warning: ‘QColor QColor::light(int) const’ is deprecated: Use QColor::lighter() instead [-Wdeprecated-declarations]
spreadsheet.cpp:203:24: warning: ‘QColor QColor::dark(int) const’ is deprecated: Use QColor::darker() instead [-Wdeprecated-declarations]
spreadsheet.cpp:238:47: warning: ‘QColor QTableWidgetItem::backgroundColor() const’ is deprecated: Use QTableWidgetItem::background() instead [-Wdeprecated-declarations]
spreadsheet.cpp:249:38: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
spreadsheet.cpp:494:58: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
spreadsheet.cpp:509:56: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
spreadsheet.cpp:513:58: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
spreadsheet.cpp:527:56: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
spreadsheet.cpp:531:58: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
spreadsheet.cpp:545:56: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
spreadsheet.cpp:549:58: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
spreadsheet.cpp:563:55: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
spreadsheet.cpp:567:58: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
spreadsheet.cpp:581:55: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
spreadsheet.cpp:585:58: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
spreadsheet.cpp:599:55: warning: ‘void QTableWidgetItem::setBackgroundColor(const QColor&)’ is deprecated: Use QTableWidgetItem::setBackground() instead [-Wdeprecated-declarations]
starrating.cpp:91:46: warning: ‘const QBrush& QPalette::foreground() const’ is deprecated: Use QPalette::windowText() instead [-Wdeprecated-declarations]
document.cpp:341:36: warning: ‘QColor QColor::dark(int) const’ is deprecated: Use QColor::darker() instead [-Wdeprecated-declarations]
document.cpp:342:39: warning: ‘QColor QColor::light(int) const’ is deprecated: Use QColor::lighter() instead [-Wdeprecated-declarations]
document.cpp:343:36: warning: ‘QColor QColor::dark(int) const’ is deprecated: Use QColor::darker() instead [-Wdeprecated-declarations]
norwegianwoodstyle.cpp:88:39: warning: ‘const QBrush& QPalette::background() const’ is deprecated: Use QPalette::window() instead [-Wdeprecated-declarations]
norwegianwoodstyle.cpp:89:39: warning: ‘QColor QColor::dark(int) const’ is deprecated: Use QColor::darker() instead [-Wdeprecated-declarations]
norwegianwoodstyle.cpp:188:52: warning: ‘const QBrush& QPalette::background() const’ is deprecated: Use QPalette::window() instead [-Wdeprecated-declarations]
norwegianwoodstyle.cpp:264:56: warning: ‘const QBrush& QPalette::foreground() const’ is deprecated: Use QPalette::windowText() instead [-Wdeprecated-declarations]
plugindialog.cpp:128:49: warning: ‘void QTreeWidget::setItemExpanded(const QTreeWidgetItem*, bool)’ is deprecated: Use QTreeWidgetItem::setExpanded() instead [-Wdeprecated-declarations]
tetrixboard.cpp:361:74: warning: ‘const QBrush& QPalette::background() const’ is deprecated: Use QPalette::window() instead [-Wdeprecated-declarations]
tetrixboard.cpp:408:32: warning: ‘QColor QColor::light(int) const’ is deprecated: Use QColor::lighter() instead [-Wdeprecated-declarations]
tetrixboard.cpp:412:31: warning: ‘QColor QColor::dark(int) const’ is deprecated: Use QColor::darker() instead [-Wdeprecated-declarations]
mandelbrotwidget.cpp:120:41: warning: ‘const QMatrix& QPainter::matrix() const’ is deprecated: Use transform() instead [-Wdeprecated-declarations]
Change-Id: If0afabbc35ef25f127f211c11699011d4ae4ae65
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
2019-02-06 09:52:23 +00:00
|
|
|
item->setExpanded(!folded);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2023-06-08 16:36:19 +00:00
|
|
|
constexpr char16_t midDot = u'\xB7';
|
|
|
|
static const QString dots = QString(30, midDot);
|
2011-04-27 10:05:43 +00:00
|
|
|
QDomElement child = element.firstChildElement();
|
|
|
|
while (!child.isNull()) {
|
2023-06-12 09:48:21 +00:00
|
|
|
if (child.tagName() == folderElement) {
|
2011-04-27 10:05:43 +00:00
|
|
|
parseFolderElement(child, item);
|
2023-06-12 09:48:21 +00:00
|
|
|
} else if (child.tagName() == bookmarkElement) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QTreeWidgetItem *childItem = createItem(child, item);
|
|
|
|
|
2023-06-12 09:48:21 +00:00
|
|
|
QString title = child.firstChildElement(titleElement).text();
|
2011-04-27 10:05:43 +00:00
|
|
|
if (title.isEmpty())
|
2023-06-12 09:48:21 +00:00
|
|
|
title = tr("Folder");
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
childItem->setFlags(item->flags() | Qt::ItemIsEditable);
|
|
|
|
childItem->setIcon(0, bookmarkIcon);
|
|
|
|
childItem->setText(0, title);
|
2023-06-12 09:48:21 +00:00
|
|
|
childItem->setText(1, child.attribute(hrefAttribute));
|
|
|
|
} else if (child.tagName() == "separator"_L1) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QTreeWidgetItem *childItem = createItem(child, item);
|
|
|
|
childItem->setFlags(item->flags() & ~(Qt::ItemIsSelectable | Qt::ItemIsEditable));
|
2023-06-08 16:36:19 +00:00
|
|
|
childItem->setText(0, dots);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
child = child.nextSiblingElement();
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 14:22:24 +00:00
|
|
|
//! [3]
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QTreeWidgetItem *XbelTree::createItem(const QDomElement &element,
|
|
|
|
QTreeWidgetItem *parentItem)
|
|
|
|
{
|
|
|
|
QTreeWidgetItem *item;
|
|
|
|
if (parentItem) {
|
|
|
|
item = new QTreeWidgetItem(parentItem);
|
|
|
|
} else {
|
|
|
|
item = new QTreeWidgetItem(this);
|
|
|
|
}
|
2016-10-31 10:09:00 +00:00
|
|
|
item->setData(0, DomElementRole, QVariant::fromValue(element));
|
2011-04-27 10:05:43 +00:00
|
|
|
return item;
|
|
|
|
}
|