|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 /* |
|
43 treemodel.cpp |
|
44 |
|
45 Provides a simple tree model to show how to create and use hierarchical |
|
46 models. |
|
47 */ |
|
48 |
|
49 #include <QtGui> |
|
50 |
|
51 #include "treeitem.h" |
|
52 #include "treemodel.h" |
|
53 |
|
54 //! [0] |
|
55 TreeModel::TreeModel(const QString &data, QObject *parent) |
|
56 : QAbstractItemModel(parent) |
|
57 { |
|
58 QList<QVariant> rootData; |
|
59 rootData << "Title" << "Summary"; |
|
60 rootItem = new TreeItem(rootData); |
|
61 setupModelData(data.split(QString("\n")), rootItem); |
|
62 } |
|
63 //! [0] |
|
64 |
|
65 //! [1] |
|
66 TreeModel::~TreeModel() |
|
67 { |
|
68 delete rootItem; |
|
69 } |
|
70 //! [1] |
|
71 |
|
72 //! [2] |
|
73 int TreeModel::columnCount(const QModelIndex &parent) const |
|
74 { |
|
75 if (parent.isValid()) |
|
76 return static_cast<TreeItem*>(parent.internalPointer())->columnCount(); |
|
77 else |
|
78 return rootItem->columnCount(); |
|
79 } |
|
80 //! [2] |
|
81 |
|
82 //! [3] |
|
83 QVariant TreeModel::data(const QModelIndex &index, int role) const |
|
84 { |
|
85 if (!index.isValid()) |
|
86 return QVariant(); |
|
87 |
|
88 if (role != Qt::DisplayRole) |
|
89 return QVariant(); |
|
90 |
|
91 TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); |
|
92 |
|
93 return item->data(index.column()); |
|
94 } |
|
95 //! [3] |
|
96 |
|
97 //! [4] |
|
98 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const |
|
99 { |
|
100 if (!index.isValid()) |
|
101 return 0; |
|
102 |
|
103 return Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
|
104 } |
|
105 //! [4] |
|
106 |
|
107 //! [5] |
|
108 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, |
|
109 int role) const |
|
110 { |
|
111 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) |
|
112 return rootItem->data(section); |
|
113 |
|
114 return QVariant(); |
|
115 } |
|
116 //! [5] |
|
117 |
|
118 //! [6] |
|
119 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) |
|
120 const |
|
121 { |
|
122 if (!hasIndex(row, column, parent)) |
|
123 return QModelIndex(); |
|
124 |
|
125 TreeItem *parentItem; |
|
126 |
|
127 if (!parent.isValid()) |
|
128 parentItem = rootItem; |
|
129 else |
|
130 parentItem = static_cast<TreeItem*>(parent.internalPointer()); |
|
131 |
|
132 TreeItem *childItem = parentItem->child(row); |
|
133 if (childItem) |
|
134 return createIndex(row, column, childItem); |
|
135 else |
|
136 return QModelIndex(); |
|
137 } |
|
138 //! [6] |
|
139 |
|
140 //! [7] |
|
141 QModelIndex TreeModel::parent(const QModelIndex &index) const |
|
142 { |
|
143 if (!index.isValid()) |
|
144 return QModelIndex(); |
|
145 |
|
146 TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer()); |
|
147 TreeItem *parentItem = childItem->parent(); |
|
148 |
|
149 if (parentItem == rootItem) |
|
150 return QModelIndex(); |
|
151 |
|
152 return createIndex(parentItem->row(), 0, parentItem); |
|
153 } |
|
154 //! [7] |
|
155 |
|
156 //! [8] |
|
157 int TreeModel::rowCount(const QModelIndex &parent) const |
|
158 { |
|
159 TreeItem *parentItem; |
|
160 if (parent.column() > 0) |
|
161 return 0; |
|
162 |
|
163 if (!parent.isValid()) |
|
164 parentItem = rootItem; |
|
165 else |
|
166 parentItem = static_cast<TreeItem*>(parent.internalPointer()); |
|
167 |
|
168 return parentItem->childCount(); |
|
169 } |
|
170 //! [8] |
|
171 |
|
172 void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent) |
|
173 { |
|
174 QList<TreeItem*> parents; |
|
175 QList<int> indentations; |
|
176 parents << parent; |
|
177 indentations << 0; |
|
178 |
|
179 int number = 0; |
|
180 |
|
181 while (number < lines.count()) { |
|
182 int position = 0; |
|
183 while (position < lines[number].length()) { |
|
184 if (lines[number].mid(position, 1) != " ") |
|
185 break; |
|
186 position++; |
|
187 } |
|
188 |
|
189 QString lineData = lines[number].mid(position).trimmed(); |
|
190 |
|
191 if (!lineData.isEmpty()) { |
|
192 // Read the column data from the rest of the line. |
|
193 QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts); |
|
194 QList<QVariant> columnData; |
|
195 for (int column = 0; column < columnStrings.count(); ++column) |
|
196 columnData << columnStrings[column]; |
|
197 |
|
198 if (position > indentations.last()) { |
|
199 // The last child of the current parent is now the new parent |
|
200 // unless the current parent has no children. |
|
201 |
|
202 if (parents.last()->childCount() > 0) { |
|
203 parents << parents.last()->child(parents.last()->childCount()-1); |
|
204 indentations << position; |
|
205 } |
|
206 } else { |
|
207 while (position < indentations.last() && parents.count() > 0) { |
|
208 parents.pop_back(); |
|
209 indentations.pop_back(); |
|
210 } |
|
211 } |
|
212 |
|
213 // Append a new item to the current parent's list of children. |
|
214 parents.last()->appendChild(new TreeItem(columnData, parents.last())); |
|
215 } |
|
216 |
|
217 number++; |
|
218 } |
|
219 } |