source: trunk/examples/xml/dombookmarks/xbeltree.cpp@ 385

Last change on this file since 385 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "xbeltree.h"
45
46XbelTree::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
62bool 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
108bool 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
117void 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
136void 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
176QTreeWidgetItem *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}
Note: See TracBrowser for help on using the repository browser.