1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #include <QtGui>
42 #include <QtXml>
43 
44 #include "domitem.h"
45 #include "dommodel.h"
46 
47 //! [0]
DomModel(QDomDocument document,QObject * parent)48 DomModel::DomModel(QDomDocument document, QObject *parent)
49     : QAbstractItemModel(parent), domDocument(document)
50 {
51     rootItem = new DomItem(domDocument, 0);
52 }
53 //! [0]
54 
55 //! [1]
~DomModel()56 DomModel::~DomModel()
57 {
58     delete rootItem;
59 }
60 //! [1]
61 
62 //! [2]
columnCount(const QModelIndex &) const63 int DomModel::columnCount(const QModelIndex &/*parent*/) const
64 {
65     return 3;
66 }
67 //! [2]
68 
69 //! [3]
data(const QModelIndex & index,int role) const70 QVariant DomModel::data(const QModelIndex &index, int role) const
71 {
72     if (!index.isValid())
73         return QVariant();
74 
75     if (role != Qt::DisplayRole)
76         return QVariant();
77 
78     DomItem *item = static_cast<DomItem*>(index.internalPointer());
79 
80     QDomNode node = item->node();
81 //! [3] //! [4]
82     QStringList attributes;
83     QDomNamedNodeMap attributeMap = node.attributes();
84 
85     switch (index.column()) {
86         case 0:
87             return node.nodeName();
88         case 1:
89             for (int i = 0; i < attributeMap.count(); ++i) {
90                 QDomNode attribute = attributeMap.item(i);
91                 attributes << attribute.nodeName() + "=\""
92                               +attribute.nodeValue() + "\"";
93             }
94             return attributes.join(" ");
95         case 2:
96             return node.nodeValue().split("\n").join(" ");
97         default:
98             return QVariant();
99     }
100 }
101 //! [4]
102 
103 //! [5]
flags(const QModelIndex & index) const104 Qt::ItemFlags DomModel::flags(const QModelIndex &index) const
105 {
106     if (!index.isValid())
107         return 0;
108 
109     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
110 }
111 //! [5]
112 
113 //! [6]
headerData(int section,Qt::Orientation orientation,int role) const114 QVariant DomModel::headerData(int section, Qt::Orientation orientation,
115                               int role) const
116 {
117     if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
118         switch (section) {
119             case 0:
120                 return tr("Name");
121             case 1:
122                 return tr("Attributes");
123             case 2:
124                 return tr("Value");
125             default:
126                 return QVariant();
127         }
128     }
129 
130     return QVariant();
131 }
132 //! [6]
133 
134 //! [7]
index(int row,int column,const QModelIndex & parent) const135 QModelIndex DomModel::index(int row, int column, const QModelIndex &parent)
136             const
137 {
138     if (!hasIndex(row, column, parent))
139         return QModelIndex();
140 
141     DomItem *parentItem;
142 
143     if (!parent.isValid())
144         parentItem = rootItem;
145     else
146         parentItem = static_cast<DomItem*>(parent.internalPointer());
147 //! [7]
148 
149 //! [8]
150     DomItem *childItem = parentItem->child(row);
151     if (childItem)
152         return createIndex(row, column, childItem);
153     else
154         return QModelIndex();
155 }
156 //! [8]
157 
158 //! [9]
parent(const QModelIndex & child) const159 QModelIndex DomModel::parent(const QModelIndex &child) const
160 {
161     if (!child.isValid())
162         return QModelIndex();
163 
164     DomItem *childItem = static_cast<DomItem*>(child.internalPointer());
165     DomItem *parentItem = childItem->parent();
166 
167     if (!parentItem || parentItem == rootItem)
168         return QModelIndex();
169 
170     return createIndex(parentItem->row(), 0, parentItem);
171 }
172 //! [9]
173 
174 //! [10]
rowCount(const QModelIndex & parent) const175 int DomModel::rowCount(const QModelIndex &parent) const
176 {
177     if (parent.column() > 0)
178         return 0;
179 
180     DomItem *parentItem;
181 
182     if (!parent.isValid())
183         parentItem = rootItem;
184     else
185         parentItem = static_cast<DomItem*>(parent.internalPointer());
186 
187     return parentItem->node().childNodes().count();
188 }
189 //! [10]
190