1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "xbeltree.h"
|
---|
44 |
|
---|
45 | XbelTree::XbelTree(QWidget *parent)
|
---|
46 | : QTreeWidget(parent)
|
---|
47 | {
|
---|
48 | QStringList labels;
|
---|
49 | labels << tr("Title") << tr("Location");
|
---|
50 |
|
---|
51 | header()->setResizeMode(QHeaderView::Stretch);
|
---|
52 | setHeaderLabels(labels);
|
---|
53 |
|
---|
54 | folderIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon),
|
---|
55 | QIcon::Normal, QIcon::Off);
|
---|
56 | folderIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon),
|
---|
57 | QIcon::Normal, QIcon::On);
|
---|
58 | bookmarkIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon));
|
---|
59 | }
|
---|
60 |
|
---|
61 | bool XbelTree::read(QIODevice *device)
|
---|
62 | {
|
---|
63 | QString errorStr;
|
---|
64 | int errorLine;
|
---|
65 | int errorColumn;
|
---|
66 |
|
---|
67 | if (!domDocument.setContent(device, true, &errorStr, &errorLine,
|
---|
68 | &errorColumn)) {
|
---|
69 | QMessageBox::information(window(), tr("DOM Bookmarks"),
|
---|
70 | tr("Parse error at line %1, column %2:\n%3")
|
---|
71 | .arg(errorLine)
|
---|
72 | .arg(errorColumn)
|
---|
73 | .arg(errorStr));
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | QDomElement root = domDocument.documentElement();
|
---|
78 | if (root.tagName() != "xbel") {
|
---|
79 | QMessageBox::information(window(), tr("DOM Bookmarks"),
|
---|
80 | tr("The file is not an XBEL file."));
|
---|
81 | return false;
|
---|
82 | } else if (root.hasAttribute("version")
|
---|
83 | && root.attribute("version") != "1.0") {
|
---|
84 | QMessageBox::information(window(), tr("DOM Bookmarks"),
|
---|
85 | tr("The file is not an XBEL version 1.0 "
|
---|
86 | "file."));
|
---|
87 | return false;
|
---|
88 | }
|
---|
89 |
|
---|
90 | clear();
|
---|
91 |
|
---|
92 | disconnect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
|
---|
93 | this, SLOT(updateDomElement(QTreeWidgetItem*,int)));
|
---|
94 |
|
---|
95 | QDomElement child = root.firstChildElement("folder");
|
---|
96 | while (!child.isNull()) {
|
---|
97 | parseFolderElement(child);
|
---|
98 | child = child.nextSiblingElement("folder");
|
---|
99 | }
|
---|
100 |
|
---|
101 | connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
|
---|
102 | this, SLOT(updateDomElement(QTreeWidgetItem*,int)));
|
---|
103 |
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 |
|
---|
107 | bool XbelTree::write(QIODevice *device)
|
---|
108 | {
|
---|
109 | const int IndentSize = 4;
|
---|
110 |
|
---|
111 | QTextStream out(device);
|
---|
112 | domDocument.save(out, IndentSize);
|
---|
113 | return true;
|
---|
114 | }
|
---|
115 |
|
---|
116 | void XbelTree::updateDomElement(QTreeWidgetItem *item, int column)
|
---|
117 | {
|
---|
118 | QDomElement element = domElementForItem.value(item);
|
---|
119 | if (!element.isNull()) {
|
---|
120 | if (column == 0) {
|
---|
121 | QDomElement oldTitleElement = element.firstChildElement("title");
|
---|
122 | QDomElement newTitleElement = domDocument.createElement("title");
|
---|
123 |
|
---|
124 | QDomText newTitleText = domDocument.createTextNode(item->text(0));
|
---|
125 | newTitleElement.appendChild(newTitleText);
|
---|
126 |
|
---|
127 | element.replaceChild(newTitleElement, oldTitleElement);
|
---|
128 | } else {
|
---|
129 | if (element.tagName() == "bookmark")
|
---|
130 | element.setAttribute("href", item->text(1));
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | void XbelTree::parseFolderElement(const QDomElement &element,
|
---|
136 | QTreeWidgetItem *parentItem)
|
---|
137 | {
|
---|
138 | QTreeWidgetItem *item = createItem(element, parentItem);
|
---|
139 |
|
---|
140 | QString title = element.firstChildElement("title").text();
|
---|
141 | if (title.isEmpty())
|
---|
142 | title = QObject::tr("Folder");
|
---|
143 |
|
---|
144 | item->setFlags(item->flags() | Qt::ItemIsEditable);
|
---|
145 | item->setIcon(0, folderIcon);
|
---|
146 | item->setText(0, title);
|
---|
147 |
|
---|
148 | bool folded = (element.attribute("folded") != "no");
|
---|
149 | setItemExpanded(item, !folded);
|
---|
150 |
|
---|
151 | QDomElement child = element.firstChildElement();
|
---|
152 | while (!child.isNull()) {
|
---|
153 | if (child.tagName() == "folder") {
|
---|
154 | parseFolderElement(child, item);
|
---|
155 | } else if (child.tagName() == "bookmark") {
|
---|
156 | QTreeWidgetItem *childItem = createItem(child, item);
|
---|
157 |
|
---|
158 | QString title = child.firstChildElement("title").text();
|
---|
159 | if (title.isEmpty())
|
---|
160 | title = QObject::tr("Folder");
|
---|
161 |
|
---|
162 | childItem->setFlags(item->flags() | Qt::ItemIsEditable);
|
---|
163 | childItem->setIcon(0, bookmarkIcon);
|
---|
164 | childItem->setText(0, title);
|
---|
165 | childItem->setText(1, child.attribute("href"));
|
---|
166 | } else if (child.tagName() == "separator") {
|
---|
167 | QTreeWidgetItem *childItem = createItem(child, item);
|
---|
168 | childItem->setFlags(item->flags() & ~(Qt::ItemIsSelectable | Qt::ItemIsEditable));
|
---|
169 | childItem->setText(0, QString(30, 0xB7));
|
---|
170 | }
|
---|
171 | child = child.nextSiblingElement();
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | QTreeWidgetItem *XbelTree::createItem(const QDomElement &element,
|
---|
176 | QTreeWidgetItem *parentItem)
|
---|
177 | {
|
---|
178 | QTreeWidgetItem *item;
|
---|
179 | if (parentItem) {
|
---|
180 | item = new QTreeWidgetItem(parentItem);
|
---|
181 | } else {
|
---|
182 | item = new QTreeWidgetItem(this);
|
---|
183 | }
|
---|
184 | domElementForItem.insert(item, element);
|
---|
185 | return item;
|
---|
186 | }
|
---|