1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtDebug>
30
31#include "TestContainer.h"
32
33#include "TreeModel.h"
34
35using namespace QPatternistSDK;
36
37TreeModel::TreeModel(const QStringList columnData,
38 QObject *p) : QAbstractItemModel(p),
39 m_root(0),
40 m_columnData(columnData)
41{
42}
43
44TreeModel::~TreeModel()
45{
46}
47
48QVariant TreeModel::data(const QModelIndex &idx, int role) const
49{
50 if(!idx.isValid())
51 return QVariant();
52
53 TreeItem *item = static_cast<TreeItem *>(idx.internalPointer());
54 Q_ASSERT(item);
55
56 return item->data(role: static_cast<Qt::ItemDataRole>(role), column: idx.column());
57}
58
59QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
60{
61 if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
62 return m_columnData.value(i: section);
63
64 return QVariant();
65}
66
67void TreeModel::childChanged(TreeItem *item)
68{
69 if (item) {
70 const QModelIndex index = createIndex(arow: item->row(), acolumn: 0, adata: item);
71 dataChanged(topLeft: index, bottomRight: index);
72 } else {
73 layoutChanged();
74 }
75}
76
77QModelIndex TreeModel::index(int row, int column, const QModelIndex &p) const
78{
79 const int c = columnCount(parent: p);
80
81 if(row < 0 || column < 0 || column >= c)
82 return QModelIndex();
83
84 TreeItem *parentItem;
85
86 if(p.isValid())
87 parentItem = static_cast<TreeItem *>(p.internalPointer());
88 else
89 parentItem = m_root;
90
91 if(!parentItem)
92 return QModelIndex();
93
94 TreeItem *childItem = parentItem->child(row);
95
96 if(childItem)
97 return createIndex(arow: row, acolumn: column, adata: childItem);
98 else
99 return QModelIndex();
100}
101
102QModelIndex TreeModel::parent(const QModelIndex &idx) const
103{
104 if(!idx.isValid())
105 return QModelIndex();
106
107 TreeItem *childItem = static_cast<TreeItem *>(idx.internalPointer());
108 Q_ASSERT(childItem);
109 TreeItem *parentItem = childItem->parent();
110
111 if(!parentItem || parentItem == m_root)
112 return QModelIndex();
113
114 Q_ASSERT(parentItem);
115 return createIndex(arow: parentItem->row(), acolumn: 0, adata: parentItem);
116}
117
118Qt::ItemFlags TreeModel::flags(const QModelIndex &idx) const
119{
120 /* Not sure about this code. */
121 if(!idx.isValid())
122 return Qt::ItemFlags();
123
124 return QAbstractItemModel::flags(index: idx);
125}
126
127int TreeModel::rowCount(const QModelIndex &p) const
128{
129 if(p.column() > 0)
130 return 0;
131
132 const TreeItem *parentItem;
133
134 if(p.isValid())
135 parentItem = static_cast<TreeItem *>(p.internalPointer());
136 else
137 {
138 if(m_root)
139 parentItem = m_root;
140 else
141 return 0;
142 }
143
144 return parentItem->childCount();
145}
146
147int TreeModel::columnCount(const QModelIndex &p) const
148{
149 if(p.isValid())
150 return static_cast<TreeItem *>(p.internalPointer())->columnCount();
151 else
152 return m_columnData.count();
153}
154
155TreeItem *TreeModel::root() const
156{
157 return m_root;
158}
159
160void TreeModel::setRoot(TreeItem *r)
161{
162 TreeItem *const oldRoot = m_root;
163
164 /* Notify views that we are radically changing. */
165 beginResetModel();
166 m_root = r;
167
168 if(m_root)
169 connect(asender: r, SIGNAL(changed(TreeItem *)), SLOT(childChanged(TreeItem *)));
170
171 endResetModel();
172
173 delete oldRoot;
174}
175
176// vim: et:ts=4:sw=4:sts=4
177

source code of qtxmlpatterns/tests/auto/xmlpatternssdk/TreeModel.cpp