1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QtWidgets>
52
53#include "xbeltree.h"
54
55enum { DomElementRole = Qt::UserRole + 1 };
56
57Q_DECLARE_METATYPE(QDomElement)
58
59static inline QString titleElement() { return QStringLiteral("title"); }
60static inline QString folderElement() { return QStringLiteral("folder"); }
61static inline QString bookmarkElement() { return QStringLiteral("bookmark"); }
62
63static inline QString versionAttribute() { return QStringLiteral("version"); }
64static inline QString hrefAttribute() { return QStringLiteral("href"); }
65static inline QString foldedAttribute() { return QStringLiteral("folded"); }
66
67XbelTree::XbelTree(QWidget *parent)
68 : QTreeWidget(parent)
69{
70 QStringList labels;
71 labels << tr(s: "Title") << tr(s: "Location");
72
73 header()->setSectionResizeMode(QHeaderView::Stretch);
74 setHeaderLabels(labels);
75
76 folderIcon.addPixmap(pixmap: style()->standardPixmap(standardPixmap: QStyle::SP_DirClosedIcon),
77 mode: QIcon::Normal, state: QIcon::Off);
78 folderIcon.addPixmap(pixmap: style()->standardPixmap(standardPixmap: QStyle::SP_DirOpenIcon),
79 mode: QIcon::Normal, state: QIcon::On);
80 bookmarkIcon.addPixmap(pixmap: style()->standardPixmap(standardPixmap: QStyle::SP_FileIcon));
81}
82
83#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
84void XbelTree::contextMenuEvent(QContextMenuEvent *event)
85{
86 const QTreeWidgetItem *item = itemAt(p: event->pos());
87 if (!item)
88 return;
89 const QString url = item->text(column: 1);
90 QMenu contextMenu;
91 QAction *copyAction = contextMenu.addAction(text: tr(s: "Copy Link to Clipboard"));
92 QAction *openAction = contextMenu.addAction(text: tr(s: "Open"));
93 QAction *action = contextMenu.exec(pos: event->globalPos());
94 if (action == copyAction)
95 QGuiApplication::clipboard()->setText(url);
96 else if (action == openAction)
97 QDesktopServices::openUrl(url: QUrl(url));
98}
99#endif // !QT_NO_CONTEXTMENU && !QT_NO_CLIPBOARD
100
101bool XbelTree::read(QIODevice *device)
102{
103 QString errorStr;
104 int errorLine;
105 int errorColumn;
106
107 if (!domDocument.setContent(dev: device, namespaceProcessing: true, errorMsg: &errorStr, errorLine: &errorLine,
108 errorColumn: &errorColumn)) {
109 QMessageBox::information(parent: window(), title: tr(s: "DOM Bookmarks"),
110 text: tr(s: "Parse error at line %1, column %2:\n%3")
111 .arg(a: errorLine)
112 .arg(a: errorColumn)
113 .arg(a: errorStr));
114 return false;
115 }
116
117 QDomElement root = domDocument.documentElement();
118 if (root.tagName() != "xbel") {
119 QMessageBox::information(parent: window(), title: tr(s: "DOM Bookmarks"),
120 text: tr(s: "The file is not an XBEL file."));
121 return false;
122 } else if (root.hasAttribute(name: versionAttribute())
123 && root.attribute(name: versionAttribute()) != QLatin1String("1.0")) {
124 QMessageBox::information(parent: window(), title: tr(s: "DOM Bookmarks"),
125 text: tr(s: "The file is not an XBEL version 1.0 "
126 "file."));
127 return false;
128 }
129
130 clear();
131
132 disconnect(sender: this, signal: &QTreeWidget::itemChanged, receiver: this, slot: &XbelTree::updateDomElement);
133
134 QDomElement child = root.firstChildElement(tagName: folderElement());
135 while (!child.isNull()) {
136 parseFolderElement(element: child);
137 child = child.nextSiblingElement(taName: folderElement());
138 }
139
140 connect(sender: this, signal: &QTreeWidget::itemChanged, receiver: this, slot: &XbelTree::updateDomElement);
141
142 return true;
143}
144
145bool XbelTree::write(QIODevice *device) const
146{
147 const int IndentSize = 4;
148
149 QTextStream out(device);
150 domDocument.save(out, IndentSize);
151 return true;
152}
153
154void XbelTree::updateDomElement(const QTreeWidgetItem *item, int column)
155{
156 QDomElement element = qvariant_cast<QDomElement>(v: item->data(column: 0, role: DomElementRole));
157 if (!element.isNull()) {
158 if (column == 0) {
159 QDomElement oldTitleElement = element.firstChildElement(tagName: titleElement());
160 QDomElement newTitleElement = domDocument.createElement(tagName: titleElement());
161
162 QDomText newTitleText = domDocument.createTextNode(data: item->text(column: 0));
163 newTitleElement.appendChild(newChild: newTitleText);
164
165 element.replaceChild(newChild: newTitleElement, oldChild: oldTitleElement);
166 } else {
167 if (element.tagName() == bookmarkElement())
168 element.setAttribute(name: hrefAttribute(), value: item->text(column: 1));
169 }
170 }
171}
172
173void XbelTree::parseFolderElement(const QDomElement &element,
174 QTreeWidgetItem *parentItem)
175{
176 QTreeWidgetItem *item = createItem(element, parentItem);
177
178 QString title = element.firstChildElement(tagName: titleElement()).text();
179 if (title.isEmpty())
180 title = QObject::tr(s: "Folder");
181
182 item->setFlags(item->flags() | Qt::ItemIsEditable);
183 item->setIcon(column: 0, aicon: folderIcon);
184 item->setText(column: 0, atext: title);
185
186 bool folded = (element.attribute(name: foldedAttribute()) != QLatin1String("no"));
187 item->setExpanded(!folded);
188
189 QDomElement child = element.firstChildElement();
190 while (!child.isNull()) {
191 if (child.tagName() == folderElement()) {
192 parseFolderElement(element: child, parentItem: item);
193 } else if (child.tagName() == bookmarkElement()) {
194 QTreeWidgetItem *childItem = createItem(element: child, parentItem: item);
195
196 QString title = child.firstChildElement(tagName: titleElement()).text();
197 if (title.isEmpty())
198 title = QObject::tr(s: "Folder");
199
200 childItem->setFlags(item->flags() | Qt::ItemIsEditable);
201 childItem->setIcon(column: 0, aicon: bookmarkIcon);
202 childItem->setText(column: 0, atext: title);
203 childItem->setText(column: 1, atext: child.attribute(name: hrefAttribute()));
204 } else if (child.tagName() == QLatin1String("separator")) {
205 QTreeWidgetItem *childItem = createItem(element: child, parentItem: item);
206 childItem->setFlags(item->flags() & ~(Qt::ItemIsSelectable | Qt::ItemIsEditable));
207 childItem->setText(column: 0, atext: QString(30, 0xB7));
208 }
209 child = child.nextSiblingElement();
210 }
211}
212
213QTreeWidgetItem *XbelTree::createItem(const QDomElement &element,
214 QTreeWidgetItem *parentItem)
215{
216 QTreeWidgetItem *item;
217 if (parentItem) {
218 item = new QTreeWidgetItem(parentItem);
219 } else {
220 item = new QTreeWidgetItem(this);
221 }
222 item->setData(column: 0, role: DomElementRole, value: QVariant::fromValue(value: element));
223 return item;
224}
225

source code of qtbase/examples/xml/dombookmarks/xbeltree.cpp