1
/****************************************************************************
3
** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be used under the terms of the GNU General Public
8
** License version 2.0 as published by the Free Software Foundation
9
** and appearing in the file LICENSE.GPL included in the packaging of
10
** this file. Please review the following information to ensure GNU
11
** General Public Licensing requirements will be met:
12
** http://www.trolltech.com/products/qt/opensource.html
14
** If you are unsure which license is appropriate for your use, please
15
** review the following information:
16
** http://www.trolltech.com/products/qt/licensing.html or contact the
17
** sales department at sales@trolltech.com.
19
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22
****************************************************************************/
27
Provides a simple tree model to show how to create and use hierarchical
34
#include "treemodel.h"
36
TreeModel::TreeModel(const QString &data, QObject *parent)
37
: QAbstractItemModel(parent)
39
QList<QVariant> rootData;
40
rootData << "Title" << "Summary";
41
rootItem = new TreeItem(rootData);
42
setupModelData(data.split(QString("\n")), rootItem);
45
TreeModel::~TreeModel()
50
int TreeModel::columnCount(const QModelIndex &parent) const
53
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
55
return rootItem->columnCount();
58
QVariant TreeModel::data(const QModelIndex &index, int role) const
63
if (role != Qt::DisplayRole)
66
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
68
return item->data(index.column());
71
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
74
return Qt::ItemIsEnabled;
76
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
79
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
82
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
83
return rootItem->data(section);
88
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
93
if (!parent.isValid())
94
parentItem = rootItem;
96
parentItem = static_cast<TreeItem*>(parent.internalPointer());
98
TreeItem *childItem = parentItem->child(row);
100
return createIndex(row, column, childItem);
102
return QModelIndex();
105
QModelIndex TreeModel::parent(const QModelIndex &index) const
107
if (!index.isValid())
108
return QModelIndex();
110
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
111
TreeItem *parentItem = childItem->parent();
113
if (parentItem == rootItem)
114
return QModelIndex();
116
return createIndex(parentItem->row(), 0, parentItem);
119
int TreeModel::rowCount(const QModelIndex &parent) const
121
TreeItem *parentItem;
123
if (!parent.isValid())
124
parentItem = rootItem;
126
parentItem = static_cast<TreeItem*>(parent.internalPointer());
128
return parentItem->childCount();
131
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
133
QList<TreeItem*> parents;
134
QList<int> indentations;
140
while (number < lines.count()) {
142
while (position < lines[number].length()) {
143
if (lines[number].mid(position, 1) != " ")
148
QString lineData = lines[number].mid(position).trimmed();
150
if (!lineData.isEmpty()) {
151
// Read the column data from the rest of the line.
152
QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
153
QList<QVariant> columnData;
154
for (int column = 0; column < columnStrings.count(); ++column)
155
columnData << columnStrings[column];
157
if (position > indentations.last()) {
158
// The last child of the current parent is now the new parent
159
// unless the current parent has no children.
161
if (parents.last()->childCount() > 0) {
162
parents << parents.last()->child(parents.last()->childCount()-1);
163
indentations << position;
166
while (position < indentations.last() && parents.count() > 0) {
168
indentations.pop_back();
172
// Append a new item to the current parent's list of children.
173
parents.last()->appendChild(new TreeItem(columnData, parents.last()));
1
/****************************************************************************
3
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
7
** This file is part of the examples of the Qt Toolkit.
9
** $QT_BEGIN_LICENSE:BSD$
10
** You may use this file under the terms of the BSD license as follows:
12
** "Redistribution and use in source and binary forms, with or without
13
** modification, are permitted provided that the following conditions are
15
** * Redistributions of source code must retain the above copyright
16
** notice, this list of conditions and the following disclaimer.
17
** * Redistributions in binary form must reproduce the above copyright
18
** notice, this list of conditions and the following disclaimer in
19
** the documentation and/or other materials provided with the
21
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22
** the names of its contributors may be used to endorse or promote
23
** products derived from this software without specific prior written
26
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
44
Provides a simple tree model to show how to create and use hierarchical
51
#include "treemodel.h"
54
TreeModel::TreeModel(const QString &data, QObject *parent)
55
: QAbstractItemModel(parent)
57
QList<QVariant> rootData;
58
rootData << "Title" << "Summary";
59
rootItem = new TreeItem(rootData);
60
setupModelData(data.split(QString("\n")), rootItem);
65
TreeModel::~TreeModel()
72
int TreeModel::columnCount(const QModelIndex &parent) const
75
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
77
return rootItem->columnCount();
82
QVariant TreeModel::data(const QModelIndex &index, int role) const
87
if (role != Qt::DisplayRole)
90
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
92
return item->data(index.column());
97
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
102
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
107
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
110
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
111
return rootItem->data(section);
118
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
121
if (!hasIndex(row, column, parent))
122
return QModelIndex();
124
TreeItem *parentItem;
126
if (!parent.isValid())
127
parentItem = rootItem;
129
parentItem = static_cast<TreeItem*>(parent.internalPointer());
131
TreeItem *childItem = parentItem->child(row);
133
return createIndex(row, column, childItem);
135
return QModelIndex();
140
QModelIndex TreeModel::parent(const QModelIndex &index) const
142
if (!index.isValid())
143
return QModelIndex();
145
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
146
TreeItem *parentItem = childItem->parent();
148
if (parentItem == rootItem)
149
return QModelIndex();
151
return createIndex(parentItem->row(), 0, parentItem);
156
int TreeModel::rowCount(const QModelIndex &parent) const
158
TreeItem *parentItem;
159
if (parent.column() > 0)
162
if (!parent.isValid())
163
parentItem = rootItem;
165
parentItem = static_cast<TreeItem*>(parent.internalPointer());
167
return parentItem->childCount();
171
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
173
QList<TreeItem*> parents;
174
QList<int> indentations;
180
while (number < lines.count()) {
182
while (position < lines[number].length()) {
183
if (lines[number].mid(position, 1) != " ")
188
QString lineData = lines[number].mid(position).trimmed();
190
if (!lineData.isEmpty()) {
191
// Read the column data from the rest of the line.
192
QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
193
QList<QVariant> columnData;
194
for (int column = 0; column < columnStrings.count(); ++column)
195
columnData << columnStrings[column];
197
if (position > indentations.last()) {
198
// The last child of the current parent is now the new parent
199
// unless the current parent has no children.
201
if (parents.last()->childCount() > 0) {
202
parents << parents.last()->child(parents.last()->childCount()-1);
203
indentations << position;
206
while (position < indentations.last() && parents.count() > 0) {
208
indentations.pop_back();
212
// Append a new item to the current parent's list of children.
213
parents.last()->appendChild(new TreeItem(columnData, parents.last()));