1
/****************************************************************************
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and The Qt Company. For licensing terms
14
** and conditions see https://www.qt.io/terms-conditions. For further
15
** information use the contact form at https://www.qt.io/contact-us.
17
** GNU General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU
19
** General Public License version 3 as published by the Free Software
20
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21
** included in the packaging of this file. Please review the following
22
** information to ensure the GNU General Public License requirements will
23
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
27
****************************************************************************/
30
#include <QtTest/QtTest>
32
#include <qstandarditemmodel.h>
34
class tst_QStandardItem : public QObject
42
void rowsColumnsCtor();
45
void getSetRowAndColumnCount();
46
void getSetChild_data();
49
void insertColumn_data();
51
void insertColumns_data();
53
void insertRow_data();
55
void insertRows_data();
57
void appendColumn_data();
59
void appendRow_data();
62
void takeColumn_data();
74
void tst_QStandardItem::ctor()
77
QVERIFY(!item.hasChildren());
80
void tst_QStandardItem::textCtor()
82
QLatin1String text("text");
83
QStandardItem item(text);
84
QCOMPARE(item.text(), text);
85
QVERIFY(!item.hasChildren());
88
void tst_QStandardItem::iconTextCtor()
90
QPixmap pixmap(32, 32);
93
QLatin1String text("text");
94
QStandardItem item(icon, text);
95
QCOMPARE(item.icon(), icon);
96
QCOMPARE(item.text(), text);
97
QVERIFY(!item.hasChildren());
100
void tst_QStandardItem::rowsColumnsCtor()
103
const int columns = 12;
104
QStandardItem item(rows, columns);
105
QCOMPARE(item.rowCount(), rows);
106
QCOMPARE(item.columnCount(), columns);
109
void tst_QStandardItem::getSetData()
112
for (int x = 0; x < 2; ++x) {
113
for (int i = 1; i <= 2; ++i) {
114
const QString iS = QString::number(i);
115
QString text = QLatin1String("text ") + iS;
117
QCOMPARE(item.text(), text);
119
QPixmap pixmap(32, 32);
120
pixmap.fill((i == 1) ? Qt::red : Qt::green);
123
QCOMPARE(item.icon(), icon);
125
QString toolTip = QLatin1String("toolTip ") + iS;
126
item.setToolTip(toolTip);
127
QCOMPARE(item.toolTip(), toolTip);
129
QString statusTip = QLatin1String("statusTip ") + iS;
130
item.setStatusTip(statusTip);
131
QCOMPARE(item.statusTip(), statusTip);
133
QString whatsThis = QLatin1String("whatsThis ") + iS;
134
item.setWhatsThis(whatsThis);
135
QCOMPARE(item.whatsThis(), whatsThis);
137
QSize sizeHint(64*i, 48*i);
138
item.setSizeHint(sizeHint);
139
QCOMPARE(item.sizeHint(), sizeHint);
143
QCOMPARE(item.font(), font);
145
Qt::Alignment textAlignment((i == 1)
146
? Qt::AlignLeft|Qt::AlignVCenter
148
item.setTextAlignment(textAlignment);
149
QCOMPARE(item.textAlignment(), textAlignment);
151
QColor backgroundColor((i == 1) ? Qt::blue : Qt::yellow);
152
item.setBackground(backgroundColor);
153
QCOMPARE(item.background().color(), backgroundColor);
155
QColor textColor((i == 1) ? Qt::green : Qt::cyan);
156
item.setForeground(textColor);
157
QCOMPARE(item.foreground().color(), textColor);
159
Qt::CheckState checkState((i == 1) ? Qt::PartiallyChecked : Qt::Checked);
160
item.setCheckState(checkState);
161
QCOMPARE(item.checkState(), checkState);
163
QString accessibleText = QLatin1String("accessibleText ") + iS;
164
item.setAccessibleText(accessibleText);
165
QCOMPARE(item.accessibleText(), accessibleText);
167
QString accessibleDescription = QLatin1String("accessibleDescription ") + iS;
168
item.setAccessibleDescription(accessibleDescription);
169
QCOMPARE(item.accessibleDescription(), accessibleDescription);
171
QCOMPARE(item.text(), text);
172
QCOMPARE(item.icon(), icon);
173
QCOMPARE(item.toolTip(), toolTip);
174
QCOMPARE(item.statusTip(), statusTip);
175
QCOMPARE(item.whatsThis(), whatsThis);
176
QCOMPARE(item.sizeHint(), sizeHint);
177
QCOMPARE(item.font(), font);
178
QCOMPARE(item.textAlignment(), textAlignment);
179
QCOMPARE(item.background().color(), backgroundColor);
180
QCOMPARE(item.foreground().color(), textColor);
181
QCOMPARE(item.checkState(), checkState);
182
QCOMPARE(item.accessibleText(), accessibleText);
183
QCOMPARE(item.accessibleDescription(), accessibleDescription);
185
QCOMPARE(qvariant_cast<QString>(item.data(Qt::DisplayRole)), text);
186
QCOMPARE(qvariant_cast<QIcon>(item.data(Qt::DecorationRole)), icon);
187
QCOMPARE(qvariant_cast<QString>(item.data(Qt::ToolTipRole)), toolTip);
188
QCOMPARE(qvariant_cast<QString>(item.data(Qt::StatusTipRole)), statusTip);
189
QCOMPARE(qvariant_cast<QString>(item.data(Qt::WhatsThisRole)), whatsThis);
190
QCOMPARE(qvariant_cast<QSize>(item.data(Qt::SizeHintRole)), sizeHint);
191
QCOMPARE(qvariant_cast<QFont>(item.data(Qt::FontRole)), font);
192
QCOMPARE(qvariant_cast<int>(item.data(Qt::TextAlignmentRole)), int(textAlignment));
193
QCOMPARE(qvariant_cast<QBrush>(item.data(Qt::BackgroundColorRole)), QBrush(backgroundColor));
194
QCOMPARE(qvariant_cast<QBrush>(item.data(Qt::BackgroundRole)), QBrush(backgroundColor));
195
QCOMPARE(qvariant_cast<QBrush>(item.data(Qt::TextColorRole)), QBrush(textColor));
196
QCOMPARE(qvariant_cast<QBrush>(item.data(Qt::ForegroundRole)), QBrush(textColor));
197
QCOMPARE(qvariant_cast<int>(item.data(Qt::CheckStateRole)), int(checkState));
198
QCOMPARE(qvariant_cast<QString>(item.data(Qt::AccessibleTextRole)), accessibleText);
199
QCOMPARE(qvariant_cast<QString>(item.data(Qt::AccessibleDescriptionRole)), accessibleDescription);
201
item.setBackground(pixmap);
202
QCOMPARE(item.background().texture(), pixmap);
203
QCOMPARE(qvariant_cast<QBrush>(item.data(Qt::BackgroundRole)).texture(), pixmap);
205
item.setData(QVariant(), Qt::DisplayRole);
206
item.setData(QVariant(), Qt::DecorationRole);
207
item.setData(QVariant(), Qt::ToolTipRole);
208
item.setData(QVariant(), Qt::StatusTipRole);
209
item.setData(QVariant(), Qt::WhatsThisRole);
210
item.setData(QVariant(), Qt::SizeHintRole);
211
item.setData(QVariant(), Qt::FontRole);
212
item.setData(QVariant(), Qt::TextAlignmentRole);
213
item.setData(QVariant(), Qt::BackgroundRole);
214
item.setData(QVariant(), Qt::ForegroundRole);
215
item.setData(QVariant(), Qt::CheckStateRole);
216
item.setData(QVariant(), Qt::AccessibleTextRole);
217
item.setData(QVariant(), Qt::AccessibleDescriptionRole);
219
QCOMPARE(item.data(Qt::DisplayRole), QVariant());
220
QCOMPARE(item.data(Qt::DecorationRole), QVariant());
221
QCOMPARE(item.data(Qt::ToolTipRole), QVariant());
222
QCOMPARE(item.data(Qt::StatusTipRole), QVariant());
223
QCOMPARE(item.data(Qt::WhatsThisRole), QVariant());
224
QCOMPARE(item.data(Qt::SizeHintRole), QVariant());
225
QCOMPARE(item.data(Qt::FontRole), QVariant());
226
QCOMPARE(item.data(Qt::TextAlignmentRole), QVariant());
227
QCOMPARE(item.data(Qt::BackgroundColorRole), QVariant());
228
QCOMPARE(item.data(Qt::BackgroundRole), QVariant());
229
QCOMPARE(item.data(Qt::TextColorRole), QVariant());
230
QCOMPARE(item.data(Qt::ForegroundRole), QVariant());
231
QCOMPARE(item.data(Qt::CheckStateRole), QVariant());
232
QCOMPARE(item.data(Qt::AccessibleTextRole), QVariant());
233
QCOMPARE(item.data(Qt::AccessibleDescriptionRole), QVariant());
237
void tst_QStandardItem::getSetFlags()
240
item.setEnabled(true);
241
QVERIFY(item.isEnabled());
242
QVERIFY(item.flags() & Qt::ItemIsEnabled);
243
item.setEditable(true);
244
QVERIFY(item.isEditable());
245
QVERIFY(item.flags() & Qt::ItemIsEditable);
246
item.setSelectable(true);
247
QVERIFY(item.isSelectable());
248
QVERIFY(item.flags() & Qt::ItemIsSelectable);
249
item.setCheckable(true);
250
QVERIFY(item.isCheckable());
251
QCOMPARE(item.checkState(), Qt::Unchecked);
252
QVERIFY(item.flags() & Qt::ItemIsUserCheckable);
253
item.setUserTristate(true);
254
QVERIFY(item.isUserTristate());
255
QVERIFY(item.flags() & Qt::ItemIsUserTristate);
256
item.setAutoTristate(true);
257
QVERIFY(item.isAutoTristate());
258
QVERIFY(item.flags() & Qt::ItemIsAutoTristate);
259
#ifndef QT_NO_DRAGANDDROP
260
item.setDragEnabled(true);
261
QVERIFY(item.isDragEnabled());
262
QVERIFY(item.flags() & Qt::ItemIsDragEnabled);
263
item.setDropEnabled(true);
264
QVERIFY(item.isDropEnabled());
265
QVERIFY(item.flags() & Qt::ItemIsDropEnabled);
268
QVERIFY(item.isEnabled());
269
item.setEnabled(false);
270
QVERIFY(!item.isEnabled());
271
QVERIFY(!(item.flags() & Qt::ItemIsEnabled));
272
QVERIFY(item.isEditable());
273
item.setEditable(false);
274
QVERIFY(!item.isEditable());
275
QVERIFY(!(item.flags() & Qt::ItemIsEditable));
276
QVERIFY(item.isSelectable());
277
item.setSelectable(false);
278
QVERIFY(!item.isSelectable());
279
QVERIFY(!(item.flags() & Qt::ItemIsSelectable));
280
QVERIFY(item.isCheckable());
281
item.setCheckable(false);
282
QVERIFY(!item.isCheckable());
283
QVERIFY(!(item.flags() & Qt::ItemIsUserCheckable));
284
item.setUserTristate(false);
285
QVERIFY(!item.isUserTristate());
286
QVERIFY(!(item.flags() & Qt::ItemIsUserTristate));
287
item.setAutoTristate(false);
288
QVERIFY(!item.isAutoTristate());
289
QVERIFY(!(item.flags() & Qt::ItemIsAutoTristate));
290
#ifndef QT_NO_DRAGANDDROP
291
QVERIFY(item.isDragEnabled());
292
item.setDragEnabled(false);
293
QVERIFY(!item.isDragEnabled());
294
QVERIFY(!(item.flags() & Qt::ItemIsDragEnabled));
295
QVERIFY(item.isDropEnabled());
296
item.setDropEnabled(false);
297
QVERIFY(!item.isDropEnabled());
298
QVERIFY(!(item.flags() & Qt::ItemIsDropEnabled));
301
item.setCheckable(false);
302
item.setCheckState(Qt::Checked);
303
item.setCheckable(true);
304
QCOMPARE(item.checkState(), Qt::Checked);
307
item.setTristate(true);
308
QVERIFY(item.isTristate());
309
QVERIFY(item.flags() & Qt::ItemIsTristate);
310
item.setTristate(false);
311
QVERIFY(!(item.flags() & Qt::ItemIsTristate));
314
void tst_QStandardItem::getSetRowAndColumnCount()
318
item.setRowCount(-1);
319
QCOMPARE(item.rowCount(), 0);
321
item.setColumnCount(-1);
322
QCOMPARE(item.columnCount(), 0);
325
QCOMPARE(item.rowCount(), 1);
326
QCOMPARE(item.columnCount(), 0);
328
item.setColumnCount(1);
329
QCOMPARE(item.columnCount(), 1);
330
QCOMPARE(item.rowCount(), 1);
332
item.setColumnCount(10);
333
QCOMPARE(item.columnCount(), 10);
334
QCOMPARE(item.rowCount(), 1);
336
item.setRowCount(20);
337
QCOMPARE(item.rowCount(), 20);
338
QCOMPARE(item.columnCount(), 10);
340
item.setRowCount(-1);
341
QCOMPARE(item.rowCount(), 20);
343
item.setColumnCount(-1);
344
QCOMPARE(item.columnCount(), 10);
346
item.setColumnCount(0);
347
QCOMPARE(item.columnCount(), 0);
348
QCOMPARE(item.rowCount(), 20);
351
QCOMPARE(item.rowCount(), 0);
354
void tst_QStandardItem::getSetChild_data()
356
QTest::addColumn<int>("rows");
357
QTest::addColumn<int>("columns");
358
QTest::addColumn<int>("row");
359
QTest::addColumn<int>("column");
361
QTest::newRow("0x0 children, child at (-1,-1)") << 0 << 0 << -1 << -1;
362
QTest::newRow("0x0 children, child at (0,0)") << 0 << 0 << 0 << 0;
365
void tst_QStandardItem::getSetChild()
368
QFETCH(int, columns);
372
QStandardItem item(rows, columns);
373
bool shouldHaveChildren = (rows > 0) && (columns > 0);
374
QCOMPARE(item.hasChildren(), shouldHaveChildren);
375
QCOMPARE(item.child(row, column), static_cast<QStandardItem*>(0));
377
QStandardItem *child = new QStandardItem;
378
item.setChild(row, column, child);
379
if ((row >= 0) && (column >= 0)) {
380
QCOMPARE(item.rowCount(), qMax(rows, row + 1));
381
QCOMPARE(item.columnCount(), qMax(columns, column + 1));
383
QCOMPARE(item.child(row, column), child);
384
QCOMPARE(child->row(), row);
385
QCOMPARE(child->column(), column);
387
QStandardItem *anotherChild = new QStandardItem;
388
item.setChild(row, column, anotherChild);
389
QCOMPARE(item.child(row, column), anotherChild);
390
QCOMPARE(anotherChild->row(), row);
391
QCOMPARE(anotherChild->column(), column);
392
item.setChild(row, column, 0);
396
QCOMPARE(item.child(row, column), static_cast<QStandardItem*>(0));
399
void tst_QStandardItem::parent()
403
QStandardItem *child = new QStandardItem;
404
QCOMPARE(child->parent(), static_cast<QStandardItem*>(0));
405
item.setChild(0, 0, child);
406
QCOMPARE(child->parent(), &item);
408
QStandardItem *childOfChild = new QStandardItem;
409
child->setChild(0, 0, childOfChild);
410
QCOMPARE(childOfChild->parent(), child);
414
QStandardItemModel model;
415
QStandardItem *item = new QStandardItem;
416
model.appendRow(item);
417
// parent of a top-level item should be 0
418
QCOMPARE(item->parent(), static_cast<QStandardItem*>(0));
422
void tst_QStandardItem::insertColumn_data()
424
QTest::addColumn<int>("rows");
425
QTest::addColumn<int>("columns");
426
QTest::addColumn<int>("column");
427
QTest::addColumn<int>("count");
429
QTest::newRow("insert 0 at -1 in 0x0") << 0 << 0 << -1 << 0;
430
QTest::newRow("insert 0 at 0 in 0x0") << 0 << 0 << 0 << 0;
431
QTest::newRow("insert 0 at 0 in 1x0") << 1 << 0 << 0 << 0;
432
QTest::newRow("insert 0 at 0 in 0x1") << 0 << 1 << 0 << 0;
433
QTest::newRow("insert 0 at 0 in 1x1") << 1 << 1 << 0 << 0;
434
QTest::newRow("insert 1 at -1 in 0x0") << 0 << 0 << -1 << 1;
435
QTest::newRow("insert 1 at 0 in 0x0") << 0 << 0 << 0 << 1;
436
QTest::newRow("insert 1 at 0 in 1x0") << 1 << 0 << 0 << 1;
437
QTest::newRow("insert 1 at 0 in 0x1") << 0 << 1 << 0 << 1;
438
QTest::newRow("insert 1 at 0 in 1x1") << 1 << 1 << 0 << 1;
439
QTest::newRow("insert 1 at 1 in 1x1") << 1 << 1 << 1 << 1;
440
QTest::newRow("insert 1 at 0 in 2x1") << 2 << 1 << 0 << 1;
441
QTest::newRow("insert 1 at 1 in 2x1") << 2 << 1 << 1 << 1;
442
QTest::newRow("insert 1 at 0 in 1x2") << 1 << 2 << 0 << 1;
443
QTest::newRow("insert 1 at 1 in 1x2") << 1 << 2 << 1 << 1;
444
QTest::newRow("insert 1 at 0 in 8x4") << 8 << 4 << 0 << 1;
445
QTest::newRow("insert 1 at 1 in 8x4") << 8 << 4 << 1 << 1;
446
QTest::newRow("insert 1 at 2 in 8x4") << 8 << 4 << 2 << 1;
447
QTest::newRow("insert 1 at 3 in 8x4") << 8 << 4 << 3 << 1;
448
QTest::newRow("insert 1 at 4 in 8x4") << 8 << 4 << 4 << 1;
449
QTest::newRow("insert 4 at 0 in 8x4") << 8 << 4 << 0 << 4;
450
QTest::newRow("insert 4 at 4 in 8x4") << 8 << 4 << 4 << 4;
451
QTest::newRow("insert 6 at 0 in 8x4") << 8 << 4 << 0 << 6;
452
QTest::newRow("insert 6 at 4 in 8x4") << 8 << 4 << 4 << 6;
455
void tst_QStandardItem::insertColumn()
458
QFETCH(int, columns);
462
QStandardItem item(rows, columns);
464
// make items for a new column
465
QList<QStandardItem*> columnItems;
466
for (int i = 0; i < count; ++i)
467
columnItems.append(new QStandardItem);
469
item.insertColumn(column, columnItems);
472
QCOMPARE(item.columnCount(), columns + 1);
473
QCOMPARE(item.rowCount(), qMax(rows, count));
474
// check to make sure items were inserted in correct place
475
for (int i = 0; i < count; ++i)
476
QCOMPARE(item.child(i, column), columnItems.at(i));
477
for (int i = count; i < item.rowCount(); ++i)
478
QCOMPARE(item.child(i, column), static_cast<QStandardItem*>(0));
480
QCOMPARE(item.columnCount(), columns);
481
QCOMPARE(item.rowCount(), rows);
482
qDeleteAll(columnItems);
486
void tst_QStandardItem::insertColumns_data()
490
void tst_QStandardItem::insertColumns()
494
void tst_QStandardItem::insertRow_data()
496
QTest::addColumn<int>("rows");
497
QTest::addColumn<int>("columns");
498
QTest::addColumn<int>("row");
499
QTest::addColumn<int>("count");
501
QTest::newRow("insert 0 at -1 in 0x0") << 0 << 0 << -1 << 0;
502
QTest::newRow("insert 0 at 0 in 0x0") << 0 << 0 << 0 << 0;
503
QTest::newRow("insert 0 at 0 in 1x0") << 1 << 0 << 0 << 0;
504
QTest::newRow("insert 0 at 0 in 0x1") << 0 << 1 << 0 << 0;
505
QTest::newRow("insert 0 at 0 in 1x1") << 1 << 1 << 0 << 0;
506
QTest::newRow("insert 1 at -1 in 0x0") << 0 << 0 << -1 << 1;
507
QTest::newRow("insert 1 at 0 in 0x0") << 0 << 0 << 0 << 1;
508
QTest::newRow("insert 1 at 0 in 1x0") << 1 << 0 << 0 << 1;
509
QTest::newRow("insert 1 at 0 in 0x1") << 0 << 1 << 0 << 1;
510
QTest::newRow("insert 1 at 0 in 1x1") << 1 << 1 << 0 << 1;
511
QTest::newRow("insert 1 at 1 in 1x1") << 1 << 1 << 1 << 1;
512
QTest::newRow("insert 1 at 0 in 2x1") << 2 << 1 << 0 << 1;
513
QTest::newRow("insert 1 at 1 in 2x1") << 2 << 1 << 1 << 1;
514
QTest::newRow("insert 1 at 0 in 1x2") << 1 << 2 << 0 << 1;
515
QTest::newRow("insert 1 at 1 in 1x2") << 1 << 2 << 1 << 1;
516
QTest::newRow("insert 1 at 0 in 4x8") << 4 << 8 << 0 << 1;
517
QTest::newRow("insert 1 at 1 in 4x8") << 4 << 8 << 1 << 1;
518
QTest::newRow("insert 1 at 2 in 4x8") << 4 << 8 << 2 << 1;
519
QTest::newRow("insert 1 at 3 in 4x8") << 4 << 8 << 3 << 1;
520
QTest::newRow("insert 1 at 4 in 4x8") << 4 << 8 << 4 << 1;
521
QTest::newRow("insert 4 at 0 in 4x8") << 4 << 8 << 0 << 4;
522
QTest::newRow("insert 4 at 4 in 4x8") << 4 << 8 << 4 << 4;
523
QTest::newRow("insert 6 at 0 in 4x8") << 4 << 8 << 0 << 6;
524
QTest::newRow("insert 6 at 4 in 4x8") << 4 << 8 << 4 << 6;
527
void tst_QStandardItem::insertRow()
530
QFETCH(int, columns);
534
QStandardItem item(rows, columns);
536
// make items for a new column
537
QList<QStandardItem*> rowItems;
538
for (int i = 0; i < count; ++i)
539
rowItems.append(new QStandardItem);
541
item.insertRow(row, rowItems);
544
QCOMPARE(item.columnCount(), qMax(columns, count));
545
QCOMPARE(item.rowCount(), rows + 1);
546
// check to make sure items were inserted in correct place
547
for (int i = 0; i < count; ++i)
548
QCOMPARE(item.child(row, i), rowItems.at(i));
549
for (int i = count; i < item.columnCount(); ++i)
550
QCOMPARE(item.child(row, i), static_cast<QStandardItem*>(0));
552
QCOMPARE(item.columnCount(), columns);
553
QCOMPARE(item.rowCount(), rows);
554
qDeleteAll(rowItems);
558
void tst_QStandardItem::insertRows_data()
560
QTest::addColumn<int>("rows");
561
QTest::addColumn<int>("columns");
562
QTest::addColumn<int>("insertAt");
563
QTest::addColumn<int>("insertCount");
565
QTest::newRow("insert {0,1} at 0 in 0x0") << 0 << 0 << 0 << 2;
568
void tst_QStandardItem::insertRows()
571
QFETCH(int, columns);
572
QFETCH(int, insertAt);
573
QFETCH(int, insertCount);
575
QStandardItem item(rows, columns);
577
QList<QStandardItem*> items;
578
for (int i = 0; i < insertCount; ++i) {
579
items.append(new QStandardItem());
581
item.insertRows(insertAt, items);
583
QCOMPARE(item.rowCount(), rows + insertCount);
586
void tst_QStandardItem::appendColumn_data()
588
QTest::addColumn<int>("rows");
589
QTest::addColumn<int>("columns");
590
QTest::addColumn<int>("count");
592
QTest::newRow("append 0 to 0x0") << 0 << 0 << 0;
593
QTest::newRow("append 1 to 0x0") << 0 << 0 << 1;
594
QTest::newRow("append 1 to 1x0") << 1 << 0 << 1;
595
QTest::newRow("append 1 to 0x1") << 0 << 1 << 1;
596
QTest::newRow("append 1 to 1x1") << 1 << 1 << 1;
597
QTest::newRow("append 1 to 2x0") << 2 << 0 << 1;
598
QTest::newRow("append 1 to 0x2") << 0 << 2 << 1;
599
QTest::newRow("append 1 to 2x1") << 2 << 1 << 1;
600
QTest::newRow("append 1 to 1x2") << 1 << 2 << 1;
601
QTest::newRow("append 1 to 2x2") << 2 << 2 << 1;
602
QTest::newRow("append 2 to 0x0") << 0 << 0 << 2;
603
QTest::newRow("append 2 to 1x0") << 1 << 0 << 2;
604
QTest::newRow("append 2 to 0x1") << 0 << 1 << 2;
605
QTest::newRow("append 2 to 1x1") << 1 << 1 << 2;
606
QTest::newRow("append 2 to 2x0") << 2 << 0 << 2;
607
QTest::newRow("append 2 to 0x2") << 0 << 2 << 2;
608
QTest::newRow("append 2 to 2x1") << 2 << 1 << 2;
609
QTest::newRow("append 2 to 1x2") << 1 << 2 << 2;
610
QTest::newRow("append 2 to 2x2") << 2 << 2 << 2;
611
QTest::newRow("append 3 to 2x1") << 2 << 1 << 3;
612
QTest::newRow("append 3 to 1x2") << 1 << 2 << 3;
613
QTest::newRow("append 3 to 2x2") << 2 << 2 << 3;
614
QTest::newRow("append 3 to 4x2") << 4 << 2 << 3;
615
QTest::newRow("append 3 to 2x4") << 2 << 4 << 3;
616
QTest::newRow("append 3 to 4x4") << 4 << 4 << 3;
617
QTest::newRow("append 7 to 4x2") << 4 << 2 << 7;
618
QTest::newRow("append 7 to 2x4") << 2 << 4 << 7;
619
QTest::newRow("append 7 to 4x4") << 4 << 4 << 7;
622
void tst_QStandardItem::appendColumn()
625
QFETCH(int, columns);
628
QStandardItem item(rows, columns);
629
QList<QStandardItem*> originalChildren;
630
// initialize children
631
for (int i = 0; i < rows; ++i) {
632
for (int j = 0; j < columns; ++j) {
633
QStandardItem *child = new QStandardItem;
634
originalChildren.append(child);
635
item.setChild(i, j, child);
639
// make items for a new column
640
QList<QStandardItem*> columnItems;
641
for (int i = 0; i < count; ++i)
642
columnItems.append(new QStandardItem);
644
item.appendColumn(columnItems);
646
QCOMPARE(item.columnCount(), columns + 1);
647
QCOMPARE(item.rowCount(), qMax(rows, count));
648
// check to make sure items were inserted in correct place
649
for (int i = 0; i < count; ++i)
650
QCOMPARE(item.child(i, columns), columnItems.at(i));
651
for (int i = count; i < item.rowCount(); ++i)
652
QCOMPARE(item.child(i, columns), static_cast<QStandardItem*>(0));
654
// make sure original children remained unchanged
655
for (int i = 0; i < rows; ++i) {
656
for (int j = 0; j < columns; ++j)
657
QCOMPARE(item.child(i, j), originalChildren.at(i*columns+j));
661
void tst_QStandardItem::appendRow_data()
663
QTest::addColumn<int>("rows");
664
QTest::addColumn<int>("columns");
665
QTest::addColumn<int>("count");
667
QTest::newRow("append 0 to 0x0") << 0 << 0 << 0;
668
QTest::newRow("append 1 to 0x0") << 0 << 0 << 1;
669
QTest::newRow("append 1 to 1x0") << 1 << 0 << 1;
670
QTest::newRow("append 1 to 0x1") << 0 << 1 << 1;
671
QTest::newRow("append 1 to 1x1") << 1 << 1 << 1;
672
QTest::newRow("append 1 to 2x0") << 2 << 0 << 1;
673
QTest::newRow("append 1 to 0x2") << 0 << 2 << 1;
674
QTest::newRow("append 1 to 2x1") << 2 << 1 << 1;
675
QTest::newRow("append 1 to 1x2") << 1 << 2 << 1;
676
QTest::newRow("append 1 to 2x2") << 2 << 2 << 1;
677
QTest::newRow("append 2 to 0x0") << 0 << 0 << 2;
678
QTest::newRow("append 2 to 1x0") << 1 << 0 << 2;
679
QTest::newRow("append 2 to 0x1") << 0 << 1 << 2;
680
QTest::newRow("append 2 to 1x1") << 1 << 1 << 2;
681
QTest::newRow("append 2 to 2x0") << 2 << 0 << 2;
682
QTest::newRow("append 2 to 0x2") << 0 << 2 << 2;
683
QTest::newRow("append 2 to 2x1") << 2 << 1 << 2;
684
QTest::newRow("append 2 to 1x2") << 1 << 2 << 2;
685
QTest::newRow("append 2 to 2x2") << 2 << 2 << 2;
686
QTest::newRow("append 3 to 2x1") << 2 << 1 << 3;
687
QTest::newRow("append 3 to 1x2") << 1 << 2 << 3;
688
QTest::newRow("append 3 to 2x2") << 2 << 2 << 3;
689
QTest::newRow("append 3 to 4x2") << 4 << 2 << 3;
690
QTest::newRow("append 3 to 2x4") << 2 << 4 << 3;
691
QTest::newRow("append 3 to 4x4") << 4 << 4 << 3;
692
QTest::newRow("append 7 to 4x2") << 4 << 2 << 7;
693
QTest::newRow("append 7 to 2x4") << 2 << 4 << 7;
694
QTest::newRow("append 7 to 4x4") << 4 << 4 << 7;
697
void tst_QStandardItem::appendRow()
700
QFETCH(int, columns);
703
QStandardItem item(rows, columns);
704
QList<QStandardItem*> originalChildren;
705
// initialize children
706
for (int i = 0; i < rows; ++i) {
707
for (int j = 0; j < columns; ++j) {
708
QStandardItem *child = new QStandardItem;
709
originalChildren.append(child);
710
item.setChild(i, j, child);
714
// make items for a new row
715
QList<QStandardItem*> rowItems;
716
for (int i = 0; i < count; ++i)
717
rowItems.append(new QStandardItem);
719
item.appendRow(rowItems);
721
QCOMPARE(item.rowCount(), rows + 1);
722
QCOMPARE(item.columnCount(), qMax(columns, count));
723
// check to make sure items were inserted in correct place
724
for (int i = 0; i < count; ++i)
725
QCOMPARE(item.child(rows, i), rowItems.at(i));
726
for (int i = count; i < item.columnCount(); ++i)
727
QCOMPARE(item.child(rows, i), static_cast<QStandardItem*>(0));
729
// make sure original children remained unchanged
730
for (int i = 0; i < rows; ++i) {
731
for (int j = 0; j < columns; ++j)
732
QCOMPARE(item.child(i, j), originalChildren.at(i*columns+j));
736
void tst_QStandardItem::takeChild()
738
QList<QStandardItem*> itemList;
739
for (int i = 0; i < 10; ++i)
740
itemList.append(new QStandardItem);
742
item.appendColumn(itemList);
744
for (int i = 0; i < item.rowCount(); ++i) {
745
QCOMPARE(item.takeChild(i), itemList.at(i));
746
QCOMPARE(item.takeChild(0, 0), static_cast<QStandardItem*>(0));
747
for (int j = i + 1; j < item.rowCount(); ++j)
748
QCOMPARE(item.child(j), itemList.at(j));
750
qDeleteAll(itemList);
753
void tst_QStandardItem::takeColumn_data()
755
QTest::addColumn<int>("rows");
756
QTest::addColumn<int>("columns");
757
QTest::addColumn<int>("column");
758
QTest::addColumn<bool>("expectSuccess");
760
QTest::newRow("take -1 from 0x0") << 0 << 0 << -1 << false;
761
QTest::newRow("take 0 from 0x0") << 0 << 0 << 0 << false;
762
QTest::newRow("take 0 from 1x0") << 1 << 0 << 0 << false;
763
QTest::newRow("take 0 from 0x1") << 0 << 1 << 0 << true;
764
QTest::newRow("take 1 from 0x1") << 0 << 1 << 1 << false;
765
QTest::newRow("take 0 from 1x1") << 1 << 1 << 0 << true;
766
QTest::newRow("take 1 from 1x1") << 0 << 1 << 1 << false;
767
QTest::newRow("take 0 from 4x1") << 4 << 1 << 0 << true;
768
QTest::newRow("take 1 from 4x1") << 4 << 1 << 1 << false;
769
QTest::newRow("take 0 from 4x8") << 4 << 8 << 0 << true;
770
QTest::newRow("take 7 from 4x8") << 4 << 8 << 7 << true;
771
QTest::newRow("take 8 from 4x8") << 4 << 8 << 8 << false;
774
void tst_QStandardItem::takeColumn()
777
QFETCH(int, columns);
779
QFETCH(bool, expectSuccess);
781
QStandardItem item(rows, columns);
782
QList<QStandardItem*> originalChildren;
783
// initialize children
784
for (int i = 0; i < rows; ++i) {
785
for (int j = 0; j < columns; ++j) {
786
QStandardItem *child = new QStandardItem;
787
originalChildren.append(child);
788
item.setChild(i, j, child);
792
QList<QStandardItem *> taken = item.takeColumn(column);
794
QCOMPARE(taken.count(), item.rowCount());
795
QCOMPARE(item.columnCount(), columns - 1);
797
for (int i = 0; i < taken.count(); ++i) {
798
QCOMPARE(taken.at(i), originalChildren.takeAt(index));
799
index += item.columnCount();
802
for (int i = 0; i < item.rowCount(); ++i) {
803
for (int j = 0; j < item.columnCount(); ++j) {
804
QCOMPARE(item.child(i, j), originalChildren.at(index));
809
QVERIFY(taken.isEmpty());
814
void tst_QStandardItem::takeRow_data()
816
QTest::addColumn<int>("rows");
817
QTest::addColumn<int>("columns");
818
QTest::addColumn<int>("row");
819
QTest::addColumn<bool>("expectSuccess");
821
QTest::newRow("take -1 from 0x0") << 0 << 0 << -1 << false;
822
QTest::newRow("take 0 from 0x0") << 0 << 0 << 0 << false;
823
QTest::newRow("take 0 from 1x0") << 1 << 0 << 0 << true;
824
QTest::newRow("take 0 from 0x1") << 0 << 1 << 0 << false;
825
QTest::newRow("take 1 from 0x1") << 0 << 1 << 1 << false;
826
QTest::newRow("take 0 from 1x1") << 1 << 1 << 0 << true;
827
QTest::newRow("take 1 from 1x1") << 0 << 1 << 1 << false;
828
QTest::newRow("take 0 from 1x4") << 1 << 4 << 0 << true;
829
QTest::newRow("take 1 from 1x4") << 1 << 4 << 1 << false;
830
QTest::newRow("take 0 from 8x4") << 8 << 4 << 0 << true;
831
QTest::newRow("take 7 from 8x4") << 8 << 4 << 7 << true;
832
QTest::newRow("take 8 from 8x4") << 8 << 4 << 8 << false;
835
void tst_QStandardItem::takeRow()
838
QFETCH(int, columns);
840
QFETCH(bool, expectSuccess);
842
QStandardItem item(rows, columns);
843
QList<QStandardItem*> originalChildren;
844
// initialize children
845
for (int i = 0; i < rows; ++i) {
846
for (int j = 0; j < columns; ++j) {
847
QStandardItem *child = new QStandardItem;
848
originalChildren.append(child);
849
item.setChild(i, j, child);
853
QList<QStandardItem *> taken = item.takeRow(row);
855
QCOMPARE(taken.count(), item.columnCount());
856
QCOMPARE(item.rowCount(), rows - 1);
857
int index = row * columns;
858
for (int i = 0; i < taken.count(); ++i) {
859
QCOMPARE(taken.at(i), originalChildren.takeAt(index));
862
for (int i = 0; i < item.rowCount(); ++i) {
863
for (int j = 0; j < item.columnCount(); ++j) {
864
QCOMPARE(item.child(i, j), originalChildren.at(index));
869
QVERIFY(taken.isEmpty());
874
void tst_QStandardItem::streamItem()
878
item.setText(QLatin1String("text"));
879
item.setToolTip(QLatin1String("toolTip"));
880
item.setStatusTip(QLatin1String("statusTip"));
881
item.setWhatsThis(QLatin1String("whatsThis"));
882
item.setSizeHint(QSize(64, 48));
883
item.setFont(QFont());
884
item.setTextAlignment(Qt::AlignLeft|Qt::AlignVCenter);
885
item.setBackground(QColor(Qt::blue));
886
item.setForeground(QColor(Qt::green));
887
item.setCheckState(Qt::PartiallyChecked);
888
item.setAccessibleText(QLatin1String("accessibleText"));
889
item.setAccessibleDescription(QLatin1String("accessibleDescription"));
893
QDataStream ds(&ba, QIODevice::WriteOnly);
897
QStandardItem streamedItem;
898
QDataStream ds(&ba, QIODevice::ReadOnly);
900
QCOMPARE(streamedItem.text(), item.text());
901
QCOMPARE(streamedItem.toolTip(), item.toolTip());
902
QCOMPARE(streamedItem.statusTip(), item.statusTip());
903
QCOMPARE(streamedItem.whatsThis(), item.whatsThis());
904
QCOMPARE(streamedItem.sizeHint(), item.sizeHint());
905
QCOMPARE(streamedItem.font(), item.font());
906
QCOMPARE(streamedItem.textAlignment(), item.textAlignment());
907
QCOMPARE(streamedItem.background(), item.background());
908
QCOMPARE(streamedItem.foreground(), item.foreground());
909
QCOMPARE(streamedItem.checkState(), item.checkState());
910
QCOMPARE(streamedItem.accessibleText(), item.accessibleText());
911
QCOMPARE(streamedItem.accessibleDescription(), item.accessibleDescription());
912
QCOMPARE(streamedItem.flags(), item.flags());
916
void tst_QStandardItem::deleteItem()
918
QStandardItemModel model(4, 6);
920
for (int i = 0; i < model.rowCount(); ++i) {
921
for (int j = 0; j < model.columnCount(); ++j) {
922
QStandardItem *item = new QStandardItem();
923
model.setItem(i, j, item);
927
for (int i = 0; i < model.rowCount(); ++i) {
928
for (int j = 0; j < model.columnCount(); ++j) {
929
QStandardItem *item = model.item(i, j);
931
QCOMPARE(model.item(i, j), static_cast<QStandardItem*>(0));
936
void tst_QStandardItem::clone()
939
item.setText(QLatin1String("text"));
940
item.setToolTip(QLatin1String("toolTip"));
941
item.setStatusTip(QLatin1String("statusTip"));
942
item.setWhatsThis(QLatin1String("whatsThis"));
943
item.setSizeHint(QSize(64, 48));
944
item.setFont(QFont());
945
item.setTextAlignment(Qt::AlignLeft|Qt::AlignVCenter);
946
item.setBackground(QColor(Qt::blue));
947
item.setForeground(QColor(Qt::green));
948
item.setCheckState(Qt::PartiallyChecked);
949
item.setAccessibleText(QLatin1String("accessibleText"));
950
item.setAccessibleDescription(QLatin1String("accessibleDescription"));
951
item.setFlags(Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
953
QStandardItem *clone = item.clone();
954
QCOMPARE(clone->text(), item.text());
955
QCOMPARE(clone->toolTip(), item.toolTip());
956
QCOMPARE(clone->statusTip(), item.statusTip());
957
QCOMPARE(clone->whatsThis(), item.whatsThis());
958
QCOMPARE(clone->sizeHint(), item.sizeHint());
959
QCOMPARE(clone->font(), item.font());
960
QCOMPARE(clone->textAlignment(), item.textAlignment());
961
QCOMPARE(clone->background(), item.background());
962
QCOMPARE(clone->foreground(), item.foreground());
963
QCOMPARE(clone->checkState(), item.checkState());
964
QCOMPARE(clone->accessibleText(), item.accessibleText());
965
QCOMPARE(clone->accessibleDescription(), item.accessibleDescription());
966
QCOMPARE(clone->flags(), item.flags());
967
QVERIFY(!(*clone < item));
971
void tst_QStandardItem::sortChildren()
973
for (int x = 0; x < 2; ++x) {
974
QStandardItemModel *model = new QStandardItemModel;
975
QStandardItem *item = (x == 0) ? new QStandardItem : model->invisibleRootItem();
976
QStandardItem *one = new QStandardItem;
977
one->appendRow(new QStandardItem(QLatin1String("a")));
978
one->appendRow(new QStandardItem(QLatin1String("b")));
979
one->appendRow(new QStandardItem(QLatin1String("c")));
980
QStandardItem *two = new QStandardItem;
981
two->appendRow(new QStandardItem(QLatin1String("f")));
982
two->appendRow(new QStandardItem(QLatin1String("d")));
983
two->appendRow(new QStandardItem(QLatin1String("e")));
984
item->appendRow(one);
985
item->appendRow(two);
987
QSignalSpy layoutAboutToBeChangedSpy(
988
model, SIGNAL(layoutAboutToBeChanged()));
989
QSignalSpy layoutChangedSpy(
990
model, SIGNAL(layoutChanged()));
992
one->sortChildren(0, Qt::DescendingOrder);
994
QCOMPARE(one->child(0)->text(), QLatin1String("c"));
995
QCOMPARE(one->child(1)->text(), QLatin1String("b"));
996
QCOMPARE(one->child(2)->text(), QLatin1String("a"));
997
// verify siblings unaffected
998
QCOMPARE(two->child(0)->text(), QLatin1String("f"));
999
QCOMPARE(two->child(1)->text(), QLatin1String("d"));
1000
QCOMPARE(two->child(2)->text(), QLatin1String("e"));
1002
two->sortChildren(0, Qt::AscendingOrder);
1004
QCOMPARE(two->child(0)->text(), QLatin1String("d"));
1005
QCOMPARE(two->child(1)->text(), QLatin1String("e"));
1006
QCOMPARE(two->child(2)->text(), QLatin1String("f"));
1007
// verify siblings unaffected
1008
QCOMPARE(one->child(0)->text(), QLatin1String("c"));
1009
QCOMPARE(one->child(1)->text(), QLatin1String("b"));
1010
QCOMPARE(one->child(2)->text(), QLatin1String("a"));
1012
item->sortChildren(0, Qt::AscendingOrder);
1013
// verify everything sorted
1014
QCOMPARE(one->child(0)->text(), QLatin1String("a"));
1015
QCOMPARE(one->child(1)->text(), QLatin1String("b"));
1016
QCOMPARE(one->child(2)->text(), QLatin1String("c"));
1017
QCOMPARE(two->child(0)->text(), QLatin1String("d"));
1018
QCOMPARE(two->child(1)->text(), QLatin1String("e"));
1019
QCOMPARE(two->child(2)->text(), QLatin1String("f"));
1021
QCOMPARE(layoutAboutToBeChangedSpy.count(), (x == 0) ? 0 : 3);
1022
QCOMPARE(layoutChangedSpy.count(), (x == 0) ? 0 : 3);
1030
class CustomItem : public QStandardItem
1033
CustomItem(const QString &text) : QStandardItem(text) { }
1035
virtual ~CustomItem() { }
1037
virtual int type() const { return QStandardItem::UserType + 1; }
1039
virtual QStandardItem *clone() const { return QStandardItem::clone(); }
1041
void emitDataChanged() { QStandardItem::emitDataChanged(); }
1043
virtual bool operator<(const QStandardItem &other) const {
1044
return text().length() < other.text().length();
1048
Q_DECLARE_METATYPE(QStandardItem*)
1050
void tst_QStandardItem::subclassing()
1052
qMetaTypeId<QStandardItem*>();
1054
CustomItem *item = new CustomItem;
1055
QCOMPARE(item->type(), int(QStandardItem::UserType + 1));
1057
item->setText(QString::fromLatin1("foo"));
1058
QCOMPARE(item->text(), QString::fromLatin1("foo"));
1060
item->emitDataChanged(); // does nothing
1062
QStandardItemModel model;
1063
model.appendRow(item);
1065
QSignalSpy itemChangedSpy(&model, SIGNAL(itemChanged(QStandardItem*)));
1066
item->emitDataChanged();
1067
QCOMPARE(itemChangedSpy.count(), 1);
1068
QCOMPARE(itemChangedSpy.at(0).count(), 1);
1069
QCOMPARE(qvariant_cast<QStandardItem*>(itemChangedSpy.at(0).at(0)), (QStandardItem*)item);
1071
CustomItem *child0 = new CustomItem("cc");
1072
CustomItem *child1 = new CustomItem("bbb");
1073
CustomItem *child2 = new CustomItem("a");
1074
item->appendRow(child0);
1075
item->appendRow(child1);
1076
item->appendRow(child2);
1077
item->sortChildren(0);
1078
QCOMPARE(item->child(0), (QStandardItem*)child2);
1079
QCOMPARE(item->child(1), (QStandardItem*)child0);
1080
QCOMPARE(item->child(2), (QStandardItem*)child1);
1083
void tst_QStandardItem::lessThan()
1085
QStandardItem stringA("A");
1086
QStandardItem stringB("B");
1087
QStandardItem invalid1;
1088
QStandardItem invalid2;
1089
QVERIFY(stringA < stringB);
1090
QVERIFY(!(stringB < stringA));
1091
// Items with invalid data go to the end.
1092
QVERIFY(stringA < invalid1);
1093
QVERIFY(!(invalid1 < stringA));
1094
QVERIFY(!(invalid1 < invalid2));
1097
QTEST_MAIN(tst_QStandardItem)
1098
#include "tst_qstandarditem.moc"