summaryrefslogtreecommitdiffstats
path: root/examples/xml
diff options
context:
space:
mode:
Diffstat (limited to 'examples/xml')
-rw-r--r--examples/xml/CMakeLists.txt3
-rw-r--r--examples/xml/dombookmarks/CMakeLists.txt21
-rw-r--r--examples/xml/dombookmarks/doc/images/dombookmarks-example.pngbin19405 -> 0 bytes
-rw-r--r--examples/xml/dombookmarks/doc/images/screenshot.pngbin0 -> 67075 bytes
-rw-r--r--examples/xml/dombookmarks/doc/src/dombookmarks.qdoc89
-rw-r--r--examples/xml/dombookmarks/dombookmarks.pro2
-rw-r--r--examples/xml/dombookmarks/jennifer.xbel46
-rw-r--r--examples/xml/dombookmarks/mainwindow.cpp49
-rw-r--r--examples/xml/dombookmarks/mainwindow.h2
-rw-r--r--examples/xml/dombookmarks/xbeltree.cpp84
-rw-r--r--examples/xml/dombookmarks/xbeltree.h6
-rw-r--r--examples/xml/streambookmarks/CMakeLists.txt39
-rw-r--r--examples/xml/streambookmarks/doc/images/xmlstreamexample-filemenu.pngbin9380 -> 0 bytes
-rw-r--r--examples/xml/streambookmarks/doc/images/xmlstreamexample-helpmenu.pngbin10856 -> 0 bytes
-rw-r--r--examples/xml/streambookmarks/doc/images/xmlstreamexample-screenshot.pngbin22323 -> 0 bytes
-rw-r--r--examples/xml/streambookmarks/doc/src/qxmlstreambookmarks.qdoc170
-rw-r--r--examples/xml/streambookmarks/jennifer.xbel69
-rw-r--r--examples/xml/streambookmarks/main.cpp17
-rw-r--r--examples/xml/streambookmarks/mainwindow.cpp142
-rw-r--r--examples/xml/streambookmarks/mainwindow.h35
-rw-r--r--examples/xml/streambookmarks/streambookmarks.pro15
-rw-r--r--examples/xml/streambookmarks/xbelreader.cpp140
-rw-r--r--examples/xml/streambookmarks/xbelreader.h50
-rw-r--r--examples/xml/streambookmarks/xbelwriter.cpp60
-rw-r--r--examples/xml/streambookmarks/xbelwriter.h28
-rw-r--r--examples/xml/xml.pro3
26 files changed, 213 insertions, 857 deletions
diff --git a/examples/xml/CMakeLists.txt b/examples/xml/CMakeLists.txt
index d97185f647..aca12accfe 100644
--- a/examples/xml/CMakeLists.txt
+++ b/examples/xml/CMakeLists.txt
@@ -1,7 +1,6 @@
# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
if(TARGET Qt6::Widgets)
qt_internal_add_example(dombookmarks)
- qt_internal_add_example(streambookmarks)
endif()
diff --git a/examples/xml/dombookmarks/CMakeLists.txt b/examples/xml/dombookmarks/CMakeLists.txt
index d8277bc2e4..58efd94a55 100644
--- a/examples/xml/dombookmarks/CMakeLists.txt
+++ b/examples/xml/dombookmarks/CMakeLists.txt
@@ -1,15 +1,9 @@
# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(dombookmarks LANGUAGES CXX)
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/xml/dombookmarks")
-
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Xml)
qt_standard_project_setup()
@@ -33,7 +27,14 @@ target_link_libraries(dombookmarks PRIVATE
)
install(TARGETS dombookmarks
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION .
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+qt_generate_deploy_app_script(
+ TARGET dombookmarks
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
)
+install(SCRIPT ${deploy_script})
diff --git a/examples/xml/dombookmarks/doc/images/dombookmarks-example.png b/examples/xml/dombookmarks/doc/images/dombookmarks-example.png
deleted file mode 100644
index abacacbbfc..0000000000
--- a/examples/xml/dombookmarks/doc/images/dombookmarks-example.png
+++ /dev/null
Binary files differ
diff --git a/examples/xml/dombookmarks/doc/images/screenshot.png b/examples/xml/dombookmarks/doc/images/screenshot.png
new file mode 100644
index 0000000000..462052ce8a
--- /dev/null
+++ b/examples/xml/dombookmarks/doc/images/screenshot.png
Binary files differ
diff --git a/examples/xml/dombookmarks/doc/src/dombookmarks.qdoc b/examples/xml/dombookmarks/doc/src/dombookmarks.qdoc
index be9a41db1f..0b3ca2f47d 100644
--- a/examples/xml/dombookmarks/doc/src/dombookmarks.qdoc
+++ b/examples/xml/dombookmarks/doc/src/dombookmarks.qdoc
@@ -3,15 +3,94 @@
/*!
\example dombookmarks
- \title DOM Bookmarks Example
+ \title DOM Bookmarks Application
+ \examplecategory {Data Processing & I/O}
\ingroup xml-examples
+ \meta tag {xml}
\brief Provides a reader for XML Bookmark Exchange Language files.
- The DOM Bookmarks example provides a reader for XML Bookmark Exchange Language (XBEL)
- files that uses Qt's DOM-based XML API to read and parse the files. The SAX Bookmarks
- example provides an alternative way to read this type of file.
+ The DOM Bookmarks Application provides a reader for XML Bookmark Exchange
+ Language (XBEL) files that uses Qt's DOM-based XML API to read and parse
+ the files. The \l {QXmlStream Bookmarks Example} provides an alternative
+ way to read this type of file.
- \image dombookmarks-example.png
+ \image screenshot.png
+
+ \section1 The XbelTree Class Definition
+
+ The XbelTree class has functions for reading and writing to the filesystem.
+ It inherits from the QTreeWidget class, contains the model for the
+ displaying of the bookmarks, and allows it to be edited.
+
+ \snippet dombookmarks/xbeltree.h 0
+
+ \section1 The XbelTree Class Implementation
+
+ The \c XbelTree constructor accepts a QWidget within which it is placed.
+ The \c folderIcon is set to QIcon::Normal mode where the pixmap is only
+ displayed when the user is not interacting with the icon. The
+ QStyle::SP_DirClosedIcon, QStyle::SP_DirOpenIcon, and QStyle::SP_FileIcon
+ correspond to standard pixmaps that follow the style of your GUI.
+
+ \snippet dombookmarks/xbeltree.cpp 0
+
+ The \c read() function opens the given QIODevice using
+ QDomDocument::setContent. If it succeeds opening the file and the top
+ level headers are verified, the contents of the class is cleared before
+ the file contents is parsed by iterating all the top level XML nodes and
+ calling \c parseFolderElement() on each of them.
+
+ \snippet dombookmarks/xbeltree.cpp 1
+
+ The \c parseFolderElement() function handles the different element types
+ and calls itself recursively if the element is a subfolder.
+
+ \snippet dombookmarks/xbeltree.cpp 3
+
+ The \c write() function saves the domDocument to the given QIODevice using
+ QDomDocument::save.
+
+ \snippet dombookmarks/xbeltree.cpp 2
+
+ \section1 The MainWindow Class Definition
+
+ The \c MainWindow class is a subclass of QMainWindow, with a
+ \c File menu and a \c Help menu.
+
+ \snippet dombookmarks/mainwindow.h 0
+
+ \section1 The MainWindow Class Implementation
+
+ The \c MainWindow constructor instantiates the member XbelTree object,
+ and sets its header with a QStringList object, \c labels.
+ The constructor also invokes \c createMenus() to set up the menus.
+ The \c statusBar() is used to display the message "Ready".
+
+ \snippet dombookmarks/mainwindow.cpp 0
+
+ The \c createMenus() function populates the menus and sets keyboard
+ shortcuts.
+
+ \snippet dombookmarks/mainwindow.cpp 4
+
+ The \c open() function enables the user to open an XBEL file using
+ QFileDialog. A warning message is displayed along
+ with the \c fileName and \c errorString if the file cannot be read or
+ if there is a parse error. If it succeeds it calls \c XbelTree::read().
+
+ \snippet dombookmarks/mainwindow.cpp 1
+
+ The \c saveAs() function displays a QFileDialog, prompting the user for
+ a \c fileName. Similar to the \c open() function, this function also
+ displays a warning message if the file cannot be written to. If this
+ succeeds it calls \c XbelTree::write().
+
+ \snippet dombookmarks/mainwindow.cpp 2
+
+ The \c about() function displays a QMessageBox with a brief description
+ of the example.
+
+ \snippet dombookmarks/mainwindow.cpp 3
See the \l{http://pyxml.sourceforge.net/topics/xbel/}{XML Bookmark Exchange Language
Resource Page} for more information about XBEL files.
diff --git a/examples/xml/dombookmarks/dombookmarks.pro b/examples/xml/dombookmarks/dombookmarks.pro
index 280207eebb..f6cf6ffc6d 100644
--- a/examples/xml/dombookmarks/dombookmarks.pro
+++ b/examples/xml/dombookmarks/dombookmarks.pro
@@ -6,7 +6,7 @@ SOURCES = main.cpp \
QT += xml widgets
requires(qtConfig(filedialog))
-EXAMPLE_FILES = frank.xbel jennifer.xbel
+EXAMPLE_FILES = jennifer.xbel
# install
target.path = $$[QT_INSTALL_EXAMPLES]/xml/dombookmarks
diff --git a/examples/xml/dombookmarks/jennifer.xbel b/examples/xml/dombookmarks/jennifer.xbel
index 2501c118af..d504236830 100644
--- a/examples/xml/dombookmarks/jennifer.xbel
+++ b/examples/xml/dombookmarks/jennifer.xbel
@@ -3,66 +3,66 @@
<xbel version="1.0">
<folder folded="no">
<title>Qt Resources</title>
- <bookmark href="http://qt.io/">
+ <bookmark href="https://www.qt.io/">
<title>Qt home page</title>
</bookmark>
- <bookmark href="https://www.qt.io/partners/">
+ <bookmark href="https://www.qt.io/contact-us/partners">
<title>Qt Partners</title>
</bookmark>
- <bookmark href="https://www.qt.io/qt-training/">
- <title>Training</title>
+ <bookmark href="https://www.qt.io/qt-professional-services">
+ <title>Professional Services</title>
</bookmark>
- <bookmark href="http://doc.qt.io/">
- <title>Qt 5 documentation</title>
- </bookmark>
- <bookmark href="http://qt-project.org/faq/">
- <title>Frequently Asked Questions</title>
+ <bookmark href="https://doc.qt.io/">
+ <title>Qt Documentation</title>
</bookmark>
<folder folded="yes">
<title>Community Resources</title>
- <bookmark href="http://www.qtcentre.org/content/">
+ <bookmark href="https://contribute.qt-project.org">
+ <title>The Qt Project</title>
+ </bookmark>
+ <bookmark href="https://www.qtcentre.org/content/">
<title>Qt Centre</title>
</bookmark>
- <bookmark href="http://www.qtforum.org/">
- <title>QtForum.org</title>
+ <bookmark href="https://forum.qt.io/">
+ <title>Forum.Qt.org</title>
</bookmark>
- <bookmark href="http://digitalfanatics.org/projects/qt_tutorial/">
+ <bookmark href="https://digitalfanatics.org/projects/qt_tutorial/">
<title>The Independent Qt Tutorial</title>
</bookmark>
- <bookmark href="http://www.qtforum.de/">
+ <bookmark href="https://www.qtforum.de/">
<title>German Qt Forum</title>
</bookmark>
- <bookmark href="http://www.korone.net/">
+ <bookmark href="https://www.qt-dev.com/">
<title>Korean Qt Community Site</title>
</bookmark>
- <bookmark href="http://prog.org.ru/">
+ <bookmark href="http://www.prog.org.ru/">
<title>Russian Qt Forum</title>
</bookmark>
</folder>
</folder>
<folder folded="no">
<title>Online Dictionaries</title>
- <bookmark href="http://www.dictionary.com/">
+ <bookmark href="https://www.dictionary.com/">
<title>Dictionary.com</title>
</bookmark>
- <bookmark href="http://www.m-w.com/">
+ <bookmark href="https://www.merriam-webster.com/">
<title>Merriam-Webster Online</title>
</bookmark>
- <bookmark href="http://dictionary.cambridge.org/">
+ <bookmark href="https://dictionary.cambridge.org/">
<title>Cambridge Dictionaries Online</title>
</bookmark>
- <bookmark href="http://www.onelook.com/">
+ <bookmark href="https://www.onelook.com/">
<title>OneLook Dictionary Search</title>
</bookmark>
<separator/>
- <bookmark href="http://dict.tu-chemnitz.de/">
- <title>TU Chemnitz German-English Dictionary</title>
+ <bookmark href="https://dict.tu-chemnitz.de/">
+ <title>BEOLINGUS, a service of TU Chemnitz</title>
</bookmark>
<separator/>
<bookmark href="http://atilf.atilf.fr/tlf.htm">
<title>Trésor de la Langue Française informatisé</title>
</bookmark>
- <bookmark href="http://dictionnaires.atilf.fr/dictionnaires/ACADEMIE/">
+ <bookmark href="https://www.dictionnaire-academie.fr/">
<title>Dictionnaire de l'Académie Française</title>
</bookmark>
</folder>
diff --git a/examples/xml/dombookmarks/mainwindow.cpp b/examples/xml/dombookmarks/mainwindow.cpp
index 6a03a57bb0..5e6c68ce8d 100644
--- a/examples/xml/dombookmarks/mainwindow.cpp
+++ b/examples/xml/dombookmarks/mainwindow.cpp
@@ -1,11 +1,21 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-#include <QtWidgets>
-
#include "mainwindow.h"
#include "xbeltree.h"
+#include <QApplication>
+#include <QFileDialog>
+#include <QMenuBar>
+#include <QMessageBox>
+#include <QStatusBar>
+
+#include <QAction>
+#include <QScreen>
+
+using namespace Qt::StringLiterals;
+
+//! [0]
MainWindow::MainWindow()
{
xbelTree = new XbelTree;
@@ -19,19 +29,20 @@ MainWindow::MainWindow()
const QSize availableSize = screen()->availableGeometry().size();
resize(availableSize.width() / 2, availableSize.height() / 3);
}
+//! [0]
+//! [1]
void MainWindow::open()
{
- QString fileName =
- QFileDialog::getOpenFileName(this, tr("Open Bookmark File"),
- QDir::currentPath(),
- tr("XBEL Files (*.xbel *.xml)"));
- if (fileName.isEmpty())
+ QFileDialog fileDialog(this, tr("Open Bookmark File"), QDir::currentPath());
+ fileDialog.setMimeTypeFilters({"application/x-xbel"_L1});
+ if (fileDialog.exec() != QDialog::Accepted)
return;
+ const QString fileName = fileDialog.selectedFiles().constFirst();
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
- QMessageBox::warning(this, tr("SAX Bookmarks"),
+ QMessageBox::warning(this, tr("DOM Bookmarks"),
tr("Cannot read file %1:\n%2.")
.arg(QDir::toNativeSeparators(fileName),
file.errorString()));
@@ -41,19 +52,22 @@ void MainWindow::open()
if (xbelTree->read(&file))
statusBar()->showMessage(tr("File loaded"), 2000);
}
+//! [1]
+//! [2]
void MainWindow::saveAs()
{
- QString fileName =
- QFileDialog::getSaveFileName(this, tr("Save Bookmark File"),
- QDir::currentPath(),
- tr("XBEL Files (*.xbel *.xml)"));
- if (fileName.isEmpty())
+ QFileDialog fileDialog(this, tr("Save Bookmark File"), QDir::currentPath());
+ fileDialog.setAcceptMode(QFileDialog::AcceptSave);
+ fileDialog.setDefaultSuffix("xbel"_L1);
+ fileDialog.setMimeTypeFilters({"application/x-xbel"_L1});
+ if (fileDialog.exec() != QDialog::Accepted)
return;
+ const QString fileName = fileDialog.selectedFiles().constFirst();
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
- QMessageBox::warning(this, tr("SAX Bookmarks"),
+ QMessageBox::warning(this, tr("DOM Bookmarks"),
tr("Cannot write file %1:\n%2.")
.arg(QDir::toNativeSeparators(fileName),
file.errorString()));
@@ -63,7 +77,9 @@ void MainWindow::saveAs()
if (xbelTree->write(&file))
statusBar()->showMessage(tr("File saved"), 2000);
}
+//! [2]
+//! [3]
void MainWindow::about()
{
QMessageBox::about(this, tr("About DOM Bookmarks"),
@@ -71,7 +87,9 @@ void MainWindow::about()
"use Qt's DOM classes to read and write XML "
"documents."));
}
+//! [3]
+//! [4]
void MainWindow::createMenus()
{
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
@@ -88,5 +106,6 @@ void MainWindow::createMenus()
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(tr("&About"), this, &MainWindow::about);
- helpMenu->addAction(tr("About &Qt"), qApp, &QCoreApplication::quit);
+ helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt);
}
+//! [4]
diff --git a/examples/xml/dombookmarks/mainwindow.h b/examples/xml/dombookmarks/mainwindow.h
index 55b78c8449..82e29012f2 100644
--- a/examples/xml/dombookmarks/mainwindow.h
+++ b/examples/xml/dombookmarks/mainwindow.h
@@ -8,6 +8,7 @@
class XbelTree;
+//! [0]
class MainWindow : public QMainWindow
{
Q_OBJECT
@@ -25,5 +26,6 @@ private:
XbelTree *xbelTree;
};
+//! [0]
#endif
diff --git a/examples/xml/dombookmarks/xbeltree.cpp b/examples/xml/dombookmarks/xbeltree.cpp
index 5d2e14f198..eff2fea8f0 100644
--- a/examples/xml/dombookmarks/xbeltree.cpp
+++ b/examples/xml/dombookmarks/xbeltree.cpp
@@ -1,30 +1,41 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-#include <QtWidgets>
-
#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);
@@ -32,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());
@@ -49,8 +61,9 @@ 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)
{
QDomDocument::ParseResult result =
@@ -69,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."));
@@ -81,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;
@@ -100,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)
diff --git a/examples/xml/dombookmarks/xbeltree.h b/examples/xml/dombookmarks/xbeltree.h
index d39caad51a..caaf4c0062 100644
--- a/examples/xml/dombookmarks/xbeltree.h
+++ b/examples/xml/dombookmarks/xbeltree.h
@@ -8,18 +8,19 @@
#include <QIcon>
#include <QTreeWidget>
+//! [0]
class XbelTree : public QTreeWidget
{
Q_OBJECT
public:
- XbelTree(QWidget *parent = nullptr);
+ explicit XbelTree(QWidget *parent = nullptr);
bool read(QIODevice *device);
bool write(QIODevice *device) const;
protected:
-#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
+#if QT_CONFIG(clipboard) && QT_CONFIG(contextmenu)
void contextMenuEvent(QContextMenuEvent *event) override;
#endif
@@ -36,5 +37,6 @@ private:
QIcon folderIcon;
QIcon bookmarkIcon;
};
+//! [0]
#endif
diff --git a/examples/xml/streambookmarks/CMakeLists.txt b/examples/xml/streambookmarks/CMakeLists.txt
deleted file mode 100644
index cefc30453e..0000000000
--- a/examples/xml/streambookmarks/CMakeLists.txt
+++ /dev/null
@@ -1,39 +0,0 @@
-# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
-
-cmake_minimum_required(VERSION 3.16)
-project(streambookmarks LANGUAGES CXX)
-
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/xml/streambookmarks")
-
-find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
-
-qt_standard_project_setup()
-
-qt_add_executable(streambookmarks
- main.cpp
- mainwindow.cpp mainwindow.h
- xbelreader.cpp xbelreader.h
- xbelwriter.cpp xbelwriter.h
-)
-
-set_target_properties(streambookmarks PROPERTIES
- WIN32_EXECUTABLE TRUE
- MACOSX_BUNDLE TRUE
-)
-
-target_link_libraries(streambookmarks PRIVATE
- Qt6::Core
- Qt6::Gui
- Qt6::Widgets
-)
-
-install(TARGETS streambookmarks
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
-)
diff --git a/examples/xml/streambookmarks/doc/images/xmlstreamexample-filemenu.png b/examples/xml/streambookmarks/doc/images/xmlstreamexample-filemenu.png
deleted file mode 100644
index e074fb7c41..0000000000
--- a/examples/xml/streambookmarks/doc/images/xmlstreamexample-filemenu.png
+++ /dev/null
Binary files differ
diff --git a/examples/xml/streambookmarks/doc/images/xmlstreamexample-helpmenu.png b/examples/xml/streambookmarks/doc/images/xmlstreamexample-helpmenu.png
deleted file mode 100644
index 0dc4392b94..0000000000
--- a/examples/xml/streambookmarks/doc/images/xmlstreamexample-helpmenu.png
+++ /dev/null
Binary files differ
diff --git a/examples/xml/streambookmarks/doc/images/xmlstreamexample-screenshot.png b/examples/xml/streambookmarks/doc/images/xmlstreamexample-screenshot.png
deleted file mode 100644
index bbaa423061..0000000000
--- a/examples/xml/streambookmarks/doc/images/xmlstreamexample-screenshot.png
+++ /dev/null
Binary files differ
diff --git a/examples/xml/streambookmarks/doc/src/qxmlstreambookmarks.qdoc b/examples/xml/streambookmarks/doc/src/qxmlstreambookmarks.qdoc
deleted file mode 100644
index 546962162f..0000000000
--- a/examples/xml/streambookmarks/doc/src/qxmlstreambookmarks.qdoc
+++ /dev/null
@@ -1,170 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
-
-/*!
- \example streambookmarks
- \title QXmlStream Bookmarks Example
- \brief Demonstrates how to read and write to XBEL files.
- \ingroup xml-examples
-
- The QXmlStream Bookmarks example provides a reader for XML Bookmark
- Exchange Language (XBEL) files using Qt's QXmlStreamReader class
- for reading, and QXmlStreamWriter class for writing the files.
-
- \image xmlstreamexample-screenshot.png
-
- \section1 XbelWriter Class Definition
-
- The \c XbelWriter class contains a private instance of QXmlStreamWriter,
- which provides an XML writer with a streaming API. \c XbelWriter also
- has a reference to the QTreeWidget instance where the bookmark hierarchy
- is stored.
-
- \snippet streambookmarks/xbelwriter.h 0
-
- \section1 XbelWriter Class Implementation
-
- The \c XbelWriter constructor accepts a \a treeWidget to initialize within
- its definition. We enable \l{QXmlStreamWriter}'s auto-formatting property
- to ensure line-breaks and indentations are added automatically to empty
- sections between elements, increasing readability as the data is split into
- several lines.
-
- \snippet streambookmarks/xbelwriter.cpp 0
-
- The \c writeFile() function accepts a QIODevice object and sets it using
- \c setDevice(). This function then writes the document type
- definition(DTD), the start element, the version, and \c{treeWidget}'s
- top-level items.
-
- \snippet streambookmarks/xbelwriter.cpp 1
-
- The \c writeItem() function accepts a QTreeWidgetItem object and writes it
- to the stream, depending on its \c tagName, which can either be a "folder",
- "bookmark", or "separator".
-
- \snippet streambookmarks/xbelwriter.cpp 2
-
- \section1 XbelReader Class Definition
-
- The \c XbelReader contains a private instance of QXmlStreamReader, the
- companion class to QXmlStreamWriter. \c XbelReader also contains a
- reference to the QTreeWidget that is used to group the bookmarks according
- to their hierarchy.
-
- \snippet streambookmarks/xbelreader.h 0
-
- \section1 XbelReader Class Implementation
-
- The \c XbelReader constructor accepts a QTreeWidget to initialize the
- \c treeWidget within its definition. A QStyle object is used to set
- \c{treeWidget}'s style property. The \c folderIcon is set to QIcon::Normal
- mode where the pixmap is only displayed when the user is not interacting
- with the icon. The QStyle::SP_DirClosedIcon, QStyle::SP_DirOpenIcon, and
- QStyle::SP_FileIcon correspond to standard pixmaps that follow the style
- of your GUI.
-
- \snippet streambookmarks/xbelreader.cpp 0
-
- The \c read() function accepts a QIODevice and sets it using
- \l{QXmlStreamReader::}{setDevice()}. The actual process of reading only
- takes place if the file is a valid XBEL 1.0 file. Note that the XML input
- needs to be well-formed to be accepted by QXmlStreamReader. Otherwise, the
- \l{QXmlStreamReader::}{raiseError()} function is used to display an error
- message. Since the XBEL reader is only concerned with reading XML elements,
- it makes extensive use of the \l{QXmlStreamReader::}{readNextStartElement()}
- convenience function.
-
- \snippet streambookmarks/xbelreader.cpp 1
-
- The \c errorString() function is used if an error occurred, in order to
- obtain a description of the error complete with line and column number
- information.
-
- \snippet streambookmarks/xbelreader.cpp 2
-
- The \c readXBEL() function reads the name of a startElement and calls
- the appropriate function to read it, depending on whether if its a
- "folder", "bookmark" or "separator". Otherwise, it calls
- \l{QXmlStreamReader::}{skipCurrentElement()}. The Q_ASSERT() macro is used
- to provide a pre-condition for the function.
-
- \snippet streambookmarks/xbelreader.cpp 3
-
- The \c readTitle() function reads the bookmark's title.
-
- \snippet streambookmarks/xbelreader.cpp 4
-
- The \c readSeparator() function creates a separator and sets its flags.
- The text is set to 30 "0xB7", the HEX equivalent for period. The element
- is then skipped using \l{QXmlStreamReader::}{skipCurrentElement()}.
-
- \snippet streambookmarks/xbelreader.cpp 5
-
- \section1 MainWindow Class Definition
-
- The \c MainWindow class is a subclass of QMainWindow, with a
- \c File menu and a \c Help menu.
-
- \snippet streambookmarks/mainwindow.h 0
-
- \section1 MainWindow Class Implementation
-
- The \c MainWindow constructor instantiates the QTreeWidget object, \c
- treeWidget and sets its header with a QStringList object, \c labels.
- The constructor also invokes \c createActions() and \c createMenus()
- to set up the menus and their corresponding actions. The \c statusBar()
- is used to display the message "Ready" and the window's size is fixed
- to 480x320 pixels.
-
- \snippet streambookmarks/mainwindow.cpp 0
-
- The \c open() function enables the user to open an XBEL file using
- QFileDialog::getOpenFileName(). A warning message is displayed along
- with the \c fileName and \c errorString if the file cannot be read or
- if there is a parse error.
-
- \snippet streambookmarks/mainwindow.cpp 1
-
- The \c saveAs() function displays a QFileDialog, prompting the user for
- a \c fileName using QFileDialog::getSaveFileName(). Similar to the
- \c open() function, this function also displays a warning message if
- the file cannot be written to.
-
- \snippet streambookmarks/mainwindow.cpp 2
-
- The \c about() function displays a QMessageBox with a brief description
- of the example.
-
- \snippet streambookmarks/mainwindow.cpp 3
-
- In order to implement the \c open(), \c saveAs(), \c exit(), \c about()
- and \c aboutQt() functions, we connect them to QAction objects and
- add them to the \c fileMenu and \c helpMenu. The connections are as shown
- below:
-
- \snippet streambookmarks/mainwindow.cpp 5
-
- The \c createMenus() function creates the \c fileMenu and \c helpMenu
- and adds the QAction objects to them in order to create the menu shown
- in the screenshot below:
-
- \table
- \row
- \li \inlineimage xmlstreamexample-filemenu.png
- \li \inlineimage xmlstreamexample-helpmenu.png
- \endtable
-
- \snippet streambookmarks/mainwindow.cpp 5
-
- \section1 \c{main()} Function
-
- The \c main() function instantiates \c MainWindow and invokes the \c show()
- function.
-
- \snippet streambookmarks/main.cpp 0
-
- See the \l{http://pyxml.sourceforge.net/topics/xbel/}
- {XML Bookmark Exchange Language Resource Page} for more information
- about XBEL files.
-*/
diff --git a/examples/xml/streambookmarks/jennifer.xbel b/examples/xml/streambookmarks/jennifer.xbel
deleted file mode 100644
index 2501c118af..0000000000
--- a/examples/xml/streambookmarks/jennifer.xbel
+++ /dev/null
@@ -1,69 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE xbel>
-<xbel version="1.0">
- <folder folded="no">
- <title>Qt Resources</title>
- <bookmark href="http://qt.io/">
- <title>Qt home page</title>
- </bookmark>
- <bookmark href="https://www.qt.io/partners/">
- <title>Qt Partners</title>
- </bookmark>
- <bookmark href="https://www.qt.io/qt-training/">
- <title>Training</title>
- </bookmark>
- <bookmark href="http://doc.qt.io/">
- <title>Qt 5 documentation</title>
- </bookmark>
- <bookmark href="http://qt-project.org/faq/">
- <title>Frequently Asked Questions</title>
- </bookmark>
- <folder folded="yes">
- <title>Community Resources</title>
- <bookmark href="http://www.qtcentre.org/content/">
- <title>Qt Centre</title>
- </bookmark>
- <bookmark href="http://www.qtforum.org/">
- <title>QtForum.org</title>
- </bookmark>
- <bookmark href="http://digitalfanatics.org/projects/qt_tutorial/">
- <title>The Independent Qt Tutorial</title>
- </bookmark>
- <bookmark href="http://www.qtforum.de/">
- <title>German Qt Forum</title>
- </bookmark>
- <bookmark href="http://www.korone.net/">
- <title>Korean Qt Community Site</title>
- </bookmark>
- <bookmark href="http://prog.org.ru/">
- <title>Russian Qt Forum</title>
- </bookmark>
- </folder>
- </folder>
- <folder folded="no">
- <title>Online Dictionaries</title>
- <bookmark href="http://www.dictionary.com/">
- <title>Dictionary.com</title>
- </bookmark>
- <bookmark href="http://www.m-w.com/">
- <title>Merriam-Webster Online</title>
- </bookmark>
- <bookmark href="http://dictionary.cambridge.org/">
- <title>Cambridge Dictionaries Online</title>
- </bookmark>
- <bookmark href="http://www.onelook.com/">
- <title>OneLook Dictionary Search</title>
- </bookmark>
- <separator/>
- <bookmark href="http://dict.tu-chemnitz.de/">
- <title>TU Chemnitz German-English Dictionary</title>
- </bookmark>
- <separator/>
- <bookmark href="http://atilf.atilf.fr/tlf.htm">
- <title>Trésor de la Langue Française informatisé</title>
- </bookmark>
- <bookmark href="http://dictionnaires.atilf.fr/dictionnaires/ACADEMIE/">
- <title>Dictionnaire de l'Académie Française</title>
- </bookmark>
- </folder>
-</xbel>
diff --git a/examples/xml/streambookmarks/main.cpp b/examples/xml/streambookmarks/main.cpp
deleted file mode 100644
index 75b5d646c6..0000000000
--- a/examples/xml/streambookmarks/main.cpp
+++ /dev/null
@@ -1,17 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include <QApplication>
-
-#include "mainwindow.h"
-
-//! [0]
-int main(int argc, char *argv[])
-{
- QApplication app(argc, argv);
- MainWindow mainWin;
- mainWin.show();
- mainWin.open();
- return app.exec();
-}
-//! [0]
diff --git a/examples/xml/streambookmarks/mainwindow.cpp b/examples/xml/streambookmarks/mainwindow.cpp
deleted file mode 100644
index 644681d706..0000000000
--- a/examples/xml/streambookmarks/mainwindow.cpp
+++ /dev/null
@@ -1,142 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include <QtWidgets>
-
-#include "mainwindow.h"
-#include "xbelreader.h"
-#include "xbelwriter.h"
-
-//! [0]
-MainWindow::MainWindow()
-{
- QStringList labels;
- labels << tr("Title") << tr("Location");
-
- treeWidget = new QTreeWidget;
- treeWidget->header()->setSectionResizeMode(QHeaderView::Stretch);
- treeWidget->setHeaderLabels(labels);
-#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
- treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
- connect(treeWidget, &QWidget::customContextMenuRequested,
- this, &MainWindow::onCustomContextMenuRequested);
-#endif
- setCentralWidget(treeWidget);
-
- createMenus();
-
- statusBar()->showMessage(tr("Ready"));
-
- setWindowTitle(tr("QXmlStream Bookmarks"));
- const QSize availableSize = screen()->availableGeometry().size();
- resize(availableSize.width() / 2, availableSize.height() / 3);
-}
-//! [0]
-
-#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
-void MainWindow::onCustomContextMenuRequested(const QPoint &pos)
-{
- const QTreeWidgetItem *item = treeWidget->itemAt(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(treeWidget->viewport()->mapToGlobal(pos));
- if (action == copyAction)
- QGuiApplication::clipboard()->setText(url);
- else if (action == openAction)
- QDesktopServices::openUrl(QUrl(url));
-}
-#endif // !QT_NO_CONTEXTMENU && !QT_NO_CLIPBOARD
-
-//! [1]
-void MainWindow::open()
-{
- QString fileName =
- QFileDialog::getOpenFileName(this, tr("Open Bookmark File"),
- QDir::currentPath(),
- tr("XBEL Files (*.xbel *.xml)"));
- if (fileName.isEmpty())
- return;
-
- treeWidget->clear();
-
-
- QFile file(fileName);
- if (!file.open(QFile::ReadOnly | QFile::Text)) {
- QMessageBox::warning(this, tr("QXmlStream Bookmarks"),
- tr("Cannot read file %1:\n%2.")
- .arg(QDir::toNativeSeparators(fileName),
- file.errorString()));
- return;
- }
-
- XbelReader reader(treeWidget);
- if (!reader.read(&file)) {
- QMessageBox::warning(this, tr("QXmlStream Bookmarks"),
- tr("Parse error in file %1:\n\n%2")
- .arg(QDir::toNativeSeparators(fileName),
- reader.errorString()));
- } else {
- statusBar()->showMessage(tr("File loaded"), 2000);
- }
-
-}
-//! [1]
-
-//! [2]
-void MainWindow::saveAs()
-{
- QString fileName =
- QFileDialog::getSaveFileName(this, tr("Save Bookmark File"),
- QDir::currentPath(),
- tr("XBEL Files (*.xbel *.xml)"));
- if (fileName.isEmpty())
- return;
-
- QFile file(fileName);
- if (!file.open(QFile::WriteOnly | QFile::Text)) {
- QMessageBox::warning(this, tr("QXmlStream Bookmarks"),
- tr("Cannot write file %1:\n%2.")
- .arg(QDir::toNativeSeparators(fileName),
- file.errorString()));
- return;
- }
-
- XbelWriter writer(treeWidget);
- if (writer.writeFile(&file))
- statusBar()->showMessage(tr("File saved"), 2000);
-}
-//! [2]
-
-//! [3]
-void MainWindow::about()
-{
- QMessageBox::about(this, tr("About QXmlStream Bookmarks"),
- tr("The <b>QXmlStream Bookmarks</b> example demonstrates how to use Qt's "
- "QXmlStream classes to read and write XML documents."));
-}
-//! [3]
-
-//! [5]
-void MainWindow::createMenus()
-{
- QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
- QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &MainWindow::open);
- openAct->setShortcuts(QKeySequence::Open);
-
- QAction *saveAsAct = fileMenu->addAction(tr("&Save As..."), this, &MainWindow::saveAs);
- saveAsAct->setShortcuts(QKeySequence::SaveAs);
-
- QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close);
- exitAct->setShortcuts(QKeySequence::Quit);
-
- menuBar()->addSeparator();
-
- QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
- helpMenu->addAction(tr("&About"), this, &MainWindow::about);
- helpMenu->addAction(tr("About &Qt"), qApp, &QCoreApplication::quit);
-}
-//! [5]
diff --git a/examples/xml/streambookmarks/mainwindow.h b/examples/xml/streambookmarks/mainwindow.h
deleted file mode 100644
index 7a4a922e43..0000000000
--- a/examples/xml/streambookmarks/mainwindow.h
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#ifndef MAINWINDOW_H
-#define MAINWINDOW_H
-
-#include <QMainWindow>
-
-QT_BEGIN_NAMESPACE
-class QTreeWidget;
-QT_END_NAMESPACE
-
-//! [0]
-class MainWindow : public QMainWindow
-{
- Q_OBJECT
-
-public:
- MainWindow();
-
-public slots:
- void open();
- void saveAs();
- void about();
-#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
- void onCustomContextMenuRequested(const QPoint &pos);
-#endif
-private:
- void createMenus();
-
- QTreeWidget *treeWidget;
-};
-//! [0]
-
-#endif
diff --git a/examples/xml/streambookmarks/streambookmarks.pro b/examples/xml/streambookmarks/streambookmarks.pro
deleted file mode 100644
index 23eebdfe7d..0000000000
--- a/examples/xml/streambookmarks/streambookmarks.pro
+++ /dev/null
@@ -1,15 +0,0 @@
-HEADERS = mainwindow.h \
- xbelreader.h \
- xbelwriter.h
-SOURCES = main.cpp \
- mainwindow.cpp \
- xbelreader.cpp \
- xbelwriter.cpp
-QT += widgets
-requires(qtConfig(filedialog))
-
-EXAMPLE_FILES = frank.xbel jennifer.xbel
-
-# install
-target.path = $$[QT_INSTALL_EXAMPLES]/xml/streambookmarks
-INSTALLS += target
diff --git a/examples/xml/streambookmarks/xbelreader.cpp b/examples/xml/streambookmarks/xbelreader.cpp
deleted file mode 100644
index 74e25f12a8..0000000000
--- a/examples/xml/streambookmarks/xbelreader.cpp
+++ /dev/null
@@ -1,140 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include <QtWidgets>
-
-#include "xbelreader.h"
-
-//! [0]
-XbelReader::XbelReader(QTreeWidget *treeWidget)
- : treeWidget(treeWidget)
-{
- QStyle *style = treeWidget->style();
-
- 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));
-}
-//! [0]
-
-//! [1]
-bool XbelReader::read(QIODevice *device)
-{
- xml.setDevice(device);
-
- if (xml.readNextStartElement()) {
- if (xml.name() == QLatin1String("xbel")
- && xml.attributes().value(versionAttribute()) == QLatin1String("1.0")) {
- readXBEL();
- } else {
- xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file."));
- }
- }
-
- return !xml.error();
-}
-//! [1]
-
-//! [2]
-QString XbelReader::errorString() const
-{
- return QObject::tr("%1\nLine %2, column %3")
- .arg(xml.errorString())
- .arg(xml.lineNumber())
- .arg(xml.columnNumber());
-}
-//! [2]
-
-//! [3]
-void XbelReader::readXBEL()
-{
- Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("xbel"));
-
- while (xml.readNextStartElement()) {
- if (xml.name() == QLatin1String("folder"))
- readFolder(0);
- else if (xml.name() == QLatin1String("bookmark"))
- readBookmark(0);
- else if (xml.name() == QLatin1String("separator"))
- readSeparator(0);
- else
- xml.skipCurrentElement();
- }
-}
-//! [3]
-
-//! [4]
-void XbelReader::readTitle(QTreeWidgetItem *item)
-{
- Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("title"));
-
- QString title = xml.readElementText();
- item->setText(0, title);
-}
-//! [4]
-
-//! [5]
-void XbelReader::readSeparator(QTreeWidgetItem *item)
-{
- Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("separator"));
-
- QTreeWidgetItem *separator = createChildItem(item);
- separator->setFlags(item->flags() & ~Qt::ItemIsSelectable);
- separator->setText(0, QString(30, u'\xB7'));
- xml.skipCurrentElement();
-}
-//! [5]
-
-void XbelReader::readFolder(QTreeWidgetItem *item)
-{
- Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("folder"));
-
- QTreeWidgetItem *folder = createChildItem(item);
- bool folded = (xml.attributes().value(foldedAttribute()) != QLatin1String("no"));
- folder->setExpanded(!folded);
-
- while (xml.readNextStartElement()) {
- if (xml.name() == QLatin1String("title"))
- readTitle(folder);
- else if (xml.name() == QLatin1String("folder"))
- readFolder(folder);
- else if (xml.name() == QLatin1String("bookmark"))
- readBookmark(folder);
- else if (xml.name() == QLatin1String("separator"))
- readSeparator(folder);
- else
- xml.skipCurrentElement();
- }
-}
-
-void XbelReader::readBookmark(QTreeWidgetItem *item)
-{
- Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("bookmark"));
-
- QTreeWidgetItem *bookmark = createChildItem(item);
- bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable);
- bookmark->setIcon(0, bookmarkIcon);
- bookmark->setText(0, QObject::tr("Unknown title"));
- bookmark->setText(1, xml.attributes().value(hrefAttribute()).toString());
-
- while (xml.readNextStartElement()) {
- if (xml.name() == QLatin1String("title"))
- readTitle(bookmark);
- else
- xml.skipCurrentElement();
- }
-}
-
-QTreeWidgetItem *XbelReader::createChildItem(QTreeWidgetItem *item)
-{
- QTreeWidgetItem *childItem;
- if (item) {
- childItem = new QTreeWidgetItem(item);
- } else {
- childItem = new QTreeWidgetItem(treeWidget);
- }
- childItem->setData(0, Qt::UserRole, xml.name().toString());
- return childItem;
-}
diff --git a/examples/xml/streambookmarks/xbelreader.h b/examples/xml/streambookmarks/xbelreader.h
deleted file mode 100644
index 81a59b32b5..0000000000
--- a/examples/xml/streambookmarks/xbelreader.h
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#ifndef XBELREADER_H
-#define XBELREADER_H
-
-#include <QIcon>
-#include <QXmlStreamReader>
-
-QT_BEGIN_NAMESPACE
-class QTreeWidget;
-class QTreeWidgetItem;
-QT_END_NAMESPACE
-
-//! [0]
-class XbelReader
-{
-public:
-//! [1]
- XbelReader(QTreeWidget *treeWidget);
-//! [1]
-
- bool read(QIODevice *device);
-
- QString errorString() const;
-
- static inline QString versionAttribute() { return QStringLiteral("version"); }
- static inline QString hrefAttribute() { return QStringLiteral("href"); }
- static inline QString foldedAttribute() { return QStringLiteral("folded"); }
-
-private:
-//! [2]
- void readXBEL();
- void readTitle(QTreeWidgetItem *item);
- void readSeparator(QTreeWidgetItem *item);
- void readFolder(QTreeWidgetItem *item);
- void readBookmark(QTreeWidgetItem *item);
-
- QTreeWidgetItem *createChildItem(QTreeWidgetItem *item);
-
- QXmlStreamReader xml;
- QTreeWidget *treeWidget;
-//! [2]
-
- QIcon folderIcon;
- QIcon bookmarkIcon;
-};
-//! [0]
-
-#endif
diff --git a/examples/xml/streambookmarks/xbelwriter.cpp b/examples/xml/streambookmarks/xbelwriter.cpp
deleted file mode 100644
index 6cfcd2bc1a..0000000000
--- a/examples/xml/streambookmarks/xbelwriter.cpp
+++ /dev/null
@@ -1,60 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include <QtWidgets>
-
-#include "xbelwriter.h"
-#include "xbelreader.h"
-
-static inline QString yesValue() { return QStringLiteral("yes"); }
-static inline QString noValue() { return QStringLiteral("no"); }
-static inline QString titleElement() { return QStringLiteral("title"); }
-
-//! [0]
-XbelWriter::XbelWriter(const QTreeWidget *treeWidget)
- : treeWidget(treeWidget)
-{
- xml.setAutoFormatting(true);
-}
-//! [0]
-
-//! [1]
-bool XbelWriter::writeFile(QIODevice *device)
-{
- xml.setDevice(device);
-
- xml.writeStartDocument();
- xml.writeDTD(QStringLiteral("<!DOCTYPE xbel>"));
- xml.writeStartElement(QStringLiteral("xbel"));
- xml.writeAttribute(XbelReader::versionAttribute(), QStringLiteral("1.0"));
- for (int i = 0; i < treeWidget->topLevelItemCount(); ++i)
- writeItem(treeWidget->topLevelItem(i));
-
- xml.writeEndDocument();
- return true;
-}
-//! [1]
-
-//! [2]
-void XbelWriter::writeItem(const QTreeWidgetItem *item)
-{
- QString tagName = item->data(0, Qt::UserRole).toString();
- if (tagName == QLatin1String("folder")) {
- bool folded = !item->isExpanded();
- xml.writeStartElement(tagName);
- xml.writeAttribute(XbelReader::foldedAttribute(), folded ? yesValue() : noValue());
- xml.writeTextElement(titleElement(), item->text(0));
- for (int i = 0; i < item->childCount(); ++i)
- writeItem(item->child(i));
- xml.writeEndElement();
- } else if (tagName == QLatin1String("bookmark")) {
- xml.writeStartElement(tagName);
- if (!item->text(1).isEmpty())
- xml.writeAttribute(XbelReader::hrefAttribute(), item->text(1));
- xml.writeTextElement(titleElement(), item->text(0));
- xml.writeEndElement();
- } else if (tagName == QLatin1String("separator")) {
- xml.writeEmptyElement(tagName);
- }
-}
-//! [2]
diff --git a/examples/xml/streambookmarks/xbelwriter.h b/examples/xml/streambookmarks/xbelwriter.h
deleted file mode 100644
index ec95315c4b..0000000000
--- a/examples/xml/streambookmarks/xbelwriter.h
+++ /dev/null
@@ -1,28 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#ifndef XBELWRITER_H
-#define XBELWRITER_H
-
-#include <QXmlStreamWriter>
-
-QT_BEGIN_NAMESPACE
-class QTreeWidget;
-class QTreeWidgetItem;
-QT_END_NAMESPACE
-
-//! [0]
-class XbelWriter
-{
-public:
- explicit XbelWriter(const QTreeWidget *treeWidget);
- bool writeFile(QIODevice *device);
-
-private:
- void writeItem(const QTreeWidgetItem *item);
- QXmlStreamWriter xml;
- const QTreeWidget *treeWidget;
-};
-//! [0]
-
-#endif
diff --git a/examples/xml/xml.pro b/examples/xml/xml.pro
index 991d5a69d2..d80d8193ed 100644
--- a/examples/xml/xml.pro
+++ b/examples/xml/xml.pro
@@ -1,6 +1,5 @@
TEMPLATE = subdirs
qtHaveModule(widgets) {
- SUBDIRS += dombookmarks \
- streambookmarks
+ SUBDIRS += dombookmarks
}