author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 22 Apr 2010 16:15:11 +0300 | |
branch | RCL_3 |
changeset 14 | 8c4229025c0b |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
#include <QtCore/QtCore> |
|
45 |
||
46 |
#include <QtGui/QSortFilterProxyModel> |
|
47 |
||
48 |
//TESTED_CLASS=QAbstractListModel QAbstractTableModel |
|
49 |
//TESTED_FILES= |
|
50 |
||
51 |
#include "dynamictreemodel.h" |
|
52 |
||
53 |
Q_DECLARE_METATYPE(QModelIndex) |
|
54 |
||
55 |
/*! |
|
56 |
Note that this doesn't test models, but any functionality that QAbstractItemModel shoudl provide |
|
57 |
*/ |
|
58 |
class tst_QAbstractItemModel : public QObject |
|
59 |
{ |
|
60 |
Q_OBJECT |
|
61 |
||
62 |
public: |
|
63 |
tst_QAbstractItemModel(); |
|
64 |
virtual ~tst_QAbstractItemModel(); |
|
65 |
||
66 |
public slots: |
|
67 |
void initTestCase(); |
|
68 |
void cleanupTestCase(); |
|
69 |
void init(); |
|
70 |
void cleanup(); |
|
71 |
||
72 |
private slots: |
|
73 |
void index(); |
|
74 |
void parent(); |
|
75 |
void hasChildren(); |
|
76 |
void _data(); |
|
77 |
void headerData(); |
|
78 |
void itemData(); |
|
79 |
void itemFlags(); |
|
80 |
void match(); |
|
81 |
void dropMimeData_data(); |
|
82 |
void dropMimeData(); |
|
83 |
void changePersistentIndex(); |
|
84 |
void movePersistentIndex(); |
|
85 |
||
86 |
void insertRows(); |
|
87 |
void insertColumns(); |
|
88 |
void removeRows(); |
|
89 |
void removeColumns(); |
|
90 |
||
91 |
void reset(); |
|
92 |
||
93 |
void complexChangesWithPersistent(); |
|
94 |
||
95 |
void testMoveSameParentUp_data(); |
|
96 |
void testMoveSameParentUp(); |
|
97 |
||
98 |
void testMoveSameParentDown_data(); |
|
99 |
void testMoveSameParentDown(); |
|
100 |
||
101 |
void testMoveToGrandParent_data(); |
|
102 |
void testMoveToGrandParent(); |
|
103 |
||
104 |
void testMoveToSibling_data(); |
|
105 |
void testMoveToSibling(); |
|
106 |
||
107 |
void testMoveToUncle_data(); |
|
108 |
void testMoveToUncle(); |
|
109 |
||
110 |
void testMoveToDescendants(); |
|
111 |
||
112 |
void testMoveWithinOwnRange_data(); |
|
113 |
void testMoveWithinOwnRange(); |
|
114 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
115 |
void testMoveThroughProxy(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
116 |
|
0 | 117 |
void testReset(); |
118 |
||
119 |
||
120 |
private: |
|
121 |
DynamicTreeModel *m_model; |
|
122 |
||
123 |
}; |
|
124 |
||
125 |
/*! |
|
126 |
Test model that impliments the pure vitual functions and anything else that is |
|
127 |
needed. |
|
128 |
||
129 |
It is a table implimented as a vector of vectors of strings. |
|
130 |
*/ |
|
131 |
class QtTestModel: public QAbstractItemModel |
|
132 |
{ |
|
133 |
public: |
|
134 |
QtTestModel(int rows, int columns, QObject *parent = 0); |
|
135 |
QtTestModel(const QVector<QVector<QString> > tbl, QObject *parent = 0); |
|
136 |
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; |
|
137 |
QModelIndex parent(const QModelIndex &) const; |
|
138 |
int rowCount(const QModelIndex &parent) const; |
|
139 |
int columnCount(const QModelIndex &parent) const; |
|
140 |
bool hasChildren(const QModelIndex &) const; |
|
141 |
QVariant data(const QModelIndex &idx, int) const; |
|
142 |
bool setData(const QModelIndex &idx, const QVariant &value, int); |
|
143 |
bool insertRows(int row, int count, const QModelIndex &parent= QModelIndex()); |
|
144 |
bool insertColumns(int column, int count, const QModelIndex &parent= QModelIndex()); |
|
145 |
void setPersistent(const QModelIndex &from, const QModelIndex &to); |
|
146 |
bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() ); |
|
147 |
bool removeColumns( int column, int count, const QModelIndex & parent = QModelIndex()); |
|
148 |
void reset(); |
|
149 |
||
150 |
int cCount, rCount; |
|
151 |
mutable bool wrongIndex; |
|
152 |
QVector<QVector<QString> > table; |
|
153 |
}; |
|
154 |
||
155 |
QtTestModel::QtTestModel(int rows, int columns, QObject *parent) |
|
156 |
: QAbstractItemModel(parent), cCount(columns), rCount(rows), wrongIndex(false) { |
|
157 |
||
158 |
table.resize(rows); |
|
159 |
for (int r = 0; r < rows; ++r) { |
|
160 |
table[r].resize(columns); |
|
161 |
for (int c = 0; c < columns; ++c) |
|
162 |
table[r][c] = QString("%1/%2").arg(r).arg(c); |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
QtTestModel::QtTestModel(const QVector<QVector<QString> > tbl, QObject *parent) |
|
167 |
: QAbstractItemModel(parent), wrongIndex(false) { |
|
168 |
table = tbl; |
|
169 |
rCount = tbl.count(); |
|
170 |
cCount = tbl.at(0).count(); |
|
171 |
} |
|
172 |
||
173 |
QModelIndex QtTestModel::index(int row, int column, const QModelIndex &parent) const |
|
174 |
{ return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex(); } |
|
175 |
||
176 |
QModelIndex QtTestModel::parent(const QModelIndex &) const { return QModelIndex(); } |
|
177 |
int QtTestModel::rowCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : rCount; } |
|
178 |
int QtTestModel::columnCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : cCount; } |
|
179 |
bool QtTestModel::hasChildren(const QModelIndex &) const { return false; } |
|
180 |
||
181 |
QVariant QtTestModel::data(const QModelIndex &idx, int) const |
|
182 |
{ |
|
183 |
if (idx.row() < 0 || idx.column() < 0 || idx.column() > cCount || idx.row() > rCount) { |
|
184 |
wrongIndex = true; |
|
185 |
qWarning("got invalid modelIndex %d/%d", idx.row(), idx.column()); |
|
186 |
return QVariant(); |
|
187 |
} |
|
188 |
return table.at(idx.row()).at(idx.column()); |
|
189 |
} |
|
190 |
||
191 |
bool QtTestModel::setData(const QModelIndex &idx, const QVariant &value, int) |
|
192 |
{ |
|
193 |
table[idx.row()][idx.column()] = value.toString(); |
|
194 |
return true; |
|
195 |
} |
|
196 |
||
197 |
bool QtTestModel::insertRows(int row, int count, const QModelIndex &parent) |
|
198 |
{ |
|
199 |
QAbstractItemModel::beginInsertRows(parent, row, row + count - 1); |
|
200 |
int cc = columnCount(parent); |
|
201 |
table.insert(row, count, QVector<QString>(cc)); |
|
202 |
rCount = table.count(); |
|
203 |
QAbstractItemModel::endInsertRows(); |
|
204 |
return true; |
|
205 |
} |
|
206 |
||
207 |
bool QtTestModel::insertColumns(int column, int count, const QModelIndex &parent) |
|
208 |
{ |
|
209 |
QAbstractItemModel::beginInsertColumns(parent, column, column + count - 1); |
|
210 |
int rc = rowCount(parent); |
|
211 |
for (int i = 0; i < rc; ++i) |
|
212 |
table[i].insert(column, 1, ""); |
|
213 |
cCount = table.at(0).count(); |
|
214 |
QAbstractItemModel::endInsertColumns(); |
|
215 |
return true; |
|
216 |
} |
|
217 |
||
218 |
void QtTestModel::setPersistent(const QModelIndex &from, const QModelIndex &to) |
|
219 |
{ |
|
220 |
changePersistentIndex(from, to); |
|
221 |
} |
|
222 |
||
223 |
bool QtTestModel::removeRows( int row, int count, const QModelIndex & parent) |
|
224 |
{ |
|
225 |
QAbstractItemModel::beginRemoveRows(parent, row, row + count - 1); |
|
226 |
||
227 |
for (int r = row+count-1; r >= row; --r) |
|
228 |
table.remove(r); |
|
229 |
rCount = table.count(); |
|
230 |
||
231 |
QAbstractItemModel::endRemoveRows(); |
|
232 |
return true; |
|
233 |
} |
|
234 |
||
235 |
bool QtTestModel::removeColumns(int column, int count, const QModelIndex & parent) |
|
236 |
{ |
|
237 |
QAbstractItemModel::beginRemoveColumns(parent, column, column + count - 1); |
|
238 |
||
239 |
for (int c = column+count-1; c > column; --c) |
|
240 |
for (int r = 0; r < rCount; ++r) |
|
241 |
table[r].remove(c); |
|
242 |
||
243 |
cCount = table.at(0).count(); |
|
244 |
||
245 |
QAbstractItemModel::endRemoveColumns(); |
|
246 |
return true; |
|
247 |
} |
|
248 |
||
249 |
void QtTestModel::reset() |
|
250 |
{ |
|
251 |
QAbstractItemModel::reset(); |
|
252 |
} |
|
253 |
||
254 |
tst_QAbstractItemModel::tst_QAbstractItemModel() |
|
255 |
{ |
|
256 |
} |
|
257 |
||
258 |
tst_QAbstractItemModel::~tst_QAbstractItemModel() |
|
259 |
{ |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* The source Model *must* be initialized before the _data function, since the _data function uses QModelIndexes to reference the items in the tables. |
|
264 |
* Therefore, we must initialize it globally. |
|
265 |
*/ |
|
266 |
||
267 |
void tst_QAbstractItemModel::initTestCase() |
|
268 |
{ |
|
269 |
qRegisterMetaType<QModelIndex>("QModelIndex"); |
|
270 |
} |
|
271 |
||
272 |
void tst_QAbstractItemModel::cleanupTestCase() |
|
273 |
{ |
|
274 |
||
275 |
} |
|
276 |
||
277 |
void tst_QAbstractItemModel::init() |
|
278 |
{ |
|
279 |
m_model = new DynamicTreeModel(this); |
|
280 |
||
281 |
ModelInsertCommand *insertCommand = new ModelInsertCommand(m_model, this); |
|
282 |
insertCommand->setNumCols(4); |
|
283 |
insertCommand->setStartRow(0); |
|
284 |
insertCommand->setEndRow(9); |
|
285 |
insertCommand->doCommand(); |
|
286 |
||
287 |
insertCommand = new ModelInsertCommand(m_model, this); |
|
288 |
insertCommand->setAncestorRowNumbers(QList<int>() << 5); |
|
289 |
insertCommand->setNumCols(4); |
|
290 |
insertCommand->setStartRow(0); |
|
291 |
insertCommand->setEndRow(9); |
|
292 |
insertCommand->doCommand(); |
|
293 |
} |
|
294 |
||
295 |
void tst_QAbstractItemModel::cleanup() |
|
296 |
{ |
|
297 |
||
298 |
} |
|
299 |
||
300 |
/* |
|
301 |
tests |
|
302 |
*/ |
|
303 |
||
304 |
void tst_QAbstractItemModel::index() |
|
305 |
{ |
|
306 |
QtTestModel model(1, 1); |
|
307 |
QModelIndex idx = model.index(0, 0, QModelIndex()); |
|
308 |
QVERIFY(idx.isValid()); |
|
309 |
} |
|
310 |
||
311 |
void tst_QAbstractItemModel::parent() |
|
312 |
{ |
|
313 |
QtTestModel model(1, 1); |
|
314 |
QModelIndex idx = model.index(0, 0, QModelIndex()); |
|
315 |
QModelIndex par = model.parent(idx); |
|
316 |
QVERIFY(!par.isValid()); |
|
317 |
} |
|
318 |
||
319 |
void tst_QAbstractItemModel::hasChildren() |
|
320 |
{ |
|
321 |
QtTestModel model(1, 1); |
|
322 |
QModelIndex idx = model.index(0, 0, QModelIndex()); |
|
323 |
QVERIFY(model.hasChildren(idx) == false); |
|
324 |
} |
|
325 |
||
326 |
void tst_QAbstractItemModel::_data() |
|
327 |
{ |
|
328 |
QtTestModel model(1, 1); |
|
329 |
QModelIndex idx = model.index(0, 0, QModelIndex()); |
|
330 |
QVERIFY(idx.isValid()); |
|
331 |
QCOMPARE(model.data(idx, Qt::DisplayRole).toString(), QString("0/0")); |
|
332 |
||
333 |
// Default does nothing |
|
334 |
QCOMPARE(model.setHeaderData(0, Qt::Horizontal, QVariant(0), 0), false); |
|
335 |
} |
|
336 |
||
337 |
void tst_QAbstractItemModel::headerData() |
|
338 |
{ |
|
339 |
QtTestModel model(1, 1); |
|
340 |
QCOMPARE(model.headerData(0, Qt::Horizontal, Qt::DisplayRole).toString(), |
|
341 |
QString("1")); |
|
342 |
||
343 |
// Default text alignment for header must be invalid |
|
344 |
QVERIFY( !model.headerData(0, Qt::Horizontal, Qt::TextAlignmentRole).isValid() ); |
|
345 |
} |
|
346 |
||
347 |
void tst_QAbstractItemModel::itemData() |
|
348 |
{ |
|
349 |
QtTestModel model(1, 1); |
|
350 |
QModelIndex idx = model.index(0, 0, QModelIndex()); |
|
351 |
QVERIFY(idx.isValid()); |
|
352 |
QMap<int, QVariant> dat = model.itemData(idx); |
|
353 |
QCOMPARE(dat.count(Qt::DisplayRole), 1); |
|
354 |
QCOMPARE(dat.value(Qt::DisplayRole).toString(), QString("0/0")); |
|
355 |
} |
|
356 |
||
357 |
void tst_QAbstractItemModel::itemFlags() |
|
358 |
{ |
|
359 |
QtTestModel model(1, 1); |
|
360 |
QModelIndex idx = model.index(0, 0, QModelIndex()); |
|
361 |
QVERIFY(idx.isValid()); |
|
362 |
Qt::ItemFlags flags = model.flags(idx); |
|
363 |
QCOMPARE(Qt::ItemIsSelectable|Qt::ItemIsEnabled, flags); |
|
364 |
} |
|
365 |
||
366 |
void tst_QAbstractItemModel::match() |
|
367 |
{ |
|
368 |
QtTestModel model(4, 1); |
|
369 |
QModelIndex start = model.index(0, 0, QModelIndex()); |
|
370 |
QVERIFY(start.isValid()); |
|
371 |
QModelIndexList res = model.match(start, Qt::DisplayRole, QVariant("1"), 3); |
|
372 |
QCOMPARE(res.count(), 1); |
|
373 |
QModelIndex idx = model.index(1, 0, QModelIndex()); |
|
374 |
bool areEqual = (idx == res.first()); |
|
375 |
QVERIFY(areEqual); |
|
376 |
||
377 |
model.setData(model.index(0, 0, QModelIndex()), "bat", Qt::DisplayRole); |
|
378 |
model.setData(model.index(1, 0, QModelIndex()), "cat", Qt::DisplayRole); |
|
379 |
model.setData(model.index(2, 0, QModelIndex()), "dog", Qt::DisplayRole); |
|
380 |
model.setData(model.index(3, 0, QModelIndex()), "boar", Qt::DisplayRole); |
|
381 |
||
382 |
res = model.match(start, Qt::DisplayRole, QVariant("dog"), -1, Qt::MatchExactly); |
|
383 |
QCOMPARE(res.count(), 1); |
|
384 |
res = model.match(start, Qt::DisplayRole, QVariant("a"), -1, Qt::MatchContains); |
|
385 |
QCOMPARE(res.count(), 3); |
|
386 |
res = model.match(start, Qt::DisplayRole, QVariant("b"), -1, Qt::MatchStartsWith); |
|
387 |
QCOMPARE(res.count(), 2); |
|
388 |
res = model.match(start, Qt::DisplayRole, QVariant("t"), -1, Qt::MatchEndsWith); |
|
389 |
QCOMPARE(res.count(), 2); |
|
390 |
res = model.match(start, Qt::DisplayRole, QVariant("*a*"), -1, Qt::MatchWildcard); |
|
391 |
QCOMPARE(res.count(), 3); |
|
392 |
res = model.match(start, Qt::DisplayRole, QVariant(".*O.*"), -1, Qt::MatchRegExp); |
|
393 |
QCOMPARE(res.count(), 2); |
|
394 |
res = model.match(start, Qt::DisplayRole, QVariant(".*O.*"), -1, Qt::MatchRegExp | Qt::MatchCaseSensitive); |
|
395 |
QCOMPARE(res.count(), 0); |
|
396 |
res = model.match(start, Qt::DisplayRole, QVariant("BOAR"), -1, Qt::MatchFixedString); |
|
397 |
QCOMPARE(res.count(), 1); |
|
398 |
res = model.match(start, Qt::DisplayRole, QVariant("bat"), -1, |
|
399 |
Qt::MatchFixedString | Qt::MatchCaseSensitive); |
|
400 |
QCOMPARE(res.count(), 1); |
|
401 |
} |
|
402 |
||
403 |
typedef QPair<int, int> Position; |
|
404 |
typedef QVector<QPair<int, int> > Selection; |
|
405 |
typedef QVector<QVector<QString> > StringTable; |
|
406 |
typedef QVector<QString> StringTableRow; |
|
407 |
Q_DECLARE_METATYPE(Position) |
|
408 |
Q_DECLARE_METATYPE(Selection) |
|
409 |
Q_DECLARE_METATYPE(StringTable) |
|
410 |
||
411 |
static StringTableRow qStringTableRow(const QString &s1, const QString &s2, const QString &s3) |
|
412 |
{ |
|
413 |
StringTableRow row; |
|
414 |
row << s1 << s2 << s3; |
|
415 |
return row; |
|
416 |
} |
|
417 |
||
418 |
#ifdef Q_CC_MSVC |
|
419 |
# define STRINGTABLE (StringTable()) |
|
420 |
#else |
|
421 |
# define STRINGTABLE StringTable() |
|
422 |
#endif |
|
423 |
||
424 |
void tst_QAbstractItemModel::dropMimeData_data() |
|
425 |
{ |
|
426 |
QTest::addColumn<StringTable>("src_table"); // drag source |
|
427 |
QTest::addColumn<StringTable>("dst_table"); // drop target |
|
428 |
QTest::addColumn<Selection>("selection"); // dragged items |
|
429 |
QTest::addColumn<Position>("dst_position"); // drop position |
|
430 |
QTest::addColumn<StringTable>("res_table"); // expected result |
|
431 |
||
432 |
{ |
|
433 |
QTest::newRow("2x2 dropped at [0, 0]") |
|
434 |
<< (STRINGTABLE // source table |
|
435 |
<< (qStringTableRow("A", "B", "C")) |
|
436 |
<< (qStringTableRow("D", "E", "F"))) |
|
437 |
<< (STRINGTABLE // destination table |
|
438 |
<< (qStringTableRow("0", "1", "2")) |
|
439 |
<< (qStringTableRow("3", "4", "5"))) |
|
440 |
<< (Selection() // selection |
|
441 |
<< Position(0, 0) << Position(0, 1) |
|
442 |
<< Position(1, 0) << Position(1, 1)) |
|
443 |
<< Position(0, 0) // drop position |
|
444 |
<< (STRINGTABLE // resulting table |
|
445 |
<< (qStringTableRow("A", "B", "" )) |
|
446 |
<< (qStringTableRow("D", "E", "" )) |
|
447 |
<< (qStringTableRow("0", "1", "2")) |
|
448 |
<< (qStringTableRow("3", "4", "5"))); |
|
449 |
} |
|
450 |
||
451 |
{ |
|
452 |
QTest::newRow("2x2 dropped at [1, 0]") |
|
453 |
<< (STRINGTABLE // source table |
|
454 |
<< (qStringTableRow("A", "B", "C")) |
|
455 |
<< (qStringTableRow("D", "E", "F"))) |
|
456 |
<< (STRINGTABLE // destination table |
|
457 |
<< (qStringTableRow("0", "1", "2")) |
|
458 |
<< (qStringTableRow("3", "4", "5"))) |
|
459 |
<< (Selection() // selection |
|
460 |
<< Position(0, 0) << Position(0, 1) |
|
461 |
<< Position(1, 0) << Position(1, 1)) |
|
462 |
<< Position(1, 0) // drop position |
|
463 |
<< (STRINGTABLE // resulting table |
|
464 |
<< (qStringTableRow("0", "1", "2")) |
|
465 |
<< (qStringTableRow("A", "B", "" )) |
|
466 |
<< (qStringTableRow("D", "E", "" )) |
|
467 |
<< (qStringTableRow("3", "4", "5"))); |
|
468 |
} |
|
469 |
||
470 |
{ |
|
471 |
QTest::newRow("2x2 dropped at [3, 0]") |
|
472 |
<< (STRINGTABLE // source table |
|
473 |
<< (qStringTableRow("A", "B", "C")) |
|
474 |
<< (qStringTableRow("D", "E", "F"))) |
|
475 |
<< (STRINGTABLE // destination table |
|
476 |
<< (qStringTableRow("0", "1", "2")) |
|
477 |
<< (qStringTableRow("3", "4", "5"))) |
|
478 |
<< (Selection() // selection |
|
479 |
<< Position(0, 0) << Position(0, 1) |
|
480 |
<< Position(1, 0) << Position(1, 1)) |
|
481 |
<< Position(3, 0) // drop position |
|
482 |
<< (STRINGTABLE // resulting table |
|
483 |
<< (qStringTableRow("0", "1", "2")) |
|
484 |
<< (qStringTableRow("3", "4", "5")) |
|
485 |
<< (qStringTableRow("A", "B", "" )) |
|
486 |
<< (qStringTableRow("D", "E", "" ))); |
|
487 |
} |
|
488 |
||
489 |
{ |
|
490 |
QTest::newRow("2x2 dropped at [0, 1]") |
|
491 |
<< (STRINGTABLE // source table |
|
492 |
<< (qStringTableRow("A", "B", "C")) |
|
493 |
<< (qStringTableRow("D", "E", "F"))) |
|
494 |
<< (STRINGTABLE // destination table |
|
495 |
<< (qStringTableRow("0", "1", "2")) |
|
496 |
<< (qStringTableRow("3", "4", "5"))) |
|
497 |
<< (Selection() // selection |
|
498 |
<< Position(0, 0) << Position(0, 1) |
|
499 |
<< Position(1, 0) << Position(1, 1)) |
|
500 |
<< Position(0, 1) // drop position |
|
501 |
<< (STRINGTABLE // resulting table |
|
502 |
<< (qStringTableRow("" , "A", "B")) |
|
503 |
<< (qStringTableRow("" , "D", "E")) |
|
504 |
<< (qStringTableRow("0", "1", "2")) |
|
505 |
<< (qStringTableRow("3", "4", "5"))); |
|
506 |
} |
|
507 |
||
508 |
{ |
|
509 |
QTest::newRow("2x2 dropped at [0, 2] (line break)") |
|
510 |
<< (STRINGTABLE // source table |
|
511 |
<< (qStringTableRow("A", "B", "C")) |
|
512 |
<< (qStringTableRow("D", "E", "F"))) |
|
513 |
<< (STRINGTABLE // destination table |
|
514 |
<< (qStringTableRow("0", "1", "2")) |
|
515 |
<< (qStringTableRow("3", "4", "5"))) |
|
516 |
<< (Selection() // selection |
|
517 |
<< Position(0, 0) << Position(0, 1) |
|
518 |
<< Position(1, 0) << Position(1, 1)) |
|
519 |
<< Position(0, 2) // drop position |
|
520 |
<< (STRINGTABLE // resulting table |
|
521 |
<< (qStringTableRow("" , "" , "A")) |
|
522 |
<< (qStringTableRow("" , "" , "D")) |
|
523 |
<< (qStringTableRow("" , "" , "B")) |
|
524 |
<< (qStringTableRow("" , "" , "E")) |
|
525 |
<< (qStringTableRow("0", "1", "2")) |
|
526 |
<< (qStringTableRow("3", "4", "5"))); |
|
527 |
} |
|
528 |
||
529 |
{ |
|
530 |
QTest::newRow("2x2 dropped at [3, 2] (line break)") |
|
531 |
<< (STRINGTABLE // source table |
|
532 |
<< (qStringTableRow("A", "B", "C")) |
|
533 |
<< (qStringTableRow("D", "E", "F"))) |
|
534 |
<< (STRINGTABLE // destination table |
|
535 |
<< (qStringTableRow("0", "1", "2")) |
|
536 |
<< (qStringTableRow("3", "4", "5"))) |
|
537 |
<< (Selection() // selection |
|
538 |
<< Position(0, 0) << Position(0, 1) |
|
539 |
<< Position(1, 0) << Position(1, 1)) |
|
540 |
<< Position(3, 2) // drop position |
|
541 |
<< (STRINGTABLE // resulting table |
|
542 |
<< (qStringTableRow("0", "1", "2")) |
|
543 |
<< (qStringTableRow("3", "4", "5")) |
|
544 |
<< (qStringTableRow("" , "" , "A")) |
|
545 |
<< (qStringTableRow("" , "" , "D")) |
|
546 |
<< (qStringTableRow("" , "" , "B")) |
|
547 |
<< (qStringTableRow("" , "" , "E"))); |
|
548 |
} |
|
549 |
||
550 |
{ |
|
551 |
QTest::newRow("non-square dropped at [0, 0]") |
|
552 |
<< (STRINGTABLE // source table |
|
553 |
<< (qStringTableRow("A", "B", "C")) |
|
554 |
<< (qStringTableRow("D", "E", "F"))) |
|
555 |
<< (STRINGTABLE // destination table |
|
556 |
<< (qStringTableRow("0", "1", "2")) |
|
557 |
<< (qStringTableRow("3", "4", "5"))) |
|
558 |
<< (Selection() // selection |
|
559 |
<< Position(0, 0) << Position(0, 1) |
|
560 |
<< Position(1, 0)) |
|
561 |
<< Position(0, 0) // drop position |
|
562 |
<< (STRINGTABLE // resulting table |
|
563 |
<< (qStringTableRow("A", "B", "" )) |
|
564 |
<< (qStringTableRow("D", "" , "" )) |
|
565 |
<< (qStringTableRow("0", "1", "2")) |
|
566 |
<< (qStringTableRow("3", "4", "5"))); |
|
567 |
} |
|
568 |
||
569 |
{ |
|
570 |
QTest::newRow("non-square dropped at [0, 2]") |
|
571 |
<< (STRINGTABLE // source table |
|
572 |
<< (qStringTableRow("A", "B", "C")) |
|
573 |
<< (qStringTableRow("D", "E", "F"))) |
|
574 |
<< (STRINGTABLE // destination table |
|
575 |
<< (qStringTableRow("0", "1", "2")) |
|
576 |
<< (qStringTableRow("3", "4", "5"))) |
|
577 |
<< (Selection() // selection |
|
578 |
<< Position(0, 0) << Position(0, 1) |
|
579 |
<< Position(1, 0)) |
|
580 |
<< Position(0, 2) // drop position |
|
581 |
<< (STRINGTABLE // resulting table |
|
582 |
<< (qStringTableRow("" , "" , "A")) |
|
583 |
<< (qStringTableRow("" , "" , "D")) |
|
584 |
<< (qStringTableRow("" , "" , "B")) |
|
585 |
<< (qStringTableRow("0", "1", "2")) |
|
586 |
<< (qStringTableRow("3", "4", "5"))); |
|
587 |
} |
|
588 |
||
589 |
{ |
|
590 |
QTest::newRow("2x 1x2 dropped at [0, 0] (duplicates)") |
|
591 |
<< (STRINGTABLE // source table |
|
592 |
<< (qStringTableRow("A", "B", "C")) |
|
593 |
<< (qStringTableRow("D", "E", "F"))) |
|
594 |
<< (STRINGTABLE // destination table |
|
595 |
<< (qStringTableRow("0", "1", "2")) |
|
596 |
<< (qStringTableRow("3", "4", "5"))) |
|
597 |
<< (Selection() // selection; 2x the same row (to simulate selections in hierarchy) |
|
598 |
<< Position(0, 0) << Position(0, 1) |
|
599 |
<< Position(0, 0) << Position(0, 1)) |
|
600 |
<< Position(0, 0) // drop position |
|
601 |
<< (STRINGTABLE // resulting table |
|
602 |
<< (qStringTableRow("A", "B", "" )) |
|
603 |
<< (qStringTableRow("A", "" , "" )) |
|
604 |
<< (qStringTableRow("" , "B", "" )) // ### FIXME: strange behavior, but rare case |
|
605 |
<< (qStringTableRow("0", "1", "2")) |
|
606 |
<< (qStringTableRow("3", "4", "5"))); |
|
607 |
} |
|
608 |
||
609 |
{ |
|
610 |
QTest::newRow("2x 1x2 dropped at [3, 2] (duplicates)") |
|
611 |
<< (STRINGTABLE // source table |
|
612 |
<< (qStringTableRow("A", "B", "C")) |
|
613 |
<< (qStringTableRow("D", "E", "F"))) |
|
614 |
<< (STRINGTABLE // destination table |
|
615 |
<< (qStringTableRow("0", "1", "2")) |
|
616 |
<< (qStringTableRow("3", "4", "5"))) |
|
617 |
<< (Selection() // selection; 2x the same row (to simulate selections in hierarchy) |
|
618 |
<< Position(0, 0) << Position(0, 1) |
|
619 |
<< Position(0, 0) << Position(0, 1)) |
|
620 |
<< Position(3, 2) // drop position |
|
621 |
<< (STRINGTABLE // resulting table |
|
622 |
<< (qStringTableRow("0", "1", "2")) |
|
623 |
<< (qStringTableRow("3", "4", "5")) |
|
624 |
<< (qStringTableRow("" , "" , "A")) |
|
625 |
<< (qStringTableRow("" , "" , "B")) |
|
626 |
<< (qStringTableRow("" , "" , "A")) |
|
627 |
<< (qStringTableRow("" , "" , "B"))); |
|
628 |
} |
|
629 |
{ |
|
630 |
QTest::newRow("2x 1x2 dropped at [3, 2] (different rows)") |
|
631 |
<< (STRINGTABLE // source table |
|
632 |
<< (qStringTableRow("A", "B", "C")) |
|
633 |
<< (qStringTableRow("D", "E", "F")) |
|
634 |
<< (qStringTableRow("G", "H", "I"))) |
|
635 |
<< (STRINGTABLE // destination table |
|
636 |
<< (qStringTableRow("0", "1", "2")) |
|
637 |
<< (qStringTableRow("3", "4", "5"))) |
|
638 |
<< (Selection() // selection; 2x the same row (to simulate selections in hierarchy) |
|
639 |
<< Position(0, 0) << Position(0, 1) |
|
640 |
<< Position(2, 0) << Position(2, 1)) |
|
641 |
<< Position(2, 1) // drop position |
|
642 |
<< (STRINGTABLE // resulting table |
|
643 |
<< (qStringTableRow("0", "1", "2")) |
|
644 |
<< (qStringTableRow("3", "4", "5")) |
|
645 |
<< (qStringTableRow("" , "A" , "B")) |
|
646 |
<< (qStringTableRow("" , "G" , "H"))); |
|
647 |
} |
|
648 |
||
649 |
{ |
|
650 |
QTest::newRow("2x 1x2 dropped at [3, 2] (different rows, over the edge)") |
|
651 |
<< (STRINGTABLE // source table |
|
652 |
<< (qStringTableRow("A", "B", "C")) |
|
653 |
<< (qStringTableRow("D", "E", "F")) |
|
654 |
<< (qStringTableRow("G", "H", "I"))) |
|
655 |
<< (STRINGTABLE // destination table |
|
656 |
<< (qStringTableRow("0", "1", "2")) |
|
657 |
<< (qStringTableRow("3", "4", "5"))) |
|
658 |
<< (Selection() // selection; 2x the same row (to simulate selections in hierarchy) |
|
659 |
<< Position(0, 0) << Position(0, 1) |
|
660 |
<< Position(2, 0) << Position(2, 1)) |
|
661 |
<< Position(3, 2) // drop position |
|
662 |
<< (STRINGTABLE // resulting table |
|
663 |
<< (qStringTableRow("0", "1", "2")) |
|
664 |
<< (qStringTableRow("3", "4", "5")) |
|
665 |
<< (qStringTableRow("" , "" , "A")) |
|
666 |
<< (qStringTableRow("" , "" , "G")) |
|
667 |
<< (qStringTableRow("" , "" , "B")) |
|
668 |
<< (qStringTableRow("" , "" , "H"))); |
|
669 |
} |
|
670 |
} |
|
671 |
||
672 |
void tst_QAbstractItemModel::dropMimeData() |
|
673 |
{ |
|
674 |
QFETCH(StringTable, src_table); |
|
675 |
QFETCH(StringTable, dst_table); |
|
676 |
QFETCH(Selection, selection); |
|
677 |
QFETCH(Position, dst_position); |
|
678 |
QFETCH(StringTable, res_table); |
|
679 |
||
680 |
QtTestModel src(src_table); |
|
681 |
QtTestModel dst(dst_table); |
|
682 |
QtTestModel res(res_table); |
|
683 |
||
684 |
// qDebug() << "src" << src.rowCount(QModelIndex()) << src.columnCount(QModelIndex()); |
|
685 |
// qDebug() << "dst" << dst.rowCount(QModelIndex()) << dst.columnCount(QModelIndex()); |
|
686 |
// qDebug() << "res" << res.rowCount(QModelIndex()) << res.columnCount(QModelIndex()); |
|
687 |
||
688 |
// get the mimeData from the "selected" indexes |
|
689 |
QModelIndexList selectedIndexes; |
|
690 |
for (int i = 0; i < selection.count(); ++i) |
|
691 |
selectedIndexes << src.index(selection.at(i).first, selection.at(i).second, QModelIndex()); |
|
692 |
QMimeData *md = src.mimeData(selectedIndexes); |
|
693 |
// do the drop |
|
694 |
dst.dropMimeData(md, Qt::CopyAction, dst_position.first, dst_position.second, QModelIndex()); |
|
695 |
delete md; |
|
696 |
||
697 |
// compare to the expected results |
|
698 |
QCOMPARE(dst.rowCount(QModelIndex()), res.rowCount(QModelIndex())); |
|
699 |
QCOMPARE(dst.columnCount(QModelIndex()), res.columnCount(QModelIndex())); |
|
700 |
for (int r = 0; r < dst.rowCount(QModelIndex()); ++r) { |
|
701 |
for (int c = 0; c < dst.columnCount(QModelIndex()); ++c) { |
|
702 |
QModelIndex dst_idx = dst.index(r, c, QModelIndex()); |
|
703 |
QModelIndex res_idx = res.index(r, c, QModelIndex()); |
|
704 |
QMap<int, QVariant> dst_data = dst.itemData(dst_idx); |
|
705 |
QMap<int, QVariant> res_data = res.itemData(res_idx); |
|
706 |
//if(dst_data != res_data) |
|
707 |
// qDebug() << r << c << dst_data.value(0).toString() << res_data.value(0).toString(); |
|
708 |
QCOMPARE(dst_data , res_data); |
|
709 |
} |
|
710 |
} |
|
711 |
||
712 |
} |
|
713 |
||
714 |
||
715 |
void tst_QAbstractItemModel::changePersistentIndex() |
|
716 |
{ |
|
717 |
QtTestModel model(3, 3); |
|
718 |
QModelIndex a = model.index(1, 2, QModelIndex()); |
|
719 |
QModelIndex b = model.index(2, 1, QModelIndex()); |
|
720 |
QPersistentModelIndex p(a); |
|
721 |
QVERIFY(p == a); |
|
722 |
model.setPersistent(a, b); |
|
723 |
QVERIFY(p == b); |
|
724 |
} |
|
725 |
||
726 |
void tst_QAbstractItemModel::movePersistentIndex() |
|
727 |
{ |
|
728 |
QtTestModel model(3, 3); |
|
729 |
||
730 |
QPersistentModelIndex a = model.index(1, 1); |
|
731 |
QVERIFY(a.isValid()); |
|
732 |
QCOMPARE(a.row(), 1); |
|
733 |
QCOMPARE(a.column(), 1); |
|
734 |
||
735 |
model.insertRow(0); |
|
736 |
QCOMPARE(a.row(), 2); |
|
737 |
||
738 |
model.insertRow(1); |
|
739 |
QCOMPARE(a.row(), 3); |
|
740 |
||
741 |
model.insertColumn(0); |
|
742 |
QCOMPARE(a.column(), 2); |
|
743 |
} |
|
744 |
||
745 |
void tst_QAbstractItemModel::removeRows() |
|
746 |
{ |
|
747 |
QtTestModel model(10, 10); |
|
748 |
||
749 |
QSignalSpy rowsAboutToBeRemovedSpy(&model, SIGNAL(rowsAboutToBeRemoved( const QModelIndex &, int , int ))); |
|
750 |
QSignalSpy rowsRemovedSpy(&model, SIGNAL(rowsRemoved( const QModelIndex &, int, int ))); |
|
751 |
||
752 |
QCOMPARE(model.removeRows(6, 4), true); |
|
753 |
QCOMPARE(rowsAboutToBeRemovedSpy.count(), 1); |
|
754 |
QCOMPARE(rowsRemovedSpy.count(), 1); |
|
755 |
} |
|
756 |
||
757 |
void tst_QAbstractItemModel::removeColumns() |
|
758 |
{ |
|
759 |
QtTestModel model(10, 10); |
|
760 |
||
761 |
QSignalSpy columnsAboutToBeRemovedSpy(&model, SIGNAL(columnsAboutToBeRemoved( const QModelIndex &, int , int ))); |
|
762 |
QSignalSpy columnsRemovedSpy(&model, SIGNAL(columnsRemoved( const QModelIndex &, int, int ))); |
|
763 |
||
764 |
QCOMPARE(model.removeColumns(6, 4), true); |
|
765 |
QCOMPARE(columnsAboutToBeRemovedSpy.count(), 1); |
|
766 |
QCOMPARE(columnsRemovedSpy.count(), 1); |
|
767 |
} |
|
768 |
||
769 |
void tst_QAbstractItemModel::insertRows() |
|
770 |
{ |
|
771 |
QtTestModel model(10, 10); |
|
772 |
||
773 |
QSignalSpy rowsAboutToBeInsertedSpy(&model, SIGNAL(rowsAboutToBeInserted( const QModelIndex &, int , int ))); |
|
774 |
QSignalSpy rowsInsertedSpy(&model, SIGNAL(rowsInserted( const QModelIndex &, int, int ))); |
|
775 |
||
776 |
QCOMPARE(model.insertRows(6, 4), true); |
|
777 |
QCOMPARE(rowsAboutToBeInsertedSpy.count(), 1); |
|
778 |
QCOMPARE(rowsInsertedSpy.count(), 1); |
|
779 |
} |
|
780 |
||
781 |
void tst_QAbstractItemModel::insertColumns() |
|
782 |
{ |
|
783 |
QtTestModel model(10, 10); |
|
784 |
||
785 |
QSignalSpy columnsAboutToBeInsertedSpy(&model, SIGNAL(columnsAboutToBeInserted( const QModelIndex &, int , int ))); |
|
786 |
QSignalSpy columnsInsertedSpy(&model, SIGNAL(columnsInserted( const QModelIndex &, int, int ))); |
|
787 |
||
788 |
QCOMPARE(model.insertColumns(6, 4), true); |
|
789 |
QCOMPARE(columnsAboutToBeInsertedSpy.count(), 1); |
|
790 |
QCOMPARE(columnsInsertedSpy.count(), 1); |
|
791 |
} |
|
792 |
||
793 |
void tst_QAbstractItemModel::reset() |
|
794 |
{ |
|
795 |
QtTestModel model(10, 10); |
|
796 |
||
797 |
QSignalSpy resetSpy(&model, SIGNAL(modelReset())); |
|
798 |
model.reset(); |
|
799 |
QCOMPARE(resetSpy.count(), 1); |
|
800 |
} |
|
801 |
||
802 |
void tst_QAbstractItemModel::complexChangesWithPersistent() |
|
803 |
{ |
|
804 |
QtTestModel model(10, 10); |
|
805 |
QPersistentModelIndex a = model.index(1, 1, QModelIndex()); |
|
806 |
QPersistentModelIndex b = model.index(9, 7, QModelIndex()); |
|
807 |
QPersistentModelIndex c = model.index(5, 6, QModelIndex()); |
|
808 |
QPersistentModelIndex d = model.index(3, 9, QModelIndex()); |
|
809 |
QPersistentModelIndex e[10]; |
|
810 |
for (int i=0; i <10 ; i++) { |
|
811 |
e[i] = model.index(2, i , QModelIndex()); |
|
812 |
} |
|
813 |
||
814 |
QVERIFY(a == model.index(1, 1, QModelIndex())); |
|
815 |
QVERIFY(b == model.index(9, 7, QModelIndex())); |
|
816 |
QVERIFY(c == model.index(5, 6, QModelIndex())); |
|
817 |
QVERIFY(d == model.index(3, 9, QModelIndex())); |
|
818 |
for (int i=0; i <8 ; i++) |
|
819 |
QVERIFY(e[i] == model.index(2, i , QModelIndex())); |
|
820 |
||
821 |
//remove a bunch of columns |
|
822 |
model.removeColumns(2, 4); |
|
823 |
||
824 |
QVERIFY(a == model.index(1, 1, QModelIndex())); |
|
825 |
QVERIFY(b == model.index(9, 3, QModelIndex())); |
|
826 |
QVERIFY(c == model.index(5, 2, QModelIndex())); |
|
827 |
QVERIFY(d == model.index(3, 5, QModelIndex())); |
|
828 |
for (int i=0; i <2 ; i++) |
|
829 |
QVERIFY(e[i] == model.index(2, i , QModelIndex())); |
|
830 |
for (int i=2; i <6 ; i++) |
|
831 |
QVERIFY(!e[i].isValid()); |
|
832 |
for (int i=6; i <10 ; i++) |
|
833 |
QVERIFY(e[i] == model.index(2, i-4 , QModelIndex())); |
|
834 |
||
835 |
//move some indexes around |
|
836 |
model.setPersistent(model.index(1, 1 , QModelIndex()), model.index(9, 3 , QModelIndex())); |
|
837 |
model.setPersistent(model.index(9, 3 , QModelIndex()), model.index(8, 4 , QModelIndex())); |
|
838 |
||
839 |
QVERIFY(a == model.index(9, 3, QModelIndex())); |
|
840 |
QVERIFY(b == model.index(8, 4, QModelIndex())); |
|
841 |
QVERIFY(c == model.index(5, 2, QModelIndex())); |
|
842 |
QVERIFY(d == model.index(3, 5, QModelIndex())); |
|
843 |
for (int i=0; i <2 ; i++) |
|
844 |
QVERIFY(e[i] == model.index(2, i , QModelIndex())); |
|
845 |
for (int i=2; i <6 ; i++) |
|
846 |
QVERIFY(!e[i].isValid()); |
|
847 |
for (int i=6; i <10 ; i++) |
|
848 |
QVERIFY(e[i] == model.index(2, i-4 , QModelIndex())); |
|
849 |
||
850 |
//inserting a bunch of columns |
|
851 |
model.insertColumns(2, 2); |
|
852 |
QVERIFY(a == model.index(9, 5, QModelIndex())); |
|
853 |
QVERIFY(b == model.index(8, 6, QModelIndex())); |
|
854 |
QVERIFY(c == model.index(5, 4, QModelIndex())); |
|
855 |
QVERIFY(d == model.index(3, 7, QModelIndex())); |
|
856 |
for (int i=0; i <2 ; i++) |
|
857 |
QVERIFY(e[i] == model.index(2, i , QModelIndex())); |
|
858 |
for (int i=2; i <6 ; i++) |
|
859 |
QVERIFY(!e[i].isValid()); |
|
860 |
for (int i=6; i <10 ; i++) |
|
861 |
QVERIFY(e[i] == model.index(2, i-2 , QModelIndex())); |
|
862 |
||
863 |
} |
|
864 |
||
865 |
void tst_QAbstractItemModel::testMoveSameParentDown_data() |
|
866 |
{ |
|
867 |
QTest::addColumn<int>("startRow"); |
|
868 |
QTest::addColumn<int>("endRow"); |
|
869 |
QTest::addColumn<int>("destRow"); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
870 |
// We can't put the actual parent index for the move in here because m_model is not defined until init() is run. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
871 |
QTest::addColumn<bool>("topLevel"); |
0 | 872 |
|
873 |
// Move from the start to the middle |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
874 |
QTest::newRow("move01") << 0 << 2 << 8 << true; |
0 | 875 |
// Move from the start to the end |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
876 |
QTest::newRow("move02") << 0 << 2 << 10 << true; |
0 | 877 |
// Move from the middle to the middle |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
878 |
QTest::newRow("move03") << 3 << 5 << 8 << true; |
0 | 879 |
// Move from the middle to the end |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
880 |
QTest::newRow("move04") << 3 << 5 << 10 << true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
881 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
882 |
QTest::newRow("move05") << 0 << 2 << 8 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
883 |
QTest::newRow("move06") << 0 << 2 << 10 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
884 |
QTest::newRow("move07") << 3 << 5 << 8 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
885 |
QTest::newRow("move08") << 3 << 5 << 10 << false; |
0 | 886 |
} |
887 |
||
888 |
void tst_QAbstractItemModel::testMoveSameParentDown() |
|
889 |
{ |
|
890 |
QFETCH( int, startRow); |
|
891 |
QFETCH( int, endRow); |
|
892 |
QFETCH( int, destRow); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
893 |
QFETCH( bool, topLevel); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
894 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
895 |
QModelIndex moveParent = topLevel ? QModelIndex() : m_model->index(5, 0); |
0 | 896 |
|
897 |
QList<QPersistentModelIndex> persistentList; |
|
898 |
QModelIndexList indexList; |
|
899 |
||
900 |
for (int column = 0; column < m_model->columnCount(); ++column) |
|
901 |
{ |
|
902 |
for (int row= 0; row < m_model->rowCount(); ++row) |
|
903 |
{ |
|
904 |
QModelIndex idx = m_model->index(row, column); |
|
905 |
QVERIFY(idx.isValid()); |
|
906 |
indexList << idx; |
|
907 |
persistentList << QPersistentModelIndex(idx); |
|
908 |
} |
|
909 |
} |
|
910 |
||
911 |
QModelIndex parent = m_model->index(5, 0); |
|
912 |
for (int column = 0; column < m_model->columnCount(); ++column) |
|
913 |
{ |
|
914 |
for (int row= 0; row < m_model->rowCount(parent); ++row) |
|
915 |
{ |
|
916 |
QModelIndex idx = m_model->index(row, column, parent); |
|
917 |
QVERIFY(idx.isValid()); |
|
918 |
indexList << idx; |
|
919 |
persistentList << QPersistentModelIndex(idx); |
|
920 |
} |
|
921 |
} |
|
922 |
||
923 |
QSignalSpy beforeSpy(m_model, SIGNAL(rowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
924 |
QSignalSpy afterSpy(m_model, SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
925 |
||
926 |
ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this); |
|
927 |
moveCommand->setNumCols(4); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
928 |
if (!topLevel) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
929 |
moveCommand->setAncestorRowNumbers(QList<int>() << 5); |
0 | 930 |
moveCommand->setStartRow(startRow); |
931 |
moveCommand->setEndRow(endRow); |
|
932 |
moveCommand->setDestRow(destRow); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
933 |
if (!topLevel) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
934 |
moveCommand->setDestAncestors(QList<int>() << 5); |
0 | 935 |
moveCommand->doCommand(); |
936 |
||
937 |
QVariantList beforeSignal = beforeSpy.takeAt(0); |
|
938 |
QVariantList afterSignal = afterSpy.takeAt(0); |
|
939 |
||
940 |
QCOMPARE(beforeSignal.size(), 5); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
941 |
QCOMPARE(beforeSignal.at(0).value<QModelIndex>(), moveParent); |
0 | 942 |
QCOMPARE(beforeSignal.at(1).toInt(), startRow); |
943 |
QCOMPARE(beforeSignal.at(2).toInt(), endRow); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
944 |
QCOMPARE(beforeSignal.at(3).value<QModelIndex>(), moveParent); |
0 | 945 |
QCOMPARE(beforeSignal.at(4).toInt(), destRow); |
946 |
||
947 |
QCOMPARE(afterSignal.size(), 5); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
948 |
QCOMPARE(afterSignal.at(0).value<QModelIndex>(), moveParent); |
0 | 949 |
QCOMPARE(afterSignal.at(1).toInt(), startRow); |
950 |
QCOMPARE(afterSignal.at(2).toInt(), endRow); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
951 |
QCOMPARE(afterSignal.at(3).value<QModelIndex>(), moveParent); |
0 | 952 |
QCOMPARE(afterSignal.at(4).toInt(), destRow); |
953 |
||
954 |
for (int i = 0; i < indexList.size(); i++) |
|
955 |
{ |
|
956 |
QModelIndex idx = indexList.at(i); |
|
957 |
QModelIndex persistentIndex = persistentList.at(i); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
958 |
if (idx.parent() == moveParent) |
0 | 959 |
{ |
960 |
int row = idx.row(); |
|
961 |
if ( row >= startRow) |
|
962 |
{ |
|
963 |
if (row <= endRow) |
|
964 |
{ |
|
965 |
QCOMPARE(row + destRow - endRow - 1, persistentIndex.row() ); |
|
966 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
967 |
QCOMPARE(idx.parent(), persistentIndex.parent()); |
|
968 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
969 |
} else if ( row < destRow) |
|
970 |
{ |
|
971 |
QCOMPARE(row - (endRow - startRow + 1), persistentIndex.row() ); |
|
972 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
973 |
QCOMPARE(idx.parent(), persistentIndex.parent()); |
|
974 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
975 |
} else |
|
976 |
{ |
|
977 |
QCOMPARE(idx, persistentIndex); |
|
978 |
} |
|
979 |
} else |
|
980 |
{ |
|
981 |
QCOMPARE(idx, persistentIndex); |
|
982 |
} |
|
983 |
} else |
|
984 |
{ |
|
985 |
QCOMPARE(idx, persistentIndex); |
|
986 |
} |
|
987 |
} |
|
988 |
} |
|
989 |
||
990 |
void tst_QAbstractItemModel::testMoveSameParentUp_data() |
|
991 |
{ |
|
992 |
QTest::addColumn<int>("startRow"); |
|
993 |
QTest::addColumn<int>("endRow"); |
|
994 |
QTest::addColumn<int>("destRow"); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
995 |
QTest::addColumn<bool>("topLevel"); |
0 | 996 |
|
997 |
// Move from the middle to the start |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
998 |
QTest::newRow("move01") << 5 << 7 << 0 << true; |
0 | 999 |
// Move from the end to the start |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1000 |
QTest::newRow("move02") << 8 << 9 << 0 << true; |
0 | 1001 |
// Move from the middle to the middle |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1002 |
QTest::newRow("move03") << 5 << 7 << 2 << true; |
0 | 1003 |
// Move from the end to the middle |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1004 |
QTest::newRow("move04") << 8 << 9 << 5 << true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1005 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1006 |
QTest::newRow("move05") << 5 << 7 << 0 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1007 |
QTest::newRow("move06") << 8 << 9 << 0 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1008 |
QTest::newRow("move07") << 5 << 7 << 2 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1009 |
QTest::newRow("move08") << 8 << 9 << 5 << false; |
0 | 1010 |
} |
1011 |
||
1012 |
void tst_QAbstractItemModel::testMoveSameParentUp() |
|
1013 |
{ |
|
1014 |
||
1015 |
QFETCH( int, startRow); |
|
1016 |
QFETCH( int, endRow); |
|
1017 |
QFETCH( int, destRow); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1018 |
QFETCH( bool, topLevel); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1019 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1020 |
QModelIndex moveParent = topLevel ? QModelIndex() : m_model->index(5, 0); |
0 | 1021 |
|
1022 |
QList<QPersistentModelIndex> persistentList; |
|
1023 |
QModelIndexList indexList; |
|
1024 |
||
1025 |
for (int column = 0; column < m_model->columnCount(); ++column) |
|
1026 |
{ |
|
1027 |
for (int row= 0; row < m_model->rowCount(); ++row) |
|
1028 |
{ |
|
1029 |
QModelIndex idx = m_model->index(row, column); |
|
1030 |
QVERIFY(idx.isValid()); |
|
1031 |
indexList << idx; |
|
1032 |
persistentList << QPersistentModelIndex(idx); |
|
1033 |
} |
|
1034 |
} |
|
1035 |
||
1036 |
QModelIndex parent = m_model->index(2, 0); |
|
1037 |
for (int column = 0; column < m_model->columnCount(); ++column) |
|
1038 |
{ |
|
1039 |
for (int row= 0; row < m_model->rowCount(parent); ++row) |
|
1040 |
{ |
|
1041 |
QModelIndex idx = m_model->index(row, column, parent); |
|
1042 |
QVERIFY(idx.isValid()); |
|
1043 |
indexList << idx; |
|
1044 |
persistentList << QPersistentModelIndex(idx); |
|
1045 |
} |
|
1046 |
} |
|
1047 |
||
1048 |
QSignalSpy beforeSpy(m_model, SIGNAL(rowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
1049 |
QSignalSpy afterSpy(m_model, SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
1050 |
||
1051 |
||
1052 |
ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this); |
|
1053 |
moveCommand->setNumCols(4); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1054 |
if (!topLevel) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1055 |
moveCommand->setAncestorRowNumbers(QList<int>() << 5); |
0 | 1056 |
moveCommand->setStartRow(startRow); |
1057 |
moveCommand->setEndRow(endRow); |
|
1058 |
moveCommand->setDestRow(destRow); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1059 |
if (!topLevel) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1060 |
moveCommand->setDestAncestors(QList<int>() << 5); |
0 | 1061 |
moveCommand->doCommand(); |
1062 |
||
1063 |
QVariantList beforeSignal = beforeSpy.takeAt(0); |
|
1064 |
QVariantList afterSignal = afterSpy.takeAt(0); |
|
1065 |
||
1066 |
QCOMPARE(beforeSignal.size(), 5); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1067 |
QCOMPARE(beforeSignal.at(0).value<QModelIndex>(), moveParent); |
0 | 1068 |
QCOMPARE(beforeSignal.at(1).toInt(), startRow); |
1069 |
QCOMPARE(beforeSignal.at(2).toInt(), endRow); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1070 |
QCOMPARE(beforeSignal.at(3).value<QModelIndex>(), moveParent); |
0 | 1071 |
QCOMPARE(beforeSignal.at(4).toInt(), destRow); |
1072 |
||
1073 |
QCOMPARE(afterSignal.size(), 5); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1074 |
QCOMPARE(afterSignal.at(0).value<QModelIndex>(), moveParent); |
0 | 1075 |
QCOMPARE(afterSignal.at(1).toInt(), startRow); |
1076 |
QCOMPARE(afterSignal.at(2).toInt(), endRow); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1077 |
QCOMPARE(afterSignal.at(3).value<QModelIndex>(), moveParent); |
0 | 1078 |
QCOMPARE(afterSignal.at(4).toInt(), destRow); |
1079 |
||
1080 |
||
1081 |
for (int i = 0; i < indexList.size(); i++) |
|
1082 |
{ |
|
1083 |
QModelIndex idx = indexList.at(i); |
|
1084 |
QModelIndex persistentIndex = persistentList.at(i); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1085 |
if (idx.parent() == moveParent) |
0 | 1086 |
{ |
1087 |
int row = idx.row(); |
|
1088 |
if ( row >= destRow) |
|
1089 |
{ |
|
1090 |
if (row < startRow) |
|
1091 |
{ |
|
1092 |
QCOMPARE(row + endRow - startRow + 1, persistentIndex.row() ); |
|
1093 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
1094 |
QCOMPARE(idx.parent(), persistentIndex.parent()); |
|
1095 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
1096 |
} else if ( row <= endRow) |
|
1097 |
{ |
|
1098 |
QCOMPARE(row + destRow - startRow, persistentIndex.row() ); |
|
1099 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
1100 |
QCOMPARE(idx.parent(), persistentIndex.parent()); |
|
1101 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
1102 |
} else |
|
1103 |
{ |
|
1104 |
QCOMPARE(idx, persistentIndex); |
|
1105 |
} |
|
1106 |
} else |
|
1107 |
{ |
|
1108 |
QCOMPARE(idx, persistentIndex); |
|
1109 |
} |
|
1110 |
} else |
|
1111 |
{ |
|
1112 |
QCOMPARE(idx, persistentIndex); |
|
1113 |
} |
|
1114 |
} |
|
1115 |
} |
|
1116 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1117 |
void tst_QAbstractItemModel::testMoveThroughProxy() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1118 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1119 |
QSortFilterProxyModel *proxy = new QSortFilterProxyModel(this); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1120 |
proxy->setSourceModel(m_model); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1121 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1122 |
QList<QPersistentModelIndex> persistentList; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1123 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1124 |
persistentList.append(proxy->index(0, 0)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1125 |
persistentList.append(proxy->index(0, 0, proxy->mapFromSource(m_model->index(5, 0)))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1126 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1127 |
ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1128 |
moveCommand->setNumCols(4); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1129 |
moveCommand->setAncestorRowNumbers(QList<int>() << 5); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1130 |
moveCommand->setStartRow(0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1131 |
moveCommand->setEndRow(0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1132 |
moveCommand->setDestRow(0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1133 |
moveCommand->doCommand(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1134 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1135 |
|
0 | 1136 |
void tst_QAbstractItemModel::testMoveToGrandParent_data() |
1137 |
{ |
|
1138 |
QTest::addColumn<int>("startRow"); |
|
1139 |
QTest::addColumn<int>("endRow"); |
|
1140 |
QTest::addColumn<int>("destRow"); |
|
1141 |
||
1142 |
// Move from the start to the middle |
|
1143 |
QTest::newRow("move01") << 0 << 2 << 8; |
|
1144 |
// Move from the start to the end |
|
1145 |
QTest::newRow("move02") << 0 << 2 << 10; |
|
1146 |
// Move from the middle to the middle |
|
1147 |
QTest::newRow("move03") << 3 << 5 << 8; |
|
1148 |
// Move from the middle to the end |
|
1149 |
QTest::newRow("move04") << 3 << 5 << 10; |
|
1150 |
||
1151 |
// Move from the middle to the start |
|
1152 |
QTest::newRow("move05") << 5 << 7 << 0; |
|
1153 |
// Move from the end to the start |
|
1154 |
QTest::newRow("move06") << 8 << 9 << 0; |
|
1155 |
// Move from the middle to the middle |
|
1156 |
QTest::newRow("move07") << 5 << 7 << 2; |
|
1157 |
// Move from the end to the middle |
|
1158 |
QTest::newRow("move08") << 8 << 9 << 5; |
|
1159 |
||
1160 |
// Moving to the same row in a different parent doesn't confuse things. |
|
1161 |
QTest::newRow("move09") << 8 << 8 << 8; |
|
1162 |
||
1163 |
// Moving to the row of my parent and its neighbours doesn't confuse things |
|
1164 |
QTest::newRow("move09") << 8 << 8 << 4; |
|
1165 |
QTest::newRow("move10") << 8 << 8 << 5; |
|
1166 |
QTest::newRow("move11") << 8 << 8 << 6; |
|
1167 |
||
1168 |
// Moving everything from one parent to another |
|
1169 |
QTest::newRow("move12") << 0 << 9 << 10; |
|
1170 |
} |
|
1171 |
||
1172 |
void tst_QAbstractItemModel::testMoveToGrandParent() |
|
1173 |
{ |
|
1174 |
||
1175 |
QFETCH( int, startRow); |
|
1176 |
QFETCH( int, endRow); |
|
1177 |
QFETCH( int, destRow); |
|
1178 |
||
1179 |
QList<QPersistentModelIndex> persistentList; |
|
1180 |
QModelIndexList indexList; |
|
1181 |
QModelIndexList parentsList; |
|
1182 |
||
1183 |
for (int column = 0; column < m_model->columnCount(); ++column) |
|
1184 |
{ |
|
1185 |
for (int row= 0; row < m_model->rowCount(); ++row) |
|
1186 |
{ |
|
1187 |
QModelIndex idx = m_model->index(row, column); |
|
1188 |
QVERIFY(idx.isValid()); |
|
1189 |
indexList << idx; |
|
1190 |
parentsList << idx.parent(); |
|
1191 |
persistentList << QPersistentModelIndex(idx); |
|
1192 |
} |
|
1193 |
} |
|
1194 |
||
1195 |
QModelIndex sourceIndex = m_model->index(5, 0); |
|
1196 |
for (int column = 0; column < m_model->columnCount(); ++column) |
|
1197 |
{ |
|
1198 |
for (int row= 0; row < m_model->rowCount(sourceIndex); ++row) |
|
1199 |
{ |
|
1200 |
QModelIndex idx = m_model->index(row, column, sourceIndex); |
|
1201 |
QVERIFY(idx.isValid()); |
|
1202 |
indexList << idx; |
|
1203 |
parentsList << idx.parent(); |
|
1204 |
persistentList << QPersistentModelIndex(idx); |
|
1205 |
} |
|
1206 |
} |
|
1207 |
||
1208 |
QSignalSpy beforeSpy(m_model, SIGNAL(rowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
1209 |
QSignalSpy afterSpy(m_model, SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
1210 |
||
1211 |
||
1212 |
ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this); |
|
1213 |
moveCommand->setAncestorRowNumbers(QList<int>() << 5); |
|
1214 |
moveCommand->setNumCols(4); |
|
1215 |
moveCommand->setStartRow(startRow); |
|
1216 |
moveCommand->setEndRow(endRow); |
|
1217 |
moveCommand->setDestRow(destRow); |
|
1218 |
moveCommand->doCommand(); |
|
1219 |
||
1220 |
QVariantList beforeSignal = beforeSpy.takeAt(0); |
|
1221 |
QVariantList afterSignal = afterSpy.takeAt(0); |
|
1222 |
||
1223 |
QCOMPARE(beforeSignal.size(), 5); |
|
1224 |
QCOMPARE(beforeSignal.at(0).value<QModelIndex>(), sourceIndex); |
|
1225 |
QCOMPARE(beforeSignal.at(1).toInt(), startRow); |
|
1226 |
QCOMPARE(beforeSignal.at(2).toInt(), endRow); |
|
1227 |
QCOMPARE(beforeSignal.at(3).value<QModelIndex>(), QModelIndex()); |
|
1228 |
QCOMPARE(beforeSignal.at(4).toInt(), destRow); |
|
1229 |
||
1230 |
QCOMPARE(afterSignal.size(), 5); |
|
1231 |
QCOMPARE(afterSignal.at(0).value<QModelIndex>(), sourceIndex); |
|
1232 |
QCOMPARE(afterSignal.at(1).toInt(), startRow); |
|
1233 |
QCOMPARE(afterSignal.at(2).toInt(), endRow); |
|
1234 |
QCOMPARE(afterSignal.at(3).value<QModelIndex>(), QModelIndex()); |
|
1235 |
QCOMPARE(afterSignal.at(4).toInt(), destRow); |
|
1236 |
||
1237 |
for (int i = 0; i < indexList.size(); i++) |
|
1238 |
{ |
|
1239 |
QModelIndex idx = indexList.at(i); |
|
1240 |
QModelIndex idxParent = parentsList.at(i); |
|
1241 |
QModelIndex persistentIndex = persistentList.at(i); |
|
1242 |
int row = idx.row(); |
|
1243 |
if (idxParent == QModelIndex()) |
|
1244 |
{ |
|
1245 |
if ( row >= destRow) |
|
1246 |
{ |
|
1247 |
QCOMPARE(row + endRow - startRow + 1, persistentIndex.row() ); |
|
1248 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
1249 |
QCOMPARE(idxParent, persistentIndex.parent()); |
|
1250 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
1251 |
} else |
|
1252 |
{ |
|
1253 |
QCOMPARE(idx, persistentIndex); |
|
1254 |
} |
|
1255 |
} else |
|
1256 |
{ |
|
1257 |
if (row < startRow) |
|
1258 |
{ |
|
1259 |
QCOMPARE(idx, persistentIndex); |
|
1260 |
} else if (row <= endRow) |
|
1261 |
{ |
|
1262 |
QCOMPARE(row + destRow - startRow, persistentIndex.row() ); |
|
1263 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
1264 |
QCOMPARE(QModelIndex(), persistentIndex.parent()); |
|
1265 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
1266 |
} else { |
|
1267 |
QCOMPARE(row - (endRow - startRow + 1), persistentIndex.row() ); |
|
1268 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
1269 |
||
1270 |
if (idxParent.row() >= destRow) |
|
1271 |
{ |
|
1272 |
QModelIndex adjustedParent; |
|
1273 |
adjustedParent = idxParent.sibling( idxParent.row() + endRow - startRow + 1, idxParent.column()); |
|
1274 |
QCOMPARE(adjustedParent, persistentIndex.parent()); |
|
1275 |
} else |
|
1276 |
{ |
|
1277 |
QCOMPARE(idxParent, persistentIndex.parent()); |
|
1278 |
} |
|
1279 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
1280 |
} |
|
1281 |
} |
|
1282 |
} |
|
1283 |
} |
|
1284 |
||
1285 |
void tst_QAbstractItemModel::testMoveToSibling_data() |
|
1286 |
{ |
|
1287 |
QTest::addColumn<int>("startRow"); |
|
1288 |
QTest::addColumn<int>("endRow"); |
|
1289 |
QTest::addColumn<int>("destRow"); |
|
1290 |
||
1291 |
// Move from the start to the middle |
|
1292 |
QTest::newRow("move01") << 0 << 2 << 8; |
|
1293 |
// Move from the start to the end |
|
1294 |
QTest::newRow("move02") << 0 << 2 << 10; |
|
1295 |
// Move from the middle to the middle |
|
1296 |
QTest::newRow("move03") << 2 << 4 << 8; |
|
1297 |
// Move from the middle to the end |
|
1298 |
QTest::newRow("move04") << 2 << 4 << 10; |
|
1299 |
||
1300 |
// Move from the middle to the start |
|
1301 |
QTest::newRow("move05") << 8 << 8 << 0; |
|
1302 |
// Move from the end to the start |
|
1303 |
QTest::newRow("move06") << 8 << 9 << 0; |
|
1304 |
// Move from the middle to the middle |
|
1305 |
QTest::newRow("move07") << 6 << 8 << 2; |
|
1306 |
// Move from the end to the middle |
|
1307 |
QTest::newRow("move08") << 8 << 9 << 5; |
|
1308 |
||
1309 |
// Moving to the same row in a different parent doesn't confuse things. |
|
1310 |
QTest::newRow("move09") << 8 << 8 << 8; |
|
1311 |
||
1312 |
// Moving to the row of my target and its neighbours doesn't confuse things |
|
1313 |
QTest::newRow("move09") << 8 << 8 << 4; |
|
1314 |
QTest::newRow("move10") << 8 << 8 << 5; |
|
1315 |
QTest::newRow("move11") << 8 << 8 << 6; |
|
1316 |
} |
|
1317 |
||
1318 |
void tst_QAbstractItemModel::testMoveToSibling() |
|
1319 |
{ |
|
1320 |
||
1321 |
QFETCH( int, startRow); |
|
1322 |
QFETCH( int, endRow); |
|
1323 |
QFETCH( int, destRow); |
|
1324 |
||
1325 |
QList<QPersistentModelIndex> persistentList; |
|
1326 |
QModelIndexList indexList; |
|
1327 |
QModelIndexList parentsList; |
|
1328 |
||
1329 |
const int column = 0; |
|
1330 |
||
1331 |
for (int i= 0; i < m_model->rowCount(); ++i) |
|
1332 |
{ |
|
1333 |
QModelIndex idx = m_model->index(i, column); |
|
1334 |
QVERIFY(idx.isValid()); |
|
1335 |
indexList << idx; |
|
1336 |
parentsList << idx.parent(); |
|
1337 |
persistentList << QPersistentModelIndex(idx); |
|
1338 |
} |
|
1339 |
||
1340 |
QModelIndex destIndex = m_model->index(5, 0); |
|
1341 |
QModelIndex sourceIndex; |
|
1342 |
for (int i= 0; i < m_model->rowCount(destIndex); ++i) |
|
1343 |
{ |
|
1344 |
QModelIndex idx = m_model->index(i, column, destIndex); |
|
1345 |
QVERIFY(idx.isValid()); |
|
1346 |
indexList << idx; |
|
1347 |
parentsList << idx.parent(); |
|
1348 |
persistentList << QPersistentModelIndex(idx); |
|
1349 |
} |
|
1350 |
||
1351 |
QSignalSpy beforeSpy(m_model, SIGNAL(rowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
1352 |
QSignalSpy afterSpy(m_model, SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
1353 |
||
1354 |
||
1355 |
ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this); |
|
1356 |
moveCommand->setNumCols(4); |
|
1357 |
moveCommand->setStartRow(startRow); |
|
1358 |
moveCommand->setEndRow(endRow); |
|
1359 |
moveCommand->setDestAncestors(QList<int>() << 5); |
|
1360 |
moveCommand->setDestRow(destRow); |
|
1361 |
moveCommand->doCommand(); |
|
1362 |
||
1363 |
QVariantList beforeSignal = beforeSpy.takeAt(0); |
|
1364 |
QVariantList afterSignal = afterSpy.takeAt(0); |
|
1365 |
||
1366 |
QCOMPARE(beforeSignal.size(), 5); |
|
1367 |
QCOMPARE(beforeSignal.at(0).value<QModelIndex>(), sourceIndex); |
|
1368 |
QCOMPARE(beforeSignal.at(1).toInt(), startRow); |
|
1369 |
QCOMPARE(beforeSignal.at(2).toInt(), endRow); |
|
1370 |
QCOMPARE(beforeSignal.at(3).value<QModelIndex>(), destIndex); |
|
1371 |
QCOMPARE(beforeSignal.at(4).toInt(), destRow); |
|
1372 |
||
1373 |
QCOMPARE(afterSignal.size(), 5); |
|
1374 |
QCOMPARE(afterSignal.at(0).value<QModelIndex>(), sourceIndex); |
|
1375 |
QCOMPARE(afterSignal.at(1).toInt(), startRow); |
|
1376 |
QCOMPARE(afterSignal.at(2).toInt(), endRow); |
|
1377 |
QCOMPARE(afterSignal.at(3).value<QModelIndex>(), destIndex); |
|
1378 |
QCOMPARE(afterSignal.at(4).toInt(), destRow); |
|
1379 |
||
1380 |
for (int i = 0; i < indexList.size(); i++) |
|
1381 |
{ |
|
1382 |
QModelIndex idx = indexList.at(i); |
|
1383 |
QModelIndex idxParent = parentsList.at(i); |
|
1384 |
QModelIndex persistentIndex = persistentList.at(i); |
|
1385 |
||
1386 |
QModelIndex adjustedDestination = destIndex.sibling(destIndex.row() - (endRow - startRow + 1), destIndex.column()); |
|
1387 |
int row = idx.row(); |
|
1388 |
if (idxParent == destIndex) |
|
1389 |
{ |
|
1390 |
if ( row >= destRow) |
|
1391 |
{ |
|
1392 |
QCOMPARE(row + endRow - startRow + 1, persistentIndex.row() ); |
|
1393 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
1394 |
if (idxParent.row() > startRow) |
|
1395 |
{ |
|
1396 |
QCOMPARE(adjustedDestination, persistentIndex.parent()); |
|
1397 |
} else { |
|
1398 |
QCOMPARE(destIndex, persistentIndex.parent()); |
|
1399 |
} |
|
1400 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
1401 |
} else |
|
1402 |
{ |
|
1403 |
QCOMPARE(idx, persistentIndex); |
|
1404 |
} |
|
1405 |
} else |
|
1406 |
{ |
|
1407 |
if (row < startRow) |
|
1408 |
{ |
|
1409 |
QCOMPARE(idx, persistentIndex); |
|
1410 |
} else if (row <= endRow) |
|
1411 |
{ |
|
1412 |
QCOMPARE(row + destRow - startRow, persistentIndex.row() ); |
|
1413 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
1414 |
if (destIndex.row() > startRow) |
|
1415 |
{ |
|
1416 |
QCOMPARE(adjustedDestination, persistentIndex.parent()); |
|
1417 |
} else { |
|
1418 |
QCOMPARE(destIndex, persistentIndex.parent()); |
|
1419 |
} |
|
1420 |
||
1421 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
1422 |
||
1423 |
} else { |
|
1424 |
QCOMPARE(row - (endRow - startRow + 1), persistentIndex.row() ); |
|
1425 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
1426 |
QCOMPARE(idxParent, persistentIndex.parent()); |
|
1427 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
1428 |
} |
|
1429 |
} |
|
1430 |
} |
|
1431 |
} |
|
1432 |
||
1433 |
void tst_QAbstractItemModel::testMoveToUncle_data() |
|
1434 |
{ |
|
1435 |
||
1436 |
QTest::addColumn<int>("startRow"); |
|
1437 |
QTest::addColumn<int>("endRow"); |
|
1438 |
QTest::addColumn<int>("destRow"); |
|
1439 |
||
1440 |
// Move from the start to the middle |
|
1441 |
QTest::newRow("move01") << 0 << 2 << 8; |
|
1442 |
// Move from the start to the end |
|
1443 |
QTest::newRow("move02") << 0 << 2 << 10; |
|
1444 |
// Move from the middle to the middle |
|
1445 |
QTest::newRow("move03") << 3 << 5 << 8; |
|
1446 |
// Move from the middle to the end |
|
1447 |
QTest::newRow("move04") << 3 << 5 << 10; |
|
1448 |
||
1449 |
// Move from the middle to the start |
|
1450 |
QTest::newRow("move05") << 5 << 7 << 0; |
|
1451 |
// Move from the end to the start |
|
1452 |
QTest::newRow("move06") << 8 << 9 << 0; |
|
1453 |
// Move from the middle to the middle |
|
1454 |
QTest::newRow("move07") << 5 << 7 << 2; |
|
1455 |
// Move from the end to the middle |
|
1456 |
QTest::newRow("move08") << 8 << 9 << 5; |
|
1457 |
||
1458 |
// Moving to the same row in a different parent doesn't confuse things. |
|
1459 |
QTest::newRow("move09") << 8 << 8 << 8; |
|
1460 |
||
1461 |
// Moving to the row of my parent and its neighbours doesn't confuse things |
|
1462 |
QTest::newRow("move09") << 8 << 8 << 4; |
|
1463 |
QTest::newRow("move10") << 8 << 8 << 5; |
|
1464 |
QTest::newRow("move11") << 8 << 8 << 6; |
|
1465 |
||
1466 |
// Moving everything from one parent to another |
|
1467 |
QTest::newRow("move12") << 0 << 9 << 10; |
|
1468 |
} |
|
1469 |
||
1470 |
void tst_QAbstractItemModel::testMoveToUncle() |
|
1471 |
{ |
|
1472 |
// Need to have some extra rows available. |
|
1473 |
ModelInsertCommand *insertCommand = new ModelInsertCommand(m_model, this); |
|
1474 |
insertCommand->setAncestorRowNumbers(QList<int>() << 9); |
|
1475 |
insertCommand->setNumCols(4); |
|
1476 |
insertCommand->setStartRow(0); |
|
1477 |
insertCommand->setEndRow(9); |
|
1478 |
insertCommand->doCommand(); |
|
1479 |
||
1480 |
QFETCH( int, startRow); |
|
1481 |
QFETCH( int, endRow); |
|
1482 |
QFETCH( int, destRow); |
|
1483 |
||
1484 |
QList<QPersistentModelIndex> persistentList; |
|
1485 |
QModelIndexList indexList; |
|
1486 |
QModelIndexList parentsList; |
|
1487 |
||
1488 |
const int column = 0; |
|
1489 |
||
1490 |
QModelIndex sourceIndex = m_model->index(9, 0); |
|
1491 |
for (int i= 0; i < m_model->rowCount(sourceIndex); ++i) |
|
1492 |
{ |
|
1493 |
QModelIndex idx = m_model->index(i, column, sourceIndex); |
|
1494 |
QVERIFY(idx.isValid()); |
|
1495 |
indexList << idx; |
|
1496 |
parentsList << idx.parent(); |
|
1497 |
persistentList << QPersistentModelIndex(idx); |
|
1498 |
} |
|
1499 |
||
1500 |
QModelIndex destIndex = m_model->index(5, 0); |
|
1501 |
for (int i= 0; i < m_model->rowCount(destIndex); ++i) |
|
1502 |
{ |
|
1503 |
QModelIndex idx = m_model->index(i, column, destIndex); |
|
1504 |
QVERIFY(idx.isValid()); |
|
1505 |
indexList << idx; |
|
1506 |
parentsList << idx.parent(); |
|
1507 |
persistentList << QPersistentModelIndex(idx); |
|
1508 |
} |
|
1509 |
||
1510 |
QSignalSpy beforeSpy(m_model, SIGNAL(rowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
1511 |
QSignalSpy afterSpy(m_model, SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
1512 |
||
1513 |
ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this); |
|
1514 |
moveCommand->setAncestorRowNumbers(QList<int>() << 9); |
|
1515 |
moveCommand->setNumCols(4); |
|
1516 |
moveCommand->setStartRow(startRow); |
|
1517 |
moveCommand->setEndRow(endRow); |
|
1518 |
moveCommand->setDestAncestors(QList<int>() << 5); |
|
1519 |
moveCommand->setDestRow(destRow); |
|
1520 |
moveCommand->doCommand(); |
|
1521 |
||
1522 |
QVariantList beforeSignal = beforeSpy.takeAt(0); |
|
1523 |
QVariantList afterSignal = afterSpy.takeAt(0); |
|
1524 |
||
1525 |
QCOMPARE(beforeSignal.size(), 5); |
|
1526 |
QCOMPARE(beforeSignal.at(0).value<QModelIndex>(), sourceIndex); |
|
1527 |
QCOMPARE(beforeSignal.at(1).toInt(), startRow); |
|
1528 |
QCOMPARE(beforeSignal.at(2).toInt(), endRow); |
|
1529 |
QCOMPARE(beforeSignal.at(3).value<QModelIndex>(), destIndex); |
|
1530 |
QCOMPARE(beforeSignal.at(4).toInt(), destRow); |
|
1531 |
||
1532 |
QCOMPARE(afterSignal.size(), 5); |
|
1533 |
QCOMPARE(afterSignal.at(0).value<QModelIndex>(), sourceIndex); |
|
1534 |
QCOMPARE(afterSignal.at(1).toInt(), startRow); |
|
1535 |
QCOMPARE(afterSignal.at(2).toInt(), endRow); |
|
1536 |
QCOMPARE(afterSignal.at(3).value<QModelIndex>(), destIndex); |
|
1537 |
QCOMPARE(afterSignal.at(4).toInt(), destRow); |
|
1538 |
||
1539 |
for (int i = 0; i < indexList.size(); i++) |
|
1540 |
{ |
|
1541 |
QModelIndex idx = indexList.at(i); |
|
1542 |
QModelIndex idxParent = parentsList.at(i); |
|
1543 |
QModelIndex persistentIndex = persistentList.at(i); |
|
1544 |
||
1545 |
int row = idx.row(); |
|
1546 |
if (idxParent == destIndex) |
|
1547 |
{ |
|
1548 |
if ( row >= destRow) |
|
1549 |
{ |
|
1550 |
QCOMPARE(row + endRow - startRow + 1, persistentIndex.row() ); |
|
1551 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
1552 |
QCOMPARE(destIndex, persistentIndex.parent()); |
|
1553 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
1554 |
} else |
|
1555 |
{ |
|
1556 |
QCOMPARE(idx, persistentIndex); |
|
1557 |
} |
|
1558 |
} else |
|
1559 |
{ |
|
1560 |
if (row < startRow) |
|
1561 |
{ |
|
1562 |
QCOMPARE(idx, persistentIndex); |
|
1563 |
} else if (row <= endRow) |
|
1564 |
{ |
|
1565 |
QCOMPARE(row + destRow - startRow, persistentIndex.row() ); |
|
1566 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
1567 |
QCOMPARE(destIndex, persistentIndex.parent()); |
|
1568 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
1569 |
||
1570 |
} else { |
|
1571 |
QCOMPARE(row - (endRow - startRow + 1), persistentIndex.row() ); |
|
1572 |
QCOMPARE(idx.column(), persistentIndex.column()); |
|
1573 |
QCOMPARE(idxParent, persistentIndex.parent()); |
|
1574 |
QCOMPARE(idx.model(), persistentIndex.model()); |
|
1575 |
} |
|
1576 |
} |
|
1577 |
} |
|
1578 |
} |
|
1579 |
||
1580 |
void tst_QAbstractItemModel::testMoveToDescendants() |
|
1581 |
{ |
|
1582 |
// Attempt to move a row to its ancestors depth rows deep. |
|
1583 |
const int depth = 6; |
|
1584 |
||
1585 |
// Need to have some extra rows available in a tree. |
|
1586 |
QList<int> rows; |
|
1587 |
ModelInsertCommand *insertCommand; |
|
1588 |
for (int i = 0; i < depth; i++) |
|
1589 |
{ |
|
1590 |
insertCommand = new ModelInsertCommand(m_model, this); |
|
1591 |
insertCommand->setAncestorRowNumbers(rows); |
|
1592 |
insertCommand->setNumCols(4); |
|
1593 |
insertCommand->setStartRow(0); |
|
1594 |
insertCommand->setEndRow(9); |
|
1595 |
insertCommand->doCommand(); |
|
1596 |
rows << 9; |
|
1597 |
} |
|
1598 |
||
1599 |
QList<QPersistentModelIndex> persistentList; |
|
1600 |
QModelIndexList indexList; |
|
1601 |
QModelIndexList parentsList; |
|
1602 |
||
1603 |
const int column = 0; |
|
1604 |
||
1605 |
QModelIndex sourceIndex = m_model->index(9, 0); |
|
1606 |
for (int i= 0; i < m_model->rowCount(sourceIndex); ++i) |
|
1607 |
{ |
|
1608 |
QModelIndex idx = m_model->index(i, column, sourceIndex); |
|
1609 |
QVERIFY(idx.isValid()); |
|
1610 |
indexList << idx; |
|
1611 |
parentsList << idx.parent(); |
|
1612 |
persistentList << QPersistentModelIndex(idx); |
|
1613 |
} |
|
1614 |
||
1615 |
QModelIndex destIndex = m_model->index(5, 0); |
|
1616 |
for (int i= 0; i < m_model->rowCount(destIndex); ++i) |
|
1617 |
{ |
|
1618 |
QModelIndex idx = m_model->index(i, column, destIndex); |
|
1619 |
QVERIFY(idx.isValid()); |
|
1620 |
indexList << idx; |
|
1621 |
parentsList << idx.parent(); |
|
1622 |
persistentList << QPersistentModelIndex(idx); |
|
1623 |
} |
|
1624 |
||
1625 |
QSignalSpy beforeSpy(m_model, SIGNAL(rowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
1626 |
QSignalSpy afterSpy(m_model, SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
1627 |
||
1628 |
ModelMoveCommand *moveCommand; |
|
1629 |
QList<int> ancestors; |
|
1630 |
while (ancestors.size() < depth) |
|
1631 |
{ |
|
1632 |
ancestors << 9; |
|
1633 |
for (int row = 0; row <= 9; row++) |
|
1634 |
{ |
|
1635 |
moveCommand = new ModelMoveCommand(m_model, this); |
|
1636 |
moveCommand->setNumCols(4); |
|
1637 |
moveCommand->setStartRow(9); |
|
1638 |
moveCommand->setEndRow(9); |
|
1639 |
moveCommand->setDestAncestors(ancestors); |
|
1640 |
moveCommand->setDestRow(row); |
|
1641 |
moveCommand->doCommand(); |
|
1642 |
||
1643 |
QVERIFY(beforeSpy.size() == 0); |
|
1644 |
QVERIFY(afterSpy.size() == 0); |
|
1645 |
} |
|
1646 |
} |
|
1647 |
} |
|
1648 |
||
1649 |
void tst_QAbstractItemModel::testMoveWithinOwnRange_data() |
|
1650 |
{ |
|
1651 |
QTest::addColumn<int>("startRow"); |
|
1652 |
QTest::addColumn<int>("endRow"); |
|
1653 |
QTest::addColumn<int>("destRow"); |
|
1654 |
||
1655 |
QTest::newRow("move01") << 0 << 0 << 0; |
|
1656 |
QTest::newRow("move02") << 0 << 0 << 1; |
|
1657 |
QTest::newRow("move03") << 0 << 5 << 0; |
|
1658 |
QTest::newRow("move04") << 0 << 5 << 1; |
|
1659 |
QTest::newRow("move05") << 0 << 5 << 2; |
|
1660 |
QTest::newRow("move06") << 0 << 5 << 3; |
|
1661 |
QTest::newRow("move07") << 0 << 5 << 4; |
|
1662 |
QTest::newRow("move08") << 0 << 5 << 5; |
|
1663 |
QTest::newRow("move09") << 0 << 5 << 6; |
|
1664 |
QTest::newRow("move08") << 3 << 5 << 5; |
|
1665 |
QTest::newRow("move08") << 3 << 5 << 6; |
|
1666 |
QTest::newRow("move09") << 4 << 5 << 5; |
|
1667 |
QTest::newRow("move10") << 4 << 5 << 6; |
|
1668 |
QTest::newRow("move11") << 5 << 5 << 5; |
|
1669 |
QTest::newRow("move12") << 5 << 5 << 6; |
|
1670 |
QTest::newRow("move13") << 5 << 9 << 9; |
|
1671 |
QTest::newRow("move14") << 5 << 9 << 10; |
|
1672 |
QTest::newRow("move15") << 6 << 9 << 9; |
|
1673 |
QTest::newRow("move16") << 6 << 9 << 10; |
|
1674 |
QTest::newRow("move17") << 7 << 9 << 9; |
|
1675 |
QTest::newRow("move18") << 7 << 9 << 10; |
|
1676 |
QTest::newRow("move19") << 8 << 9 << 9; |
|
1677 |
QTest::newRow("move20") << 8 << 9 << 10; |
|
1678 |
QTest::newRow("move21") << 9 << 9 << 9; |
|
1679 |
QTest::newRow("move22") << 0 << 9 << 10; |
|
1680 |
||
1681 |
} |
|
1682 |
||
1683 |
void tst_QAbstractItemModel::testMoveWithinOwnRange() |
|
1684 |
{ |
|
1685 |
||
1686 |
QFETCH( int, startRow); |
|
1687 |
QFETCH( int, endRow); |
|
1688 |
QFETCH( int, destRow); |
|
1689 |
||
1690 |
||
1691 |
QSignalSpy beforeSpy(m_model, SIGNAL(rowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
1692 |
QSignalSpy afterSpy(m_model, SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int))); |
|
1693 |
||
1694 |
ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this); |
|
1695 |
moveCommand->setNumCols(4); |
|
1696 |
moveCommand->setStartRow(startRow); |
|
1697 |
moveCommand->setEndRow(endRow); |
|
1698 |
moveCommand->setDestRow(destRow); |
|
1699 |
moveCommand->doCommand(); |
|
1700 |
||
1701 |
QVERIFY(beforeSpy.size() == 0); |
|
1702 |
QVERIFY(afterSpy.size() == 0); |
|
1703 |
||
1704 |
||
1705 |
} |
|
1706 |
||
1707 |
class ListenerObject : public QObject |
|
1708 |
{ |
|
1709 |
Q_OBJECT |
|
1710 |
public: |
|
1711 |
ListenerObject(QAbstractProxyModel *parent); |
|
1712 |
||
1713 |
protected: |
|
1714 |
void fillIndexStores(const QModelIndex &parent); |
|
1715 |
||
1716 |
public slots: |
|
1717 |
void slotAboutToBeReset(); |
|
1718 |
void slotReset(); |
|
1719 |
||
1720 |
private: |
|
1721 |
QAbstractProxyModel *m_model; |
|
1722 |
QList<QPersistentModelIndex> m_persistentIndexes; |
|
1723 |
QModelIndexList m_nonPersistentIndexes; |
|
1724 |
}; |
|
1725 |
||
1726 |
||
1727 |
ListenerObject::ListenerObject(QAbstractProxyModel *parent) |
|
1728 |
: QObject(parent), m_model(parent) |
|
1729 |
{ |
|
1730 |
connect(m_model, SIGNAL(modelAboutToBeReset()), SLOT(slotAboutToBeReset())); |
|
1731 |
connect(m_model, SIGNAL(modelReset()), SLOT(slotReset())); |
|
1732 |
||
1733 |
fillIndexStores(QModelIndex()); |
|
1734 |
} |
|
1735 |
||
1736 |
void ListenerObject::fillIndexStores(const QModelIndex &parent) |
|
1737 |
{ |
|
1738 |
const int column = 0; |
|
1739 |
int row = 0; |
|
1740 |
QModelIndex idx = m_model->index(row, column, parent); |
|
1741 |
while (idx.isValid()) |
|
1742 |
{ |
|
1743 |
m_persistentIndexes << QPersistentModelIndex(idx); |
|
1744 |
m_nonPersistentIndexes << idx; |
|
1745 |
if (m_model->hasChildren(idx)) |
|
1746 |
{ |
|
1747 |
fillIndexStores(idx); |
|
1748 |
} |
|
1749 |
++row; |
|
1750 |
idx = m_model->index(row, column, parent); |
|
1751 |
} |
|
1752 |
} |
|
1753 |
||
1754 |
void ListenerObject::slotAboutToBeReset() |
|
1755 |
{ |
|
1756 |
// Nothing has been changed yet. All indexes should be the same. |
|
1757 |
for (int i = 0; i < m_persistentIndexes.size(); ++i) |
|
1758 |
{ |
|
1759 |
QModelIndex idx = m_persistentIndexes.at(i); |
|
1760 |
QVERIFY(idx == m_nonPersistentIndexes.at(i)); |
|
1761 |
QVERIFY(m_model->mapToSource(idx).isValid()); |
|
1762 |
} |
|
1763 |
} |
|
1764 |
||
1765 |
void ListenerObject::slotReset() |
|
1766 |
{ |
|
1767 |
foreach(const QModelIndex &idx, m_persistentIndexes) |
|
1768 |
{ |
|
1769 |
QVERIFY(!idx.isValid()); |
|
1770 |
} |
|
1771 |
} |
|
1772 |
||
1773 |
||
1774 |
void tst_QAbstractItemModel::testReset() |
|
1775 |
{ |
|
1776 |
QSignalSpy beforeResetSpy(m_model, SIGNAL(modelAboutToBeReset())); |
|
1777 |
QSignalSpy afterResetSpy(m_model, SIGNAL(modelReset())); |
|
1778 |
||
1779 |
||
1780 |
QSortFilterProxyModel *nullProxy = new QSortFilterProxyModel(this); |
|
1781 |
nullProxy->setSourceModel(m_model); |
|
1782 |
||
1783 |
// Makes sure the model and proxy are in a consistent state. before and after reset. |
|
1784 |
new ListenerObject(nullProxy); |
|
1785 |
||
1786 |
ModelResetCommandFixed *resetCommand = new ModelResetCommandFixed(m_model, this); |
|
1787 |
||
1788 |
resetCommand->setNumCols(4); |
|
1789 |
resetCommand->setStartRow(0); |
|
1790 |
resetCommand->setEndRow(0); |
|
1791 |
resetCommand->setDestRow(0); |
|
1792 |
resetCommand->setDestAncestors(QList<int>() << 5); |
|
1793 |
resetCommand->doCommand(); |
|
1794 |
||
1795 |
// Verify that the correct signals were emitted |
|
1796 |
QVERIFY(beforeResetSpy.size() == 1); |
|
1797 |
QVERIFY(afterResetSpy.size() == 1); |
|
1798 |
||
1799 |
// Verify that the move actually happened. |
|
1800 |
QVERIFY(m_model->rowCount() == 9); |
|
1801 |
QModelIndex destIndex = m_model->index(4, 0); |
|
1802 |
QVERIFY(m_model->rowCount(destIndex) == 11); |
|
1803 |
||
1804 |
} |
|
1805 |
||
1806 |
||
1807 |
QTEST_MAIN(tst_QAbstractItemModel) |
|
1808 |
#include "tst_qabstractitemmodel.moc" |