~gabriel1984sibiu/minitube/qt5.6

« back to all changes in this revision

Viewing changes to tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp

  • Committer: Grevutiu Gabriel
  • Date: 2017-06-13 08:43:17 UTC
  • Revision ID: gabriel1984sibiu@gmail.com-20170613084317-ek0zqe0u9g3ocvi8
OriginalĀ upstreamĀ code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2016 The Qt Company Ltd.
 
4
** Contact: https://www.qt.io/licensing/
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
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.
 
16
**
 
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.
 
24
**
 
25
** $QT_END_LICENSE$
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
 
 
30
#include <QtTest/QtTest>
 
31
 
 
32
#include <qstandarditemmodel.h>
 
33
 
 
34
class tst_QStandardItem : public QObject
 
35
{
 
36
    Q_OBJECT
 
37
 
 
38
private slots:
 
39
    void ctor();
 
40
    void textCtor();
 
41
    void iconTextCtor();
 
42
    void rowsColumnsCtor();
 
43
    void getSetData();
 
44
    void getSetFlags();
 
45
    void getSetRowAndColumnCount();
 
46
    void getSetChild_data();
 
47
    void getSetChild();
 
48
    void parent();
 
49
    void insertColumn_data();
 
50
    void insertColumn();
 
51
    void insertColumns_data();
 
52
    void insertColumns();
 
53
    void insertRow_data();
 
54
    void insertRow();
 
55
    void insertRows_data();
 
56
    void insertRows();
 
57
    void appendColumn_data();
 
58
    void appendColumn();
 
59
    void appendRow_data();
 
60
    void appendRow();
 
61
    void takeChild();
 
62
    void takeColumn_data();
 
63
    void takeColumn();
 
64
    void takeRow_data();
 
65
    void takeRow();
 
66
    void streamItem();
 
67
    void deleteItem();
 
68
    void clone();
 
69
    void sortChildren();
 
70
    void subclassing();
 
71
    void lessThan();
 
72
};
 
73
 
 
74
void tst_QStandardItem::ctor()
 
75
{
 
76
    QStandardItem item;
 
77
    QVERIFY(!item.hasChildren());
 
78
}
 
79
 
 
80
void tst_QStandardItem::textCtor()
 
81
{
 
82
    QLatin1String text("text");
 
83
    QStandardItem item(text);
 
84
    QCOMPARE(item.text(), text);
 
85
    QVERIFY(!item.hasChildren());
 
86
}
 
87
 
 
88
void tst_QStandardItem::iconTextCtor()
 
89
{
 
90
    QPixmap pixmap(32, 32);
 
91
    pixmap.fill(Qt::red);
 
92
    QIcon icon(pixmap);
 
93
    QLatin1String text("text");
 
94
    QStandardItem item(icon, text);
 
95
    QCOMPARE(item.icon(), icon);
 
96
    QCOMPARE(item.text(), text);
 
97
    QVERIFY(!item.hasChildren());
 
98
}
 
99
 
 
100
void tst_QStandardItem::rowsColumnsCtor()
 
101
{
 
102
    const int rows = 5;
 
103
    const int columns = 12;
 
104
    QStandardItem item(rows, columns);
 
105
    QCOMPARE(item.rowCount(), rows);
 
106
    QCOMPARE(item.columnCount(), columns);
 
107
}
 
108
 
 
109
void tst_QStandardItem::getSetData()
 
