0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the examples of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include <QtGui>
|
|
43 |
|
|
44 |
#include "xbeltree.h"
|
|
45 |
|
|
46 |
XbelTree::XbelTree(QWidget *parent)
|
|
47 |
: QTreeWidget(parent)
|
|
48 |
{
|
|
49 |
QStringList labels;
|
|
50 |
labels << tr("Title") << tr("Location");
|
|
51 |
|
|
52 |
header()->setResizeMode(QHeaderView::Stretch);
|
|
53 |
setHeaderLabels(labels);
|
|
54 |
|
|
55 |
folderIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon),
|
|
56 |
QIcon::Normal, QIcon::Off);
|
|
57 |
folderIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon),
|
|
58 |
QIcon::Normal, QIcon::On);
|
|
59 |
bookmarkIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon));
|
|
60 |
}
|
|
61 |
|
|
62 |
bool XbelTree::read(QIODevice *device)
|
|
63 |
{
|
|
64 |
QString errorStr;
|
|
65 |
int errorLine;
|
|
66 |
int errorColumn;
|
|
67 |
|
|
68 |
if (!domDocument.setContent(device, true, &errorStr, &errorLine,
|
|
69 |
&errorColumn)) {
|
|
70 |
QMessageBox::information(window(), tr("DOM Bookmarks"),
|
|
71 |
tr("Parse error at line %1, column %2:\n%3")
|
|
72 |
.arg(errorLine)
|
|
73 |
.arg(errorColumn)
|
|
74 |
.arg(errorStr));
|
|
75 |
return false;
|
|
76 |
}
|
|
77 |
|
|
78 |
QDomElement root = domDocument.documentElement();
|
|
79 |
if (root.tagName() != "xbel") {
|
|
80 |
QMessageBox::information(window(), tr("DOM Bookmarks"),
|
|
81 |
tr("The file is not an XBEL file."));
|
|
82 |
return false;
|
|
83 |
} else if (root.hasAttribute("version")
|
|
84 |
&& root.attribute("version") != "1.0") {
|
|
85 |
QMessageBox::information(window(), tr("DOM Bookmarks"),
|
|
86 |
tr("The file is not an XBEL version 1.0 "
|
|
87 |
"file."));
|
|
88 |
return false;
|
|
89 |
}
|
|
90 |
|
|
91 |
clear();
|
|
92 |
|
|
93 |
disconnect(this, SIGNAL(itemChanged(QTreeWidgetItem *, int)),
|
|
94 |
this, SLOT(updateDomElement(QTreeWidgetItem *, int)));
|
|
95 |
|
|
96 |
QDomElement child = root.firstChildElement("folder");
|
|
97 |
while (!child.isNull()) {
|
|
98 |
parseFolderElement(child);
|
|
99 |
child = child.nextSiblingElement("folder");
|
|
100 |
}
|
|
101 |
|
|
102 |
connect(this, SIGNAL(itemChanged(QTreeWidgetItem *, int)),
|
|
103 |
this, SLOT(updateDomElement(QTreeWidgetItem *, int)));
|
|
104 |
|
|
105 |
return true;
|
|
106 |
}
|
|
107 |
|
|
108 |
bool XbelTree::write(QIODevice *device)
|
|
109 |
{
|
|
110 |
const int IndentSize = 4;
|
|
111 |
|
|
112 |
QTextStream out(device);
|
|
113 |
domDocument.save(out, IndentSize);
|
|
114 |
return true;
|
|
115 |
}
|
|
116 |
|
|
117 |
void XbelTree::updateDomElement(QTreeWidgetItem *item, int column)
|
|
118 |
{
|
|
119 |
QDomElement element = domElementForItem.value(item);
|
|
120 |
if (!element.isNull()) {
|
|
121 |
if (column == 0) {
|
|
122 |
QDomElement oldTitleElement = element.firstChildElement("title");
|
|
123 |
QDomElement newTitleElement = domDocument.createElement("title");
|
|
124 |
|
|
125 |
QDomText newTitleText = domDocument.createTextNode(item->text(0));
|
|
126 |
newTitleElement.appendChild(newTitleText);
|
|
127 |
|
|
128 |
element.replaceChild(newTitleElement, oldTitleElement);
|
|
129 |
} else {
|
|
130 |
if (element.tagName() == "bookmark")
|
|
131 |
element.setAttribute("href", item->text(1));
|
|
132 |
}
|
|
133 |
}
|
|
134 |
}
|
|
135 |
|
|
136 |
void XbelTree::parseFolderElement(const QDomElement &element,
|
|
137 |
QTreeWidgetItem *parentItem)
|
|
138 |
{
|
|
139 |
QTreeWidgetItem *item = createItem(element, parentItem);
|
|
140 |
|
|
141 |
QString title = element.firstChildElement("title").text();
|
|
142 |
if (title.isEmpty())
|
|
143 |
title = QObject::tr("Folder");
|
|
144 |
|
|
145 |
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
|
146 |
item->setIcon(0, folderIcon);
|
|
147 |
item->setText(0, title);
|
|
148 |
|
|
149 |
bool folded = (element.attribute("folded") != "no");
|
|
150 |
setItemExpanded(item, !folded);
|
|
151 |
|
|
152 |
QDomElement child = element.firstChildElement();
|
|
153 |
while (!child.isNull()) {
|
|
154 |
if (child.tagName() == "folder") {
|
|
155 |
parseFolderElement(child, item);
|
|
156 |
} else if (child.tagName() == "bookmark") {
|
|
157 |
QTreeWidgetItem *childItem = createItem(child, item);
|
|
158 |
|
|
159 |
QString title = child.firstChildElement("title").text();
|
|
160 |
if (title.isEmpty())
|
|
161 |
title = QObject::tr("Folder");
|
|
162 |
|
|
163 |
childItem->setFlags(item->flags() | Qt::ItemIsEditable);
|
|
164 |
childItem->setIcon(0, bookmarkIcon);
|
|
165 |
childItem->setText(0, title);
|
|
166 |
childItem->setText(1, child.attribute("href"));
|
|
167 |
} else if (child.tagName() == "separator") {
|
|
168 |
QTreeWidgetItem *childItem = createItem(child, item);
|
|
169 |
childItem->setFlags(item->flags() & ~(Qt::ItemIsSelectable | Qt::ItemIsEditable));
|
|
170 |
childItem->setText(0, QString(30, 0xB7));
|
|
171 |
}
|
|
172 |
child = child.nextSiblingElement();
|
|
173 |
}
|
|
174 |
}
|
|
175 |
|
|
176 |
QTreeWidgetItem *XbelTree::createItem(const QDomElement &element,
|
|
177 |
QTreeWidgetItem *parentItem)
|
|
178 |
{
|
|
179 |
QTreeWidgetItem *item;
|
|
180 |
if (parentItem) {
|
|
181 |
item = new QTreeWidgetItem(parentItem);
|
|
182 |
} else {
|
|
183 |
item = new QTreeWidgetItem(this);
|
|
184 |
}
|
|
185 |
domElementForItem.insert(item, element);
|
|
186 |
return item;
|
|
187 |
}
|