1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
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
|
---|
20 | ** distribution.
|
---|
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
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
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."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | /*
|
---|
42 | treemodel.cpp
|
---|
43 |
|
---|
44 | Provides a simple tree model to show how to create and use hierarchical
|
---|
45 | models.
|
---|
46 | */
|
---|
47 |
|
---|
48 | #include <QtGui>
|
---|
49 |
|
---|
50 | #include "treeitem.h"
|
---|
51 | #include "treemodel.h"
|
---|
52 |
|
---|
53 | TreeModel::TreeModel(const QStringList &strings, QObject *parent)
|
---|
54 | : QAbstractItemModel(parent)
|
---|
55 | {
|
---|
56 | QList<QVariant> rootData;
|
---|
57 | rootData << "Title" << "Summary";
|
---|
58 | rootItem = new TreeItem(rootData);
|
---|
59 | setupModelData(strings, rootItem);
|
---|
60 | }
|
---|
61 |
|
---|
62 | TreeModel::~TreeModel()
|
---|
63 | {
|
---|
64 | delete rootItem;
|
---|
65 | }
|
---|
66 |
|
---|
67 | int TreeModel::columnCount(const QModelIndex &parent) const
|
---|
68 | {
|
---|
69 | if (parent.isValid())
|
---|
70 | return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
|
---|
71 | else
|
---|
72 | return rootItem->columnCount();
|
---|
73 | }
|
---|
74 |
|
---|
75 | QVariant TreeModel::data(const QModelIndex &index, int role) const
|
---|
76 | {
|
---|
77 | if (!index.isValid())
|
---|
78 | return QVariant();
|
---|
79 |
|
---|
80 | if (role != Qt::DisplayRole)
|
---|
81 | return QVariant();
|
---|
82 |
|
---|
83 | TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
---|
84 |
|
---|
85 | return item->data(index.column());
|
---|
86 | }
|
---|
87 |
|
---|
88 | Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
|
---|
89 | {
|
---|
90 | if (!index.isValid())
|
---|
91 | return Qt::ItemIsEnabled;
|
---|
92 |
|
---|
93 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
---|
94 | }
|
---|
95 |
|
---|
96 | QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
|
---|
97 | int role) const
|
---|
98 | {
|
---|
99 | if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
---|
100 | return rootItem->data(section);
|
---|
101 |
|
---|
102 | return QVariant();
|
---|
103 | }
|
---|
104 |
|
---|
105 | QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
|
---|
106 | const
|
---|
107 | {
|
---|
108 | TreeItem *parentItem;
|
---|
109 |
|
---|
110 | if (!parent.isValid())
|
---|
111 | parentItem = rootItem;
|
---|
112 | else
|
---|
113 | parentItem = static_cast<TreeItem*>(parent.internalPointer());
|
---|
114 |
|
---|
115 | TreeItem *childItem = parentItem->child(row);
|
---|
116 | if (childItem)
|
---|
117 | return createIndex(row, column, childItem);
|
---|
118 | else
|
---|
119 | return QModelIndex();
|
---|
120 | }
|
---|
121 |
|
---|
122 | bool TreeModel::insertRows(int position, int rows, const QModelIndex &parent)
|
---|
123 | {
|
---|
124 | TreeItem *parentItem;
|
---|
125 |
|
---|
126 | if (!parent.isValid())
|
---|
127 | parentItem = rootItem;
|
---|
128 | else
|
---|
129 | parentItem = static_cast<TreeItem*>(parent.internalPointer());
|
---|
130 |
|
---|
131 | if (position < 0 || position > parentItem->childCount())
|
---|
132 | return false;
|
---|
133 |
|
---|
134 | QList<QVariant> blankList;
|
---|
135 | for (int column = 0; column < columnCount(); ++column)
|
---|
136 | blankList << QVariant("");
|
---|
137 |
|
---|
138 | beginInsertRows(parent, position, position + rows - 1);
|
---|
139 |
|
---|
140 | for (int row = 0; row < rows; ++row) {
|
---|
141 | TreeItem *newItem = new TreeItem(blankList, parentItem);
|
---|
142 | if (!parentItem->insertChild(position, newItem))
|
---|
143 | break;
|
---|
144 | }
|
---|
145 |
|
---|
146 | endInsertRows();
|
---|
147 | return true;
|
---|
148 | }
|
---|
149 |
|
---|
150 | QModelIndex TreeModel::parent(const QModelIndex &index) const
|
---|
151 | {
|
---|
152 | if (!index.isValid())
|
---|
153 | return QModelIndex();
|
---|
154 |
|
---|
155 | TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
|
---|
156 | TreeItem *parentItem = childItem->parent();
|
---|
157 |
|
---|
158 | if (parentItem == rootItem)
|
---|
159 | return QModelIndex();
|
---|
160 |
|
---|
161 | return createIndex(parentItem->row(), 0, parentItem);
|
---|
162 | }
|
---|
163 |
|
---|
164 | bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent)
|
---|
165 | {
|
---|
166 | TreeItem *parentItem;
|
---|
167 |
|
---|
168 | if (!parent.isValid())
|
---|
169 | parentItem = rootItem;
|
---|
170 | else
|
---|
171 | parentItem = static_cast<TreeItem*>(parent.internalPointer());
|
---|
172 |
|
---|
173 | if (position < 0 || position > parentItem->childCount())
|
---|
174 | return false;
|
---|
175 |
|
---|
176 | beginRemoveRows(parent, position, position + rows - 1);
|
---|
177 |
|
---|
178 | for (int row = 0; row < rows; ++row) {
|
---|
179 | if (!parentItem->removeChild(position))
|
---|
180 | break;
|
---|
181 | }
|
---|
182 |
|
---|
183 | endRemoveRows();
|
---|
184 | return true;
|
---|
185 | }
|
---|
186 |
|
---|
187 | int TreeModel::rowCount(const QModelIndex &parent) const
|
---|
188 | {
|
---|
189 | TreeItem *parentItem;
|
---|
190 |
|
---|
191 | if (!parent.isValid())
|
---|
192 | parentItem = rootItem;
|
---|
193 | else
|
---|
194 | parentItem = static_cast<TreeItem*>(parent.internalPointer());
|
---|
195 |
|
---|
196 | return parentItem->childCount();
|
---|
197 | }
|
---|
198 |
|
---|
199 | bool TreeModel::setData(const QModelIndex &index,
|
---|
200 | const QVariant &value, int role)
|
---|
201 | {
|
---|
202 | if (!index.isValid() || role != Qt::EditRole)
|
---|
203 | return false;
|
---|
204 |
|
---|
205 | TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
---|
206 |
|
---|
207 | if (item->setData(index.column(), value))
|
---|
208 | emit dataChanged(index, index);
|
---|
209 | else
|
---|
210 | return false;
|
---|
211 |
|
---|
212 | return true;
|
---|
213 | }
|
---|
214 |
|
---|
215 | void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
|
---|
216 | {
|
---|
217 | QList<TreeItem*> parents;
|
---|
218 | QList<int> indentations;
|
---|
219 | parents << parent;
|
---|
220 | indentations << 0;
|
---|
221 |
|
---|
222 | int number = 0;
|
---|
223 |
|
---|
224 | while (number < lines.count()) {
|
---|
225 | int position = 0;
|
---|
226 | while (position < lines[number].length()) {
|
---|
227 | if (lines[number].mid(position, 1) != " ")
|
---|
228 | break;
|
---|
229 | position++;
|
---|
230 | }
|
---|
231 |
|
---|
232 | QString lineData = lines[number].mid(position).trimmed();
|
---|
233 |
|
---|
234 | if (!lineData.isEmpty()) {
|
---|
235 | // Read the column data from the rest of the line.
|
---|
236 | QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
|
---|
237 | QList<QVariant> columnData;
|
---|
238 | for (int column = 0; column < columnStrings.count(); ++column)
|
---|
239 | columnData << columnStrings[column];
|
---|
240 |
|
---|
241 | if (position > indentations.last()) {
|
---|
242 | // The last child of the current parent is now the new parent
|
---|
243 | // unless the current parent has no children.
|
---|
244 |
|
---|
245 | if (parents.last()->childCount() > 0) {
|
---|
246 | parents << parents.last()->child(parents.last()->childCount()-1);
|
---|
247 | indentations << position;
|
---|
248 | }
|
---|
249 | } else {
|
---|
250 | while (position < indentations.last() && parents.count() > 0) {
|
---|
251 | parents.pop_back();
|
---|
252 | indentations.pop_back();
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | // Append a new item to the current parent's list of children.
|
---|
257 | parents.last()->appendChild(new TreeItem(columnData, parents.last()));
|
---|
258 | }
|
---|
259 |
|
---|
260 | number++;
|
---|
261 | }
|
---|
262 | }
|
---|