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/streambookmarks/xbelwriter.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'examples/xml/streambookmarks/xbelwriter.cpp') diff --git a/examples/xml/streambookmarks/xbelwriter.cpp b/examples/xml/streambookmarks/xbelwriter.cpp index fdf2e1095e..2959680678 100644 --- a/examples/xml/streambookmarks/xbelwriter.cpp +++ b/examples/xml/streambookmarks/xbelwriter.cpp @@ -51,9 +51,14 @@ #include #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(QTreeWidget *treeWidget) +XbelWriter::XbelWriter(const QTreeWidget *treeWidget) : treeWidget(treeWidget) { xml.setAutoFormatting(true); @@ -66,9 +71,9 @@ bool XbelWriter::writeFile(QIODevice *device) xml.setDevice(device); xml.writeStartDocument(); - xml.writeDTD(""); - xml.writeStartElement("xbel"); - xml.writeAttribute("version", "1.0"); + xml.writeDTD(QStringLiteral("")); + xml.writeStartElement(QStringLiteral("xbel")); + xml.writeAttribute(XbelReader::versionAttribute(), QStringLiteral("1.0")); for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) writeItem(treeWidget->topLevelItem(i)); @@ -78,24 +83,24 @@ bool XbelWriter::writeFile(QIODevice *device) //! [1] //! [2] -void XbelWriter::writeItem(QTreeWidgetItem *item) +void XbelWriter::writeItem(const QTreeWidgetItem *item) { QString tagName = item->data(0, Qt::UserRole).toString(); - if (tagName == "folder") { + if (tagName == QLatin1String("folder")) { bool folded = !treeWidget->isItemExpanded(item); xml.writeStartElement(tagName); - xml.writeAttribute("folded", folded ? "yes" : "no"); - xml.writeTextElement("title", item->text(0)); + 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 == "bookmark") { + } else if (tagName == QLatin1String("bookmark")) { xml.writeStartElement(tagName); if (!item->text(1).isEmpty()) - xml.writeAttribute("href", item->text(1)); - xml.writeTextElement("title", item->text(0)); + xml.writeAttribute(XbelReader::hrefAttribute(), item->text(1)); + xml.writeTextElement(titleElement(), item->text(0)); xml.writeEndElement(); - } else if (tagName == "separator") { + } else if (tagName == QLatin1String("separator")) { xml.writeEmptyElement(tagName); } } -- cgit v1.2.3