diff options
Diffstat (limited to 'examples/xml/dombookmarks')
-rw-r--r-- | examples/xml/dombookmarks/CMakeLists.txt | 36 | ||||
-rw-r--r-- | examples/xml/dombookmarks/doc/images/dombookmarks-example.png | bin | 19405 -> 0 bytes | |||
-rw-r--r-- | examples/xml/dombookmarks/doc/images/screenshot.png | bin | 0 -> 67075 bytes | |||
-rw-r--r-- | examples/xml/dombookmarks/doc/src/dombookmarks.qdoc | 117 | ||||
-rw-r--r-- | examples/xml/dombookmarks/dombookmarks.pro | 2 | ||||
-rw-r--r-- | examples/xml/dombookmarks/jennifer.xbel | 46 | ||||
-rw-r--r-- | examples/xml/dombookmarks/main.cpp | 51 | ||||
-rw-r--r-- | examples/xml/dombookmarks/mainwindow.cpp | 100 | ||||
-rw-r--r-- | examples/xml/dombookmarks/mainwindow.h | 53 | ||||
-rw-r--r-- | examples/xml/dombookmarks/xbeltree.cpp | 150 | ||||
-rw-r--r-- | examples/xml/dombookmarks/xbeltree.h | 57 |
11 files changed, 238 insertions, 374 deletions
diff --git a/examples/xml/dombookmarks/CMakeLists.txt b/examples/xml/dombookmarks/CMakeLists.txt index d813ace193..58efd94a55 100644 --- a/examples/xml/dombookmarks/CMakeLists.txt +++ b/examples/xml/dombookmarks/CMakeLists.txt @@ -1,16 +1,13 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + cmake_minimum_required(VERSION 3.16) project(dombookmarks LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - -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() + qt_add_executable(dombookmarks main.cpp mainwindow.cpp mainwindow.h @@ -22,15 +19,22 @@ set_target_properties(dombookmarks PROPERTIES MACOSX_BUNDLE TRUE ) -target_link_libraries(dombookmarks PUBLIC - Qt::Core - Qt::Gui - Qt::Widgets - Qt::Xml +target_link_libraries(dombookmarks PRIVATE + Qt6::Core + Qt6::Gui + Qt6::Widgets + Qt6::Xml ) 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 Binary files differdeleted file mode 100644 index abacacbbfc..0000000000 --- a/examples/xml/dombookmarks/doc/images/dombookmarks-example.png +++ /dev/null diff --git a/examples/xml/dombookmarks/doc/images/screenshot.png b/examples/xml/dombookmarks/doc/images/screenshot.png Binary files differnew file mode 100644 index 0000000000..462052ce8a --- /dev/null +++ b/examples/xml/dombookmarks/doc/images/screenshot.png diff --git a/examples/xml/dombookmarks/doc/src/dombookmarks.qdoc b/examples/xml/dombookmarks/doc/src/dombookmarks.qdoc index 84ac1723d5..0b3ca2f47d 100644 --- a/examples/xml/dombookmarks/doc/src/dombookmarks.qdoc +++ b/examples/xml/dombookmarks/doc/src/dombookmarks.qdoc @@ -1,41 +1,96 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:FDL$ -** 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. -** -** GNU Free Documentation License Usage -** Alternatively, this file may be used under the terms of the GNU Free -** Documentation License version 1.3 as published by the Free Software -** Foundation and appearing in the file included in the packaging of -** this file. Please review the following information to ensure -** the GNU Free Documentation License version 1.3 requirements -** will be met: https://www.gnu.org/licenses/fdl-1.3.html. -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only /*! \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/main.cpp b/examples/xml/dombookmarks/main.cpp index 0c175c9fc1..43f9088dad 100644 --- a/examples/xml/dombookmarks/main.cpp +++ b/examples/xml/dombookmarks/main.cpp @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include <QApplication> diff --git a/examples/xml/dombookmarks/mainwindow.cpp b/examples/xml/dombookmarks/mainwindow.cpp index b8f2e12918..5e6c68ce8d 100644 --- a/examples/xml/dombookmarks/mainwindow.cpp +++ b/examples/xml/dombookmarks/mainwindow.cpp @@ -1,58 +1,21 @@ -/**************************************************************************** -** -** 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 "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; @@ -66,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())); @@ -88,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())); @@ -110,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"), @@ -118,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")); @@ -135,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 0a8c7bdd82..82e29012f2 100644 --- a/examples/xml/dombookmarks/mainwindow.h +++ b/examples/xml/dombookmarks/mainwindow.h @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef MAINWINDOW_H #define MAINWINDOW_H @@ -55,6 +8,7 @@ class XbelTree; +//! [0] class MainWindow : public QMainWindow { Q_OBJECT @@ -72,5 +26,6 @@ private: XbelTree *xbelTree; }; +//! [0] #endif 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) diff --git a/examples/xml/dombookmarks/xbeltree.h b/examples/xml/dombookmarks/xbeltree.h index 989841d339..caaf4c0062 100644 --- a/examples/xml/dombookmarks/xbeltree.h +++ b/examples/xml/dombookmarks/xbeltree.h @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef XBELTREE_H #define XBELTREE_H @@ -55,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 @@ -83,5 +37,6 @@ private: QIcon folderIcon; QIcon bookmarkIcon; }; +//! [0] #endif |