1
/****************************************************************************
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
32
Provides a simple tree model to show how to create and use hierarchical
39
#include "treemodel.h"
41
TreeModel::TreeModel(const QString &data, QObject *parent)
42
: QAbstractItemModel(parent)
44
QList<QVariant> rootData;
45
rootData << "Title" << "Summary";
46
rootItem = new TreeItem(rootData);
47
setupModelData(data.split(QString("\n")), rootItem);
50
TreeModel::~TreeModel()
55
int TreeModel::columnCount(const QModelIndex &parent) const
58
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
60
return rootItem->columnCount();
63
QVariant TreeModel::data(const QModelIndex &index, int role) const
68
if (role != Qt::DisplayRole)
71
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
73
return item->data(index.column());
76
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
79
return Qt::ItemIsEnabled;
81
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
84
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
87
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
88
return rootItem->data(section);
93
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
98
if (!parent.isValid())
99
parentItem = rootItem;
101
parentItem = static_cast<TreeItem*>(parent.internalPointer());
103
TreeItem *childItem = parentItem->child(row);
105
return createIndex(row, column, childItem);
107
return QModelIndex();
110
QModelIndex TreeModel::parent(const QModelIndex &index) const
112
if (!index.isValid())
113
return QModelIndex();
115
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
116
TreeItem *parentItem = childItem->parent();
118
if (parentItem == rootItem)
119
return QModelIndex();
121
return createIndex(parentItem->row(), 0, parentItem);
124
int TreeModel::rowCount(const QModelIndex &parent) const
126
TreeItem *parentItem;
128
if (!parent.isValid())
129
parentItem = rootItem;
131
parentItem = static_cast<TreeItem*>(parent.internalPointer());
133
return parentItem->childCount();
136
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
138
QList<TreeItem*> parents;
139
QList<int> indentations;
145
while (number < lines.count()) {
147
while (position < lines[number].length()) {
148
if (lines[number].mid(position, 1) != " ")
153
QString lineData = lines[number].mid(position).trimmed();
155
if (!lineData.isEmpty()) {
156
// Read the column data from the rest of the line.
157
QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
158
QList<QVariant> columnData;
159
for (int column = 0; column < columnStrings.count(); ++column)
160
columnData << columnStrings[column];
162
if (position > indentations.last()) {
163
// The last child of the current parent is now the new parent
164
// unless the current parent has no children.
166
if (parents.last()->childCount() > 0) {
167
parents << parents.last()->child(parents.last()->childCount()-1);
168
indentations << position;
171
while (position < indentations.last() && parents.count() > 0) {
173
indentations.pop_back();
177
// Append a new item to the current parent's list of children.
178
parents.last()->appendChild(new TreeItem(columnData, parents.last()));