From 8d71fae08078afbf4aa2de7429302f46d46fb43f Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 31 Oct 2016 11:09:00 +0100 Subject: Polish the XML bookmarks examples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Use Qt 5 connect syntax. - Streamline code, remove unused members. - Add a context menu for copying and opening the URLs. - Add const to XML code. - In the XML code, show the use of QStringLiteral in static inline functions to create strings versus QLatin1String in comparison overloads to avoid allocating strings from const char * literals. Change-Id: Ib5e62ca188e271ffe01996dff3c9ea8e0b60739a Reviewed-by: Topi Reiniƶ --- examples/xml/dombookmarks/mainwindow.cpp | 44 ++++++------------- examples/xml/dombookmarks/mainwindow.h | 9 ---- examples/xml/dombookmarks/xbeltree.cpp | 74 ++++++++++++++++++++++---------- examples/xml/dombookmarks/xbeltree.h | 11 +++-- 4 files changed, 72 insertions(+), 66 deletions(-) (limited to 'examples/xml/dombookmarks') diff --git a/examples/xml/dombookmarks/mainwindow.cpp b/examples/xml/dombookmarks/mainwindow.cpp index e0f208d336..fade2dfc96 100644 --- a/examples/xml/dombookmarks/mainwindow.cpp +++ b/examples/xml/dombookmarks/mainwindow.cpp @@ -58,13 +58,13 @@ MainWindow::MainWindow() xbelTree = new XbelTree; setCentralWidget(xbelTree); - createActions(); createMenus(); statusBar()->showMessage(tr("Ready")); setWindowTitle(tr("DOM Bookmarks")); - resize(480, 320); + const QSize availableSize = QApplication::desktop()->availableGeometry(this).size(); + resize(availableSize.width() / 2, availableSize.height() / 3); } void MainWindow::open() @@ -80,8 +80,8 @@ void MainWindow::open() if (!file.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning(this, tr("SAX Bookmarks"), tr("Cannot read file %1:\n%2.") - .arg(fileName) - .arg(file.errorString())); + .arg(QDir::toNativeSeparators(fileName), + file.errorString())); return; } @@ -102,8 +102,8 @@ void MainWindow::saveAs() if (!file.open(QFile::WriteOnly | QFile::Text)) { QMessageBox::warning(this, tr("SAX Bookmarks"), tr("Cannot write file %1:\n%2.") - .arg(fileName) - .arg(file.errorString())); + .arg(QDir::toNativeSeparators(fileName), + file.errorString())); return; } @@ -119,37 +119,21 @@ void MainWindow::about() "documents.")); } -void MainWindow::createActions() +void MainWindow::createMenus() { - openAct = new QAction(tr("&Open..."), this); + QMenu *fileMenu = menuBar()->addMenu(tr("&File")); + QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &MainWindow::open); openAct->setShortcuts(QKeySequence::Open); - connect(openAct, SIGNAL(triggered()), this, SLOT(open())); - saveAsAct = new QAction(tr("&Save As..."), this); + QAction *saveAsAct = fileMenu->addAction(tr("&Save As..."), this, &MainWindow::saveAs); saveAsAct->setShortcuts(QKeySequence::SaveAs); - connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); - exitAct = new QAction(tr("E&xit"), this); + QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close); exitAct->setShortcuts(QKeySequence::Quit); - connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); - - aboutAct = new QAction(tr("&About"), this); - connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); - - aboutQtAct = new QAction(tr("About &Qt"), this); - connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); -} - -void MainWindow::createMenus() -{ - fileMenu = menuBar()->addMenu(tr("&File")); - fileMenu->addAction(openAct); - fileMenu->addAction(saveAsAct); - fileMenu->addAction(exitAct); menuBar()->addSeparator(); - helpMenu = menuBar()->addMenu(tr("&Help")); - helpMenu->addAction(aboutAct); - helpMenu->addAction(aboutQtAct); + QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(tr("&About"), this, &MainWindow::about); + helpMenu->addAction(tr("About &Qt"), qApp, &QCoreApplication::quit); } diff --git a/examples/xml/dombookmarks/mainwindow.h b/examples/xml/dombookmarks/mainwindow.h index 6d490caf64..0a8c7bdd82 100644 --- a/examples/xml/dombookmarks/mainwindow.h +++ b/examples/xml/dombookmarks/mainwindow.h @@ -68,18 +68,9 @@ public slots: void about(); private: - void createActions(); void createMenus(); XbelTree *xbelTree; - - QMenu *fileMenu; - QMenu *helpMenu; - QAction *openAct; - QAction *saveAsAct; - QAction *exitAct; - QAction *aboutAct; - QAction *aboutQtAct; }; #endif diff --git a/examples/xml/dombookmarks/xbeltree.cpp b/examples/xml/dombookmarks/xbeltree.cpp index 82afc48bd9..f7ff1de638 100644 --- a/examples/xml/dombookmarks/xbeltree.cpp +++ b/examples/xml/dombookmarks/xbeltree.cpp @@ -52,6 +52,18 @@ #include "xbeltree.h" +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 inline QString versionAttribute() { return QStringLiteral("version"); } +static inline QString hrefAttribute() { return QStringLiteral("href"); } +static inline QString foldedAttribute() { return QStringLiteral("folded"); } + XbelTree::XbelTree(QWidget *parent) : QTreeWidget(parent) { @@ -68,6 +80,24 @@ XbelTree::XbelTree(QWidget *parent) bookmarkIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon)); } +#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD) +void XbelTree::contextMenuEvent(QContextMenuEvent *event) +{ + const QTreeWidgetItem *item = itemAt(event->pos()); + if (!item) + return; + const QString url = item->text(1); + QMenu contextMenu; + QAction *copyAction = contextMenu.addAction(tr("Copy Link to Clipboard")); + QAction *openAction = contextMenu.addAction(tr("Open")); + QAction *action = contextMenu.exec(event->globalPos()); + if (action == copyAction) + QGuiApplication::clipboard()->setText(url); + else if (action == openAction) + QDesktopServices::openUrl(QUrl(url)); +} +#endif // !QT_NO_CONTEXTMENU && !QT_NO_CLIPBOARD + bool XbelTree::read(QIODevice *device) { QString errorStr; @@ -89,8 +119,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("version") - && root.attribute("version") != "1.0") { + } else if (root.hasAttribute(versionAttribute()) + && root.attribute(versionAttribute()) != QLatin1String("1.0")) { QMessageBox::information(window(), tr("DOM Bookmarks"), tr("The file is not an XBEL version 1.0 " "file.")); @@ -99,22 +129,20 @@ bool XbelTree::read(QIODevice *device) clear(); - disconnect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), - this, SLOT(updateDomElement(QTreeWidgetItem*,int))); + disconnect(this, &QTreeWidget::itemChanged, this, &XbelTree::updateDomElement); - QDomElement child = root.firstChildElement("folder"); + QDomElement child = root.firstChildElement(folderElement()); while (!child.isNull()) { parseFolderElement(child); - child = child.nextSiblingElement("folder"); + child = child.nextSiblingElement(folderElement()); } - connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), - this, SLOT(updateDomElement(QTreeWidgetItem*,int))); + connect(this, &QTreeWidget::itemChanged, this, &XbelTree::updateDomElement); return true; } -bool XbelTree::write(QIODevice *device) +bool XbelTree::write(QIODevice *device) const { const int IndentSize = 4; @@ -123,21 +151,21 @@ bool XbelTree::write(QIODevice *device) return true; } -void XbelTree::updateDomElement(QTreeWidgetItem *item, int column) +void XbelTree::updateDomElement(const QTreeWidgetItem *item, int column) { - QDomElement element = domElementForItem.value(item); + QDomElement element = item->data(0, DomElementRole).value(); if (!element.isNull()) { if (column == 0) { - QDomElement oldTitleElement = element.firstChildElement("title"); - QDomElement newTitleElement = domDocument.createElement("title"); + 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() == "bookmark") - element.setAttribute("href", item->text(1)); + if (element.tagName() == bookmarkElement()) + element.setAttribute(hrefAttribute(), item->text(1)); } } } @@ -147,7 +175,7 @@ void XbelTree::parseFolderElement(const QDomElement &element, { QTreeWidgetItem *item = createItem(element, parentItem); - QString title = element.firstChildElement("title").text(); + QString title = element.firstChildElement(titleElement()).text(); if (title.isEmpty()) title = QObject::tr("Folder"); @@ -155,25 +183,25 @@ void XbelTree::parseFolderElement(const QDomElement &element, item->setIcon(0, folderIcon); item->setText(0, title); - bool folded = (element.attribute("folded") != "no"); + bool folded = (element.attribute(foldedAttribute()) != QLatin1String("no")); setItemExpanded(item, !folded); QDomElement child = element.firstChildElement(); while (!child.isNull()) { - if (child.tagName() == "folder") { + if (child.tagName() == folderElement()) { parseFolderElement(child, item); - } else if (child.tagName() == "bookmark") { + } else if (child.tagName() == bookmarkElement()) { QTreeWidgetItem *childItem = createItem(child, item); - QString title = child.firstChildElement("title").text(); + QString title = child.firstChildElement(titleElement()).text(); if (title.isEmpty()) title = QObject::tr("Folder"); childItem->setFlags(item->flags() | Qt::ItemIsEditable); childItem->setIcon(0, bookmarkIcon); childItem->setText(0, title); - childItem->setText(1, child.attribute("href")); - } else if (child.tagName() == "separator") { + childItem->setText(1, child.attribute(hrefAttribute())); + } else if (child.tagName() == QLatin1String("separator")) { QTreeWidgetItem *childItem = createItem(child, item); childItem->setFlags(item->flags() & ~(Qt::ItemIsSelectable | Qt::ItemIsEditable)); childItem->setText(0, QString(30, 0xB7)); @@ -191,6 +219,6 @@ QTreeWidgetItem *XbelTree::createItem(const QDomElement &element, } else { item = new QTreeWidgetItem(this); } - domElementForItem.insert(item, element); + item->setData(0, DomElementRole, QVariant::fromValue(element)); return item; } diff --git a/examples/xml/dombookmarks/xbeltree.h b/examples/xml/dombookmarks/xbeltree.h index bf4b55ea74..f6c7ef8bfe 100644 --- a/examples/xml/dombookmarks/xbeltree.h +++ b/examples/xml/dombookmarks/xbeltree.h @@ -52,7 +52,6 @@ #define XBELTREE_H #include -#include #include #include @@ -64,10 +63,15 @@ public: XbelTree(QWidget *parent = 0); bool read(QIODevice *device); - bool write(QIODevice *device); + bool write(QIODevice *device) const; + +protected: +#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD) + void contextMenuEvent(QContextMenuEvent *event) Q_DECL_OVERRIDE; +#endif private slots: - void updateDomElement(QTreeWidgetItem *item, int column); + void updateDomElement(const QTreeWidgetItem *item, int column); private: void parseFolderElement(const QDomElement &element, @@ -76,7 +80,6 @@ private: QTreeWidgetItem *parentItem = 0); QDomDocument domDocument; - QHash domElementForItem; QIcon folderIcon; QIcon bookmarkIcon; }; -- cgit v1.2.3