|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Stephen Kelly <steveire@gmail.com> |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite 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 #include "dynamictreemodel.h" |
|
43 |
|
44 #include <QtCore/QHash> |
|
45 #include <QtCore/QList> |
|
46 #include <QtCore/QTimer> |
|
47 |
|
48 |
|
49 DynamicTreeModel::DynamicTreeModel(QObject *parent) |
|
50 : QAbstractItemModel(parent), |
|
51 nextId(1) |
|
52 { |
|
53 } |
|
54 |
|
55 QModelIndex DynamicTreeModel::index(int row, int column, const QModelIndex &parent) const |
|
56 { |
|
57 // if (column != 0) |
|
58 // return QModelIndex(); |
|
59 |
|
60 |
|
61 if ( column < 0 || row < 0 ) |
|
62 return QModelIndex(); |
|
63 |
|
64 QList<QList<qint64> > childIdColumns = m_childItems.value(parent.internalId()); |
|
65 |
|
66 |
|
67 if (childIdColumns.size() == 0) |
|
68 return QModelIndex(); |
|
69 |
|
70 if (column >= childIdColumns.size()) |
|
71 return QModelIndex(); |
|
72 |
|
73 QList<qint64> rowIds = childIdColumns.at(column); |
|
74 |
|
75 if ( row >= rowIds.size()) |
|
76 return QModelIndex(); |
|
77 |
|
78 qint64 id = rowIds.at(row); |
|
79 |
|
80 return createIndex(row, column, reinterpret_cast<void *>(id)); |
|
81 |
|
82 } |
|
83 |
|
84 qint64 DynamicTreeModel::findParentId(qint64 searchId) const |
|
85 { |
|
86 if (searchId <= 0) |
|
87 return -1; |
|
88 |
|
89 QHashIterator<qint64, QList<QList<qint64> > > i(m_childItems); |
|
90 while (i.hasNext()) |
|
91 { |
|
92 i.next(); |
|
93 QListIterator<QList<qint64> > j(i.value()); |
|
94 while (j.hasNext()) |
|
95 { |
|
96 QList<qint64> l = j.next(); |
|
97 if (l.contains(searchId)) |
|
98 { |
|
99 return i.key(); |
|
100 } |
|
101 } |
|
102 } |
|
103 return -1; |
|
104 } |
|
105 |
|
106 QModelIndex DynamicTreeModel::parent(const QModelIndex &index) const |
|
107 { |
|
108 if (!index.isValid()) |
|
109 return QModelIndex(); |
|
110 |
|
111 qint64 searchId = index.internalId(); |
|
112 qint64 parentId = findParentId(searchId); |
|
113 // Will never happen for valid index, but what the hey... |
|
114 if (parentId <= 0) |
|
115 return QModelIndex(); |
|
116 |
|
117 qint64 grandParentId = findParentId(parentId); |
|
118 if (grandParentId < 0) |
|
119 grandParentId = 0; |
|
120 |
|
121 int column = 0; |
|
122 QList<qint64> childList = m_childItems.value(grandParentId).at(column); |
|
123 |
|
124 int row = childList.indexOf(parentId); |
|
125 |
|
126 return createIndex(row, column, reinterpret_cast<void *>(parentId)); |
|
127 |
|
128 } |
|
129 |
|
130 int DynamicTreeModel::rowCount(const QModelIndex &index ) const |
|
131 { |
|
132 QList<QList<qint64> > cols = m_childItems.value(index.internalId()); |
|
133 |
|
134 if (cols.size() == 0 ) |
|
135 return 0; |
|
136 |
|
137 if (index.column() > 0) |
|
138 return 0; |
|
139 |
|
140 return cols.at(0).size(); |
|
141 } |
|
142 |
|
143 int DynamicTreeModel::columnCount(const QModelIndex &index ) const |
|
144 { |
|
145 // Q_UNUSED(index); |
|
146 return m_childItems.value(index.internalId()).size(); |
|
147 } |
|
148 |
|
149 QVariant DynamicTreeModel::data(const QModelIndex &index, int role) const |
|
150 { |
|
151 if (!index.isValid()) |
|
152 return QVariant(); |
|
153 |
|
154 if (Qt::DisplayRole == role) |
|
155 { |
|
156 return m_items.value(index.internalId()); |
|
157 } |
|
158 return QVariant(); |
|
159 } |
|
160 |
|
161 void DynamicTreeModel::clear() |
|
162 { |
|
163 m_items.clear(); |
|
164 m_childItems.clear(); |
|
165 nextId = 1; |
|
166 reset(); |
|
167 } |
|
168 |
|
169 |
|
170 ModelChangeCommand::ModelChangeCommand( DynamicTreeModel *model, QObject *parent ) |
|
171 : QObject(parent), m_model(model), m_numCols(1), m_startRow(-1), m_endRow(-1) |
|
172 { |
|
173 |
|
174 } |
|
175 |
|
176 QModelIndex ModelChangeCommand::findIndex(QList<int> rows) |
|
177 { |
|
178 const int col = 0; |
|
179 QModelIndex parent = QModelIndex(); |
|
180 QListIterator<int> i(rows); |
|
181 while (i.hasNext()) |
|
182 { |
|
183 parent = m_model->index(i.next(), col, parent); |
|
184 Q_ASSERT(parent.isValid()); |
|
185 } |
|
186 return parent; |
|
187 } |
|
188 |
|
189 ModelInsertCommand::ModelInsertCommand(DynamicTreeModel *model, QObject *parent ) |
|
190 : ModelChangeCommand(model, parent) |
|
191 { |
|
192 |
|
193 } |
|
194 |
|
195 void ModelInsertCommand::doCommand() |
|
196 { |
|
197 QModelIndex parent = findIndex(m_rowNumbers); |
|
198 m_model->beginInsertRows(parent, m_startRow, m_endRow); |
|
199 qint64 parentId = parent.internalId(); |
|
200 for (int row = m_startRow; row <= m_endRow; row++) |
|
201 { |
|
202 for(int col = 0; col < m_numCols; col++ ) |
|
203 { |
|
204 if (m_model->m_childItems[parentId].size() <= col) |
|
205 { |
|
206 m_model->m_childItems[parentId].append(QList<qint64>()); |
|
207 } |
|
208 // QString name = QUuid::createUuid().toString(); |
|
209 qint64 id = m_model->newId(); |
|
210 QString name = QString::number(id); |
|
211 |
|
212 m_model->m_items.insert(id, name); |
|
213 m_model->m_childItems[parentId][col].insert(row, id); |
|
214 |
|
215 } |
|
216 } |
|
217 m_model->endInsertRows(); |
|
218 } |
|
219 |
|
220 |
|
221 ModelMoveCommand::ModelMoveCommand(DynamicTreeModel *model, QObject *parent) |
|
222 : ModelChangeCommand(model, parent) |
|
223 { |
|
224 |
|
225 } |
|
226 bool ModelMoveCommand::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow) |
|
227 { |
|
228 return m_model->beginMoveRows(srcParent, srcStart, srcEnd, destParent, destRow); |
|
229 } |
|
230 |
|
231 void ModelMoveCommand::doCommand() |
|
232 { |
|
233 QModelIndex srcParent = findIndex(m_rowNumbers); |
|
234 QModelIndex destParent = findIndex(m_destRowNumbers); |
|
235 |
|
236 if (!emitPreSignal(srcParent, m_startRow, m_endRow, destParent, m_destRow)) |
|
237 { |
|
238 return; |
|
239 } |
|
240 |
|
241 for (int column = 0; column < m_numCols; ++column) |
|
242 { |
|
243 QList<qint64> l = m_model->m_childItems.value(srcParent.internalId())[column].mid(m_startRow, m_endRow - m_startRow + 1 ); |
|
244 |
|
245 for (int i = m_startRow; i <= m_endRow ; i++) |
|
246 { |
|
247 m_model->m_childItems[srcParent.internalId()][column].removeAt(m_startRow); |
|
248 } |
|
249 int d; |
|
250 if (m_destRow < m_startRow) |
|
251 d = m_destRow; |
|
252 else |
|
253 { |
|
254 if (srcParent == destParent) |
|
255 d = m_destRow - (m_endRow - m_startRow + 1); |
|
256 else |
|
257 d = m_destRow - (m_endRow - m_startRow) + 1; |
|
258 } |
|
259 |
|
260 foreach(const qint64 id, l) |
|
261 { |
|
262 m_model->m_childItems[destParent.internalId()][column].insert(d++, id); |
|
263 } |
|
264 } |
|
265 |
|
266 emitPostSignal(); |
|
267 } |
|
268 |
|
269 void ModelMoveCommand::emitPostSignal() |
|
270 { |
|
271 m_model->endMoveRows(); |
|
272 } |
|
273 |
|
274 ModelResetCommand::ModelResetCommand(DynamicTreeModel* model, QObject* parent) |
|
275 : ModelMoveCommand(model, parent) |
|
276 { |
|
277 |
|
278 } |
|
279 |
|
280 ModelResetCommand::~ModelResetCommand() |
|
281 { |
|
282 |
|
283 } |
|
284 |
|
285 bool ModelResetCommand::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow) |
|
286 { |
|
287 Q_UNUSED(srcParent); |
|
288 Q_UNUSED(srcStart); |
|
289 Q_UNUSED(srcEnd); |
|
290 Q_UNUSED(destParent); |
|
291 Q_UNUSED(destRow); |
|
292 |
|
293 return true; |
|
294 } |
|
295 |
|
296 void ModelResetCommand::emitPostSignal() |
|
297 { |
|
298 m_model->reset(); |
|
299 } |
|
300 |
|
301 ModelResetCommandFixed::ModelResetCommandFixed(DynamicTreeModel* model, QObject* parent) |
|
302 : ModelMoveCommand(model, parent) |
|
303 { |
|
304 |
|
305 } |
|
306 |
|
307 ModelResetCommandFixed::~ModelResetCommandFixed() |
|
308 { |
|
309 |
|
310 } |
|
311 |
|
312 bool ModelResetCommandFixed::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow) |
|
313 { |
|
314 Q_UNUSED(srcParent); |
|
315 Q_UNUSED(srcStart); |
|
316 Q_UNUSED(srcEnd); |
|
317 Q_UNUSED(destParent); |
|
318 Q_UNUSED(destRow); |
|
319 |
|
320 m_model->beginResetModel(); |
|
321 return true; |
|
322 } |
|
323 |
|
324 void ModelResetCommandFixed::emitPostSignal() |
|
325 { |
|
326 m_model->endResetModel(); |
|
327 } |
|
328 |