1/* This file is part of the KDE project
2 Copyright (C) 2010 Dag Andersen <danders@get2net.dk>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19#include "FlatProxyModelTester.h"
20
21
22#include <QModelIndex>
23#include <QStandardItemModel>
24#include <QStandardItem>
25#include <QSortFilterProxyModel>
26
27#include <qtest_kde.h>
28#include <kdebug.h>
29
30namespace KPlato
31{
32
33void FlatProxyModelTester::emptyModel()
34{
35 QCOMPARE( m_flatmodel.columnCount(), 0 );
36 QCOMPARE( m_flatmodel.rowCount(), 0 );
37}
38
39void FlatProxyModelTester::test()
40{
41 qDebug()<<"set 1 row, 1 column";
42 m_standardmodel.setColumnCount( 1 );
43 m_standardmodel.setRowCount( 1 );
44
45 m_standardmodel.setHorizontalHeaderLabels( QStringList() << "Column 1" );
46 m_flatmodel.setSourceModel( &m_standardmodel );
47
48 QCOMPARE( m_flatmodel.columnCount(), 2 ); // it adds an extra column
49 QCOMPARE( m_flatmodel.rowCount(), 1 );
50 QCOMPARE( m_flatmodel.headerData( 0, Qt::Horizontal ), QVariant( "Column 1" ) );
51
52 m_standardmodel.setData( m_standardmodel.index( 0, 0 ), "Index 0,0" );
53
54 QModelIndex idx = m_flatmodel.index( 0, 0 );
55 QVERIFY( idx.isValid() );
56 qDebug()<<"Index 0,0:"<<idx.data();
57
58 QCOMPARE( idx.data(), QVariant( "Index 0,0" ) );
59
60 qDebug()<<"1 row, set 2 columns";
61 m_standardmodel.setColumnCount( 2 );
62 QCOMPARE( m_flatmodel.columnCount(), 3 ); // it adds an extra column
63 m_flatmodel.setHeaderData( 1, Qt::Horizontal, "Column 2" );
64 QCOMPARE( m_flatmodel.headerData( 1, Qt::Horizontal ), QVariant( "Column 2" ) );
65 m_standardmodel.setData( m_standardmodel.index( 0, 1 ), "Index 0,1" );
66 idx = m_flatmodel.index( 0, 1 );
67 QVERIFY( idx.isValid() );
68 qDebug()<<"Index 0,1:"<<idx.data();
69 QCOMPARE( idx.data(), QVariant( "Index 0,1" ) );
70
71 qDebug()<<"Set 2 rows, 2 columns";
72 m_standardmodel.setRowCount( 2 );
73 QCOMPARE( m_flatmodel.rowCount(), 2 );
74 m_standardmodel.setData( m_standardmodel.index( 1, 0 ), "Index 1,0" );
75 idx = m_flatmodel.index( 1, 0 );
76 QVERIFY( idx.isValid() );
77 qDebug()<<"Index 1,0:"<<idx.data();
78 QCOMPARE( idx.data(), QVariant( "Index 1,0" ) );
79
80 m_standardmodel.setData( m_standardmodel.index( 1, 1 ), "Index 1,1" );
81 idx = m_flatmodel.index( 1, 1 );
82 QVERIFY( idx.isValid() );
83 qDebug()<<"Index 1,1:"<<idx.data();
84 QCOMPARE( idx.data(), QVariant( "Index 1,1" ) );
85
86 qDebug()<<"Add child on last index, adds a new row 1 in the flat model";
87
88 // NOTE: m_standardmodel.insertRow( 0, m_standardmodel.index( 1, 0 ) );
89 // does not work as there will be no columns and thus no valid indexes for this row
90 QStandardItem *item = m_standardmodel.itemFromIndex( m_standardmodel.index( 1, 0 ) );
91 QList<QStandardItem*> items;
92 items << new QStandardItem( "Child last column 1" ) << new QStandardItem( "Child last column 2" );
93 item->appendRow( items );
94 QCOMPARE( m_flatmodel.rowCount(), 3 );
95 idx = m_flatmodel.index( 2, 0 );
96 QCOMPARE( idx.data(), QVariant( "Child last column 1" ) );
97 idx = m_flatmodel.index( 2, 1 );
98 QCOMPARE( idx.data(), QVariant( "Child last column 2" ) );
99
100 qDebug()<<"add child on first index";
101
102 item = m_standardmodel.itemFromIndex( m_standardmodel.index( 0, 0 ) );
103 items.clear();
104 items << new QStandardItem( "Child first column 1" ) << new QStandardItem( "Child first column 2" );
105 item->appendRow( items );
106 QCOMPARE( m_flatmodel.rowCount(), 4 );
107 idx = m_flatmodel.index( 1, 0 );
108 QCOMPARE( idx.data(), QVariant( "Child first column 1" ) );
109 idx = m_flatmodel.index( 1, 1 );
110 QCOMPARE( idx.data(), QVariant( "Child first column 2" ) );
111
112 qDebug()<<"add row (2) on top level between first and last";
113
114 item = m_standardmodel.invisibleRootItem();
115 items.clear();
116 items << new QStandardItem( "New index 1,0" ) << new QStandardItem( "New index 1,1" );
117 item->insertRow( 1, items );
118 QCOMPARE( m_flatmodel.rowCount(), 5 );
119 idx = m_flatmodel.index( 2, 0 );
120 QCOMPARE( idx.data(), QVariant( "New index 1,0" ) );
121 idx = m_flatmodel.index( 2, 1 );
122 QCOMPARE( idx.data(), QVariant( "New index 1,1" ) );
123
124 qDebug()<<"Add child on middle index, adds row 3 to flat model";
125
126 item = m_standardmodel.itemFromIndex( m_standardmodel.index( 1, 0 ) );
127 items.clear();
128 items << new QStandardItem( "Child middle column 1" ) << new QStandardItem( "Child middle column 2" );
129 item->appendRow( items );
130 QCOMPARE( m_flatmodel.rowCount(), 6 );
131 idx = m_flatmodel.index( 3, 0 );
132 QCOMPARE( idx.data().toString(), QVariant( "Child middle column 1" ).toString() );
133 idx = m_flatmodel.index( 3, 1 );
134 QCOMPARE( idx.data(), QVariant( "Child middle column 2" ) );
135
136 qDebug()<<"Add child on middle index's child, adds row 4 to flat model";
137
138 QModelIndex parent = m_standardmodel.index( 1, 0 );
139 QCOMPARE( parent.data().toString(), QString( "New index 1,0" ) );
140 idx = m_standardmodel.index( 0, 0, parent );
141 QCOMPARE( idx.data().toString(), QString( "Child middle column 1" ) );
142 item = m_standardmodel.itemFromIndex( idx );
143 items.clear();
144 items << new QStandardItem( "Child of middle child column 1" ) << new QStandardItem( "Child of middle child column 2" );
145 item->appendRow( items );
146 QCOMPARE( m_flatmodel.rowCount(), 7 );
147 idx = m_flatmodel.index( 4, 0 );
148 QCOMPARE( idx.data().toString(), QVariant( "Child of middle child column 1" ).toString() );
149 idx = m_flatmodel.index( 4, 1 );
150 QCOMPARE( idx.data(), QVariant( "Child of middle child column 2" ) );
151
152 qDebug()<<"Add child on middle index at row 0, adds row 3 to flat model";
153
154 idx = m_standardmodel.index( 1, 0 );
155 QCOMPARE( idx.data().toString(), QString( "New index 1,0" ) );
156 item = m_standardmodel.itemFromIndex( idx );
157 items.clear();
158 items << new QStandardItem( "Index 1,0 -> Index 0,0" ) << new QStandardItem( "Index 1,0 -> Index 0,1" );
159 item->insertRow( 0, items );
160 QCOMPARE( m_flatmodel.rowCount(), 8 );
161 idx = m_flatmodel.index( 3, 0 );
162 QCOMPARE( idx.data().toString(), QVariant( "Index 1,0 -> Index 0,0" ).toString() );
163 idx = m_flatmodel.index( 3, 1 );
164 QCOMPARE( idx.data(), QVariant( "Index 1,0 -> Index 0,1" ) );
165
166 qDebug()<<"Add child on middle index at row 1, adds row 4 to flat model";
167
168 idx = m_standardmodel.index( 1, 0 );
169 QCOMPARE( idx.data().toString(), QString( "New index 1,0" ) );
170 item = m_standardmodel.itemFromIndex( idx );
171 items.clear();
172 items << new QStandardItem( "Index 1,0 -> Index 1,0" ) << new QStandardItem( "Index 1,0 -> Index 1,1" );
173 item->insertRow( 1, items );
174 QCOMPARE( m_flatmodel.rowCount(), 9 );
175 idx = m_flatmodel.index( 4, 0 );
176 QCOMPARE( idx.data().toString(), QVariant( "Index 1,0 -> Index 1,0" ).toString() );
177 idx = m_flatmodel.index( 4, 1 );
178 QCOMPARE( idx.data(), QVariant( "Index 1,0 -> Index 1,1" ) );
179
180 qDebug()<<"Add child on middle index at last row (4), adds row 8 to flat model";
181
182 idx = m_standardmodel.index( 1, 0 );
183 QCOMPARE( idx.data().toString(), QString( "New index 1,0" ) );
184 item = m_standardmodel.itemFromIndex( idx );
185 items.clear();
186 items << new QStandardItem( "Index 1,0 -> Index 4,0" ) << new QStandardItem( "Index 1,0 -> Index 4,1" );
187 item->insertRow( 3, items );
188 QCOMPARE( m_standardmodel.rowCount( m_standardmodel.index( 1, 0 ) ), 4 );
189 QCOMPARE( m_flatmodel.rowCount(), 10 );
190 idx = m_flatmodel.index( 7, 0 );
191 QCOMPARE( idx.data().toString(), QVariant( "Index 1,0 -> Index 4,0" ).toString() );
192 idx = m_flatmodel.index( 7, 1 );
193 QCOMPARE( idx.data(), QVariant( "Index 1,0 -> Index 4,1" ) );
194}
195
196void FlatProxyModelTester::testInsertRemoveTop()
197{
198 QSortFilterProxyModel sf;
199 FlatProxyModel fm;
200 QStandardItemModel sm;
201
202 sf.setSourceModel( &fm );
203 fm.setSourceModel( &sm );
204
205 sm.setHeaderData( 0, Qt::Horizontal, "Column 0" );
206 QCOMPARE( sm.rowCount(), 0 );
207
208 // insert
209 sm.insertRow( 0, new QStandardItem( "First" ) );
210 QCOMPARE( sm.rowCount(), 1 );
211 QCOMPARE( fm.rowCount(), 1 );
212 QCOMPARE( sf.rowCount(), 1 );
213 QCOMPARE( sm.index( 0, 0 ).data(), sf.index( 0, 0 ).data() );
214 QCOMPARE( sf.index( 0, 0 ).data(), QVariant( "First" ) );
215
216 sm.insertRow( 1, new QStandardItem( "Second" ) );
217 QCOMPARE( sm.rowCount(), 2 );
218 QCOMPARE( fm.rowCount(), 2 );
219 QCOMPARE( sf.rowCount(), 2 );
220 QCOMPARE( sm.index( 0, 0 ).data(), sf.index( 0, 0 ).data() );
221 QCOMPARE( sm.index( 1, 0 ).data(), sf.index( 1, 0 ).data() );
222 QCOMPARE( sf.index( 0, 0 ).data(), QVariant( "First" ) );
223 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "Second" ) );
224
225 sm.insertRow( 1, new QStandardItem( "In between" ) );
226 QCOMPARE( sm.rowCount(), 3 );
227 QCOMPARE( fm.rowCount(), 3 );
228 QCOMPARE( sf.rowCount(), 3 );
229 QCOMPARE( sm.index( 0, 0 ).data(), sf.index( 0, 0 ).data() );
230 QCOMPARE( sm.index( 1, 0 ).data(), sf.index( 1, 0 ).data() );
231 QCOMPARE( sm.index( 2, 0 ).data(), sf.index( 2, 0 ).data() );
232 QCOMPARE( sf.index( 0, 0 ).data(), QVariant( "First" ) );
233 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "In between" ) );
234 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "Second" ) );
235
236 sm.insertRow( 0, new QStandardItem( "Before first" ) );
237 QCOMPARE( sm.rowCount(), 4 );
238 QCOMPARE( fm.rowCount(), 4 );
239 QCOMPARE( sf.rowCount(), 4 );
240 QCOMPARE( sm.index( 0, 0 ).data(), sf.index( 0, 0 ).data() );
241 QCOMPARE( sm.index( 1, 0 ).data(), sf.index( 1, 0 ).data() );
242 QCOMPARE( sm.index( 2, 0 ).data(), sf.index( 2, 0 ).data() );
243 QCOMPARE( sm.index( 3, 0 ).data(), sf.index( 3, 0 ).data() );
244 QCOMPARE( sf.index( 0, 0 ).data(), QVariant( "Before first" ) );
245 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "First" ) );
246 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "In between" ) );
247 QCOMPARE( sf.index( 3, 0 ).data(), QVariant( "Second" ) );
248
249 // remove
250 sm.removeRow( 0 );
251 QCOMPARE( sm.rowCount(), 3 );
252 QCOMPARE( fm.rowCount(), 3 );
253 QCOMPARE( sf.rowCount(), 3 );
254 QCOMPARE( sm.index( 0, 0 ).data(), sf.index( 0, 0 ).data() );
255 QCOMPARE( sm.index( 1, 0 ).data(), sf.index( 1, 0 ).data() );
256 QCOMPARE( sm.index( 2, 0 ).data(), sf.index( 2, 0 ).data() );
257 QCOMPARE( sf.index( 0, 0 ).data(), QVariant( "First" ) );
258 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "In between" ) );
259 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "Second" ) );
260
261 sm.removeRow( 1 );
262 QCOMPARE( sm.rowCount(), 2 );
263 QCOMPARE( fm.rowCount(), 2 );
264 QCOMPARE( sf.rowCount(), 2 );
265 QCOMPARE( sm.index( 0, 0 ).data(), sf.index( 0, 0 ).data() );
266 QCOMPARE( sm.index( 1, 0 ).data(), sf.index( 1, 0 ).data() );
267 QCOMPARE( sf.index( 0, 0 ).data(), QVariant( "First" ) );
268 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "Second" ) );
269
270 sm.removeRow( 1 );
271 QCOMPARE( sm.rowCount(), 1 );
272 QCOMPARE( fm.rowCount(), 1 );
273 QCOMPARE( sf.rowCount(), 1 );
274 QCOMPARE( sm.index( 0, 0 ).data(), sf.index( 0, 0 ).data() );
275 QCOMPARE( sf.index( 0, 0 ).data(), QVariant( "First" ) );
276
277 sm.removeRow( 0 );
278 QCOMPARE( sm.rowCount(), 0 );
279 QCOMPARE( fm.rowCount(), 0 );
280 QCOMPARE( sf.rowCount(), 0 );
281}
282
283void FlatProxyModelTester::testInsertRemoveChildren()
284{
285 QSortFilterProxyModel sf;
286 FlatProxyModel fm;
287 QStandardItemModel sm;
288
289 sf.setSourceModel( &fm );
290 fm.setSourceModel( &sm );
291
292 sm.setHeaderData( 0, Qt::Horizontal, "Column 0" );
293 QCOMPARE( sm.rowCount(), 0 );
294
295 QStandardItem *pitem = new QStandardItem( "Parent" );
296 sm.insertRow( 0, pitem );
297 QCOMPARE( sm.rowCount(), 1 );
298 QCOMPARE( fm.rowCount(), 1 );
299 QCOMPARE( sf.rowCount(), 1 );
300 QCOMPARE( sm.index( 0, 0 ).data(), sf.index( 0, 0 ).data() );
301 QCOMPARE( sf.index( 0, 0 ).data(), QVariant( "Parent" ) );
302
303 QModelIndex parent = sm.index( 0, 0 );
304 QVERIFY( parent.isValid() );
305
306 QStandardItem *item = new QStandardItem( "First child" );
307 pitem->insertRow( 0, item );
308 QCOMPARE( sm.rowCount( parent ), 1 );
309 QCOMPARE( fm.rowCount(), 2 );
310 QCOMPARE( sf.rowCount(), 2 );
311 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 1, 0 ).data() );
312 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "First child" ) );
313
314 item = new QStandardItem( "Second child" );
315 pitem->insertRow( 1, item );
316 QCOMPARE( sm.rowCount( parent ), 2 );
317 QCOMPARE( fm.rowCount(), 3 );
318 QCOMPARE( sf.rowCount(), 3 );
319 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 1, 0 ).data() );
320 QCOMPARE( sm.index( 1, 0, parent ).data(), sf.index( 2, 0 ).data() );
321 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "First child" ) );
322 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "Second child" ) );
323
324 item = new QStandardItem( "In between" );
325 pitem->insertRow( 1, item );
326 QCOMPARE( sm.rowCount( parent ), 3 );
327 QCOMPARE( fm.rowCount(), 4 );
328 QCOMPARE( sf.rowCount(), 4 );
329 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 1, 0 ).data() );
330 QCOMPARE( sm.index( 1, 0, parent ).data(), sf.index( 2, 0 ).data() );
331 QCOMPARE( sm.index( 2, 0, parent ).data(), sf.index( 3, 0 ).data() );
332 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "First child" ) );
333 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "In between" ) );
334 QCOMPARE( sf.index( 3, 0 ).data(), QVariant( "Second child" ) );
335
336 item = new QStandardItem( "Before first" );
337 pitem->insertRow( 0, item );
338 QCOMPARE( sm.rowCount( parent ), 4 );
339 QCOMPARE( fm.rowCount(), 5 );
340 QCOMPARE( sf.rowCount(), 5 );
341 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 1, 0 ).data() );
342 QCOMPARE( sm.index( 1, 0, parent ).data(), sf.index( 2, 0 ).data() );
343 QCOMPARE( sm.index( 2, 0, parent ).data(), sf.index( 3, 0 ).data() );
344 QCOMPARE( sm.index( 3, 0, parent ).data(), sf.index( 4, 0 ).data() );
345 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "Before first" ) );
346 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "First child" ) );
347 QCOMPARE( sf.index( 3, 0 ).data(), QVariant( "In between" ) );
348 QCOMPARE( sf.index( 4, 0 ).data(), QVariant( "Second child" ) );
349
350 sm.removeRow( 0, parent );
351 QCOMPARE( sm.rowCount( parent ), 3 );
352 QCOMPARE( fm.rowCount(), 4 );
353 QCOMPARE( sf.rowCount(), 4 );
354 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 1, 0 ).data() );
355 QCOMPARE( sm.index( 1, 0, parent ).data(), sf.index( 2, 0 ).data() );
356 QCOMPARE( sm.index( 2, 0, parent ).data(), sf.index( 3, 0 ).data() );
357 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "First child" ) );
358 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "In between" ) );
359 QCOMPARE( sf.index( 3, 0 ).data(), QVariant( "Second child" ) );
360
361 sm.removeRow( 1, parent );
362 QCOMPARE( sm.rowCount( parent ), 2 );
363 QCOMPARE( fm.rowCount(), 3 );
364 QCOMPARE( sf.rowCount(), 3 );
365 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 1, 0 ).data() );
366 QCOMPARE( sm.index( 1, 0, parent ).data(), sf.index( 2, 0 ).data() );
367 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "First child" ) );
368 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "Second child" ) );
369
370 sm.removeRow( 1, parent );
371 QCOMPARE( sm.rowCount( parent ), 1 );
372 QCOMPARE( fm.rowCount(), 2 );
373 QCOMPARE( sf.rowCount(), 2 );
374 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 1, 0 ).data() );
375 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "First child" ) );
376
377 sm.removeRow( 0, parent );
378 QCOMPARE( sm.rowCount( parent ), 0 );
379 QCOMPARE( fm.rowCount(), 1 );
380 QCOMPARE( sf.rowCount(), 1 );
381 QCOMPARE( sm.index( 0, 0 ).data(), sf.index( 0, 0 ).data() );
382
383 sm.removeRow( 0 );
384 QCOMPARE( sm.rowCount(), 0 );
385 QCOMPARE( fm.rowCount(), 0 );
386 QCOMPARE( sf.rowCount(), 0 );
387}
388
389void FlatProxyModelTester::testInsertRemoveGrandChildren()
390{
391 QSortFilterProxyModel sf;
392 FlatProxyModel fm;
393 QStandardItemModel sm;
394
395 sf.setSourceModel( &fm );
396 fm.setSourceModel( &sm );
397
398 sm.setHeaderData( 0, Qt::Horizontal, "Column 0" );
399 QCOMPARE( sm.rowCount(), 0 );
400
401 QStandardItem *pitem = new QStandardItem( "Parent" );
402 sm.insertRow( 0, pitem );
403 QCOMPARE( sm.rowCount(), 1 );
404 QCOMPARE( fm.rowCount(), 1 );
405 QCOMPARE( sf.rowCount(), 1 );
406 QCOMPARE( sm.index( 0, 0 ).data(), sf.index( 0, 0 ).data() );
407 QCOMPARE( sf.index( 0, 0 ).data(), QVariant( "Parent" ) );
408
409 QModelIndex grandparent = sm.index( 0, 0 );
410 QVERIFY( grandparent.isValid() );
411
412 QStandardItem *gitem = new QStandardItem( "First child" );
413 pitem->insertRow( 0, gitem );
414 QCOMPARE( sm.rowCount( grandparent ), 1 );
415 QCOMPARE( fm.rowCount(), 2 );
416 QCOMPARE( sf.rowCount(), 2 );
417 QCOMPARE( sm.index( 0, 0, grandparent ).data(), sf.index( 1, 0 ).data() );
418 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "First child" ) );
419
420 QModelIndex parent = sm.index( 0, 0, grandparent );
421 QVERIFY( parent.isValid() );
422
423 QStandardItem *item = new QStandardItem( "First grandchild" );
424 gitem->insertRow( 0, item );
425 QCOMPARE( sm.rowCount( parent ), 1 );
426 QCOMPARE( fm.rowCount(), 3 );
427 QCOMPARE( sf.rowCount(), 3 );
428 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 2, 0 ).data() );
429 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "First grandchild" ) );
430
431 item = new QStandardItem( "Second grandchild" );
432 gitem->insertRow( 1, item );
433 QCOMPARE( sm.rowCount( parent ), 2 );
434 QCOMPARE( fm.rowCount(), 4 );
435 QCOMPARE( sf.rowCount(), 4 );
436 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 2, 0 ).data() );
437 QCOMPARE( sm.index( 1, 0, parent ).data(), sf.index( 3, 0 ).data() );
438 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "First grandchild" ) );
439 QCOMPARE( sf.index( 3, 0 ).data(), QVariant( "Second grandchild" ) );
440
441 item = new QStandardItem( "In between" );
442 gitem->insertRow( 1, item );
443 QCOMPARE( sm.rowCount( parent ), 3 );
444 QCOMPARE( fm.rowCount(), 5 );
445 QCOMPARE( sf.rowCount(), 5 );
446 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 2, 0 ).data() );
447 QCOMPARE( sm.index( 1, 0, parent ).data(), sf.index( 3, 0 ).data() );
448 QCOMPARE( sm.index( 2, 0, parent ).data(), sf.index( 4, 0 ).data() );
449 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "First grandchild" ) );
450 QCOMPARE( sf.index( 3, 0 ).data(), QVariant( "In between" ) );
451 QCOMPARE( sf.index( 4, 0 ).data(), QVariant( "Second grandchild" ) );
452
453 item = new QStandardItem( "Before first" );
454 gitem->insertRow( 0, item );
455 QCOMPARE( sm.rowCount( parent ), 4 );
456 QCOMPARE( fm.rowCount(), 6 );
457 QCOMPARE( sf.rowCount(), 6 );
458 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 2, 0 ).data() );
459 QCOMPARE( sm.index( 1, 0, parent ).data(), sf.index( 3, 0 ).data() );
460 QCOMPARE( sm.index( 2, 0, parent ).data(), sf.index( 4, 0 ).data() );
461 QCOMPARE( sm.index( 3, 0, parent ).data(), sf.index( 5, 0 ).data() );
462 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "Before first" ) );
463 QCOMPARE( sf.index( 3, 0 ).data(), QVariant( "First grandchild" ) );
464 QCOMPARE( sf.index( 4, 0 ).data(), QVariant( "In between" ) );
465 QCOMPARE( sf.index( 5, 0 ).data(), QVariant( "Second grandchild" ) );
466
467 sm.removeRow( 0, parent );
468 QCOMPARE( sm.rowCount( parent ), 3 );
469 QCOMPARE( fm.rowCount(), 5 );
470 QCOMPARE( sf.rowCount(), 5 );
471 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 2, 0 ).data() );
472 QCOMPARE( sm.index( 1, 0, parent ).data(), sf.index( 3, 0 ).data() );
473 QCOMPARE( sm.index( 2, 0, parent ).data(), sf.index( 4, 0 ).data() );
474 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "First grandchild" ) );
475 QCOMPARE( sf.index( 3, 0 ).data(), QVariant( "In between" ) );
476 QCOMPARE( sf.index( 4, 0 ).data(), QVariant( "Second grandchild" ) );
477
478 sm.removeRow( 1, parent );
479 QCOMPARE( sm.rowCount( parent ), 2 );
480 QCOMPARE( fm.rowCount(), 4 );
481 QCOMPARE( sf.rowCount(), 4 );
482 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 2, 0 ).data() );
483 QCOMPARE( sm.index( 1, 0, parent ).data(), sf.index( 3, 0 ).data() );
484 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "First grandchild" ) );
485 QCOMPARE( sf.index( 3, 0 ).data(), QVariant( "Second grandchild" ) );
486
487 sm.removeRow( 1, parent );
488 QCOMPARE( sm.rowCount( parent ), 1 );
489 QCOMPARE( fm.rowCount(), 3 );
490 QCOMPARE( sf.rowCount(), 3 );
491 QCOMPARE( sm.index( 0, 0, parent ).data(), sf.index( 2, 0 ).data() );
492 QCOMPARE( sf.index( 2, 0 ).data(), QVariant( "First grandchild" ) );
493
494 sm.removeRow( 0, parent );
495 QCOMPARE( sm.rowCount( parent ), 0 );
496 QCOMPARE( fm.rowCount(), 2 );
497 QCOMPARE( sf.rowCount(), 2 );
498 QCOMPARE( sm.index( 0, 0, grandparent ).data(), sf.index( 1, 0 ).data() );
499 QCOMPARE( sf.index( 1, 0 ).data(), QVariant( "First child" ) );
500
501 QCOMPARE( sm.rowCount( grandparent ), 1 );
502 sm.removeRow( 0, grandparent );
503 QCOMPARE( sm.rowCount( grandparent ), 0 );
504 QCOMPARE( fm.rowCount(), 1 );
505 QCOMPARE( sf.rowCount(), 1 );
506 QCOMPARE( sm.index( 0, 0 ).data(), sf.index( 0, 0 ).data() );
507 QCOMPARE( sf.index( 0, 0 ).data(), QVariant( "Parent" ) );
508
509 QCOMPARE( sm.rowCount(), 1 );
510 sm.removeRow( 0 );
511 QCOMPARE( sm.rowCount(), 0 );
512 QCOMPARE( fm.rowCount(), 0 );
513 QCOMPARE( sf.rowCount(), 0 );
514}
515
516} //namespace KPlato
517
518QTEST_KDEMAIN_CORE( KPlato::FlatProxyModelTester )
519
520#include "FlatProxyModelTester.moc"
521