110
{
 
111
    QStandardItem item;
 
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;
 
116
            item.setText(text);
 
117
            QCOMPARE(item.text(), text);
 
118
 
 
119
            QPixmap pixmap(32, 32);
 
120
            pixmap.fill((i == 1) ? Qt::red : Qt::green);
 
121
            QIcon icon(pixmap);
 
122
            item.setIcon(icon);
 
123
            QCOMPARE(item.icon(), icon);
 
124
 
 
125
            QString toolTip = QLatin1String("toolTip ") + iS;
 
126
            item.setToolTip(toolTip);
 
127
            QCOMPARE(item.toolTip(), toolTip);
 
128
 
 
129
            QString statusTip = QLatin1String("statusTip ") + iS;
 
130
            item.setStatusTip(statusTip);
 
131
            QCOMPARE(item.statusTip(), statusTip);
 
132
 
 
133
            QString whatsThis = QLatin1String("whatsThis ") + iS;
 
134
            item.setWhatsThis(whatsThis);
 
135
            QCOMPARE(item.whatsThis(), whatsThis);
 
136
 
 
137
            QSize sizeHint(64*i, 48*i);
 
138
            item.setSizeHint(sizeHint);
 
139
            QCOMPARE(item.sizeHint(), sizeHint);
 
140
 
 
141
            QFont font;
 
142
            item.setFont(font);
 
143
            QCOMPARE(item.font(), font);
 
144
 
 
145
            Qt::Alignment textAlignment((i == 1)
 
146
                                        ? Qt::AlignLeft|Qt::AlignVCenter
 
147
                                        : Qt::AlignRight);
 
148
            item.setTextAlignment(textAlignment);
 
149
            QCOMPARE(item.textAlignment(), textAlignment);
 
150
 
 
151
            QColor backgroundColor((i == 1) ? Qt::blue : Qt::yellow);
 
152
            item.setBackground(backgroundColor);
 
153
            QCOMPARE(item.background().color(), backgroundColor);
 
154
 
 
155
            QColor textColor((i == 1) ? Qt::green : Qt::cyan);
 
156
            item.setForeground(textColor);
 
157
            QCOMPARE(item.foreground().color(), textColor);
 
158
 
 
159
            Qt::CheckState checkState((i == 1) ? Qt::PartiallyChecked : Qt::Checked);
 
160
            item.setCheckState(checkState);
 
161
            QCOMPARE(item.checkState(), checkState);
 
162
 
 
163
            QString accessibleText = QLatin1String("accessibleText ") + iS;
 
164
            item.setAccessibleText(accessibleText);
 
165
            QCOMPARE(item.accessibleText(), accessibleText);
 
166
 
 
167
            QString accessibleDescription = QLatin1String("accessibleDescription ") + iS;
 
168
            item.setAccessibleDescription(accessibleDescription);
 
169
            QCOMPARE(item.accessibleDescription(), accessibleDescription);
 
170
 
 
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);
 
184
 
 
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);
 
200
 
 
201
            item.setBackground(pixmap);
 
202
            QCOMPARE(item.background().texture(), pixmap);
 
203
            QCOMPARE(qvariant_cast<QBrush>(item.data(Qt::BackgroundRole)).texture(), pixmap);
 
204
        }
 
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);
 
218
 
 
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());
 
234
    }
 
235
}
 
236
 
 
237
void tst_QStandardItem::getSetFlags()
 
238
{
 
239
    QStandardItem item;
 
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);
 
266
#endif
 
267
 
 
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));
 
299
#endif
 
300
 
 
301
    item.setCheckable(false);
 
302
    item.setCheckState(Qt::Checked);
 
303
    item.setCheckable(true);
 
304
    QCOMPARE(item.checkState(), Qt::Checked);
 
305
 
 
306
    // deprecated API
 
307
    item.setTristate(true);
 
308
    QVERIFY(item.isTristate());
 
309
    QVERIFY(item.flags() & Qt::ItemIsTristate);
 
310
    item.setTristate(false);
 
311
    QVERIFY(!(item.flags() & Qt::ItemIsTristate));
 
312
}
 
313
 
 
314
void tst_QStandardItem::getSetRowAndColumnCount()
 
