~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/itemviews/simpletreemodel/treemodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
/*
 
30
    treemodel.cpp
 
31
 
 
32
    Provides a simple tree model to show how to create and use hierarchical
 
33
    models.
 
34
*/
 
35
 
 
36
#include <QtGui>
 
37
 
 
38
#include "treeitem.h"
 
39
#include "treemodel.h"
 
40
 
 
41
TreeModel::TreeModel(const QString &data, QObject *parent)
 
42
    : QAbstractItemModel(parent)
 
43
{
 
44
    QList<QVariant> rootData;
 
45
    rootData << "Title" << "Summary";
 
46
    rootItem = new TreeItem(rootData);
 
47
    setupModelData(data.split(QString("\n")), rootItem);
 
48
}
 
49
 
 
50
TreeModel::~TreeModel()
 
51
{
 
52
    delete rootItem;
 
53
}
 
54
 
 
55
int TreeModel::columnCount(const QModelIndex &parent) const
 
56
{
 
57
    if (parent.isValid())
 
58
        return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
 
59
    else
 
60
        return rootItem->columnCount();
 
61
}
 
62
 
 
63
QVariant TreeModel::data(const QModelIndex &index, int role) const
 
64
{
 
65
    if (!index.isValid())
 
66
        return QVariant();
 
67
 
 
68
    if (role != Qt::DisplayRole)
 
69
        return QVariant();
 
70
 
 
71
    TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
 
72
 
 
73
    return item->data(index.column());
 
74
}
 
75
 
 
76
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
 
77
{
 
78
    if (!index.isValid())
 
79
        return Qt::ItemIsEnabled;
 
80
 
 
81
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 
82
}
 
83
 
 
84
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
 
85
                               int role) const
 
86
{
 
87
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
 
88
        return rootItem->data(section);
 
89
 
 
90
    return QVariant();
 
91
}
 
92
 
 
93
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
 
94
            const
 
95
{
 
96
    TreeItem *parentItem;
 
97
 
 
98
    if (!parent.isValid())
 
99
        parentItem = rootItem;
 
100
    else
 
101
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
 
102
 
 
103
    TreeItem *childItem = parentItem->child(row);
 
104
    if (childItem)
 
105
        return createIndex(row, column, childItem);
 
106
    else
 
107
        return QModelIndex();
 
108
}
 
109
 
 
110
QModelIndex TreeModel::parent(const QModelIndex &index) const
 
111
{
 
112
    if (!index.isValid())
 
113
        return QModelIndex();
 
114
 
 
115
    TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
 
116
    TreeItem *parentItem = childItem->parent();
 
117
 
 
118
    if (parentItem == rootItem)
 
119
        return QModelIndex();
 
120
 
 
121
    return createIndex(parentItem->row(), 0, parentItem);
 
122
}
 
123
 
 
124
int TreeModel::rowCount(const QModelIndex &parent) const
 
125
{
 
126
    TreeItem *parentItem;
 
127
 
 
128
    if (!parent.isValid())
 
129
        parentItem = rootItem;
 
130
    else
 
131
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
 
132
 
 
133
    return parentItem->childCount();
 
134
}
 
135
 
 
136
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
 
137
{
 
138
    QList<TreeItem*> parents;
 
139
    QList<int> indentations;
 
140
    parents << parent;
 
141
    indentations << 0;
 
142
 
 
143
    int number = 0;
 
144
 
 
145
    while (number < lines.count()) {
 
146
        int position = 0;
 
147
        while (position < lines[number].length()) {
 
148
            if (lines[number].mid(position, 1) != " ")
 
149
                break;
 
150
            position++;
 
151
        }
 
152
 
 
153
        QString lineData = lines[number].mid(position).trimmed();
 
154
 
 
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];
 
161
 
 
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.
 
165
 
 
166
                if (parents.last()->childCount() > 0) {
 
167
                    parents << parents.last()->child(parents.last()->childCount()-1);
 
168
                    indentations << position;
 
169
                }
 
170
            } else {
 
171
                while (position < indentations.last() && parents.count() > 0) {
 
172
                    parents.pop_back();
 
173
                    indentations.pop_back();
 
174
                }
 
175
            }
 
176
 
 
177
            // Append a new item to the current parent's list of children.
 
178
            parents.last()->appendChild(new TreeItem(columnData, parents.last()));
 
179
        }
 
180
 
 
181
        number++;
 
182
    }
 
183
}