|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
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 http://www.qtsoftware.com/contact. |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 #include <QtXml> |
|
44 |
|
45 #include "domitem.h" |
|
46 #include "dommodel.h" |
|
47 |
|
48 DomModel::DomModel(QDomDocument document, QObject *parent) |
|
49 : QAbstractItemModel(parent), domDocument(document) |
|
50 { |
|
51 rootItem = new DomItem(domDocument, 0); |
|
52 } |
|
53 |
|
54 DomModel::~DomModel() |
|
55 { |
|
56 delete rootItem; |
|
57 } |
|
58 |
|
59 int DomModel::columnCount(const QModelIndex &/*parent*/) const |
|
60 { |
|
61 return 3; |
|
62 } |
|
63 |
|
64 QVariant DomModel::data(const QModelIndex &index, int role) const |
|
65 { |
|
66 if (!index.isValid()) |
|
67 return QVariant(); |
|
68 |
|
69 if (role != Qt::DisplayRole) |
|
70 return QVariant(); |
|
71 |
|
72 DomItem *item = static_cast<DomItem*>(index.internalPointer()); |
|
73 |
|
74 QDomNode node = item->node(); |
|
75 QStringList attributes; |
|
76 QDomNamedNodeMap attributeMap = node.attributes(); |
|
77 |
|
78 switch (index.column()) { |
|
79 case 0: |
|
80 return node.nodeName(); |
|
81 case 1: |
|
82 for (int i = 0; i < attributeMap.count(); ++i) { |
|
83 QDomNode attribute = attributeMap.item(i); |
|
84 attributes << attribute.nodeName() + "=\"" |
|
85 +attribute.nodeValue() + "\""; |
|
86 } |
|
87 return attributes.join(" "); |
|
88 case 2: |
|
89 return node.nodeValue().split("\n").join(" "); |
|
90 default: |
|
91 return QVariant(); |
|
92 } |
|
93 } |
|
94 |
|
95 Qt::ItemFlags DomModel::flags(const QModelIndex &index) const |
|
96 { |
|
97 if (!index.isValid()) |
|
98 return 0; |
|
99 |
|
100 return Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
|
101 } |
|
102 |
|
103 QVariant DomModel::headerData(int section, Qt::Orientation orientation, |
|
104 int role) const |
|
105 { |
|
106 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
|
107 switch (section) { |
|
108 case 0: |
|
109 return tr("Name"); |
|
110 case 1: |
|
111 return tr("Attributes"); |
|
112 case 2: |
|
113 return tr("Value"); |
|
114 default: |
|
115 return QVariant(); |
|
116 } |
|
117 } |
|
118 |
|
119 return QVariant(); |
|
120 } |
|
121 |
|
122 QModelIndex DomModel::index(int row, int column, const QModelIndex &parent) |
|
123 const |
|
124 { |
|
125 if (!hasIndex(row, column, parent)) |
|
126 return QModelIndex(); |
|
127 |
|
128 DomItem *parentItem; |
|
129 |
|
130 if (!parent.isValid()) |
|
131 parentItem = rootItem; |
|
132 else |
|
133 parentItem = static_cast<DomItem*>(parent.internalPointer()); |
|
134 |
|
135 DomItem *childItem = parentItem->child(row); |
|
136 if (childItem) |
|
137 return createIndex(row, column, childItem); |
|
138 else |
|
139 return QModelIndex(); |
|
140 } |
|
141 |
|
142 QModelIndex DomModel::parent(const QModelIndex &child) const |
|
143 { |
|
144 if (!child.isValid()) |
|
145 return QModelIndex(); |
|
146 |
|
147 DomItem *childItem = static_cast<DomItem*>(child.internalPointer()); |
|
148 DomItem *parentItem = childItem->parent(); |
|
149 |
|
150 if (!parentItem || parentItem == rootItem) |
|
151 return QModelIndex(); |
|
152 |
|
153 return createIndex(parentItem->row(), 0, parentItem); |
|
154 } |
|
155 |
|
156 int DomModel::rowCount(const QModelIndex &parent) const |
|
157 { |
|
158 if (parent.column() > 0) |
|
159 return 0; |
|
160 |
|
161 DomItem *parentItem; |
|
162 |
|
163 if (!parent.isValid()) |
|
164 parentItem = rootItem; |
|
165 else |
|
166 parentItem = static_cast<DomItem*>(parent.internalPointer()); |
|
167 |
|
168 return parentItem->node().childNodes().count(); |
|
169 } |