315
{
 
316
    QStandardItem item;
 
317
 
 
318
    item.setRowCount(-1);
 
319
    QCOMPARE(item.rowCount(), 0);
 
320
 
 
321
    item.setColumnCount(-1);
 
322
    QCOMPARE(item.columnCount(), 0);
 
323
 
 
324
    item.setRowCount(1);
 
325
    QCOMPARE(item.rowCount(), 1);
 
326
    QCOMPARE(item.columnCount(), 0);
 
327
 
 
328
    item.setColumnCount(1);
 
329
    QCOMPARE(item.columnCount(), 1);
 
330
    QCOMPARE(item.rowCount(), 1);
 
331
 
 
332
    item.setColumnCount(10);
 
333
    QCOMPARE(item.columnCount(), 10);
 
334
    QCOMPARE(item.rowCount(), 1);
 
335
 
 
336
    item.setRowCount(20);
 
337
    QCOMPARE(item.rowCount(), 20);
 
338
    QCOMPARE(item.columnCount(), 10);
 
339
 
 
340
    item.setRowCount(-1);
 
341
    QCOMPARE(item.rowCount(), 20);
 
342
 
 
343
    item.setColumnCount(-1);
 
344
    QCOMPARE(item.columnCount(), 10);
 
345
 
 
346
    item.setColumnCount(0);
 
347
    QCOMPARE(item.columnCount(), 0);
 
348
    QCOMPARE(item.rowCount(), 20);
 
349
 
 
350
    item.setRowCount(0);
 
351
    QCOMPARE(item.rowCount(), 0);
 
352
}
 
353
 
 
354
void tst_QStandardItem::getSetChild_data()
 
355
{
 
356
    QTest::addColumn<int>("rows");
 
357
    QTest::addColumn<int>("columns");
 
358
    QTest::addColumn<int>("row");
 
359
    QTest::addColumn<int>("column");
 
360
 
 
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;
 
363
}
 
364
 
 
365
void tst_QStandardItem::getSetChild()
 
366
{
 
367
    QFETCH(int, rows);
 
368
    QFETCH(int, columns);
 
369
    QFETCH(int, row);
 
370
    QFETCH(int, column);
 
371
 
 
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));
 
376
 
 
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));
 
382
 
 
383
        QCOMPARE(item.child(row, column), child);
 
384
        QCOMPARE(child->row(), row);
 
385
        QCOMPARE(child->column(), column);
 
386
 
 
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);
 
393
    } else {
 
394
        delete child;
 
395
    }
 
396
    QCOMPARE(item.child(row, column), static_cast<QStandardItem*>(0));
 
397
}
 
398
 
 
399
void tst_QStandardItem::parent()
 
400
{
 
401
    {
 
402
        QStandardItem item;
 
403
        QStandardItem *child = new QStandardItem;
 
404
        QCOMPARE(child->parent(), static_cast<QStandardItem*>(0));
 
405
        item.setChild(0, 0, child);
 
406
        QCOMPARE(child->parent(), &item);
 
407
 
 
408
        QStandardItem *childOfChild = new QStandardItem;
 
409
        child->setChild(0, 0, childOfChild);
 
410
        QCOMPARE(childOfChild->parent(), child);
 
411
    }
 
412
 
 
413
    {
 
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));
 
419
    }
 
420
}
 
421
 
 
422
void tst_QStandardItem::insertColumn_data()
 
423
{
 
424
    QTest::addColumn<int>("rows");
 
425
    QTest::addColumn<int>("columns");
 
426
    QTest::addColumn<int>("column");
 
427
    QTest::addColumn<int>("count");
 
428
 
 
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;
 
453
}
 
454
 
 
455
void tst_QStandardItem::insertColumn()
 
456
{
 
457
    QFETCH(int, rows);
 
458
    QFETCH(int, columns);
 
459
    QFETCH(int, column);
 
460
    QFETCH(int, count);
 
461
 
 
462
    QStandardItem item(rows, columns);
 
463
 
 
464
    // make items for a new column
 
465
    QList<QStandardItem*> columnItems;
 
466
    for (int i = 0; i < count; ++i)
 
467
        columnItems.append(new QStandardItem);
 
468
 
 
469
    item.insertColumn(column, columnItems);
 
470
 
 
471
    if (column >= 0) {
 
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));
 
