0fdc44119d4a2a9064257748376eeb6866d87378
[profile/ivi/qtxmlpatterns.git] / tests / auto / xmlpatternssdk / TreeModel.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtDebug>
43
44 #include "TestContainer.h"
45
46 #include "TreeModel.h"
47
48 using namespace QPatternistSDK;
49
50 TreeModel::TreeModel(const QStringList columnData,
51                      QObject *p) : QAbstractItemModel(p),
52                                    m_root(0),
53                                    m_columnData(columnData)
54 {
55 }
56
57 TreeModel::~TreeModel()
58 {
59 }
60
61 QVariant TreeModel::data(const QModelIndex &idx, int role) const
62 {
63     if(!idx.isValid())
64         return QVariant();
65
66     TreeItem *item = static_cast<TreeItem *>(idx.internalPointer());
67     Q_ASSERT(item);
68
69     return item->data(static_cast<Qt::ItemDataRole>(role), idx.column());
70 }
71
72 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
73 {
74     if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
75         return m_columnData.value(section);
76
77     return QVariant();
78 }
79
80 void TreeModel::childChanged(TreeItem *item)
81 {
82     if (item) {
83         const QModelIndex index = createIndex(item->row(), 0, item);
84         dataChanged(index, index);
85     } else {
86         layoutChanged();
87     }
88 }
89
90 QModelIndex TreeModel::index(int row, int column, const QModelIndex &p) const
91 {
92     const int c = columnCount(p);
93
94     if(row < 0 || column < 0 || column >= c)
95         return QModelIndex();
96
97     TreeItem *parentItem;
98
99     if(p.isValid())
100         parentItem = static_cast<TreeItem *>(p.internalPointer());
101     else
102         parentItem = m_root;
103
104     if(!parentItem)
105         return QModelIndex();
106
107     TreeItem *childItem = parentItem->child(row);
108
109     if(childItem)
110         return createIndex(row, column, childItem);
111     else
112         return QModelIndex();
113 }
114
115 QModelIndex TreeModel::parent(const QModelIndex &idx) const
116 {
117     if(!idx.isValid())
118         return QModelIndex();
119
120     TreeItem *childItem = static_cast<TreeItem *>(idx.internalPointer());
121     Q_ASSERT(childItem);
122     TreeItem *parentItem = childItem->parent();
123
124     if(!parentItem || parentItem == m_root)
125         return QModelIndex();
126
127     Q_ASSERT(parentItem);
128     return createIndex(parentItem->row(), 0, parentItem);
129 }
130
131 Qt::ItemFlags TreeModel::flags(const QModelIndex &idx) const
132 {
133     /* Not sure about this code. */
134     if(!idx.isValid())
135         return Qt::ItemFlags();
136
137     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
138 }
139
140 int TreeModel::rowCount(const QModelIndex &p) const
141 {
142     if(p.column() > 0)
143         return 0;
144
145     const TreeItem *parentItem;
146
147     if(p.isValid())
148         parentItem = static_cast<TreeItem *>(p.internalPointer());
149     else
150     {
151         if(m_root)
152             parentItem = m_root;
153         else
154             return 0;
155     }
156
157     return parentItem->childCount();
158 }
159
160 int TreeModel::columnCount(const QModelIndex &p) const
161 {
162     if(p.isValid())
163         return static_cast<TreeItem *>(p.internalPointer())->columnCount();
164     else
165         return m_columnData.count();
166 }
167
168 TreeItem *TreeModel::root() const
169 {
170     return m_root;
171 }
172
173 void TreeModel::setRoot(TreeItem *r)
174 {
175     TreeItem *const oldRoot = m_root;
176
177     /* Notify views that we are radically changing. */
178     beginResetModel();
179     m_root = r;
180
181     if(m_root)
182         connect(r, SIGNAL(changed(TreeItem *)), SLOT(childChanged(TreeItem *)));
183
184     endResetModel();
185
186     delete oldRoot;
187 }
188
189 // vim: et:ts=4:sw=4:sts=4