summaryrefslogtreecommitdiffstats
path: root/examples/xml/dombookmarks/xbeltree.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/xml/dombookmarks/xbeltree.cpp')
-rw-r--r--examples/xml/dombookmarks/xbeltree.cpp150
1 files changed, 60 insertions, 90 deletions
diff --git a/examples/xml/dombookmarks/xbeltree.cpp b/examples/xml/dombookmarks/xbeltree.cpp
index cc2feea936..eff2fea8f0 100644
--- a/examples/xml/dombookmarks/xbeltree.cpp
+++ b/examples/xml/dombookmarks/xbeltree.cpp
@@ -1,77 +1,41 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the examples of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** BSD License Usage
-** Alternatively, you may use this file under the terms of the BSD license
-** as follows:
-**
-** "Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are
-** met:
-** * Redistributions of source code must retain the above copyright
-** notice, this list of conditions and the following disclaimer.
-** * Redistributions in binary form must reproduce the above copyright
-** notice, this list of conditions and the following disclaimer in
-** the documentation and/or other materials provided with the
-** distribution.
-** * Neither the name of The Qt Company Ltd nor the names of its
-** contributors may be used to endorse or promote products derived
-** from this software without specific prior written permission.
-**
-**
-** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#include <QtWidgets>
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "xbeltree.h"
+#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;
+
enum { DomElementRole = Qt::UserRole + 1 };
Q_DECLARE_METATYPE(QDomElement)
-static inline QString titleElement() { return QStringLiteral("title"); }
-static inline QString folderElement() { return QStringLiteral("folder"); }
-static inline QString bookmarkElement() { return QStringLiteral("bookmark"); }
+static const auto titleElement = u"title"_s;
+static const auto folderElement = u"folder"_s;
+static const auto bookmarkElement = u"bookmark"_s;
-static inline QString versionAttribute() { return QStringLiteral("version"); }
-static inline QString hrefAttribute() { return QStringLiteral("href"); }
-static inline QString foldedAttribute() { return QStringLiteral("folded"); }
+static const auto versionAttribute = u"version"_s;
+static const auto hrefAttribute = u"href"_s;
+static const auto foldedAttribute = u"folded"_s;
+//! [0]
XbelTree::XbelTree(QWidget *parent)
: QTreeWidget(parent)
{
- QStringList labels;
- labels << tr("Title") << tr("Location");
-
header()->setSectionResizeMode(QHeaderView::Stretch);
- setHeaderLabels(labels);
+ setHeaderLabels({tr("Title"), tr("Location")});
folderIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon),
QIcon::Normal, QIcon::Off);
@@ -79,8 +43,9 @@ XbelTree::XbelTree(QWidget *parent)
QIcon::Normal, QIcon::On);
bookmarkIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon));
}
+//! [0]
-#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
+#if QT_CONFIG(clipboard) && QT_CONFIG(contextmenu)
void XbelTree::contextMenuEvent(QContextMenuEvent *event)
{
const QTreeWidgetItem *item = itemAt(event->pos());
@@ -96,21 +61,19 @@ void XbelTree::contextMenuEvent(QContextMenuEvent *event)
else if (action == openAction)
QDesktopServices::openUrl(QUrl(url));
}
-#endif // !QT_NO_CONTEXTMENU && !QT_NO_CLIPBOARD
+#endif // QT_CONFIG(clipboard) && QT_CONFIG(contextmenu)
+//! [1]
bool XbelTree::read(QIODevice *device)
{
- QString errorStr;
- int errorLine;
- int errorColumn;
-
- if (!domDocument.setContent(device, true, &errorStr, &errorLine,
- &errorColumn)) {
+ QDomDocument::ParseResult result =
+ domDocument.setContent(device, QDomDocument::ParseOption::UseNamespaceProcessing);
+ if (!result) {
QMessageBox::information(window(), tr("DOM Bookmarks"),
tr("Parse error at line %1, column %2:\n%3")
- .arg(errorLine)
- .arg(errorColumn)
- .arg(errorStr));
+ .arg(result.errorLine)
+ .arg(result.errorColumn)
+ .arg(result.errorMessage));
return false;
}
@@ -119,8 +82,8 @@ bool XbelTree::read(QIODevice *device)
QMessageBox::information(window(), tr("DOM Bookmarks"),
tr("The file is not an XBEL file."));
return false;
- } else if (root.hasAttribute(versionAttribute())
- && root.attribute(versionAttribute()) != QLatin1String("1.0")) {
+ } else if (root.hasAttribute(versionAttribute)
+ && root.attribute(versionAttribute) != "1.0"_L1) {
QMessageBox::information(window(), tr("DOM Bookmarks"),
tr("The file is not an XBEL version 1.0 "
"file."));
@@ -131,17 +94,19 @@ bool XbelTree::read(QIODevice *device)
disconnect(this, &QTreeWidget::itemChanged, this, &XbelTree::updateDomElement);
- QDomElement child = root.firstChildElement(folderElement());
+ QDomElement child = root.firstChildElement(folderElement);
while (!child.isNull()) {
parseFolderElement(child);
- child = child.nextSiblingElement(folderElement());
+ child = child.nextSiblingElement(folderElement);
}
connect(this, &QTreeWidget::itemChanged, this, &XbelTree::updateDomElement);
return true;
}
+//! [1]
+//! [2]
bool XbelTree::write(QIODevice *device) const
{
const int IndentSize = 4;
@@ -150,65 +115,70 @@ bool XbelTree::write(QIODevice *device) const
domDocument.save(out, IndentSize);
return true;
}
+//! [2]
void XbelTree::updateDomElement(const QTreeWidgetItem *item, int column)
{
QDomElement element = qvariant_cast<QDomElement>(item->data(0, DomElementRole));
if (!element.isNull()) {
if (column == 0) {
- QDomElement oldTitleElement = element.firstChildElement(titleElement());
- QDomElement newTitleElement = domDocument.createElement(titleElement());
+ QDomElement oldTitleElement = element.firstChildElement(titleElement);
+ QDomElement newTitleElement = domDocument.createElement(titleElement);
QDomText newTitleText = domDocument.createTextNode(item->text(0));
newTitleElement.appendChild(newTitleText);
element.replaceChild(newTitleElement, oldTitleElement);
} else {
- if (element.tagName() == bookmarkElement())
- element.setAttribute(hrefAttribute(), item->text(1));
+ if (element.tagName() == bookmarkElement)
+ element.setAttribute(hrefAttribute, item->text(1));
}
}
}
+//! [3]
void XbelTree::parseFolderElement(const QDomElement &element,
QTreeWidgetItem *parentItem)
{
QTreeWidgetItem *item = createItem(element, parentItem);
- QString title = element.firstChildElement(titleElement()).text();
+ QString title = element.firstChildElement(titleElement).text();
if (title.isEmpty())
- title = QObject::tr("Folder");
+ title = tr("Folder");
item->setFlags(item->flags() | Qt::ItemIsEditable);
item->setIcon(0, folderIcon);
item->setText(0, title);
- bool folded = (element.attribute(foldedAttribute()) != QLatin1String("no"));
+ bool folded = (element.attribute(foldedAttribute) != "no"_L1);
item->setExpanded(!folded);
+ constexpr char16_t midDot = u'\xB7';
+ static const QString dots = QString(30, midDot);
QDomElement child = element.firstChildElement();
while (!child.isNull()) {
- if (child.tagName() == folderElement()) {
+ if (child.tagName() == folderElement) {
parseFolderElement(child, item);
- } else if (child.tagName() == bookmarkElement()) {
+ } else if (child.tagName() == bookmarkElement) {
QTreeWidgetItem *childItem = createItem(child, item);
- QString title = child.firstChildElement(titleElement()).text();
+ QString title = child.firstChildElement(titleElement).text();
if (title.isEmpty())
- title = QObject::tr("Folder");
+ title = tr("Folder");
childItem->setFlags(item->flags() | Qt::ItemIsEditable);
childItem->setIcon(0, bookmarkIcon);
childItem->setText(0, title);
- childItem->setText(1, child.attribute(hrefAttribute()));
- } else if (child.tagName() == QLatin1String("separator")) {
+ childItem->setText(1, child.attribute(hrefAttribute));
+ } else if (child.tagName() == "separator"_L1) {
QTreeWidgetItem *childItem = createItem(child, item);
childItem->setFlags(item->flags() & ~(Qt::ItemIsSelectable | Qt::ItemIsEditable));
- childItem->setText(0, QString(30, u'\xB7'));
+ childItem->setText(0, dots);
}
child = child.nextSiblingElement();
}
}
+//! [3]
QTreeWidgetItem *XbelTree::createItem(const QDomElement &element,
QTreeWidgetItem *parentItem)