479
    } else {
 
480
        QCOMPARE(item.columnCount(), columns);
 
481
        QCOMPARE(item.rowCount(), rows);
 
482
        qDeleteAll(columnItems);
 
483
    }
 
484
}
 
485
 
 
486
void tst_QStandardItem::insertColumns_data()
 
487
{
 
488
}
 
489
 
 
490
void tst_QStandardItem::insertColumns()
 
491
{
 
492
}
 
493
 
 
494
void tst_QStandardItem::insertRow_data()
 
495
{
 
496
    QTest::addColumn<int>("rows");
 
497
    QTest::addColumn<int>("columns");
 
498
    QTest::addColumn<int>("row");
 
499
    QTest::addColumn<int>("count");
 
500
 
 
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;
 
525
}
 
526
 
 
527
void tst_QStandardItem::insertRow()
 
528
{
 
529
    QFETCH(int, rows);
 
530
    QFETCH(int, columns);
 
531
    QFETCH(int, row);
 
532
    QFETCH(int, count);
 
533
 
 
534
    QStandardItem item(rows, columns);
 
535
 
 
536
    // make items for a new column
 
537
    QList<QStandardItem*> rowItems;
 
538
    for (int i = 0; i < count; ++i)
 
539
        rowItems.append(new QStandardItem);
 
540
 
 
541
    item.insertRow(row, rowItems);
 
542
 
 
543
    if (row >= 0) {
 
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));
 
551
    } else {
 
552
        QCOMPARE(item.columnCount(), columns);
 
553
        QCOMPARE(item.rowCount(), rows);
 
554
        qDeleteAll(rowItems);
 
555
    }
 
556
}
 
557
 
 
558
void tst_QStandardItem::insertRows_data()
 
559
{
 
560
    QTest::addColumn<int>("rows");
 
561
    QTest::addColumn<int>("columns");
 
562
    QTest::addColumn<int>("insertAt");
 
563
    QTest::addColumn<int>("insertCount");
 
564
 
 
565
    QTest::newRow("insert {0,1} at 0 in 0x0") << 0 << 0 << 0 << 2;
 
566
}
 
567
 
 
568
void tst_QStandardItem::insertRows()
 
569
{
 
570
    QFETCH(int, rows);
 
571
    QFETCH(int, columns);
 
572
    QFETCH(int, insertAt);
 
573
    QFETCH(int, insertCount);
 
574
 
 
575
    QStandardItem item(rows, columns);
 
576
 
 
577
    QList<QStandardItem*> items;
 
578
    for (int i = 0; i < insertCount; ++i) {
 
579
        items.append(new QStandardItem());
 
580
    }
 
581
    item.insertRows(insertAt, items);
 
582
 
 
583
    QCOMPARE(item.rowCount(), rows + insertCount);
 
584
}
 
585
 
 
586
void tst_QStandardItem::appendColumn_data()
 
587
{
 
588
    QTest::addColumn<int>("rows");
 
589
    QTest::addColumn<int>("columns");
 
590
    QTest::addColumn<int>("count");
 
591
 
 
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;
 
620
}
 
621
 
 
622
void tst_QStandardItem::appendColumn()
 
623
{
 
624
    QFETCH(int, rows);
 
625
    QFETCH(int, columns);
 
626
    QFETCH(int, count);
 
627
 
 
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);
 
636
        }
 
637
    }
 
638
 
 
639
    // make items for a new column
 
640
    QList<QStandardItem*> columnItems;
 
641
    for (int i = 0; i < count; ++i)
 
642
        columnItems.append(new QStandardItem);
 
643
 
 
644
    item.appendColumn(columnItems);
 
645
 
 
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));
 
653
 
 
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));
 
658
    }
 
659
}
 
660
 
 
661
void tst_QStandardItem::appendRow_data()
 
662
{
 
663
    QTest::addColumn<int>("rows");
 
664
    QTest::addColumn<int>("columns");
 
665
    QTest::addColumn<int>("count");
 
666
 
 
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;
 
695
}
 
696
 
 
697
void tst_QStandardItem::appendRow()
 
698
{
 
699
    QFETCH(int, rows);
 
700
    QFETCH(int, columns);
 
701
    QFETCH(int, count);
 
702
 
 
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);
 
711
        }
 
712
    }
 
713
 
 
714
    // make items for a new row
 
715
    QList<QStandardItem*> rowItems;
 
716
    for (int i = 0; i < count; ++i)
 
717
        rowItems.append(new QStandardItem);
 
718
 
 
719
    item.appendRow(rowItems);
 
720
 
 
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));
 
728
 
 
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));
 
733
    }
 
734
}
 
735
 
 
736
void tst_QStandardItem::takeChild()
 
737
{
 
738
    QList<QStandardItem*> itemList;
 
739
    for (int i = 0; i < 10; ++i)
 
740
        itemList.append(new QStandardItem);
 
741
    QStandardItem item;
 
742
    item.appendColumn(itemList);
 
743
 
 
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));
 
749
    }
 
750
    qDeleteAll(itemList);
 
751
}
 
752
 
 
753
void tst_QStandardItem::takeColumn_data()
 
754
{
 
755
    QTest::addColumn<int>("rows");
 
756
    QTest::addColumn<int>("columns");
 
757
    QTest::addColumn<int>("column");
 
758
    QTest::addColumn<bool>("expectSuccess");
 
759
 
 
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;
 
772
}
 
773
 
 
774
void tst_QStandardItem::takeColumn()
 
775
{
 
776
    QFETCH(int, rows);
 
777
    QFETCH(int, columns);
 
778
    QFETCH(int, column);
 
779
    QFETCH(bool, expectSuccess);
 
780
 
 
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);
 
789
        }
 
790
    }
 
791
 
 
792
    QList<QStandardItem *> taken = item.takeColumn(column);
 
793
    if (expectSuccess) {
 
794
        QCOMPARE(taken.count(), item.rowCount());
 
795
        QCOMPARE(item.columnCount(), columns - 1);
 
796
        int index = column;
 
797
        for (int i = 0; i < taken.count(); ++i) {
 
798
            QCOMPARE(taken.at(i), originalChildren.takeAt(index));
 
799
            index += item.columnCount();
 
800
        }
 
801
        index = 0;
 
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));
 
805
                ++index;
 
806
            }
 
807
        }
 
808
    } else {
 
809
        QVERIFY(taken.isEmpty());
 
810
    }
 
811
    qDeleteAll(taken);
 
812
}
 
813
 
 
814
void tst_QStandardItem::takeRow_data()
 
815
{
 
816
    QTest::addColumn<int>("rows");
 
817
    QTest::addColumn<int>("columns");
 
818
    QTest::addColumn<int>("row");
 
819
    QTest::addColumn<bool>("expectSuccess");
 
820
 
 
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;
 
833
}
 
834
 
 
835
void tst_QStandardItem::takeRow()
 
836
{
 
837
    QFETCH(int, rows);
 
838
    QFETCH(int, columns);
 
839
    QFETCH(int, row);
 
840
    QFETCH(bool, expectSuccess);
 
841
 
 
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);
 
850
        }
 
851
    }
 
852
 
 
853
    QList<QStandardItem *> taken = item.takeRow(row);
 
854
    if (expectSuccess) {
 
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));
 
860
        }
 
861
        index = 0;
 
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));
 
865
                ++index;
 
866
            }
 
867
        }
 
868
    } else {
 
869
        QVERIFY(taken.isEmpty());
 
870
    }
 
871
    qDeleteAll(taken);
 
872
}
 
873
 
 
874
void tst_QStandardItem::streamItem()
 
875
{
 
876
    QStandardItem item;
 
877
 
 
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"));
 
890
 
 
891
    QByteArray ba;
 
892
    {
 
893
        QDataStream ds(&ba, QIODevice::WriteOnly);
 
894
        ds << item;
 
895
    }
 
896
    {
 
897
        QStandardItem streamedItem;
 
898
        QDataStream ds(&ba, QIODevice::ReadOnly);
 
899
        ds >> streamedItem;
 
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());
 
913
    }
 
914
}
 
915
 
 
916
void tst_QStandardItem::deleteItem()
 
917
{
 
918
    QStandardItemModel model(4, 6);
 
919
    // initialize items
 
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);
 
924
        }
 
925
    }
 
926
    // delete items
 
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);
 
930
            delete item;
 
931
            QCOMPARE(model.item(i, j), static_cast<QStandardItem*>(0));
 
932
        }
 
933
    }
 
934
}
 
935
 
 
936
void tst_QStandardItem::clone()
 
937
{
 
938
    QStandardItem item;
 
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);
 
952
 
 
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));
 
968
    delete clone;
 
969
}
 
970
 
 
971
void tst_QStandardItem::sortChildren()
 
972
{
 
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);
 
986
 
 
987
        QSignalSpy layoutAboutToBeChangedSpy(
 
988
            model, SIGNAL(layoutAboutToBeChanged()));
 
989
        QSignalSpy layoutChangedSpy(
 
990
            model, SIGNAL(layoutChanged()));
 
991
 
 
992
        one->sortChildren(0, Qt::DescendingOrder);
 
993
        // verify sorted
 
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"));
 
1001
 
 
1002
        two->sortChildren(0, Qt::AscendingOrder);
 
1003
        // verify sorted
 
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"));
 
1011
 
 
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"));
 
1020
 
 
1021
        QCOMPARE(layoutAboutToBeChangedSpy.count(), (x == 0) ? 0 : 3);
 
1022
        QCOMPARE(layoutChangedSpy.count(), (x == 0) ? 0 : 3);
 
1023
 
 
1024
        if (x == 0)
 
1025
            delete item;
 
1026
        delete model;
 
1027
    }
 
1028
}
 
1029
 
 
1030
class CustomItem : public QStandardItem
 
1031
{
 
1032
public:
 
1033
    CustomItem(const QString &text) : QStandardItem(text) { }
 
1034
    CustomItem() { }
 
1035
    virtual ~CustomItem() { }
 
1036
 
 
1037
    virtual int type() const { return QStandardItem::UserType + 1; }
 
1038
 
 
1039
    virtual QStandardItem *clone() const { return QStandardItem::clone(); }
 
1040
 
 
1041
    void emitDataChanged() { QStandardItem::emitDataChanged(); }
 
1042
 
 
1043
    virtual bool operator<(const QStandardItem &other) const {
 
1044
        return text().length() < other.text().length();
 
1045
    }
 
1046
};
 
1047
 
 
1048
Q_DECLARE_METATYPE(QStandardItem*)
 
1049
 
 
1050
void tst_QStandardItem::subclassing()
 
1051
{
 
1052
    qMetaTypeId<QStandardItem*>();
 
1053
 
 
1054
    CustomItem *item = new CustomItem;
 
1055
    QCOMPARE(item->type(), int(QStandardItem::UserType + 1));
 
1056
 
 
1057
    item->setText(QString::fromLatin1("foo"));
 
1058
    QCOMPARE(item->text(), QString::fromLatin1("foo"));
 
1059
 
 
1060
    item->emitDataChanged(); // does nothing
 
1061
 
 
1062
    QStandardItemModel model;
 
1063
    model.appendRow(item);
 
1064
 
 
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);
 
1070
 
 
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);
 
1081
}
 
1082
 
 
1083
void tst_QStandardItem::lessThan()
 
1084
{
 
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));
 
1095
}
 
1096
 
 
1097
QTEST_MAIN(tst_QStandardItem)
 
1098
#include "tst_qstandarditem.moc"