|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 #include <qtreewidget.h> |
|
45 #include <qtreewidgetitemiterator.h> |
|
46 #include <qapplication.h> |
|
47 #include <qeventloop.h> |
|
48 #include <qdebug.h> |
|
49 #include <qheaderview.h> |
|
50 #include <qlineedit.h> |
|
51 #include <QScrollBar> |
|
52 #include <QStyledItemDelegate> |
|
53 |
|
54 #include "../../shared/util.h" |
|
55 |
|
56 |
|
57 //TESTED_CLASS= |
|
58 //TESTED_FILES= |
|
59 |
|
60 class CustomTreeWidget : public QTreeWidget |
|
61 { |
|
62 Q_OBJECT |
|
63 public: |
|
64 QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const |
|
65 { return QTreeWidget::indexFromItem(item, column); } |
|
66 }; |
|
67 |
|
68 class tst_QTreeWidget : public QObject |
|
69 { |
|
70 Q_OBJECT |
|
71 |
|
72 public: |
|
73 tst_QTreeWidget(); |
|
74 ~tst_QTreeWidget(); |
|
75 |
|
76 |
|
77 public slots: |
|
78 void initTestCase(); |
|
79 void cleanupTestCase(); |
|
80 void init(); |
|
81 void cleanup(); |
|
82 |
|
83 private slots: |
|
84 void getSetCheck(); |
|
85 void addTopLevelItem(); |
|
86 void currentItem_data(); |
|
87 void currentItem(); |
|
88 void editItem_data(); |
|
89 void editItem(); |
|
90 void takeItem_data(); |
|
91 void takeItem(); |
|
92 void removeChild_data(); |
|
93 void removeChild(); |
|
94 void setItemHidden(); |
|
95 void setItemHidden2(); |
|
96 void selectedItems_data(); |
|
97 void selectedItems(); |
|
98 void itemAssignment(); |
|
99 void clone_data(); |
|
100 void clone(); |
|
101 void expand_data(); |
|
102 void expand(); |
|
103 void checkState_data(); |
|
104 void checkState(); |
|
105 void findItems_data(); |
|
106 void findItems(); |
|
107 void findItemsInColumn(); |
|
108 void sortItems_data(); |
|
109 void sortItems(); |
|
110 void deleteItems_data(); |
|
111 void deleteItems(); |
|
112 void itemAboveOrBelow(); |
|
113 void itemStreaming_data(); |
|
114 void itemStreaming(); |
|
115 void insertTopLevelItems_data(); |
|
116 void insertTopLevelItems(); |
|
117 void keyboardNavigation(); |
|
118 void scrollToItem(); |
|
119 void setSortingEnabled(); |
|
120 void match(); |
|
121 void columnCount(); |
|
122 void setHeaderLabels(); |
|
123 void setHeaderItem(); |
|
124 void itemWidget_data(); |
|
125 void itemWidget(); |
|
126 void insertItemsWithSorting_data(); |
|
127 void insertItemsWithSorting(); |
|
128 void insertExpandedItemsWithSorting_data(); |
|
129 void insertExpandedItemsWithSorting(); |
|
130 void changeDataWithSorting_data(); |
|
131 void changeDataWithSorting(); |
|
132 void changeDataWithStableSorting_data(); |
|
133 void changeDataWithStableSorting(); |
|
134 |
|
135 void sortedIndexOfChild_data(); |
|
136 void sortedIndexOfChild(); |
|
137 void defaultRowSizes(); |
|
138 |
|
139 void task191552_rtl(); |
|
140 void task203673_selection(); |
|
141 void rootItemFlags(); |
|
142 void task218661_setHeaderData(); |
|
143 void task245280_sortChildren(); |
|
144 void task253109_itemHeight(); |
|
145 |
|
146 // QTreeWidgetItem |
|
147 void itemOperatorLessThan(); |
|
148 void addChild(); |
|
149 void setData(); |
|
150 void enableDisable(); |
|
151 |
|
152 void expandAndCallapse(); |
|
153 void itemData(); |
|
154 void setDisabled(); |
|
155 void removeSelectedItem(); |
|
156 void removeCurrentItem(); |
|
157 void removeCurrentItem_task186451(); |
|
158 void randomExpand(); |
|
159 void crashTest(); |
|
160 void sortAndSelect(); |
|
161 |
|
162 void task206367_duplication(); |
|
163 void selectionOrder(); |
|
164 |
|
165 void setSelectionModel(); |
|
166 void task217309(); |
|
167 void setCurrentItemExpandsParent(); |
|
168 void task239150_editorWidth(); |
|
169 void setTextUpdate(); |
|
170 |
|
171 public slots: |
|
172 void itemSelectionChanged(); |
|
173 void emitDataChanged(); |
|
174 |
|
175 private: |
|
176 CustomTreeWidget *testWidget; |
|
177 }; |
|
178 |
|
179 // Testing get/set functions |
|
180 void tst_QTreeWidget::getSetCheck() |
|
181 { |
|
182 QTreeWidget obj1; |
|
183 // int QTreeWidget::columnCount() |
|
184 // void QTreeWidget::setColumnCount(int) |
|
185 obj1.setColumnCount(0); |
|
186 QCOMPARE(obj1.columnCount(), 0); |
|
187 |
|
188 obj1.setColumnCount(INT_MIN); |
|
189 QCOMPARE(obj1.columnCount(), 0); |
|
190 |
|
191 //obj1.setColumnCount(INT_MAX); |
|
192 //QCOMPARE(obj1.columnCount(), INT_MAX); |
|
193 // Since setColumnCount allocates memory, there is no way this will succeed |
|
194 |
|
195 obj1.setColumnCount(100); |
|
196 QCOMPARE(obj1.columnCount(), 100); |
|
197 |
|
198 // QTreeWidgetItem * QTreeWidget::headerItem() |
|
199 // void QTreeWidget::setHeaderItem(QTreeWidgetItem *) |
|
200 QTreeWidgetItem *var2 = new QTreeWidgetItem(); |
|
201 obj1.setHeaderItem(var2); |
|
202 QCOMPARE(obj1.headerItem(), var2); |
|
203 |
|
204 obj1.setHeaderItem((QTreeWidgetItem *)0); |
|
205 // QCOMPARE(obj1.headerItem(), (QTreeWidgetItem *)0); |
|
206 |
|
207 // QTreeWidgetItem * QTreeWidget::currentItem() |
|
208 // void QTreeWidget::setCurrentItem(QTreeWidgetItem *) |
|
209 QTreeWidgetItem *var3 = new QTreeWidgetItem(&obj1); |
|
210 obj1.setCurrentItem(var3); |
|
211 QCOMPARE(obj1.currentItem(), var3); |
|
212 |
|
213 obj1.setCurrentItem((QTreeWidgetItem *)0); |
|
214 QCOMPARE(obj1.currentItem(), (QTreeWidgetItem *)0); |
|
215 } |
|
216 |
|
217 typedef QList<int> IntList; |
|
218 typedef QList<IntList> ListIntList; |
|
219 |
|
220 Q_DECLARE_METATYPE(IntList) |
|
221 Q_DECLARE_METATYPE(ListIntList) |
|
222 Q_DECLARE_METATYPE(QModelIndex) |
|
223 Q_DECLARE_METATYPE(Qt::Orientation) |
|
224 |
|
225 typedef QTreeWidgetItem TreeItem; |
|
226 typedef QList<TreeItem*> TreeItemList; |
|
227 |
|
228 Q_DECLARE_METATYPE(QTreeWidgetItem*) |
|
229 Q_DECLARE_METATYPE(TreeItemList) |
|
230 |
|
231 tst_QTreeWidget::tst_QTreeWidget(): testWidget(0) |
|
232 { |
|
233 } |
|
234 |
|
235 tst_QTreeWidget::~tst_QTreeWidget() |
|
236 { |
|
237 } |
|
238 |
|
239 void tst_QTreeWidget::initTestCase() |
|
240 { |
|
241 qMetaTypeId<QModelIndex>(); |
|
242 qMetaTypeId<Qt::Orientation>(); |
|
243 qRegisterMetaType<QTreeWidgetItem*>("QTreeWidgetItem*"); |
|
244 |
|
245 testWidget = new CustomTreeWidget(); |
|
246 testWidget->show(); |
|
247 #ifdef Q_WS_X11 |
|
248 qt_x11_wait_for_window_manager(testWidget); |
|
249 #endif |
|
250 } |
|
251 |
|
252 void tst_QTreeWidget::cleanupTestCase() |
|
253 { |
|
254 testWidget->hide(); |
|
255 delete testWidget; |
|
256 } |
|
257 |
|
258 void tst_QTreeWidget::init() |
|
259 { |
|
260 testWidget->clear(); |
|
261 testWidget->setColumnCount(2); |
|
262 } |
|
263 |
|
264 void tst_QTreeWidget::cleanup() |
|
265 { |
|
266 } |
|
267 |
|
268 TreeItem *operator<<(TreeItem *parent, const TreeItemList &children) { |
|
269 for (int i = 0; i < children.count(); ++i) |
|
270 parent->addChild(children.at(i)); |
|
271 return parent; |
|
272 } |
|
273 |
|
274 static void populate(QTreeWidget *widget, const TreeItemList &topLevelItems, |
|
275 TreeItem *headerItem = 0) |
|
276 { |
|
277 widget->clear(); |
|
278 widget->setHeaderItem(headerItem); |
|
279 foreach (TreeItem *item, topLevelItems) |
|
280 widget->addTopLevelItem(item); |
|
281 } |
|
282 |
|
283 void tst_QTreeWidget::addTopLevelItem() |
|
284 { |
|
285 QTreeWidget tree; |
|
286 QCOMPARE(tree.topLevelItemCount(), 0); |
|
287 |
|
288 // try to add 0 |
|
289 tree.addTopLevelItem(0); |
|
290 QCOMPARE(tree.topLevelItemCount(), 0); |
|
291 QCOMPARE(tree.indexOfTopLevelItem(0), -1); |
|
292 |
|
293 // add one at a time |
|
294 QList<TreeItem*> tops; |
|
295 for (int i = 0; i < 10; ++i) { |
|
296 TreeItem *ti = new TreeItem(); |
|
297 QCOMPARE(tree.indexOfTopLevelItem(ti), -1); |
|
298 tree.addTopLevelItem(ti); |
|
299 QCOMPARE(tree.topLevelItemCount(), i+1); |
|
300 QCOMPARE(tree.topLevelItem(i), ti); |
|
301 QCOMPARE(tree.topLevelItem(-1), static_cast<TreeItem*>(0)); |
|
302 QCOMPARE(tree.indexOfTopLevelItem(ti), i); |
|
303 QCOMPARE(ti->parent(), static_cast<TreeItem*>(0)); |
|
304 tree.addTopLevelItem(ti); |
|
305 QCOMPARE(tree.topLevelItemCount(), i+1); |
|
306 tops.append(ti); |
|
307 } |
|
308 |
|
309 // delete one at a time |
|
310 while (!tops.isEmpty()) { |
|
311 TreeItem *ti = tops.takeFirst(); |
|
312 delete ti; |
|
313 QCOMPARE(tree.topLevelItemCount(), tops.count()); |
|
314 for (int i = 0; i < tops.count(); ++i) |
|
315 QCOMPARE(tree.topLevelItem(i), tops.at(i)); |
|
316 } |
|
317 |
|
318 // add none |
|
319 { |
|
320 int count = tree.topLevelItemCount(); |
|
321 tree.addTopLevelItems(tops); |
|
322 QCOMPARE(tree.topLevelItemCount(), count); |
|
323 } |
|
324 |
|
325 // add many at a time |
|
326 { |
|
327 const int count = 10; |
|
328 for (int i = 0; i < 100; i += count) { |
|
329 tops.clear(); |
|
330 for (int j = 0; j < count; ++j) |
|
331 tops << new TreeItem(QStringList() << QString("%0").arg(j)); |
|
332 tree.addTopLevelItems(tops); |
|
333 QCOMPARE(tree.topLevelItemCount(), count + i); |
|
334 for (int j = 0; j < count; ++j) |
|
335 QCOMPARE(tree.topLevelItem(i+j), tops.at(j)); |
|
336 |
|
337 tree.addTopLevelItems(tops); |
|
338 QCOMPARE(tree.topLevelItemCount(), count + i); |
|
339 } |
|
340 } |
|
341 |
|
342 // insert |
|
343 { |
|
344 tops.clear(); |
|
345 for (int i = 0; i < 10; ++i) |
|
346 tops << new TreeItem(); |
|
347 int count = tree.topLevelItemCount(); |
|
348 tree.insertTopLevelItems(100000, tops); |
|
349 // ### fixme |
|
350 QCOMPARE(tree.topLevelItemCount(), count + 10); |
|
351 } |
|
352 } |
|
353 |
|
354 void tst_QTreeWidget::currentItem_data() |
|
355 { |
|
356 QTest::addColumn<TreeItemList>("topLevelItems"); |
|
357 |
|
358 QTest::newRow("only top-level items, 2 columns") |
|
359 << (TreeItemList() |
|
360 << new TreeItem(QStringList() << "a" << "b") |
|
361 << new TreeItem(QStringList() << "c" << "d")); |
|
362 TreeItemList lst; |
|
363 lst << (new TreeItem(QStringList() << "a" << "b") |
|
364 << (TreeItemList() |
|
365 << new TreeItem(QStringList() << "c" << "d") |
|
366 << new TreeItem(QStringList() << "c" << "d") |
|
367 ) |
|
368 ) |
|
369 << (new TreeItem(QStringList() << "e" << "f") |
|
370 << (TreeItemList() |
|
371 << new TreeItem(QStringList() << "g" << "h") |
|
372 << new TreeItem(QStringList() << "g" << "h") |
|
373 ) |
|
374 ); |
|
375 QTest::newRow("hierarchy, 2 columns") << lst; |
|
376 } |
|
377 |
|
378 void tst_QTreeWidget::currentItem() |
|
379 { |
|
380 QFETCH(TreeItemList, topLevelItems); |
|
381 |
|
382 QTreeWidget tree; |
|
383 tree.show(); |
|
384 populate(&tree, topLevelItems, new TreeItem(QStringList() << "1" << "2")); |
|
385 QTreeWidgetItem *previous = 0; |
|
386 for (int x = 0; x < 2; ++x) { |
|
387 tree.setSelectionBehavior(x ? QAbstractItemView::SelectItems |
|
388 : QAbstractItemView::SelectRows); |
|
389 QSignalSpy currentItemChangedSpy( |
|
390 &tree, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*))); |
|
391 QSignalSpy itemSelectionChangedSpy( |
|
392 &tree, SIGNAL(itemSelectionChanged())); |
|
393 |
|
394 QTreeWidgetItemIterator it(&tree); |
|
395 // do all items |
|
396 while (QTreeWidgetItem *item = (*it++)) { |
|
397 tree.setCurrentItem(item); |
|
398 QCOMPARE(tree.currentItem(), item); |
|
399 |
|
400 QCOMPARE(currentItemChangedSpy.count(), 1); |
|
401 QVariantList args = currentItemChangedSpy.takeFirst(); |
|
402 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
403 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(1)), previous); |
|
404 |
|
405 QCOMPARE(itemSelectionChangedSpy.count(), 1); |
|
406 itemSelectionChangedSpy.clear(); |
|
407 |
|
408 previous = item; |
|
409 // do all columns |
|
410 for (int col = 0; col < item->columnCount(); ++col) { |
|
411 tree.setCurrentItem(item, col); |
|
412 QCOMPARE(tree.currentItem(), item); |
|
413 QCOMPARE(tree.currentColumn(), col); |
|
414 |
|
415 if (!currentItemChangedSpy.isEmpty()) { |
|
416 // ### we get a currentItemChanged() when what really |
|
417 // changed was just currentColumn(). Should it be like this? |
|
418 QCOMPARE(currentItemChangedSpy.count(), 1); |
|
419 QVariantList args = currentItemChangedSpy.takeFirst(); |
|
420 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
421 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(1)), item); |
|
422 if (tree.selectionBehavior() == QAbstractItemView::SelectItems) { |
|
423 QCOMPARE(itemSelectionChangedSpy.count(), 1); |
|
424 itemSelectionChangedSpy.clear(); |
|
425 } else { |
|
426 QCOMPARE(itemSelectionChangedSpy.count(), 0); |
|
427 } |
|
428 } |
|
429 } |
|
430 } |
|
431 } |
|
432 |
|
433 // can't set the headerItem to be the current item |
|
434 tree.setCurrentItem(tree.headerItem()); |
|
435 QCOMPARE(tree.currentItem(), static_cast<TreeItem*>(0)); |
|
436 } |
|
437 |
|
438 void tst_QTreeWidget::editItem_data() |
|
439 { |
|
440 QTest::addColumn<TreeItemList>("topLevelItems"); |
|
441 |
|
442 { |
|
443 TreeItemList list; |
|
444 for (int i = 0; i < 10; i++) { |
|
445 TreeItem *item = new TreeItem(QStringList() << "col1" << "col2"); |
|
446 if ((i & 1) == 0) |
|
447 item->setFlags(item->flags() | Qt::ItemIsEditable); |
|
448 else |
|
449 item->setFlags(item->flags() & ~Qt::ItemIsEditable); |
|
450 list << item; |
|
451 } |
|
452 QTest::newRow("2 columns, only even items editable") |
|
453 << list; |
|
454 } |
|
455 } |
|
456 |
|
457 void tst_QTreeWidget::editItem() |
|
458 { |
|
459 QFETCH(TreeItemList, topLevelItems); |
|
460 |
|
461 QTreeWidget tree; |
|
462 populate(&tree, topLevelItems, new TreeItem(QStringList() << "1" << "2")); |
|
463 tree.show(); |
|
464 |
|
465 QSignalSpy itemChangedSpy( |
|
466 &tree, SIGNAL(itemChanged(QTreeWidgetItem*,int))); |
|
467 |
|
468 QTreeWidgetItemIterator it(&tree); |
|
469 while (QTreeWidgetItem *item = (*it++)) { |
|
470 for (int col = 0; col < item->columnCount(); ++col) { |
|
471 if (!(item->flags() & Qt::ItemIsEditable)) |
|
472 QTest::ignoreMessage(QtWarningMsg, "edit: editing failed"); |
|
473 tree.editItem(item, col); |
|
474 QApplication::instance()->processEvents(); |
|
475 QApplication::instance()->processEvents(); |
|
476 QLineEdit *editor = qFindChild<QLineEdit*>(&tree); |
|
477 if (editor) { |
|
478 QVERIFY(item->flags() & Qt::ItemIsEditable); |
|
479 QCOMPARE(editor->selectedText(), editor->text()); |
|
480 QTest::keyClick(editor, Qt::Key_A); |
|
481 QTest::keyClick(editor, Qt::Key_Enter); |
|
482 QApplication::instance()->processEvents(); |
|
483 QCOMPARE(itemChangedSpy.count(), 1); |
|
484 QVariantList args = itemChangedSpy.takeFirst(); |
|
485 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
486 QCOMPARE(qvariant_cast<int>(args.at(1)), col); |
|
487 } else { |
|
488 QVERIFY(!(item->flags() & Qt::ItemIsEditable)); |
|
489 } |
|
490 } |
|
491 } |
|
492 } |
|
493 |
|
494 void tst_QTreeWidget::takeItem_data() |
|
495 { |
|
496 QTest::addColumn<int>("index"); |
|
497 QTest::addColumn<bool>("topLevel"); |
|
498 QTest::addColumn<bool>("outOfBounds"); |
|
499 |
|
500 QTest::newRow("First, topLevel") << 0 << true << false; |
|
501 QTest::newRow("Last, topLevel") << 2 << true << false; |
|
502 QTest::newRow("Middle, topLevel") << 1 << true << false; |
|
503 QTest::newRow("Out of bounds, toplevel, (index: -1)") << -1 << true << true; |
|
504 QTest::newRow("Out of bounds, toplevel, (index: 3)") << 3 << true << true; |
|
505 |
|
506 QTest::newRow("First, child of topLevel") << 0 << false << false; |
|
507 QTest::newRow("Last, child of topLevel") << 2 << false << false; |
|
508 QTest::newRow("Middle, child of topLevel") << 1 << false << false; |
|
509 QTest::newRow("Out of bounds, child of toplevel, (index: -1)") << -1 << false << true; |
|
510 QTest::newRow("Out of bounds, child of toplevel, (index: 3)") << 3 << false << true; |
|
511 } |
|
512 |
|
513 void tst_QTreeWidget::takeItem() |
|
514 { |
|
515 QFETCH(int, index); |
|
516 QFETCH(bool, topLevel); |
|
517 QFETCH(bool, outOfBounds); |
|
518 |
|
519 for (int i=0; i<3; ++i) { |
|
520 QTreeWidgetItem *top = new QTreeWidgetItem(testWidget); |
|
521 top->setText(0, QString("top%1").arg(i)); |
|
522 for (int j=0; j<3; ++j) { |
|
523 QTreeWidgetItem *child = new QTreeWidgetItem(top); |
|
524 child->setText(0, QString("child%1").arg(j)); |
|
525 } |
|
526 } |
|
527 |
|
528 QCOMPARE(testWidget->topLevelItemCount(), 3); |
|
529 QCOMPARE(testWidget->topLevelItem(0)->childCount(), 3); |
|
530 |
|
531 if (topLevel) { |
|
532 int count = testWidget->topLevelItemCount(); |
|
533 QTreeWidgetItem *item = testWidget->takeTopLevelItem(index); |
|
534 if (outOfBounds) { |
|
535 QCOMPARE(item, (QTreeWidgetItem *)0); |
|
536 QCOMPARE(count, testWidget->topLevelItemCount()); |
|
537 } else { |
|
538 QCOMPARE(item->text(0), QString("top%1").arg(index)); |
|
539 QCOMPARE(count-1, testWidget->topLevelItemCount()); |
|
540 delete item; |
|
541 } |
|
542 } else { |
|
543 int count = testWidget->topLevelItem(0)->childCount(); |
|
544 QTreeWidgetItem *item = testWidget->topLevelItem(0)->takeChild(index); |
|
545 if (outOfBounds) { |
|
546 QCOMPARE(item, (QTreeWidgetItem *)0); |
|
547 QCOMPARE(count, testWidget->topLevelItem(0)->childCount()); |
|
548 } else { |
|
549 QCOMPARE(item->text(0), QString("child%1").arg(index)); |
|
550 QCOMPARE(count-1, testWidget->topLevelItem(0)->childCount()); |
|
551 delete item; |
|
552 } |
|
553 } |
|
554 } |
|
555 |
|
556 void tst_QTreeWidget::removeChild_data() |
|
557 { |
|
558 QTest::addColumn<int>("childCount"); |
|
559 QTest::addColumn<int>("removeAt"); |
|
560 |
|
561 QTest::newRow("10 remove 3") << 10 << 3; |
|
562 } |
|
563 |
|
564 void tst_QTreeWidget::removeChild() |
|
565 { |
|
566 QFETCH(int, childCount); |
|
567 QFETCH(int, removeAt); |
|
568 |
|
569 QTreeWidgetItem *root = new QTreeWidgetItem; |
|
570 for (int i = 0; i < childCount; ++i) |
|
571 new QTreeWidgetItem(root, QStringList(QString::number(i))); |
|
572 |
|
573 QCOMPARE(root->childCount(), childCount); |
|
574 for (int j = 0; j < childCount; ++j) |
|
575 QCOMPARE(root->child(j)->text(0), QString::number(j)); |
|
576 |
|
577 QTreeWidgetItem *remove = root->child(removeAt); |
|
578 root->removeChild(remove); |
|
579 |
|
580 QCOMPARE(root->childCount(), childCount - 1); |
|
581 for (int k = 0; k < childCount; ++k) { |
|
582 if (k == removeAt) |
|
583 QCOMPARE(remove->text(0), QString::number(k)); |
|
584 else if (k < removeAt) |
|
585 QCOMPARE(root->child(k)->text(0), QString::number(k)); |
|
586 else if (k > removeAt) |
|
587 QCOMPARE(root->child(k - 1)->text(0), QString::number(k)); |
|
588 } |
|
589 delete root; |
|
590 } |
|
591 |
|
592 void tst_QTreeWidget::setItemHidden() |
|
593 { |
|
594 QTreeWidgetItem *parent = new QTreeWidgetItem(testWidget); |
|
595 parent->setText(0, "parent"); |
|
596 QTreeWidgetItem *child = new QTreeWidgetItem(parent); |
|
597 child->setText(0, "child"); |
|
598 QVERIFY(child->parent()); |
|
599 |
|
600 testWidget->expandItem(parent); |
|
601 testWidget->scrollToItem(child); |
|
602 |
|
603 QVERIFY(testWidget->visualItemRect(parent).isValid() |
|
604 && testWidget->viewport()->rect().contains(testWidget->visualItemRect(parent))); |
|
605 QVERIFY(testWidget->visualItemRect(child).isValid() |
|
606 && testWidget->viewport()->rect().contains(testWidget->visualItemRect(child))); |
|
607 |
|
608 QVERIFY(!testWidget->isItemHidden(parent)); |
|
609 QVERIFY(!testWidget->isItemHidden(child)); |
|
610 |
|
611 testWidget->setItemHidden(parent, true); |
|
612 |
|
613 QVERIFY(!(testWidget->visualItemRect(parent).isValid() |
|
614 && testWidget->viewport()->rect().contains(testWidget->visualItemRect(parent)))); |
|
615 QVERIFY(!(testWidget->visualItemRect(child).isValid() |
|
616 && testWidget->viewport()->rect().contains(testWidget->visualItemRect(child)))); |
|
617 |
|
618 QVERIFY(testWidget->isItemHidden(parent)); |
|
619 QVERIFY(!testWidget->isItemHidden(child)); |
|
620 |
|
621 // From task 78670 (This caused an core dump) |
|
622 // Check if we can set an item visible if it already is visible. |
|
623 testWidget->setItemHidden(parent, false); |
|
624 testWidget->setItemHidden(parent, false); |
|
625 QVERIFY(!testWidget->isItemHidden(parent)); |
|
626 |
|
627 |
|
628 // hide, hide and then unhide. |
|
629 testWidget->setItemHidden(parent, true); |
|
630 testWidget->setItemHidden(parent, true); |
|
631 testWidget->setItemHidden(parent, false); |
|
632 QVERIFY(!testWidget->isItemHidden(parent)); |
|
633 |
|
634 |
|
635 } |
|
636 |
|
637 |
|
638 void tst_QTreeWidget::setItemHidden2() |
|
639 { |
|
640 // From Task 78587 |
|
641 QStringList hl; |
|
642 hl << "ID" << "Desc"; |
|
643 testWidget->setColumnCount(hl.count()); |
|
644 testWidget->setHeaderLabels(hl); |
|
645 testWidget->setSortingEnabled(true); |
|
646 |
|
647 QTreeWidgetItem *top = new QTreeWidgetItem(testWidget); |
|
648 QTreeWidgetItem *leaf = 0; |
|
649 top->setText(0, "ItemList"); |
|
650 for (int i = 1; i <= 4; i++) { |
|
651 leaf = new QTreeWidgetItem(top); |
|
652 leaf->setText(0, QString().sprintf("%d", i)); |
|
653 leaf->setText(1, QString().sprintf("Item %d", i)); |
|
654 } |
|
655 |
|
656 if (testWidget->topLevelItemCount() > 0) { |
|
657 top = testWidget->topLevelItem(0); |
|
658 testWidget->setItemExpanded(top, true); |
|
659 } |
|
660 |
|
661 if (testWidget->topLevelItemCount() > 0) { |
|
662 top = testWidget->topLevelItem(0); |
|
663 for (int i = 0; i < top->childCount(); i++) { |
|
664 leaf = top->child(i); |
|
665 if (leaf->text(0).toInt() % 2 == 0) { |
|
666 if (!testWidget->isItemHidden(leaf)) { |
|
667 testWidget->setItemHidden(leaf, true); |
|
668 } |
|
669 } |
|
670 } |
|
671 } |
|
672 } |
|
673 |
|
674 |
|
675 void tst_QTreeWidget::selectedItems_data() |
|
676 { |
|
677 QTest::addColumn<int>("topLevel"); |
|
678 QTest::addColumn<int>("children"); |
|
679 QTest::addColumn<bool>("closeTopLevel"); |
|
680 QTest::addColumn<ListIntList>("selectedItems"); |
|
681 QTest::addColumn<ListIntList>("hiddenItems"); |
|
682 QTest::addColumn<ListIntList>("expectedItems"); |
|
683 |
|
684 ListIntList selectedItems; |
|
685 ListIntList hiddenItems; |
|
686 ListIntList expectedItems; |
|
687 |
|
688 selectedItems.clear(); hiddenItems.clear(); expectedItems.clear(); |
|
689 selectedItems |
|
690 << (IntList() |
|
691 << 0); |
|
692 expectedItems |
|
693 << (IntList() << 0); |
|
694 QTest::newRow("2 top with 2 children, closed, top0 selected, no hidden") |
|
695 << 2 << 2 << true << selectedItems << hiddenItems << expectedItems; |
|
696 |
|
697 selectedItems.clear(); hiddenItems.clear(); expectedItems.clear(); |
|
698 selectedItems |
|
699 << (IntList() |
|
700 << 0 << 0); |
|
701 expectedItems |
|
702 << (IntList() << 0 << 0); |
|
703 QTest::newRow("2 top with 2 children, closed, top0child0 selected, no hidden") |
|
704 << 2 << 2 << true << selectedItems << hiddenItems << expectedItems; |
|
705 |
|
706 selectedItems.clear(); hiddenItems.clear(); expectedItems.clear(); |
|
707 selectedItems |
|
708 << (IntList() |
|
709 << 0 << 0); |
|
710 expectedItems |
|
711 << (IntList() |
|
712 << 0 << 0); |
|
713 QTest::newRow("2 top with 2 children, open, top0child0 selected, no hidden") |
|
714 << 2 << 2 << false << selectedItems << hiddenItems << expectedItems; |
|
715 |
|
716 selectedItems.clear(); hiddenItems.clear(); expectedItems.clear(); |
|
717 selectedItems << (IntList() << 0); |
|
718 hiddenItems << (IntList() << 0); |
|
719 QTest::newRow("2 top with 2 children, closed, top0 selected, top0 hidden") |
|
720 << 2 << 2 << true << selectedItems << hiddenItems << expectedItems; |
|
721 |
|
722 selectedItems.clear(); hiddenItems.clear(); expectedItems.clear(); |
|
723 selectedItems << (IntList() << 0 << 0); |
|
724 hiddenItems << (IntList() << 0); |
|
725 expectedItems << (IntList() << 0 << 0); |
|
726 QTest::newRow("2 top with 2 children, closed, top0child0 selected, top0 hidden") |
|
727 << 2 << 2 << true << selectedItems << hiddenItems << expectedItems; |
|
728 |
|
729 selectedItems.clear(); hiddenItems.clear(); expectedItems.clear(); |
|
730 selectedItems |
|
731 << (IntList() << 0) |
|
732 << (IntList() << 0 << 0) |
|
733 << (IntList() << 0 << 1) |
|
734 << (IntList() << 1) |
|
735 << (IntList() << 1 << 0) |
|
736 << (IntList() << 1 << 1); |
|
737 expectedItems |
|
738 << (IntList() << 0) |
|
739 << (IntList() << 0 << 0) |
|
740 << (IntList() << 0 << 1) |
|
741 << (IntList() << 1) |
|
742 << (IntList() << 1 << 0) |
|
743 << (IntList() << 1 << 1); |
|
744 QTest::newRow("2 top with 2 children, closed, all selected, no hidden") |
|
745 << 2 << 2 << true << selectedItems << hiddenItems << expectedItems; |
|
746 |
|
747 |
|
748 selectedItems.clear(); hiddenItems.clear(); expectedItems.clear(); |
|
749 selectedItems |
|
750 << (IntList() << 0) |
|
751 << (IntList() << 0 << 0) |
|
752 << (IntList() << 0 << 1) |
|
753 << (IntList() << 1) |
|
754 << (IntList() << 1 << 0) |
|
755 << (IntList() << 1 << 1); |
|
756 hiddenItems |
|
757 << (IntList() << 0); |
|
758 expectedItems |
|
759 //<< (IntList() << 0) |
|
760 << (IntList() << 0 << 0) |
|
761 << (IntList() << 0 << 1) |
|
762 << (IntList() << 1) |
|
763 << (IntList() << 1 << 0) |
|
764 << (IntList() << 1 << 1); |
|
765 QTest::newRow("2 top with 2 children, closed, all selected, top0 hidden") |
|
766 << 2 << 2 << true << selectedItems << hiddenItems << expectedItems; |
|
767 |
|
768 selectedItems.clear(); hiddenItems.clear(); expectedItems.clear(); |
|
769 selectedItems |
|
770 << (IntList() << 0) |
|
771 << (IntList() << 0 << 0) |
|
772 << (IntList() << 0 << 1) |
|
773 << (IntList() << 1) |
|
774 << (IntList() << 1 << 0) |
|
775 << (IntList() << 1 << 1); |
|
776 hiddenItems |
|
777 << (IntList() << 0 << 1) |
|
778 << (IntList() << 1); |
|
779 expectedItems |
|
780 << (IntList() << 0) |
|
781 << (IntList() << 0 << 0) |
|
782 //<< (IntList() << 0 << 1) |
|
783 //<< (IntList() << 1) |
|
784 << (IntList() << 1 << 0) |
|
785 << (IntList() << 1 << 1); |
|
786 |
|
787 QTest::newRow("2 top with 2 children, closed, all selected, top0child1 and top1") |
|
788 << 2 << 2 << true << selectedItems << hiddenItems << expectedItems; |
|
789 |
|
790 } |
|
791 |
|
792 void tst_QTreeWidget::selectedItems() |
|
793 { |
|
794 QFETCH(int, topLevel); |
|
795 QFETCH(int, children); |
|
796 QFETCH(bool, closeTopLevel); |
|
797 QFETCH(ListIntList, selectedItems); |
|
798 QFETCH(ListIntList, hiddenItems); |
|
799 QFETCH(ListIntList, expectedItems); |
|
800 |
|
801 // create items |
|
802 for (int t=0; t<topLevel; ++t) { |
|
803 QTreeWidgetItem *top = new QTreeWidgetItem(testWidget); |
|
804 top->setText(0, QString("top%1").arg(t)); |
|
805 for (int c=0; c<children; ++c) { |
|
806 QTreeWidgetItem *child = new QTreeWidgetItem(top); |
|
807 child->setText(0, QString("top%1child%2").arg(t).arg(c)); |
|
808 } |
|
809 } |
|
810 |
|
811 // set selected |
|
812 foreach (IntList itemPath, selectedItems) { |
|
813 QTreeWidgetItem *item = 0; |
|
814 foreach(int index, itemPath) { |
|
815 if (!item) |
|
816 item = testWidget->topLevelItem(index); |
|
817 else |
|
818 item = item->child(index); |
|
819 } |
|
820 testWidget->setItemSelected(item, true); |
|
821 } |
|
822 |
|
823 // hide rows |
|
824 foreach (IntList itemPath, hiddenItems) { |
|
825 QTreeWidgetItem *item = 0; |
|
826 foreach(int index, itemPath) { |
|
827 if (!item) |
|
828 item = testWidget->topLevelItem(index); |
|
829 else |
|
830 item = item->child(index); |
|
831 } |
|
832 testWidget->setItemHidden(item, true); |
|
833 } |
|
834 |
|
835 // open/close toplevel |
|
836 for (int i=0; i<testWidget->topLevelItemCount(); ++i) { |
|
837 if (closeTopLevel) |
|
838 testWidget->collapseItem(testWidget->topLevelItem(i)); |
|
839 else |
|
840 testWidget->expandItem(testWidget->topLevelItem(i)); |
|
841 } |
|
842 |
|
843 // check selectedItems |
|
844 QList<QTreeWidgetItem*> sel = testWidget->selectedItems(); |
|
845 QCOMPARE(sel.count(), expectedItems.count()); |
|
846 foreach (IntList itemPath, expectedItems) { |
|
847 QTreeWidgetItem *item = 0; |
|
848 foreach(int index, itemPath) { |
|
849 if (!item) |
|
850 item = testWidget->topLevelItem(index); |
|
851 else |
|
852 item = item->child(index); |
|
853 } |
|
854 if (item) |
|
855 QVERIFY(sel.contains(item)); |
|
856 } |
|
857 |
|
858 // compare isSelected |
|
859 for (int t=0; t<testWidget->topLevelItemCount(); ++t) { |
|
860 QTreeWidgetItem *top = testWidget->topLevelItem(t); |
|
861 if (testWidget->isItemSelected(top) && !testWidget->isItemHidden(top)) |
|
862 QVERIFY(sel.contains(top)); |
|
863 for (int c=0; c<top->childCount(); ++c) { |
|
864 QTreeWidgetItem *child = top->child(c); |
|
865 if (testWidget->isItemSelected(child) && !testWidget->isItemHidden(child)) |
|
866 QVERIFY(sel.contains(child)); |
|
867 } |
|
868 } |
|
869 |
|
870 // Possible to select null without crashing? |
|
871 testWidget->setItemSelected(0, true); |
|
872 QVERIFY(!testWidget->isItemSelected(0)); |
|
873 |
|
874 // unselect |
|
875 foreach (IntList itemPath, selectedItems) { |
|
876 QTreeWidgetItem *item = 0; |
|
877 foreach(int index, itemPath) { |
|
878 if (!item) |
|
879 item = testWidget->topLevelItem(index); |
|
880 else |
|
881 item = item->child(index); |
|
882 } |
|
883 testWidget->setItemSelected(item, false); |
|
884 } |
|
885 QCOMPARE(testWidget->selectedItems().count(), 0); |
|
886 } |
|
887 |
|
888 void tst_QTreeWidget::itemAssignment() |
|
889 { |
|
890 // create item with children and parent but not insert in the view |
|
891 QTreeWidgetItem grandParent; |
|
892 QTreeWidgetItem *parent = new QTreeWidgetItem(&grandParent); |
|
893 parent->setText(0, "foo"); |
|
894 parent->setText(1, "bar"); |
|
895 for (int i=0; i<5; ++i) { |
|
896 QTreeWidgetItem *child = new QTreeWidgetItem(parent); |
|
897 child->setText(0, "bingo"); |
|
898 child->setText(1, "bango"); |
|
899 } |
|
900 QCOMPARE(parent->parent(), &grandParent); |
|
901 QVERIFY(!parent->treeWidget()); |
|
902 QCOMPARE(parent->columnCount(), 2); |
|
903 QCOMPARE(parent->text(0), QString("foo")); |
|
904 QCOMPARE(parent->childCount(), 5); |
|
905 QCOMPARE(parent->child(0)->parent(), parent); |
|
906 |
|
907 // create item which is inserted in the widget |
|
908 QTreeWidgetItem item(testWidget); |
|
909 item.setText(0, "baz"); |
|
910 QVERIFY(!item.parent()); |
|
911 QCOMPARE(item.treeWidget(), static_cast<QTreeWidget *>(testWidget)); |
|
912 QCOMPARE(item.columnCount(), 1); |
|
913 QCOMPARE(item.text(0), QString("baz")); |
|
914 QCOMPARE(item.childCount(), 0); |
|
915 |
|
916 // assign and test |
|
917 *parent = item; |
|
918 QCOMPARE(parent->parent(), &grandParent); |
|
919 QVERIFY(!parent->treeWidget()); |
|
920 QCOMPARE(parent->columnCount(), 1); |
|
921 QCOMPARE(parent->text(0), QString("baz")); |
|
922 QCOMPARE(parent->childCount(), 5); |
|
923 QCOMPARE(parent->child(0)->parent(), parent); |
|
924 } |
|
925 |
|
926 void tst_QTreeWidget::clone_data() |
|
927 { |
|
928 QTest::addColumn<int>("column"); |
|
929 QTest::addColumn<int>("topLevelIndex"); |
|
930 QTest::addColumn<int>("childIndex"); |
|
931 QTest::addColumn<QStringList>("topLevelText"); |
|
932 QTest::addColumn<QStringList>("childText"); |
|
933 QTest::addColumn<bool>("cloneChild"); |
|
934 |
|
935 QTest::newRow("clone parent with child") << 0 << 0 << 0 |
|
936 << (QStringList() << "some text") |
|
937 << (QStringList() << "more text") |
|
938 << false; |
|
939 |
|
940 QTest::newRow("clone child") << 0 << 0 << 0 |
|
941 << (QStringList() << "some text") |
|
942 << (QStringList() << "more text") |
|
943 << true; |
|
944 } |
|
945 |
|
946 void tst_QTreeWidget::clone() |
|
947 { |
|
948 QFETCH(int, column); |
|
949 QFETCH(int, topLevelIndex); |
|
950 QFETCH(int, childIndex); |
|
951 QFETCH(QStringList, topLevelText); |
|
952 QFETCH(QStringList, childText); |
|
953 QFETCH(bool, cloneChild); |
|
954 |
|
955 for (int i = 0; i < topLevelText.count(); ++i) { |
|
956 QTreeWidgetItem *item = new QTreeWidgetItem(testWidget); |
|
957 item->setText(column, topLevelText.at(i)); |
|
958 for (int j = 0; j < childText.count(); ++j) { |
|
959 QTreeWidgetItem *child = new QTreeWidgetItem(item); |
|
960 child->setText(column, childText.at(j)); |
|
961 } |
|
962 } |
|
963 |
|
964 QTreeWidgetItem *original = testWidget->topLevelItem(topLevelIndex); |
|
965 QTreeWidgetItem *copy = original->clone(); |
|
966 QCOMPARE(copy->text(column), original->text(column)); |
|
967 QCOMPARE(copy->childCount(), original->childCount()); |
|
968 QVERIFY(!copy->parent()); |
|
969 QVERIFY(!copy->treeWidget()); |
|
970 |
|
971 QTreeWidgetItem *originalChild = original->child(childIndex); |
|
972 QTreeWidgetItem *copiedChild = cloneChild ? originalChild->clone() : copy->child(childIndex); |
|
973 QVERIFY(copiedChild != originalChild); |
|
974 QCOMPARE(copiedChild->text(column), originalChild->text(column)); |
|
975 QCOMPARE(copiedChild->childCount(), originalChild->childCount()); |
|
976 QCOMPARE(copiedChild->parent(), cloneChild ? 0 : copy); |
|
977 QVERIFY(!copiedChild->treeWidget()); |
|
978 if (cloneChild) |
|
979 delete copiedChild; |
|
980 delete copy; |
|
981 } |
|
982 |
|
983 void tst_QTreeWidget::expand_data() |
|
984 { |
|
985 QTest::addColumn<int>("topLevelIndex"); |
|
986 QTest::addColumn<int>("topLevelCount"); |
|
987 QTest::addColumn<int>("childIndex"); |
|
988 QTest::addColumn<int>("childCount"); |
|
989 |
|
990 QTest::newRow("the only test data for now") << 0 << 1 << 0 << 1; |
|
991 } |
|
992 |
|
993 void tst_QTreeWidget::expand() |
|
994 { |
|
995 QFETCH(int, topLevelIndex); |
|
996 QFETCH(int, topLevelCount); |
|
997 QFETCH(int, childIndex); |
|
998 QFETCH(int, childCount); |
|
999 |
|
1000 for (int i = 0; i < topLevelCount; ++i) { |
|
1001 QTreeWidgetItem *item = new QTreeWidgetItem(testWidget); |
|
1002 for (int j = 0; j < childCount; ++j) |
|
1003 new QTreeWidgetItem(item); |
|
1004 } |
|
1005 |
|
1006 QTreeWidgetItem *topLevelItem = testWidget->topLevelItem(topLevelIndex); |
|
1007 QTreeWidgetItem *childItem = topLevelItem->child(childIndex); |
|
1008 |
|
1009 QVERIFY(!testWidget->isItemExpanded(topLevelItem)); |
|
1010 testWidget->setItemExpanded(topLevelItem, true); |
|
1011 QVERIFY(testWidget->isItemExpanded(topLevelItem)); |
|
1012 |
|
1013 QVERIFY(!testWidget->isItemExpanded(childItem)); |
|
1014 testWidget->setItemExpanded(childItem, true); |
|
1015 QVERIFY(testWidget->isItemExpanded(childItem)); |
|
1016 |
|
1017 QVERIFY(testWidget->isItemExpanded(topLevelItem)); |
|
1018 testWidget->setItemExpanded(topLevelItem, false); |
|
1019 QVERIFY(!testWidget->isItemExpanded(topLevelItem)); |
|
1020 |
|
1021 QVERIFY(testWidget->isItemExpanded(childItem)); |
|
1022 testWidget->setItemExpanded(childItem, false); |
|
1023 QVERIFY(!testWidget->isItemExpanded(childItem)); |
|
1024 } |
|
1025 |
|
1026 void tst_QTreeWidget::checkState_data() |
|
1027 { |
|
1028 } |
|
1029 |
|
1030 void tst_QTreeWidget::checkState() |
|
1031 { |
|
1032 QTreeWidgetItem *item = new QTreeWidgetItem(testWidget); |
|
1033 item->setCheckState(0, Qt::Unchecked); |
|
1034 QTreeWidgetItem *firstChild = new QTreeWidgetItem(item); |
|
1035 firstChild->setCheckState(0, Qt::Unchecked); |
|
1036 QTreeWidgetItem *seccondChild = new QTreeWidgetItem(item); |
|
1037 seccondChild->setCheckState(0, Qt::Unchecked); |
|
1038 |
|
1039 QCOMPARE(item->checkState(0), Qt::Unchecked); |
|
1040 QCOMPARE(firstChild->checkState(0), Qt::Unchecked); |
|
1041 QCOMPARE(seccondChild->checkState(0), Qt::Unchecked); |
|
1042 |
|
1043 firstChild->setCheckState(0, Qt::Checked); |
|
1044 QCOMPARE(item->checkState(0), Qt::Unchecked); |
|
1045 QCOMPARE(firstChild->checkState(0), Qt::Checked); |
|
1046 QCOMPARE(seccondChild->checkState(0), Qt::Unchecked); |
|
1047 |
|
1048 item->setFlags(item->flags()|Qt::ItemIsTristate); |
|
1049 QCOMPARE(item->checkState(0), Qt::PartiallyChecked); |
|
1050 QCOMPARE(firstChild->checkState(0), Qt::Checked); |
|
1051 QCOMPARE(seccondChild->checkState(0), Qt::Unchecked); |
|
1052 |
|
1053 seccondChild->setCheckState(0, Qt::Checked); |
|
1054 QCOMPARE(item->checkState(0), Qt::Checked); |
|
1055 QCOMPARE(firstChild->checkState(0), Qt::Checked); |
|
1056 QCOMPARE(seccondChild->checkState(0), Qt::Checked); |
|
1057 |
|
1058 firstChild->setCheckState(0, Qt::Unchecked); |
|
1059 seccondChild->setCheckState(0, Qt::Unchecked); |
|
1060 QCOMPARE(item->checkState(0), Qt::Unchecked); |
|
1061 QCOMPARE(firstChild->checkState(0), Qt::Unchecked); |
|
1062 QCOMPARE(seccondChild->checkState(0), Qt::Unchecked); |
|
1063 } |
|
1064 |
|
1065 void tst_QTreeWidget::findItems_data() |
|
1066 { |
|
1067 QTest::addColumn<int>("column"); |
|
1068 QTest::addColumn<QStringList>("topLevelText"); |
|
1069 QTest::addColumn<QStringList>("childText"); |
|
1070 QTest::addColumn<QString>("pattern"); |
|
1071 QTest::addColumn<int>("resultCount"); |
|
1072 QTest::addColumn<QStringList>("resultText"); |
|
1073 |
|
1074 QTest::newRow("find in toplevel") |
|
1075 << 0 |
|
1076 << (QStringList() << "This is a text" << "This is another" << "This is the one") |
|
1077 << (QStringList() << "A child" << "This is not the one" << "And yet another child") |
|
1078 << "This is the one" |
|
1079 << 1 |
|
1080 << (QStringList() << "This is the one"); |
|
1081 |
|
1082 QTest::newRow("find child") |
|
1083 << 0 |
|
1084 << (QStringList() << "This is a text" << "This is another" << "This is the one") |
|
1085 << (QStringList() << "A child" << "This is not the one" << "And yet another child") |
|
1086 << "A child" |
|
1087 << 3 // once for each branch |
|
1088 << (QStringList() << "A child"); |
|
1089 |
|
1090 } |
|
1091 |
|
1092 void tst_QTreeWidget::findItems() |
|
1093 { |
|
1094 QFETCH(int, column); |
|
1095 QFETCH(QStringList, topLevelText); |
|
1096 QFETCH(QStringList, childText); |
|
1097 QFETCH(QString, pattern); |
|
1098 QFETCH(int, resultCount); |
|
1099 QFETCH(QStringList, resultText); |
|
1100 |
|
1101 for (int i = 0; i < topLevelText.count(); ++i) { |
|
1102 QTreeWidgetItem *item = new QTreeWidgetItem(testWidget); |
|
1103 item->setText(column, topLevelText.at(i)); |
|
1104 for (int j = 0; j < childText.count(); ++j) { |
|
1105 QTreeWidgetItem *child = new QTreeWidgetItem(item); |
|
1106 child->setText(column, childText.at(j)); |
|
1107 } |
|
1108 } |
|
1109 |
|
1110 QList<QTreeWidgetItem*> result = testWidget->findItems(pattern, |
|
1111 Qt::MatchExactly|Qt::MatchRecursive); |
|
1112 QCOMPARE(result.count(), resultCount); |
|
1113 |
|
1114 for (int k = 0; k < result.count() && k < resultText.count(); ++k) |
|
1115 QCOMPARE(result.at(k)->text(column), resultText.at(k)); |
|
1116 } |
|
1117 |
|
1118 void tst_QTreeWidget::findItemsInColumn() |
|
1119 { |
|
1120 // Create 5 root items. |
|
1121 for (int i = 0; i < 5; i++) |
|
1122 new QTreeWidgetItem(testWidget, QStringList() << QString::number(i)); |
|
1123 |
|
1124 // Create a child with two columns for each root item. |
|
1125 for (int i = 0; i < 5; i++) { |
|
1126 QTreeWidgetItem * const parent = testWidget->topLevelItem(i); |
|
1127 new QTreeWidgetItem(parent, QStringList() << QString::number(i * 10) << QString::number(i * 100)); |
|
1128 } |
|
1129 |
|
1130 // Recursively search column one for 400. |
|
1131 QList<QTreeWidgetItem*> items = testWidget->findItems("400", Qt::MatchExactly|Qt::MatchRecursive, 1); |
|
1132 QCOMPARE(items.count(), 1); |
|
1133 } |
|
1134 |
|
1135 void tst_QTreeWidget::sortItems_data() |
|
1136 { |
|
1137 QTest::addColumn<int>("column"); |
|
1138 QTest::addColumn<int>("order"); |
|
1139 QTest::addColumn<QStringList>("topLevelText"); |
|
1140 QTest::addColumn<QStringList>("childText"); |
|
1141 QTest::addColumn<QStringList>("topLevelResult"); |
|
1142 QTest::addColumn<QStringList>("childResult"); |
|
1143 QTest::addColumn<IntList>("expectedTopRows"); |
|
1144 QTest::addColumn<IntList>("expectedChildRows"); |
|
1145 |
|
1146 QTest::newRow("ascending order") |
|
1147 << 0 |
|
1148 << static_cast<int>(Qt::AscendingOrder) |
|
1149 << (QStringList() << "c" << "d" << "a" << "b") |
|
1150 << (QStringList() << "e" << "h" << "g" << "f") |
|
1151 << (QStringList() << "a" << "b" << "c" << "d") |
|
1152 << (QStringList() << "e" << "f" << "g" << "h") |
|
1153 << (IntList() << 2 << 3 << 0 << 1) |
|
1154 << (IntList() << 0 << 3 << 2 << 1); |
|
1155 |
|
1156 QTest::newRow("descending order") |
|
1157 << 0 |
|
1158 << static_cast<int>(Qt::DescendingOrder) |
|
1159 << (QStringList() << "c" << "d" << "a" << "b") |
|
1160 << (QStringList() << "e" << "h" << "g" << "f") |
|
1161 << (QStringList() << "d" << "c" << "b" << "a") |
|
1162 << (QStringList() << "h" << "g" << "f" << "e") |
|
1163 << (IntList() << 1 << 0 << 3 << 2) |
|
1164 << (IntList() << 3 << 0 << 1 << 2); |
|
1165 } |
|
1166 |
|
1167 void tst_QTreeWidget::sortItems() |
|
1168 { |
|
1169 QFETCH(int, column); |
|
1170 QFETCH(int, order); |
|
1171 QFETCH(QStringList, topLevelText); |
|
1172 QFETCH(QStringList, childText); |
|
1173 QFETCH(QStringList, topLevelResult); |
|
1174 QFETCH(QStringList, childResult); |
|
1175 QFETCH(IntList, expectedTopRows); |
|
1176 QFETCH(IntList, expectedChildRows); |
|
1177 testWidget->setSortingEnabled(false); |
|
1178 |
|
1179 for (int i = 0; i < topLevelText.count(); ++i) { |
|
1180 QTreeWidgetItem *item = new QTreeWidgetItem(testWidget); |
|
1181 item->setText(column, topLevelText.at(i)); |
|
1182 for (int j = 0; j < childText.count(); ++j) { |
|
1183 QTreeWidgetItem *child = new QTreeWidgetItem(item); |
|
1184 child->setText(column, childText.at(j)); |
|
1185 } |
|
1186 } |
|
1187 |
|
1188 QAbstractItemModel *model = testWidget->model(); |
|
1189 QList<QPersistentModelIndex> tops; |
|
1190 for (int r = 0; r < model->rowCount(QModelIndex()); ++r) { |
|
1191 QPersistentModelIndex p = model->index(r, 0, QModelIndex()); |
|
1192 tops << p; |
|
1193 } |
|
1194 QList<QPersistentModelIndex> children; |
|
1195 for (int s = 0; s < model->rowCount(tops.first()); ++s) { |
|
1196 QPersistentModelIndex c = model->index(s, 0, tops.first()); |
|
1197 children << c; |
|
1198 } |
|
1199 |
|
1200 testWidget->sortItems(column, static_cast<Qt::SortOrder>(order)); |
|
1201 QCOMPARE(testWidget->sortColumn(), column); |
|
1202 |
|
1203 for (int k = 0; k < topLevelResult.count(); ++k) { |
|
1204 QTreeWidgetItem *item = testWidget->topLevelItem(k); |
|
1205 QCOMPARE(item->text(column), topLevelResult.at(k)); |
|
1206 for (int l = 0; l < childResult.count(); ++l) |
|
1207 QCOMPARE(item->child(l)->text(column), childResult.at(l)); |
|
1208 } |
|
1209 |
|
1210 for (int m = 0; m < tops.count(); ++m) |
|
1211 QCOMPARE(tops.at(m).row(), expectedTopRows.at(m)); |
|
1212 for (int n = 0; n < children.count(); ++n) |
|
1213 QCOMPARE(children.at(n).row(), expectedChildRows.at(n)); |
|
1214 } |
|
1215 |
|
1216 void tst_QTreeWidget::deleteItems_data() |
|
1217 { |
|
1218 QTest::addColumn<int>("topLevelCount"); |
|
1219 QTest::addColumn<int>("childCount"); |
|
1220 QTest::addColumn<int>("grandChildCount"); |
|
1221 |
|
1222 QTest::addColumn<int>("deleteTopLevelCount"); |
|
1223 QTest::addColumn<int>("deleteChildCount"); |
|
1224 QTest::addColumn<int>("deleteGrandChildCount"); |
|
1225 |
|
1226 QTest::addColumn<int>("expectedTopLevelCount"); |
|
1227 QTest::addColumn<int>("expectedChildCount"); |
|
1228 QTest::addColumn<int>("expectedGrandChildCount"); |
|
1229 |
|
1230 QTest::addColumn<int>("persistentRow"); |
|
1231 QTest::addColumn<int>("persistentColumn"); |
|
1232 QTest::addColumn<bool>("persistentIsValid"); |
|
1233 |
|
1234 QTest::newRow("start with 10, delete 1") |
|
1235 << 10 << 10 << 10 |
|
1236 << 1 << 1 << 1 |
|
1237 << 9 << 9 << 9 |
|
1238 << 0 << 0 << false; |
|
1239 QTest::newRow("start with 10, delete 5") |
|
1240 << 10 << 10 << 10 |
|
1241 << 5 << 5 << 5 |
|
1242 << 5 << 5 << 5 |
|
1243 << 0 << 0 << false; |
|
1244 QTest::newRow("mixed") |
|
1245 << 10 << 13 << 7 |
|
1246 << 3 << 7 << 4 |
|
1247 << 7 << 6 << 3 |
|
1248 << 0 << 0 << false; |
|
1249 QTest::newRow("all") |
|
1250 << 10 << 10 << 10 |
|
1251 << 10 << 10 << 10 |
|
1252 << 0 << 0 << 0 |
|
1253 << 0 << 0 << false; |
|
1254 } |
|
1255 |
|
1256 void tst_QTreeWidget::deleteItems() |
|
1257 { |
|
1258 QFETCH(int, topLevelCount); |
|
1259 QFETCH(int, childCount); |
|
1260 QFETCH(int, grandChildCount); |
|
1261 |
|
1262 QFETCH(int, deleteTopLevelCount); |
|
1263 QFETCH(int, deleteChildCount); |
|
1264 QFETCH(int, deleteGrandChildCount); |
|
1265 |
|
1266 QFETCH(int, expectedTopLevelCount); |
|
1267 QFETCH(int, expectedChildCount); |
|
1268 QFETCH(int, expectedGrandChildCount); |
|
1269 |
|
1270 QFETCH(int, persistentRow); |
|
1271 QFETCH(int, persistentColumn); |
|
1272 QFETCH(bool, persistentIsValid); |
|
1273 |
|
1274 for (int i = 0; i < topLevelCount; ++i) { |
|
1275 QTreeWidgetItem *top = new QTreeWidgetItem(testWidget); |
|
1276 for (int j = 0; j < childCount; ++j) { |
|
1277 QTreeWidgetItem *child = new QTreeWidgetItem(top); |
|
1278 for (int k = 0; k < grandChildCount; ++k) { |
|
1279 new QTreeWidgetItem(child); |
|
1280 } |
|
1281 } |
|
1282 } |
|
1283 |
|
1284 QPersistentModelIndex persistent = testWidget->model()->index(persistentRow, |
|
1285 persistentColumn); |
|
1286 QVERIFY(persistent.isValid()); |
|
1287 |
|
1288 QTreeWidgetItem *top = testWidget->topLevelItem(0); |
|
1289 QTreeWidgetItem *child = top->child(0); |
|
1290 |
|
1291 for (int n = 0; n < deleteGrandChildCount; ++n) |
|
1292 delete child->child(0); |
|
1293 QCOMPARE(child->childCount(), expectedGrandChildCount); |
|
1294 |
|
1295 for (int m = 0; m < deleteChildCount; ++m) |
|
1296 delete top->child(0); |
|
1297 QCOMPARE(top->childCount(), expectedChildCount); |
|
1298 |
|
1299 for (int l = 0; l < deleteTopLevelCount; ++l) |
|
1300 delete testWidget->topLevelItem(0); |
|
1301 QCOMPARE(testWidget->topLevelItemCount(), expectedTopLevelCount); |
|
1302 |
|
1303 QCOMPARE(persistent.isValid(), persistentIsValid); |
|
1304 } |
|
1305 |
|
1306 |
|
1307 void tst_QTreeWidget::itemAboveOrBelow() |
|
1308 { |
|
1309 QTreeWidget tw; |
|
1310 tw.setColumnCount(1); |
|
1311 QTreeWidgetItem *twi = new QTreeWidgetItem(&tw, QStringList() << "Test"); |
|
1312 QTreeWidgetItem *twi2 = new QTreeWidgetItem(&tw, QStringList() << "Test 2"); |
|
1313 QTreeWidgetItem *twi3 = new QTreeWidgetItem(&tw, QStringList() << "Test 3"); |
|
1314 tw.show(); |
|
1315 QCOMPARE(tw.itemAbove(twi2), twi); |
|
1316 QCOMPARE(tw.itemBelow(twi2), twi3); |
|
1317 } |
|
1318 |
|
1319 void tst_QTreeWidget::itemStreaming_data() |
|
1320 { |
|
1321 QTest::addColumn<QString>("text"); |
|
1322 QTest::addColumn<QString>("toolTip"); |
|
1323 QTest::addColumn<int>("column"); |
|
1324 |
|
1325 QTest::newRow("Data") << "item text" << "tool tip text" << 0; |
|
1326 } |
|
1327 |
|
1328 void tst_QTreeWidget::itemStreaming() |
|
1329 { |
|
1330 QFETCH(QString, text); |
|
1331 QFETCH(QString, toolTip); |
|
1332 QFETCH(int, column); |
|
1333 |
|
1334 QTreeWidgetItem item(testWidget); |
|
1335 QCOMPARE(item.text(column), QString()); |
|
1336 QCOMPARE(item.toolTip(column), QString()); |
|
1337 |
|
1338 item.setText(column, text); |
|
1339 item.setToolTip(column, toolTip); |
|
1340 QCOMPARE(item.text(column), text); |
|
1341 QCOMPARE(item.toolTip(column), toolTip); |
|
1342 |
|
1343 QByteArray buffer; |
|
1344 QDataStream out(&buffer, QIODevice::WriteOnly); |
|
1345 out << item; |
|
1346 |
|
1347 QTreeWidgetItem item2(testWidget); |
|
1348 QCOMPARE(item2.text(column), QString()); |
|
1349 QCOMPARE(item2.toolTip(column), QString()); |
|
1350 |
|
1351 QVERIFY(!buffer.isEmpty()); |
|
1352 |
|
1353 QDataStream in(&buffer, QIODevice::ReadOnly); |
|
1354 in >> item2; |
|
1355 QCOMPARE(item2.text(column), text); |
|
1356 QCOMPARE(item2.toolTip(column), toolTip); |
|
1357 } |
|
1358 |
|
1359 void tst_QTreeWidget::insertTopLevelItems_data() |
|
1360 { |
|
1361 QTest::addColumn<QStringList>("initialText"); |
|
1362 QTest::addColumn<QStringList>("insertText"); |
|
1363 QTest::addColumn<int>("insertTopLevelIndex"); |
|
1364 QTest::addColumn<int>("expectedTopLevelIndex"); |
|
1365 QTest::addColumn<int>("insertChildIndex"); |
|
1366 QTest::addColumn<int>("expectedChildIndex"); |
|
1367 |
|
1368 QStringList initial = (QStringList() << "foo" << "bar"); |
|
1369 QStringList insert = (QStringList() << "baz"); |
|
1370 |
|
1371 QTest::newRow("Insert at count") << initial << insert |
|
1372 << initial.count() << initial.count() |
|
1373 << initial.count() << initial.count(); |
|
1374 QTest::newRow("Insert in the middle") << initial << insert |
|
1375 << (initial.count() / 2) << (initial.count() / 2) |
|
1376 << (initial.count() / 2) << (initial.count() / 2); |
|
1377 QTest::newRow("Insert less than 0") << initial << insert |
|
1378 << -1 << -1 |
|
1379 << -1 << -1; |
|
1380 QTest::newRow("Insert beyond count") << initial << insert |
|
1381 << initial.count() + 1 << -1 |
|
1382 << initial.count() + 1 << -1; |
|
1383 } |
|
1384 |
|
1385 void tst_QTreeWidget::insertTopLevelItems() |
|
1386 { |
|
1387 QFETCH(QStringList, initialText); |
|
1388 QFETCH(QStringList, insertText); |
|
1389 QFETCH(int, insertTopLevelIndex); |
|
1390 QFETCH(int, expectedTopLevelIndex); |
|
1391 QFETCH(int, insertChildIndex); |
|
1392 QFETCH(int, expectedChildIndex); |
|
1393 testWidget->setSortingEnabled(false); |
|
1394 |
|
1395 { // insert the initial items |
|
1396 QCOMPARE(testWidget->topLevelItemCount(), 0); |
|
1397 for (int i = 0; i < initialText.count(); ++i) { |
|
1398 QTreeWidgetItem *top = new QTreeWidgetItem(QStringList(initialText.at(i))); |
|
1399 testWidget->addTopLevelItem(top); |
|
1400 QCOMPARE(testWidget->indexOfTopLevelItem(top), i); |
|
1401 } |
|
1402 QCOMPARE(testWidget->topLevelItemCount(), initialText.count()); |
|
1403 } |
|
1404 |
|
1405 { // test adding children |
|
1406 QTreeWidgetItem *topLevel = testWidget->topLevelItem(0); |
|
1407 for (int i = 0; i < initialText.count(); ++i) |
|
1408 topLevel->addChild(new QTreeWidgetItem(QStringList(initialText.at(i)))); |
|
1409 QCOMPARE(topLevel->childCount(), initialText.count()); |
|
1410 } |
|
1411 |
|
1412 { // test adding more top level items |
|
1413 QTreeWidgetItem *topsy = new QTreeWidgetItem(QStringList(insertText.at(0))); |
|
1414 testWidget->insertTopLevelItem(insertTopLevelIndex, topsy); |
|
1415 if (expectedTopLevelIndex == -1) { |
|
1416 QCOMPARE(testWidget->topLevelItemCount(), initialText.count()); |
|
1417 delete topsy; |
|
1418 } else { |
|
1419 QTreeWidgetItem *item = testWidget->topLevelItem(expectedTopLevelIndex); |
|
1420 QVERIFY(item != 0); |
|
1421 QCOMPARE(item->text(0), insertText.at(0)); |
|
1422 QCOMPARE(testWidget->indexOfTopLevelItem(item), expectedTopLevelIndex); |
|
1423 } |
|
1424 } |
|
1425 |
|
1426 { // test adding more children |
|
1427 QTreeWidgetItem *topLevel = testWidget->topLevelItem(0); |
|
1428 QVERIFY(topLevel != 0); |
|
1429 QTreeWidgetItem *child = new QTreeWidgetItem(QStringList(insertText.at(0))); |
|
1430 topLevel->insertChild(insertChildIndex, child); |
|
1431 if (expectedChildIndex == -1) { |
|
1432 QCOMPARE(topLevel->childCount(), initialText.count()); |
|
1433 delete child; |
|
1434 } else { |
|
1435 QTreeWidgetItem *item = topLevel->child(expectedChildIndex); |
|
1436 QVERIFY(item != 0); |
|
1437 QCOMPARE(item->text(0), insertText.at(0)); |
|
1438 } |
|
1439 } |
|
1440 } |
|
1441 |
|
1442 static void fillTreeWidget(QTreeWidgetItem *parent, int rows) |
|
1443 { |
|
1444 const int columns = parent->treeWidget()->columnCount(); |
|
1445 for (int r = 0; r < rows; ++r) { |
|
1446 QTreeWidgetItem *w = new QTreeWidgetItem(parent); |
|
1447 for ( int c = 0; c < columns; ++c ) { |
|
1448 QString s = QString("[r:%1,c:%2]").arg(r).arg(c); |
|
1449 w->setText(c, s); |
|
1450 } |
|
1451 fillTreeWidget(w, rows - r - 1); |
|
1452 } |
|
1453 } |
|
1454 |
|
1455 static void fillTreeWidget(QTreeWidget *tree, int rows) |
|
1456 { |
|
1457 for (int r = 0; r < rows; ++r) { |
|
1458 QTreeWidgetItem *w = new QTreeWidgetItem(); |
|
1459 for ( int c = 0; c < tree->columnCount(); ++c ) { |
|
1460 QString s = QString("[r:%1,c:%2]").arg(r).arg(c); |
|
1461 w->setText(c, s); |
|
1462 } |
|
1463 tree->insertTopLevelItem(r, w); |
|
1464 fillTreeWidget(w, rows - r - 1); |
|
1465 } |
|
1466 } |
|
1467 |
|
1468 void tst_QTreeWidget::keyboardNavigation() |
|
1469 { |
|
1470 int rows = 8; |
|
1471 |
|
1472 fillTreeWidget(testWidget, rows); |
|
1473 |
|
1474 QVector<Qt::Key> keymoves; |
|
1475 keymoves << Qt::Key_Down << Qt::Key_Right << Qt::Key_Left |
|
1476 << Qt::Key_Down << Qt::Key_Down << Qt::Key_Down << Qt::Key_Down |
|
1477 << Qt::Key_Right |
|
1478 << Qt::Key_Up << Qt::Key_Left << Qt::Key_Left |
|
1479 << Qt::Key_Up << Qt::Key_Down << Qt::Key_Up << Qt::Key_Up |
|
1480 << Qt::Key_Up << Qt::Key_Up << Qt::Key_Up << Qt::Key_Up |
|
1481 << Qt::Key_Down << Qt::Key_Right << Qt::Key_Down << Qt::Key_Down |
|
1482 << Qt::Key_Down << Qt::Key_Right << Qt::Key_Down << Qt::Key_Down |
|
1483 << Qt::Key_Left << Qt::Key_Left << Qt::Key_Up << Qt::Key_Down |
|
1484 << Qt::Key_Up << Qt::Key_Up << Qt::Key_Up << Qt::Key_Left |
|
1485 << Qt::Key_Down << Qt::Key_Right << Qt::Key_Right << Qt::Key_Right |
|
1486 << Qt::Key_Left << Qt::Key_Left << Qt::Key_Right << Qt::Key_Left; |
|
1487 |
|
1488 int row = 0; |
|
1489 QTreeWidgetItem *item = testWidget->topLevelItem(0); |
|
1490 testWidget->setCurrentItem(item); |
|
1491 QCOMPARE(testWidget->currentItem(), item); |
|
1492 QApplication::instance()->processEvents(); |
|
1493 |
|
1494 QScrollBar *scrollBar = testWidget->horizontalScrollBar(); |
|
1495 bool checkScroll = false; |
|
1496 for (int i = 0; i < keymoves.size(); ++i) { |
|
1497 Qt::Key key = keymoves.at(i); |
|
1498 int valueBeforeClick = scrollBar->value(); |
|
1499 if (valueBeforeClick >= scrollBar->singleStep()) |
|
1500 checkScroll = true; |
|
1501 else |
|
1502 checkScroll = false; |
|
1503 QTest::keyClick(testWidget, key); |
|
1504 QApplication::instance()->processEvents(); |
|
1505 |
|
1506 switch (key) { |
|
1507 case Qt::Key_Up: |
|
1508 if (row > 0) { |
|
1509 if (item->parent()) |
|
1510 item = item->parent()->child(row - 1); |
|
1511 else |
|
1512 item = testWidget->topLevelItem(row - 1); |
|
1513 row -= 1; |
|
1514 } else if (item->parent()) { |
|
1515 item = item->parent(); |
|
1516 row = item->parent() ? item->parent()->indexOfChild(item) : testWidget->indexOfTopLevelItem(item); |
|
1517 } |
|
1518 break; |
|
1519 case Qt::Key_Down: |
|
1520 if (testWidget->isItemExpanded(item)) { |
|
1521 row = 0; |
|
1522 item = item->child(row); |
|
1523 } else { |
|
1524 row = qMin(rows - 1, row + 1); |
|
1525 if (item->parent()) |
|
1526 item = item->parent()->child(row); |
|
1527 else |
|
1528 item = testWidget->topLevelItem(row); |
|
1529 } |
|
1530 break; |
|
1531 case Qt::Key_Left: |
|
1532 if (checkScroll) { |
|
1533 QVERIFY(testWidget->isItemExpanded(item)); |
|
1534 QCOMPARE(scrollBar->value(), valueBeforeClick - scrollBar->singleStep()); |
|
1535 } |
|
1536 // windows style right will walk to the parent |
|
1537 if (testWidget->currentItem() != item) { |
|
1538 QCOMPARE(testWidget->currentItem(), item->parent()); |
|
1539 item = testWidget->currentItem(); |
|
1540 row = item->parent() ? item->parent()->indexOfChild(item) : testWidget->indexOfTopLevelItem(item);; |
|
1541 } |
|
1542 break; |
|
1543 case Qt::Key_Right: |
|
1544 if (checkScroll) |
|
1545 QCOMPARE(scrollBar->value(), valueBeforeClick + scrollBar->singleStep()); |
|
1546 // windows style right will walk to the first child |
|
1547 if (testWidget->currentItem() != item) { |
|
1548 QCOMPARE(testWidget->currentItem()->parent(), item); |
|
1549 row = item->indexOfChild(testWidget->currentItem()); |
|
1550 item = testWidget->currentItem(); |
|
1551 QCOMPARE(row, 0); |
|
1552 } |
|
1553 break; |
|
1554 default: |
|
1555 QVERIFY(false); |
|
1556 } |
|
1557 |
|
1558 QTreeWidgetItem *current = testWidget->currentItem(); |
|
1559 QCOMPARE(current->text(0), QString("[r:%1,c:0]").arg(row)); |
|
1560 if (current->parent()) |
|
1561 QCOMPARE(current->parent()->indexOfChild(current), row); |
|
1562 else |
|
1563 QCOMPARE(testWidget->indexOfTopLevelItem(current), row); |
|
1564 } |
|
1565 } |
|
1566 |
|
1567 void tst_QTreeWidget::scrollToItem() |
|
1568 { |
|
1569 // Check if all parent nodes of the item found are expanded. |
|
1570 // Reported in task #78761 |
|
1571 QTreeWidgetItem *bar; |
|
1572 QTreeWidgetItem *search; |
|
1573 for (int i=0; i<2; ++i) { |
|
1574 bar = new QTreeWidgetItem(testWidget); |
|
1575 bar->setText(0, QString::number(i)); |
|
1576 |
|
1577 for (int j=0; j<2; ++j) { |
|
1578 QTreeWidgetItem *foo = new QTreeWidgetItem(bar); |
|
1579 foo->setText(0, bar->text(0) + QString::number(j)); |
|
1580 |
|
1581 for (int k=0; k<2; ++k) { |
|
1582 QTreeWidgetItem *yo = new QTreeWidgetItem(foo); |
|
1583 yo->setText(0, foo->text(0) + QString::number(k)); |
|
1584 search = yo; |
|
1585 } |
|
1586 } |
|
1587 } |
|
1588 |
|
1589 testWidget->setHeaderLabels(QStringList() << "foo"); |
|
1590 testWidget->scrollToItem(search); |
|
1591 QVERIFY(search->text(0) == "111"); |
|
1592 |
|
1593 bar = search->parent(); |
|
1594 QVERIFY(testWidget->isItemExpanded(bar)); |
|
1595 bar = bar->parent(); |
|
1596 QVERIFY(testWidget->isItemExpanded(bar)); |
|
1597 } |
|
1598 |
|
1599 // From task #85413 |
|
1600 void tst_QTreeWidget::setSortingEnabled() |
|
1601 { |
|
1602 QStringList hl; |
|
1603 hl << "ID"; |
|
1604 testWidget->setColumnCount(hl.count()); |
|
1605 testWidget->setHeaderLabels(hl); |
|
1606 |
|
1607 QTreeWidgetItem *item1 = new QTreeWidgetItem(testWidget); |
|
1608 QTreeWidgetItem *item2 = new QTreeWidgetItem(testWidget); |
|
1609 |
|
1610 testWidget->setSortingEnabled(true); |
|
1611 QCOMPARE(testWidget->isSortingEnabled(), true); |
|
1612 QCOMPARE(testWidget->isSortingEnabled(), testWidget->header()->isSortIndicatorShown()); |
|
1613 QCOMPARE(testWidget->topLevelItem(0), item1); |
|
1614 QCOMPARE(testWidget->topLevelItem(1), item2); |
|
1615 |
|
1616 // Make sure we do it twice |
|
1617 testWidget->setSortingEnabled(true); |
|
1618 QCOMPARE(testWidget->isSortingEnabled(), true); |
|
1619 QCOMPARE(testWidget->isSortingEnabled(), testWidget->header()->isSortIndicatorShown()); |
|
1620 |
|
1621 testWidget->setSortingEnabled(false); |
|
1622 QCOMPARE(testWidget->isSortingEnabled(), false); |
|
1623 QCOMPARE(testWidget->isSortingEnabled(), testWidget->header()->isSortIndicatorShown()); |
|
1624 |
|
1625 testWidget->setSortingEnabled(false); |
|
1626 QCOMPARE(testWidget->isSortingEnabled(), false); |
|
1627 QCOMPARE(testWidget->isSortingEnabled(), testWidget->header()->isSortIndicatorShown()); |
|
1628 |
|
1629 // And back again so that we make sure that we test the transition from false to true |
|
1630 testWidget->setSortingEnabled(true); |
|
1631 QCOMPARE(testWidget->isSortingEnabled(), true); |
|
1632 QCOMPARE(testWidget->isSortingEnabled(), testWidget->header()->isSortIndicatorShown()); |
|
1633 |
|
1634 testWidget->setSortingEnabled(true); |
|
1635 QCOMPARE(testWidget->isSortingEnabled(), true); |
|
1636 QCOMPARE(testWidget->isSortingEnabled(), testWidget->header()->isSortIndicatorShown()); |
|
1637 |
|
1638 testWidget->setSortingEnabled(false); |
|
1639 } |
|
1640 |
|
1641 void tst_QTreeWidget::addChild() |
|
1642 { |
|
1643 QTreeWidget tree; |
|
1644 for (int x = 0; x < 2; ++x) { |
|
1645 QTreeWidget *view = x ? &tree : static_cast<QTreeWidget*>(0); |
|
1646 QTreeWidgetItem *item = new QTreeWidgetItem((QTreeWidget*)view); |
|
1647 QCOMPARE(item->childCount(), 0); |
|
1648 |
|
1649 // try to add 0 |
|
1650 item->addChild(0); |
|
1651 QCOMPARE(item->childCount(), 0); |
|
1652 QCOMPARE(item->indexOfChild(0), -1); |
|
1653 |
|
1654 // add one at a time |
|
1655 QList<QTreeWidgetItem*> children; |
|
1656 for (int i = 0; i < 10; ++i) { |
|
1657 QTreeWidgetItem *child = new QTreeWidgetItem(); |
|
1658 item->addChild(child); |
|
1659 QCOMPARE(item->childCount(), i+1); |
|
1660 QCOMPARE(item->child(i), child); |
|
1661 QCOMPARE(item->indexOfChild(child), i); |
|
1662 QCOMPARE(child->parent(), item); |
|
1663 QCOMPARE(child->treeWidget(), view); |
|
1664 item->addChild(child); |
|
1665 QCOMPARE(item->childCount(), i+1); |
|
1666 children.append(child); |
|
1667 } |
|
1668 |
|
1669 // take them all |
|
1670 QList<QTreeWidgetItem*> taken = item->takeChildren(); |
|
1671 QCOMPARE(taken, children); |
|
1672 QCOMPARE(item->childCount(), 0); |
|
1673 for (int i = 0; i < taken.count(); ++i) { |
|
1674 QCOMPARE(taken.at(i)->parent(), static_cast<QTreeWidgetItem*>(0)); |
|
1675 QCOMPARE(taken.at(i)->treeWidget(), static_cast<QTreeWidget*>(0)); |
|
1676 item->addChild(taken.at(i)); // re-add |
|
1677 } |
|
1678 |
|
1679 // delete one at a time |
|
1680 while (!children.isEmpty()) { |
|
1681 QTreeWidgetItem *ti = children.takeFirst(); |
|
1682 delete ti; |
|
1683 QCOMPARE(item->childCount(), children.count()); |
|
1684 for (int i = 0; i < children.count(); ++i) |
|
1685 QCOMPARE(item->child(i), children.at(i)); |
|
1686 } |
|
1687 |
|
1688 // add none |
|
1689 { |
|
1690 int count = item->childCount(); |
|
1691 item->addChildren(QList<QTreeWidgetItem*>()); |
|
1692 QCOMPARE(item->childCount(), count); |
|
1693 } |
|
1694 |
|
1695 // add many at a time |
|
1696 const int count = 10; |
|
1697 for (int i = 0; i < 100; i += count) { |
|
1698 QList<QTreeWidgetItem*> list; |
|
1699 for (int j = 0; j < count; ++j) |
|
1700 list << new QTreeWidgetItem(QStringList() << QString("%0").arg(j)); |
|
1701 item->addChildren(list); |
|
1702 QCOMPARE(item->childCount(), count + i); |
|
1703 for (int j = 0; j < count; ++j) { |
|
1704 QCOMPARE(item->child(i+j), list.at(j)); |
|
1705 QCOMPARE(item->child(i+j)->parent(), item); |
|
1706 } |
|
1707 |
|
1708 item->addChildren(list); |
|
1709 QCOMPARE(item->childCount(), count + i); |
|
1710 } |
|
1711 |
|
1712 if (!view) |
|
1713 delete item; |
|
1714 } |
|
1715 } |
|
1716 |
|
1717 void tst_QTreeWidget::setData() |
|
1718 { |
|
1719 { |
|
1720 QTreeWidgetItem *headerItem = new QTreeWidgetItem(); |
|
1721 headerItem->setText(0, "Item1"); |
|
1722 testWidget->setHeaderItem(headerItem); |
|
1723 |
|
1724 QSignalSpy headerDataChangedSpy( |
|
1725 testWidget->model(), SIGNAL(headerDataChanged(Qt::Orientation, int, int))); |
|
1726 QSignalSpy dataChangedSpy( |
|
1727 testWidget->model(), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &))); |
|
1728 QSignalSpy itemChangedSpy( |
|
1729 testWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int))); |
|
1730 headerItem->setText(0, "test"); |
|
1731 QCOMPARE(dataChangedSpy.count(), 0); |
|
1732 QCOMPARE(headerDataChangedSpy.count(), 1); |
|
1733 QCOMPARE(itemChangedSpy.count(), 0); // no itemChanged() signal for header item |
|
1734 |
|
1735 headerItem->setData(-1, -1, QVariant()); |
|
1736 } |
|
1737 |
|
1738 { |
|
1739 QSignalSpy itemChangedSpy( |
|
1740 testWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int))); |
|
1741 QTreeWidgetItem *item = new QTreeWidgetItem(); |
|
1742 testWidget->addTopLevelItem(item); |
|
1743 for (int x = 0; x < 2; ++x) { |
|
1744 for (int i = 1; i <= 2; ++i) { |
|
1745 for (int j = 0; j < 5; ++j) { |
|
1746 QVariantList args; |
|
1747 QString text = QString("text %0").arg(i); |
|
1748 item->setText(j, text); |
|
1749 QCOMPARE(item->text(j), text); |
|
1750 QCOMPARE(itemChangedSpy.count(), 1); |
|
1751 args = itemChangedSpy.takeFirst(); |
|
1752 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
1753 QCOMPARE(qvariant_cast<int>(args.at(1)), j); |
|
1754 item->setText(j, text); |
|
1755 QCOMPARE(itemChangedSpy.count(), 0); |
|
1756 |
|
1757 QPixmap pixmap(32, 32); |
|
1758 pixmap.fill((i == 1) ? Qt::red : Qt::green); |
|
1759 QIcon icon(pixmap); |
|
1760 item->setIcon(j, icon); |
|
1761 QCOMPARE(item->icon(j), icon); |
|
1762 QCOMPARE(itemChangedSpy.count(), 1); |
|
1763 args = itemChangedSpy.takeFirst(); |
|
1764 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
1765 QCOMPARE(qvariant_cast<int>(args.at(1)), j); |
|
1766 item->setIcon(j, icon); |
|
1767 // #### shouldn't cause dataChanged() |
|
1768 QCOMPARE(itemChangedSpy.count(), 1); |
|
1769 itemChangedSpy.clear(); |
|
1770 |
|
1771 QString toolTip = QString("toolTip %0").arg(i); |
|
1772 item->setToolTip(j, toolTip); |
|
1773 QCOMPARE(item->toolTip(j), toolTip); |
|
1774 QCOMPARE(itemChangedSpy.count(), 1); |
|
1775 args = itemChangedSpy.takeFirst(); |
|
1776 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
1777 QCOMPARE(qvariant_cast<int>(args.at(1)), j); |
|
1778 item->setToolTip(j, toolTip); |
|
1779 QCOMPARE(itemChangedSpy.count(), 0); |
|
1780 |
|
1781 QString statusTip = QString("statusTip %0").arg(i); |
|
1782 item->setStatusTip(j, statusTip); |
|
1783 QCOMPARE(item->statusTip(j), statusTip); |
|
1784 QCOMPARE(itemChangedSpy.count(), 1); |
|
1785 args = itemChangedSpy.takeFirst(); |
|
1786 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
1787 QCOMPARE(qvariant_cast<int>(args.at(1)), j); |
|
1788 item->setStatusTip(j, statusTip); |
|
1789 QCOMPARE(itemChangedSpy.count(), 0); |
|
1790 |
|
1791 QString whatsThis = QString("whatsThis %0").arg(i); |
|
1792 item->setWhatsThis(j, whatsThis); |
|
1793 QCOMPARE(item->whatsThis(j), whatsThis); |
|
1794 QCOMPARE(itemChangedSpy.count(), 1); |
|
1795 args = itemChangedSpy.takeFirst(); |
|
1796 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
1797 QCOMPARE(qvariant_cast<int>(args.at(1)), j); |
|
1798 item->setWhatsThis(j, whatsThis); |
|
1799 QCOMPARE(itemChangedSpy.count(), 0); |
|
1800 |
|
1801 QSize sizeHint(64*i, 48*i); |
|
1802 item->setSizeHint(j, sizeHint); |
|
1803 QCOMPARE(item->sizeHint(j), sizeHint); |
|
1804 QCOMPARE(itemChangedSpy.count(), 1); |
|
1805 args = itemChangedSpy.takeFirst(); |
|
1806 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
1807 QCOMPARE(qvariant_cast<int>(args.at(1)), j); |
|
1808 item->setSizeHint(j, sizeHint); |
|
1809 QCOMPARE(itemChangedSpy.count(), 0); |
|
1810 |
|
1811 QFont font; |
|
1812 item->setFont(j, font); |
|
1813 QCOMPARE(item->font(j), font); |
|
1814 QCOMPARE(itemChangedSpy.count(), 1); |
|
1815 args = itemChangedSpy.takeFirst(); |
|
1816 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
1817 QCOMPARE(qvariant_cast<int>(args.at(1)), j); |
|
1818 item->setFont(j, font); |
|
1819 QCOMPARE(itemChangedSpy.count(), 0); |
|
1820 |
|
1821 Qt::Alignment textAlignment((i == 1) |
|
1822 ? Qt::AlignLeft|Qt::AlignVCenter |
|
1823 : Qt::AlignRight); |
|
1824 item->setTextAlignment(j, textAlignment); |
|
1825 QCOMPARE(item->textAlignment(j), int(textAlignment)); |
|
1826 QCOMPARE(itemChangedSpy.count(), 1); |
|
1827 args = itemChangedSpy.takeFirst(); |
|
1828 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
1829 QCOMPARE(qvariant_cast<int>(args.at(1)), j); |
|
1830 item->setTextAlignment(j, textAlignment); |
|
1831 QCOMPARE(itemChangedSpy.count(), 0); |
|
1832 |
|
1833 QColor backgroundColor((i == 1) ? Qt::blue : Qt::yellow); |
|
1834 item->setBackground(j, backgroundColor); |
|
1835 QCOMPARE(item->background(j).color(), backgroundColor); |
|
1836 QCOMPARE(itemChangedSpy.count(), 1); |
|
1837 args = itemChangedSpy.takeFirst(); |
|
1838 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
1839 QCOMPARE(qvariant_cast<int>(args.at(1)), j); |
|
1840 item->setBackground(j, backgroundColor); |
|
1841 QCOMPARE(itemChangedSpy.count(), 0); |
|
1842 |
|
1843 QColor textColor((i == i) ? Qt::green : Qt::cyan); |
|
1844 item->setTextColor(j, textColor); |
|
1845 QCOMPARE(item->textColor(j), textColor); |
|
1846 QCOMPARE(itemChangedSpy.count(), 1); |
|
1847 args = itemChangedSpy.takeFirst(); |
|
1848 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
1849 QCOMPARE(qvariant_cast<int>(args.at(1)), j); |
|
1850 item->setTextColor(j, textColor); |
|
1851 QCOMPARE(itemChangedSpy.count(), 0); |
|
1852 |
|
1853 Qt::CheckState checkState((i == 1) ? Qt::PartiallyChecked : Qt::Checked); |
|
1854 item->setCheckState(j, checkState); |
|
1855 QCOMPARE(item->checkState(j), checkState); |
|
1856 QCOMPARE(itemChangedSpy.count(), 1); |
|
1857 args = itemChangedSpy.takeFirst(); |
|
1858 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
1859 QCOMPARE(qvariant_cast<int>(args.at(1)), j); |
|
1860 item->setCheckState(j, checkState); |
|
1861 QCOMPARE(itemChangedSpy.count(), 0); |
|
1862 |
|
1863 QCOMPARE(item->text(j), text); |
|
1864 QCOMPARE(item->icon(j), icon); |
|
1865 QCOMPARE(item->toolTip(j), toolTip); |
|
1866 QCOMPARE(item->statusTip(j), statusTip); |
|
1867 QCOMPARE(item->whatsThis(j), whatsThis); |
|
1868 QCOMPARE(item->sizeHint(j), sizeHint); |
|
1869 QCOMPARE(item->font(j), font); |
|
1870 QCOMPARE(item->textAlignment(j), int(textAlignment)); |
|
1871 QCOMPARE(item->background(j).color(), backgroundColor); |
|
1872 QCOMPARE(item->textColor(j), textColor); |
|
1873 QCOMPARE(item->checkState(j), checkState); |
|
1874 |
|
1875 QCOMPARE(qvariant_cast<QString>(item->data(j, Qt::DisplayRole)), text); |
|
1876 QCOMPARE(qvariant_cast<QIcon>(item->data(j, Qt::DecorationRole)), icon); |
|
1877 QCOMPARE(qvariant_cast<QString>(item->data(j, Qt::ToolTipRole)), toolTip); |
|
1878 QCOMPARE(qvariant_cast<QString>(item->data(j, Qt::StatusTipRole)), statusTip); |
|
1879 QCOMPARE(qvariant_cast<QString>(item->data(j, Qt::WhatsThisRole)), whatsThis); |
|
1880 QCOMPARE(qvariant_cast<QSize>(item->data(j, Qt::SizeHintRole)), sizeHint); |
|
1881 QCOMPARE(qvariant_cast<QFont>(item->data(j, Qt::FontRole)), font); |
|
1882 QCOMPARE(qvariant_cast<int>(item->data(j, Qt::TextAlignmentRole)), int(textAlignment)); |
|
1883 QCOMPARE(qvariant_cast<QBrush>(item->data(j, Qt::BackgroundColorRole)), QBrush(backgroundColor)); |
|
1884 QCOMPARE(qvariant_cast<QBrush>(item->data(j, Qt::BackgroundRole)), QBrush(backgroundColor)); |
|
1885 QCOMPARE(qvariant_cast<QColor>(item->data(j, Qt::TextColorRole)), textColor); |
|
1886 QCOMPARE(qvariant_cast<int>(item->data(j, Qt::CheckStateRole)), int(checkState)); |
|
1887 |
|
1888 item->setBackground(j, pixmap); |
|
1889 QCOMPARE(item->background(j).texture(), pixmap); |
|
1890 QCOMPARE(qvariant_cast<QBrush>(item->data(j, Qt::BackgroundRole)).texture(), pixmap); |
|
1891 args = itemChangedSpy.takeFirst(); |
|
1892 QCOMPARE(qvariant_cast<QTreeWidgetItem*>(args.at(0)), item); |
|
1893 QCOMPARE(qvariant_cast<int>(args.at(1)), j); |
|
1894 item->setBackground(j, pixmap); |
|
1895 QCOMPARE(itemChangedSpy.count(), 0); |
|
1896 |
|
1897 item->setData(j, Qt::DisplayRole, QVariant()); |
|
1898 item->setData(j, Qt::DecorationRole, QVariant()); |
|
1899 item->setData(j, Qt::ToolTipRole, QVariant()); |
|
1900 item->setData(j, Qt::StatusTipRole, QVariant()); |
|
1901 item->setData(j, Qt::WhatsThisRole, QVariant()); |
|
1902 item->setData(j, Qt::SizeHintRole, QVariant()); |
|
1903 item->setData(j, Qt::FontRole, QVariant()); |
|
1904 item->setData(j, Qt::TextAlignmentRole, QVariant()); |
|
1905 item->setData(j, Qt::BackgroundColorRole, QVariant()); |
|
1906 item->setData(j, Qt::TextColorRole, QVariant()); |
|
1907 item->setData(j, Qt::CheckStateRole, QVariant()); |
|
1908 QCOMPARE(itemChangedSpy.count(), 11); |
|
1909 itemChangedSpy.clear(); |
|
1910 |
|
1911 QCOMPARE(item->data(j, Qt::DisplayRole).toString(), QString()); |
|
1912 QCOMPARE(item->data(j, Qt::DecorationRole), QVariant()); |
|
1913 QCOMPARE(item->data(j, Qt::ToolTipRole), QVariant()); |
|
1914 QCOMPARE(item->data(j, Qt::StatusTipRole), QVariant()); |
|
1915 QCOMPARE(item->data(j, Qt::WhatsThisRole), QVariant()); |
|
1916 QCOMPARE(item->data(j, Qt::SizeHintRole), QVariant()); |
|
1917 QCOMPARE(item->data(j, Qt::FontRole), QVariant()); |
|
1918 QCOMPARE(item->data(j, Qt::TextAlignmentRole), QVariant()); |
|
1919 QCOMPARE(item->data(j, Qt::BackgroundColorRole), QVariant()); |
|
1920 QCOMPARE(item->data(j, Qt::BackgroundRole), QVariant()); |
|
1921 QCOMPARE(item->data(j, Qt::TextColorRole), QVariant()); |
|
1922 QCOMPARE(item->data(j, Qt::CheckStateRole), QVariant()); |
|
1923 } |
|
1924 } |
|
1925 } |
|
1926 |
|
1927 // ### add more data types here |
|
1928 |
|
1929 item->setData(0, Qt::DisplayRole, 5); |
|
1930 QCOMPARE(item->data(0, Qt::DisplayRole).type(), QVariant::Int); |
|
1931 |
|
1932 item->setData(0, Qt::DisplayRole, "test"); |
|
1933 QCOMPARE(item->data(0, Qt::DisplayRole).type(), QVariant::String); |
|
1934 |
|
1935 item->setData(0, Qt::DisplayRole, 0.4); |
|
1936 QCOMPARE(item->data(0, Qt::DisplayRole).type(), QVariant::Double); |
|
1937 |
|
1938 delete item; |
|
1939 } |
|
1940 } |
|
1941 |
|
1942 void tst_QTreeWidget::itemData() |
|
1943 { |
|
1944 QTreeWidget widget; |
|
1945 QTreeWidgetItem item(&widget); |
|
1946 widget.setColumnCount(2); |
|
1947 item.setFlags(item.flags() | Qt::ItemIsEditable); |
|
1948 item.setData(0, Qt::DisplayRole, QString("0")); |
|
1949 item.setData(0, Qt::CheckStateRole, Qt::PartiallyChecked); |
|
1950 item.setData(0, Qt::UserRole + 0, QString("1")); |
|
1951 item.setData(0, Qt::UserRole + 1, QString("2")); |
|
1952 item.setData(0, Qt::UserRole + 2, QString("3")); |
|
1953 item.setData(0, Qt::UserRole + 3, QString("4")); |
|
1954 |
|
1955 QMap<int, QVariant> flags = widget.model()->itemData(widget.model()->index(0, 0)); |
|
1956 QCOMPARE(flags.count(), 6); |
|
1957 QCOMPARE(flags[Qt::UserRole + 0].toString(), QString("1")); |
|
1958 |
|
1959 flags = widget.model()->itemData(widget.model()->index(0, 1)); |
|
1960 QCOMPARE(flags.count(), 0); |
|
1961 } |
|
1962 |
|
1963 void tst_QTreeWidget::enableDisable() |
|
1964 { |
|
1965 QTreeWidgetItem *itm = new QTreeWidgetItem(); |
|
1966 for (int i = 0; i < 10; ++i) |
|
1967 new QTreeWidgetItem(itm); |
|
1968 |
|
1969 // make sure all items are enabled |
|
1970 QVERIFY(itm->flags() & Qt::ItemIsEnabled); |
|
1971 for (int j = 0; j < itm->childCount(); ++j) |
|
1972 QVERIFY(itm->child(j)->flags() & Qt::ItemIsEnabled); |
|
1973 |
|
1974 // disable root and make sure they are all disabled |
|
1975 itm->setFlags(itm->flags() & ~Qt::ItemIsEnabled); |
|
1976 QVERIFY(!(itm->flags() & Qt::ItemIsEnabled)); |
|
1977 for (int k = 0; k < itm->childCount(); ++k) |
|
1978 QVERIFY(!(itm->child(k)->flags() & Qt::ItemIsEnabled)); |
|
1979 |
|
1980 // disable a child and make sure they are all still disabled |
|
1981 itm->child(5)->setFlags(itm->child(5)->flags() & ~Qt::ItemIsEnabled); |
|
1982 QVERIFY(!(itm->flags() & Qt::ItemIsEnabled)); |
|
1983 for (int l = 0; l < itm->childCount(); ++l) |
|
1984 QVERIFY(!(itm->child(l)->flags() & Qt::ItemIsEnabled)); |
|
1985 |
|
1986 // enable root and make sure all items except one are enabled |
|
1987 itm->setFlags(itm->flags() | Qt::ItemIsEnabled); |
|
1988 QVERIFY(itm->flags() & Qt::ItemIsEnabled); |
|
1989 for (int m = 0; m < itm->childCount(); ++m) |
|
1990 if (m == 5) |
|
1991 QVERIFY(!(itm->child(m)->flags() & Qt::ItemIsEnabled)); |
|
1992 else |
|
1993 QVERIFY(itm->child(m)->flags() & Qt::ItemIsEnabled); |
|
1994 } |
|
1995 |
|
1996 void tst_QTreeWidget::match() |
|
1997 { |
|
1998 QTreeWidget tree; |
|
1999 QModelIndexList list = tree.model()->match(QModelIndex(), Qt::DisplayRole, QString()); |
|
2000 |
|
2001 QVERIFY(list.isEmpty()); |
|
2002 } |
|
2003 |
|
2004 void tst_QTreeWidget::columnCount() |
|
2005 { |
|
2006 int columnCountBefore = testWidget->columnCount(); |
|
2007 testWidget->setColumnCount(-1); |
|
2008 QCOMPARE(testWidget->columnCount(), columnCountBefore); |
|
2009 } |
|
2010 |
|
2011 void tst_QTreeWidget::setHeaderLabels() |
|
2012 { |
|
2013 QStringList list = QString("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z").split(","); |
|
2014 testWidget->setHeaderLabels(list); |
|
2015 QCOMPARE(testWidget->header()->count(), list.count()); |
|
2016 } |
|
2017 |
|
2018 void tst_QTreeWidget::setHeaderItem() |
|
2019 { |
|
2020 testWidget->setHeaderItem(0); |
|
2021 QTreeWidgetItem *headerItem = new QTreeWidgetItem(); |
|
2022 |
|
2023 testWidget->setColumnCount(0); |
|
2024 QCOMPARE(testWidget->header()->count(), 0); |
|
2025 QCOMPARE(testWidget->columnCount(), 0); |
|
2026 |
|
2027 headerItem->setText(0, "0"); |
|
2028 headerItem->setText(1, "1"); |
|
2029 testWidget->setHeaderItem(headerItem); |
|
2030 QTest::qWait(100); |
|
2031 QCOMPARE(testWidget->headerItem(), headerItem); |
|
2032 QCOMPARE(headerItem->treeWidget(), static_cast<QTreeWidget *>(testWidget)); |
|
2033 |
|
2034 QCOMPARE(testWidget->header()->count(), 2); |
|
2035 QCOMPARE(testWidget->columnCount(), 2); |
|
2036 |
|
2037 headerItem->setText(2, "2"); |
|
2038 QCOMPARE(testWidget->header()->count(), 3); |
|
2039 QCOMPARE(testWidget->columnCount(), 3); |
|
2040 |
|
2041 delete headerItem; |
|
2042 testWidget->setColumnCount(3); |
|
2043 testWidget->setColumnCount(5); |
|
2044 QCOMPARE(testWidget->model()->headerData(0, Qt::Horizontal, Qt::DisplayRole).toString(), QString("1")); |
|
2045 QCOMPARE(testWidget->model()->headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QString("2")); |
|
2046 QCOMPARE(testWidget->model()->headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QString("3")); |
|
2047 QCOMPARE(testWidget->model()->headerData(3, Qt::Horizontal, Qt::DisplayRole).toString(), QString("4")); |
|
2048 QCOMPARE(testWidget->model()->headerData(4, Qt::Horizontal, Qt::DisplayRole).toString(), QString("5")); |
|
2049 |
|
2050 headerItem = new QTreeWidgetItem(); |
|
2051 testWidget->setHeaderItem(headerItem); |
|
2052 testWidget->model()->insertColumns(0, 5, QModelIndex()); |
|
2053 QCOMPARE(testWidget->model()->headerData(0, Qt::Horizontal, Qt::DisplayRole).toString(), QString("1")); |
|
2054 QCOMPARE(testWidget->model()->headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QString("2")); |
|
2055 QCOMPARE(testWidget->model()->headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QString("3")); |
|
2056 QCOMPARE(testWidget->model()->headerData(3, Qt::Horizontal, Qt::DisplayRole).toString(), QString("4")); |
|
2057 QCOMPARE(testWidget->model()->headerData(4, Qt::Horizontal, Qt::DisplayRole).toString(), QString("5")); |
|
2058 } |
|
2059 |
|
2060 void tst_QTreeWidget::itemWidget_data() |
|
2061 { |
|
2062 editItem_data(); |
|
2063 } |
|
2064 |
|
2065 void tst_QTreeWidget::itemWidget() |
|
2066 { |
|
2067 QFETCH(TreeItemList, topLevelItems); |
|
2068 |
|
2069 QTreeWidget tree; |
|
2070 populate(&tree, topLevelItems, new TreeItem(QStringList() << "1" << "2")); |
|
2071 tree.show(); |
|
2072 |
|
2073 for (int x = 0; x < 2; ++x) { |
|
2074 QTreeWidgetItemIterator it(&tree); |
|
2075 while (QTreeWidgetItem *item = (*it++)) { |
|
2076 for (int col = 0; col < item->columnCount(); ++col) { |
|
2077 if (x == 0) { |
|
2078 QCOMPARE(tree.itemWidget(item, col), static_cast<QWidget*>(0)); |
|
2079 QWidget *editor = new QLineEdit(); |
|
2080 tree.setItemWidget(item, col, editor); |
|
2081 QCOMPARE(tree.itemWidget(item, col), editor); |
|
2082 tree.removeItemWidget(item, col); |
|
2083 QCOMPARE(tree.itemWidget(item, col), static_cast<QWidget*>(0)); |
|
2084 } else { |
|
2085 // ### should you really be able to open a persistent |
|
2086 // editor for an item that isn't editable?? |
|
2087 tree.openPersistentEditor(item, col); |
|
2088 QWidget *editor = qFindChild<QLineEdit*>(&tree); |
|
2089 QVERIFY(editor != 0); |
|
2090 tree.closePersistentEditor(item, col); |
|
2091 } |
|
2092 } |
|
2093 } |
|
2094 } |
|
2095 } |
|
2096 |
|
2097 void tst_QTreeWidget::insertItemsWithSorting_data() |
|
2098 { |
|
2099 QTest::addColumn<int>("sortOrder"); |
|
2100 QTest::addColumn<QStringList>("initialItems"); |
|
2101 QTest::addColumn<QStringList>("insertItems"); |
|
2102 QTest::addColumn<QStringList>("expectedItems"); |
|
2103 QTest::addColumn<IntList>("expectedRows"); |
|
2104 |
|
2105 QTest::newRow("() + (a) = (a)") |
|
2106 << static_cast<int>(Qt::AscendingOrder) |
|
2107 << QStringList() |
|
2108 << (QStringList() << "a") |
|
2109 << (QStringList() << "a") |
|
2110 << IntList(); |
|
2111 QTest::newRow("() + (c, b, a) = (a, b, c)") |
|
2112 << static_cast<int>(Qt::AscendingOrder) |
|
2113 << QStringList() |
|
2114 << (QStringList() << "c" << "b" << "a") |
|
2115 << (QStringList() << "a" << "b" << "c") |
|
2116 << IntList(); |
|
2117 QTest::newRow("() + (a, b, c) = (c, b, a)") |
|
2118 << static_cast<int>(Qt::DescendingOrder) |
|
2119 << QStringList() |
|
2120 << (QStringList() << "a" << "b" << "c") |
|
2121 << (QStringList() << "c" << "b" << "a") |
|
2122 << IntList(); |
|
2123 QTest::newRow("(a) + (b) = (a, b)") |
|
2124 << static_cast<int>(Qt::AscendingOrder) |
|
2125 << QStringList("a") |
|
2126 << (QStringList() << "b") |
|
2127 << (QStringList() << "a" << "b") |
|
2128 << (IntList() << 0); |
|
2129 QTest::newRow("(a) + (b) = (b, a)") |
|
2130 << static_cast<int>(Qt::DescendingOrder) |
|
2131 << QStringList("a") |
|
2132 << (QStringList() << "b") |
|
2133 << (QStringList() << "b" << "a") |
|
2134 << (IntList() << 1); |
|
2135 QTest::newRow("(a, c, b) + (d) = (a, b, c, d)") |
|
2136 << static_cast<int>(Qt::AscendingOrder) |
|
2137 << (QStringList() << "a" << "c" << "b") |
|
2138 << (QStringList() << "d") |
|
2139 << (QStringList() << "a" << "b" << "c" << "d") |
|
2140 << (IntList() << 0 << 1 << 2); |
|
2141 QTest::newRow("(b, c, a) + (d) = (d, c, b, a)") |
|
2142 << static_cast<int>(Qt::DescendingOrder) |
|
2143 << (QStringList() << "b" << "c" << "a") |
|
2144 << (QStringList() << "d") |
|
2145 << (QStringList() << "d" << "c" << "b" << "a") |
|
2146 << (IntList() << 1 << 2 << 3); |
|
2147 { |
|
2148 IntList ascendingRows; |
|
2149 IntList reverseRows; |
|
2150 QStringList ascendingItems; |
|
2151 QStringList reverseItems; |
|
2152 for (int i = 'a'; i <= 'z'; ++i) { |
|
2153 ascendingItems << QString("%0").arg(QLatin1Char(i)); |
|
2154 reverseItems << QString("%0").arg(QLatin1Char('z' - i + 'a')); |
|
2155 ascendingRows << i - 'a'; |
|
2156 reverseRows << 'z' - i + 'a'; |
|
2157 } |
|
2158 QTest::newRow("() + (sorted items) = (sorted items)") |
|
2159 << static_cast<int>(Qt::AscendingOrder) |
|
2160 << QStringList() |
|
2161 << ascendingItems |
|
2162 << ascendingItems |
|
2163 << IntList(); |
|
2164 QTest::newRow("(sorted items) + () = (sorted items)") |
|
2165 << static_cast<int>(Qt::AscendingOrder) |
|
2166 << ascendingItems |
|
2167 << QStringList() |
|
2168 << ascendingItems |
|
2169 << ascendingRows; |
|
2170 QTest::newRow("() + (ascending items) = (reverse items)") |
|
2171 << static_cast<int>(Qt::DescendingOrder) |
|
2172 << QStringList() |
|
2173 << ascendingItems |
|
2174 << reverseItems |
|
2175 << IntList(); |
|
2176 QTest::newRow("(reverse items) + () = (ascending items)") |
|
2177 << static_cast<int>(Qt::AscendingOrder) |
|
2178 << reverseItems |
|
2179 << QStringList() |
|
2180 << ascendingItems |
|
2181 << ascendingRows; |
|
2182 QTest::newRow("(reverse items) + () = (reverse items)") |
|
2183 << static_cast<int>(Qt::DescendingOrder) |
|
2184 << reverseItems |
|
2185 << QStringList() |
|
2186 << reverseItems |
|
2187 << ascendingRows; |
|
2188 } |
|
2189 } |
|
2190 |
|
2191 void tst_QTreeWidget::insertItemsWithSorting() |
|
2192 { |
|
2193 QFETCH(int, sortOrder); |
|
2194 QFETCH(QStringList, initialItems); |
|
2195 QFETCH(QStringList, insertItems); |
|
2196 QFETCH(QStringList, expectedItems); |
|
2197 QFETCH(IntList, expectedRows); |
|
2198 |
|
2199 for (int method = 0; method < 5; ++method) { |
|
2200 QTreeWidget w; |
|
2201 w.setSortingEnabled(true); |
|
2202 w.sortItems(0, static_cast<Qt::SortOrder>(sortOrder)); |
|
2203 for (int i = 0; i < initialItems.count(); ++i) |
|
2204 w.addTopLevelItem(new QTreeWidgetItem(QStringList() << initialItems.at(i))); |
|
2205 |
|
2206 QAbstractItemModel *model = w.model(); |
|
2207 QList<QPersistentModelIndex> persistent; |
|
2208 for (int j = 0; j < model->rowCount(QModelIndex()); ++j) |
|
2209 persistent << model->index(j, 0, QModelIndex()); |
|
2210 |
|
2211 switch (method) { |
|
2212 case 0: |
|
2213 // insert using item constructor |
|
2214 for (int i = 0; i < insertItems.size(); ++i) |
|
2215 new QTreeWidgetItem(&w, QStringList() << insertItems.at(i)); |
|
2216 break; |
|
2217 case 1: |
|
2218 { |
|
2219 // insert using insertTopLevelItems() |
|
2220 QList<QTreeWidgetItem*> lst; |
|
2221 for (int i = 0; i < insertItems.size(); ++i) |
|
2222 lst << new QTreeWidgetItem(QStringList() << insertItems.at(i)); |
|
2223 w.insertTopLevelItems(0, lst); |
|
2224 break; |
|
2225 } |
|
2226 case 2: |
|
2227 // insert using insertTopLevelItem() |
|
2228 for (int i = 0; i < insertItems.size(); ++i) |
|
2229 w.insertTopLevelItem(0, new QTreeWidgetItem(QStringList() << insertItems.at(i))); |
|
2230 break; |
|
2231 case 3: |
|
2232 { |
|
2233 // insert using addTopLevelItems() |
|
2234 QList<QTreeWidgetItem*> lst; |
|
2235 for (int i = 0; i < insertItems.size(); ++i) |
|
2236 lst << new QTreeWidgetItem(QStringList() << insertItems.at(i)); |
|
2237 w.addTopLevelItems(lst); |
|
2238 break; |
|
2239 } |
|
2240 case 4: |
|
2241 // insert using addTopLevelItem() |
|
2242 for (int i = 0; i < insertItems.size(); ++i) |
|
2243 w.addTopLevelItem(new QTreeWidgetItem(QStringList() << insertItems.at(i))); |
|
2244 break; |
|
2245 } |
|
2246 QCOMPARE(w.topLevelItemCount(), expectedItems.count()); |
|
2247 for (int i = 0; i < w.topLevelItemCount(); ++i) |
|
2248 QCOMPARE(w.topLevelItem(i)->text(0), expectedItems.at(i)); |
|
2249 |
|
2250 for (int k = 0; k < persistent.count(); ++k) |
|
2251 QCOMPARE(persistent.at(k).row(), expectedRows.at(k)); |
|
2252 } |
|
2253 } |
|
2254 |
|
2255 void tst_QTreeWidget::insertExpandedItemsWithSorting_data() |
|
2256 { |
|
2257 QTest::addColumn<QStringList>("parentText"); |
|
2258 QTest::addColumn<QStringList>("childText"); |
|
2259 QTest::addColumn<QStringList>("parentResult"); |
|
2260 QTest::addColumn<QStringList>("childResult"); |
|
2261 QTest::newRow("test 1") |
|
2262 << (QStringList() << "c" << "d" << "a" << "b") |
|
2263 << (QStringList() << "e" << "h" << "g" << "f") |
|
2264 << (QStringList() << "d" << "c" << "b" << "a") |
|
2265 << (QStringList() << "h" << "g" << "f" << "e"); |
|
2266 } |
|
2267 |
|
2268 // From Task 134978 |
|
2269 void tst_QTreeWidget::insertExpandedItemsWithSorting() |
|
2270 { |
|
2271 QFETCH(QStringList, parentText); |
|
2272 QFETCH(QStringList, childText); |
|
2273 QFETCH(QStringList, parentResult); |
|
2274 QFETCH(QStringList, childResult); |
|
2275 |
|
2276 // create a tree with autosorting enabled |
|
2277 CustomTreeWidget tree; |
|
2278 tree.setSortingEnabled(true); |
|
2279 |
|
2280 // insert expanded items in unsorted order |
|
2281 QList<QTreeWidgetItem *> items; |
|
2282 for (int i = 0; i < parentText.count(); ++i) { |
|
2283 QTreeWidgetItem *parent = new QTreeWidgetItem(&tree, QStringList(parentText.at(i))); |
|
2284 parent->setExpanded(true); |
|
2285 QVERIFY(parent->isExpanded()); |
|
2286 items << parent; |
|
2287 for (int j = 0; j < childText.count(); ++j) { |
|
2288 QTreeWidgetItem *child = new QTreeWidgetItem(parent, QStringList(childText.at(j))); |
|
2289 items << child; |
|
2290 } |
|
2291 QCOMPARE(parent->childCount(), childText.count()); |
|
2292 QVERIFY(parent->isExpanded()); |
|
2293 } |
|
2294 QVERIFY(tree.model()->rowCount() == parentText.count()); |
|
2295 |
|
2296 // verify that the items are still expanded |
|
2297 foreach (QTreeWidgetItem *item, items) { |
|
2298 if (item->childCount() > 0) |
|
2299 QVERIFY(item->isExpanded()); |
|
2300 QModelIndex idx = tree.indexFromItem(const_cast<QTreeWidgetItem *>(item)); |
|
2301 QVERIFY(idx.isValid()); |
|
2302 //QRect rect = tree.visualRect(idx); |
|
2303 //QVERIFY(rect.isValid()); |
|
2304 // ### it is not guarantied that the index is in the viewport |
|
2305 } |
|
2306 |
|
2307 // verify that the tree is sorted |
|
2308 QAbstractItemModel *model = tree.model(); |
|
2309 QList<QPersistentModelIndex> parents; |
|
2310 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) { |
|
2311 QPersistentModelIndex parent = model->index(i, 0, QModelIndex()); |
|
2312 parents << parent; |
|
2313 } |
|
2314 QList<QPersistentModelIndex> children; |
|
2315 for (int i = 0; i < model->rowCount(parents.first()); ++i) { |
|
2316 QPersistentModelIndex child = model->index(i, 0, parents.first()); |
|
2317 children << child; |
|
2318 } |
|
2319 for (int i = 0; i < parentResult.count(); ++i) { |
|
2320 QTreeWidgetItem *item = tree.topLevelItem(i); |
|
2321 QCOMPARE(item->text(0), parentResult.at(i)); |
|
2322 for (int j = 0; j < childResult.count(); ++j) |
|
2323 QCOMPARE(item->child(j)->text(0), childResult.at(j)); |
|
2324 } |
|
2325 } |
|
2326 |
|
2327 void tst_QTreeWidget::changeDataWithSorting_data() |
|
2328 { |
|
2329 QTest::addColumn<int>("sortOrder"); |
|
2330 QTest::addColumn<QStringList>("initialItems"); |
|
2331 QTest::addColumn<int>("itemIndex"); |
|
2332 QTest::addColumn<QString>("newValue"); |
|
2333 QTest::addColumn<QStringList>("expectedItems"); |
|
2334 QTest::addColumn<IntList>("expectedRows"); |
|
2335 QTest::addColumn<bool>("reorderingExpected"); |
|
2336 |
|
2337 QTest::newRow("change a to b in (a)") |
|
2338 << static_cast<int>(Qt::AscendingOrder) |
|
2339 << (QStringList() << "a") |
|
2340 << 0 << "b" |
|
2341 << (QStringList() << "b") |
|
2342 << (IntList() << 0) |
|
2343 << false; |
|
2344 QTest::newRow("change a to b in (a, c)") |
|
2345 << static_cast<int>(Qt::AscendingOrder) |
|
2346 << (QStringList() << "a" << "c") |
|
2347 << 0 << "b" |
|
2348 << (QStringList() << "b" << "c") |
|
2349 << (IntList() << 0 << 1) |
|
2350 << false; |
|
2351 QTest::newRow("change a to c in (a, b)") |
|
2352 << static_cast<int>(Qt::AscendingOrder) |
|
2353 << (QStringList() << "a" << "b") |
|
2354 << 0 << "c" |
|
2355 << (QStringList() << "b" << "c") |
|
2356 << (IntList() << 1 << 0) |
|
2357 << true; |
|
2358 QTest::newRow("change c to a in (c, b)") |
|
2359 << static_cast<int>(Qt::DescendingOrder) |
|
2360 << (QStringList() << "c" << "b") |
|
2361 << 0 << "a" |
|
2362 << (QStringList() << "b" << "a") |
|
2363 << (IntList() << 1 << 0) |
|
2364 << true; |
|
2365 QTest::newRow("change e to i in (a, c, e, g)") |
|
2366 << static_cast<int>(Qt::AscendingOrder) |
|
2367 << (QStringList() << "a" << "c" << "e" << "g") |
|
2368 << 2 << "i" |
|
2369 << (QStringList() << "a" << "c" << "g" << "i") |
|
2370 << (IntList() << 0 << 1 << 3 << 2) |
|
2371 << true; |
|
2372 QTest::newRow("change e to a in (c, e, g, i)") |
|
2373 << static_cast<int>(Qt::AscendingOrder) |
|
2374 << (QStringList() << "c" << "e" << "g" << "i") |
|
2375 << 1 << "a" |
|
2376 << (QStringList() << "a" << "c" << "g" << "i") |
|
2377 << (IntList() << 1 << 0 << 2 << 3) |
|
2378 << true; |
|
2379 QTest::newRow("change e to f in (c, e, g, i)") |
|
2380 << static_cast<int>(Qt::AscendingOrder) |
|
2381 << (QStringList() << "c" << "e" << "g" << "i") |
|
2382 << 1 << "f" |
|
2383 << (QStringList() << "c" << "f" << "g" << "i") |
|
2384 << (IntList() << 0 << 1 << 2 << 3) |
|
2385 << false; |
|
2386 } |
|
2387 |
|
2388 void tst_QTreeWidget::changeDataWithSorting() |
|
2389 { |
|
2390 QFETCH(int, sortOrder); |
|
2391 QFETCH(QStringList, initialItems); |
|
2392 QFETCH(int, itemIndex); |
|
2393 QFETCH(QString, newValue); |
|
2394 QFETCH(QStringList, expectedItems); |
|
2395 QFETCH(IntList, expectedRows); |
|
2396 QFETCH(bool, reorderingExpected); |
|
2397 |
|
2398 QTreeWidget w; |
|
2399 w.setSortingEnabled(true); |
|
2400 w.sortItems(0, static_cast<Qt::SortOrder>(sortOrder)); |
|
2401 for (int i = 0; i < initialItems.count(); ++i) |
|
2402 w.addTopLevelItem(new QTreeWidgetItem(QStringList() << initialItems.at(i))); |
|
2403 |
|
2404 QAbstractItemModel *model = w.model(); |
|
2405 QList<QPersistentModelIndex> persistent; |
|
2406 for (int j = 0; j < model->rowCount(QModelIndex()); ++j) |
|
2407 persistent << model->index(j, 0, QModelIndex()); |
|
2408 |
|
2409 QSignalSpy dataChangedSpy(model, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &))); |
|
2410 QSignalSpy layoutChangedSpy(model, SIGNAL(layoutChanged())); |
|
2411 |
|
2412 QTreeWidgetItem *item = w.topLevelItem(itemIndex); |
|
2413 item->setText(0, newValue); |
|
2414 for (int i = 0; i < expectedItems.count(); ++i) { |
|
2415 QCOMPARE(w.topLevelItem(i)->text(0), expectedItems.at(i)); |
|
2416 for (int j = 0; j < persistent.count(); ++j) { |
|
2417 if (persistent.at(j).row() == i) // the same toplevel row |
|
2418 QCOMPARE(persistent.at(j).internalPointer(), (void *)w.topLevelItem(i)); |
|
2419 } |
|
2420 } |
|
2421 |
|
2422 for (int k = 0; k < persistent.count(); ++k) |
|
2423 QCOMPARE(persistent.at(k).row(), expectedRows.at(k)); |
|
2424 |
|
2425 QCOMPARE(dataChangedSpy.count(), 1); |
|
2426 QCOMPARE(layoutChangedSpy.count(), reorderingExpected ? 1 : 0); |
|
2427 } |
|
2428 |
|
2429 void tst_QTreeWidget::changeDataWithStableSorting_data() |
|
2430 { |
|
2431 QTest::addColumn<int>("sortOrder"); |
|
2432 QTest::addColumn<QStringList>("initialItems"); |
|
2433 QTest::addColumn<int>("itemIndex"); |
|
2434 QTest::addColumn<QString>("newValue"); |
|
2435 QTest::addColumn<QStringList>("expectedItems"); |
|
2436 QTest::addColumn<IntList>("expectedRows"); |
|
2437 QTest::addColumn<bool>("reorderingExpected"); |
|
2438 QTest::addColumn<bool>("forceChange"); |
|
2439 |
|
2440 QTest::newRow("change a to c in (a, c, c, c, e)") |
|
2441 << static_cast<int>(Qt::AscendingOrder) |
|
2442 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2443 << 0 << "c" |
|
2444 << (QStringList() << "c" << "c" << "c" << "c" << "e") |
|
2445 << (IntList() << 0 << 1 << 2 << 3 << 4) |
|
2446 << false |
|
2447 << false; |
|
2448 QTest::newRow("change e to c in (a, c, c, c, e)") |
|
2449 << static_cast<int>(Qt::AscendingOrder) |
|
2450 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2451 << 4 << "c" |
|
2452 << (QStringList() << "a" << "c" << "c" << "c" << "c") |
|
2453 << (IntList() << 0 << 1 << 2 << 3 << 4) |
|
2454 << false |
|
2455 << false; |
|
2456 QTest::newRow("change 1st c to c in (a, c, c, c, e)") |
|
2457 << static_cast<int>(Qt::AscendingOrder) |
|
2458 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2459 << 1 << "c" |
|
2460 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2461 << (IntList() << 0 << 1 << 2 << 3 << 4) |
|
2462 << false |
|
2463 << true; |
|
2464 QTest::newRow("change 2nd c to c in (a, c, c, c, e)") |
|
2465 << static_cast<int>(Qt::AscendingOrder) |
|
2466 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2467 << 2 << "c" |
|
2468 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2469 << (IntList() << 0 << 1 << 2 << 3 << 4) |
|
2470 << false |
|
2471 << true; |
|
2472 QTest::newRow("change 3rd c to c in (a, c, c, c, e)") |
|
2473 << static_cast<int>(Qt::AscendingOrder) |
|
2474 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2475 << 3 << "c" |
|
2476 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2477 << (IntList() << 0 << 1 << 2 << 3 << 4) |
|
2478 << false |
|
2479 << true; |
|
2480 QTest::newRow("change 1st c to c in (e, c, c, c, a)") |
|
2481 << static_cast<int>(Qt::DescendingOrder) |
|
2482 << (QStringList() << "e" << "c" << "c" << "c" << "a") |
|
2483 << 1 << "c" |
|
2484 << (QStringList() << "e" << "c" << "c" << "c" << "a") |
|
2485 << (IntList() << 0 << 1 << 2 << 3 << 4) |
|
2486 << false |
|
2487 << true; |
|
2488 QTest::newRow("change 2nd c to c in (e, c, c, c, a)") |
|
2489 << static_cast<int>(Qt::DescendingOrder) |
|
2490 << (QStringList() << "e" << "c" << "c" << "c" << "a") |
|
2491 << 2 << "c" |
|
2492 << (QStringList() << "e" << "c" << "c" << "c" << "a") |
|
2493 << (IntList() << 0 << 1 << 2 << 3 << 4) |
|
2494 << false |
|
2495 << true; |
|
2496 QTest::newRow("change 3rd c to c in (e, c, c, c, a)") |
|
2497 << static_cast<int>(Qt::DescendingOrder) |
|
2498 << (QStringList() << "e" << "c" << "c" << "c" << "a") |
|
2499 << 3 << "c" |
|
2500 << (QStringList() << "e" << "c" << "c" << "c" << "a") |
|
2501 << (IntList() << 0 << 1 << 2 << 3 << 4) |
|
2502 << false |
|
2503 << true; |
|
2504 QTest::newRow("change 1st c to b in (a, c, c, c, e)") |
|
2505 << static_cast<int>(Qt::AscendingOrder) |
|
2506 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2507 << 1 << "b" |
|
2508 << (QStringList() << "a" << "b" << "c" << "c" << "e") |
|
2509 << (IntList() << 0 << 1 << 2 << 3 << 4) |
|
2510 << false |
|
2511 << false; |
|
2512 QTest::newRow("change 2nd c to b in (a, c, c, c, e)") |
|
2513 << static_cast<int>(Qt::AscendingOrder) |
|
2514 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2515 << 2 << "b" |
|
2516 << (QStringList() << "a" << "b" << "c" << "c" << "e") |
|
2517 << (IntList() << 0 << 2 << 1 << 3 << 4) |
|
2518 << true |
|
2519 << false; |
|
2520 QTest::newRow("change 3rd c to b in (a, c, c, c, e)") |
|
2521 << static_cast<int>(Qt::AscendingOrder) |
|
2522 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2523 << 3 << "b" |
|
2524 << (QStringList() << "a" << "b" << "c" << "c" << "e") |
|
2525 << (IntList() << 0 << 2 << 3 << 1 << 4) |
|
2526 << true |
|
2527 << false; |
|
2528 QTest::newRow("change 1st c to d in (a, c, c, c, e)") |
|
2529 << static_cast<int>(Qt::AscendingOrder) |
|
2530 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2531 << 1 << "d" |
|
2532 << (QStringList() << "a" << "c" << "c" << "d" << "e") |
|
2533 << (IntList() << 0 << 3 << 1 << 2 << 4) |
|
2534 << true |
|
2535 << false; |
|
2536 QTest::newRow("change 2nd c to d in (a, c, c, c, e)") |
|
2537 << static_cast<int>(Qt::AscendingOrder) |
|
2538 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2539 << 2 << "d" |
|
2540 << (QStringList() << "a" << "c" << "c" << "d" << "e") |
|
2541 << (IntList() << 0 << 1 << 3 << 2 << 4) |
|
2542 << true |
|
2543 << false; |
|
2544 QTest::newRow("change 3rd c to d in (a, c, c, c, e)") |
|
2545 << static_cast<int>(Qt::AscendingOrder) |
|
2546 << (QStringList() << "a" << "c" << "c" << "c" << "e") |
|
2547 << 3 << "d" |
|
2548 << (QStringList() << "a" << "c" << "c" << "d" << "e") |
|
2549 << (IntList() << 0 << 1 << 2 << 3 << 4) |
|
2550 << false |
|
2551 << false; |
|
2552 } |
|
2553 |
|
2554 void tst_QTreeWidget::changeDataWithStableSorting() |
|
2555 { |
|
2556 QFETCH(int, sortOrder); |
|
2557 QFETCH(QStringList, initialItems); |
|
2558 QFETCH(int, itemIndex); |
|
2559 QFETCH(QString, newValue); |
|
2560 QFETCH(QStringList, expectedItems); |
|
2561 QFETCH(IntList, expectedRows); |
|
2562 QFETCH(bool, reorderingExpected); |
|
2563 QFETCH(bool, forceChange); |
|
2564 |
|
2565 class StableItem : public QTreeWidgetItem |
|
2566 { |
|
2567 public: |
|
2568 StableItem(const QStringList &strings) : QTreeWidgetItem(strings, QTreeWidgetItem::UserType) {} |
|
2569 void forceChangeData() { |
|
2570 emitDataChanged(); |
|
2571 } |
|
2572 }; |
|
2573 |
|
2574 QTreeWidget w; |
|
2575 w.setSortingEnabled(true); |
|
2576 w.sortItems(0, static_cast<Qt::SortOrder>(sortOrder)); |
|
2577 for (int i = 0; i < initialItems.count(); ++i) |
|
2578 w.addTopLevelItem(new StableItem(QStringList() << initialItems.at(i))); |
|
2579 |
|
2580 QAbstractItemModel *model = w.model(); |
|
2581 QList<QPersistentModelIndex> persistent; |
|
2582 for (int j = 0; j < model->rowCount(QModelIndex()); ++j) |
|
2583 persistent << model->index(j, 0, QModelIndex()); |
|
2584 |
|
2585 QSignalSpy dataChangedSpy(model, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &))); |
|
2586 QSignalSpy layoutChangedSpy(model, SIGNAL(layoutChanged())); |
|
2587 |
|
2588 StableItem *item = static_cast<StableItem *>(w.topLevelItem(itemIndex)); |
|
2589 item->setText(0, newValue); |
|
2590 if (forceChange) |
|
2591 item->forceChangeData(); |
|
2592 for (int i = 0; i < expectedItems.count(); ++i) { |
|
2593 QCOMPARE(w.topLevelItem(i)->text(0), expectedItems.at(i)); |
|
2594 for (int j = 0; j < persistent.count(); ++j) { |
|
2595 if (persistent.at(j).row() == i) // the same toplevel row |
|
2596 QCOMPARE(persistent.at(j).internalPointer(), (void *)w.topLevelItem(i)); |
|
2597 } |
|
2598 } |
|
2599 |
|
2600 for (int k = 0; k < persistent.count(); ++k) |
|
2601 QCOMPARE(persistent.at(k).row(), expectedRows.at(k)); |
|
2602 |
|
2603 QCOMPARE(dataChangedSpy.count(), 1); |
|
2604 QCOMPARE(layoutChangedSpy.count(), reorderingExpected ? 1 : 0); |
|
2605 } |
|
2606 |
|
2607 void tst_QTreeWidget::itemOperatorLessThan() |
|
2608 { |
|
2609 QTreeWidget tw; |
|
2610 tw.setColumnCount(2); |
|
2611 { |
|
2612 QTreeWidgetItem item1(&tw); |
|
2613 QTreeWidgetItem item2(&tw); |
|
2614 QCOMPARE(item1 < item2, false); |
|
2615 item1.setText(1, "a"); |
|
2616 item2.setText(1, "b"); |
|
2617 QCOMPARE(item1 < item2, false); |
|
2618 item1.setText(0, "a"); |
|
2619 item2.setText(0, "b"); |
|
2620 QCOMPARE(item1 < item2, true); |
|
2621 tw.sortItems(1, Qt::AscendingOrder); |
|
2622 item1.setText(0, "b"); |
|
2623 item2.setText(0, "a"); |
|
2624 QCOMPARE(item1 < item2, true); |
|
2625 tw.sortItems(0, Qt::AscendingOrder); |
|
2626 item1.setData(0, Qt::DisplayRole, 11); |
|
2627 item2.setData(0, Qt::DisplayRole, 2); |
|
2628 QCOMPARE(item1 < item2, false); |
|
2629 } |
|
2630 } |
|
2631 |
|
2632 void tst_QTreeWidget::sortedIndexOfChild_data() |
|
2633 { |
|
2634 QTest::addColumn<int>("sortOrder"); |
|
2635 QTest::addColumn<QStringList>("itemTexts"); |
|
2636 QTest::addColumn<QList<int> >("expectedIndexes"); |
|
2637 |
|
2638 QTest::newRow("three ascending") |
|
2639 << int(Qt::AscendingOrder) |
|
2640 << (QStringList() << "A" << "B" << "C") |
|
2641 << (QList<int>() << 0 << 1 << 2); |
|
2642 |
|
2643 |
|
2644 QTest::newRow("three descending") |
|
2645 << int(Qt::DescendingOrder) |
|
2646 << (QStringList() << "A" << "B" << "C") |
|
2647 << (QList<int>() << 2 << 1 << 0); |
|
2648 } |
|
2649 |
|
2650 void tst_QTreeWidget::sortedIndexOfChild() |
|
2651 { |
|
2652 QFETCH(int, sortOrder); |
|
2653 QFETCH(QStringList, itemTexts); |
|
2654 QFETCH(QList<int>, expectedIndexes); |
|
2655 |
|
2656 QTreeWidget tw; |
|
2657 QList<QTreeWidgetItem*> itms; |
|
2658 QTreeWidgetItem *top = new QTreeWidgetItem(&tw, QStringList() << "top"); |
|
2659 |
|
2660 for (int i = 0; i < itemTexts.count(); ++i) |
|
2661 itms << new QTreeWidgetItem(top, QStringList() << itemTexts.at(i)); |
|
2662 |
|
2663 tw.sortItems(0, (Qt::SortOrder)sortOrder); |
|
2664 tw.expandAll(); |
|
2665 |
|
2666 QVERIFY(itms.count() == expectedIndexes.count()); |
|
2667 for (int j = 0; j < expectedIndexes.count(); ++j) |
|
2668 QCOMPARE(top->indexOfChild(itms.at(j)), expectedIndexes.at(j)); |
|
2669 } |
|
2670 |
|
2671 void tst_QTreeWidget::expandAndCallapse() |
|
2672 { |
|
2673 QTreeWidget tw; |
|
2674 QTreeWidgetItem *top = new QTreeWidgetItem(&tw, QStringList() << "top"); |
|
2675 QTreeWidgetItem *p; |
|
2676 for (int i = 0; i < 10; ++i) { |
|
2677 p = new QTreeWidgetItem(top, QStringList() << QString("%1").arg(i)); |
|
2678 for (int j = 0; j < 10; ++j) |
|
2679 new QTreeWidgetItem(p, QStringList() << QString("%1").arg(j)); |
|
2680 } |
|
2681 QSignalSpy spy0(&tw, SIGNAL(itemExpanded(QTreeWidgetItem *))); |
|
2682 QSignalSpy spy1(&tw, SIGNAL(itemCollapsed(QTreeWidgetItem *))); |
|
2683 |
|
2684 |
|
2685 tw.expandItem(p); |
|
2686 tw.collapseItem(p); |
|
2687 tw.expandItem(p); |
|
2688 tw.expandItem(top); |
|
2689 tw.collapseItem(top); |
|
2690 tw.collapseItem(top); |
|
2691 |
|
2692 QCOMPARE(spy0.count(), 3); |
|
2693 QCOMPARE(spy1.count(), 2); |
|
2694 } |
|
2695 |
|
2696 void tst_QTreeWidget::setDisabled() |
|
2697 { |
|
2698 QTreeWidget w; |
|
2699 QTreeWidgetItem *i1 = new QTreeWidgetItem(); |
|
2700 QTreeWidgetItem *i2 = new QTreeWidgetItem(i1); |
|
2701 QTreeWidgetItem *i3 = new QTreeWidgetItem(i1); |
|
2702 |
|
2703 QTreeWidgetItem *top = new QTreeWidgetItem(&w); |
|
2704 top->setDisabled(true); |
|
2705 top->addChild(i1); |
|
2706 QCOMPARE(i1->isDisabled(), true); |
|
2707 QCOMPARE(i2->isDisabled(), true); |
|
2708 QCOMPARE(i3->isDisabled(), true); |
|
2709 |
|
2710 i1 = top->takeChild(0); |
|
2711 QCOMPARE(i1->isDisabled(), false); |
|
2712 QCOMPARE(i2->isDisabled(), false); |
|
2713 QCOMPARE(i3->isDisabled(), false); |
|
2714 |
|
2715 top->addChild(i1); |
|
2716 QCOMPARE(i1->isDisabled(), true); |
|
2717 QCOMPARE(i2->isDisabled(), true); |
|
2718 QCOMPARE(i3->isDisabled(), true); |
|
2719 |
|
2720 top->setDisabled(false); |
|
2721 QCOMPARE(i1->isDisabled(), false); |
|
2722 QCOMPARE(i2->isDisabled(), false); |
|
2723 QCOMPARE(i3->isDisabled(), false); |
|
2724 |
|
2725 |
|
2726 |
|
2727 QList<QTreeWidgetItem*> children; |
|
2728 children.append(new QTreeWidgetItem()); |
|
2729 children.append(new QTreeWidgetItem()); |
|
2730 children.append(new QTreeWidgetItem()); |
|
2731 i1 = top->takeChild(0); |
|
2732 |
|
2733 top->addChildren(children); |
|
2734 QCOMPARE(top->child(0)->isDisabled(), false); |
|
2735 QCOMPARE(top->child(1)->isDisabled(), false); |
|
2736 QCOMPARE(top->child(1)->isDisabled(), false); |
|
2737 |
|
2738 top->setDisabled(true); |
|
2739 QCOMPARE(top->child(0)->isDisabled(), true); |
|
2740 QCOMPARE(top->child(1)->isDisabled(), true); |
|
2741 QCOMPARE(top->child(1)->isDisabled(), true); |
|
2742 |
|
2743 children = top->takeChildren(); |
|
2744 QCOMPARE(children.at(0)->isDisabled(), false); |
|
2745 QCOMPARE(children.at(1)->isDisabled(), false); |
|
2746 QCOMPARE(children.at(1)->isDisabled(), false); |
|
2747 |
|
2748 } |
|
2749 |
|
2750 void tst_QTreeWidget::removeSelectedItem() |
|
2751 { |
|
2752 QTreeWidget *w = new QTreeWidget(); |
|
2753 w->setSortingEnabled(true); |
|
2754 |
|
2755 QTreeWidgetItem *first = new QTreeWidgetItem(); |
|
2756 first->setText(0, QLatin1String("D")); |
|
2757 w->addTopLevelItem(first); |
|
2758 |
|
2759 QTreeWidgetItem *itm = new QTreeWidgetItem(); |
|
2760 itm->setText(0, QLatin1String("D")); |
|
2761 w->addTopLevelItem(itm); |
|
2762 |
|
2763 itm = new QTreeWidgetItem(); |
|
2764 itm->setText(0, QLatin1String("C")); |
|
2765 w->addTopLevelItem(itm); |
|
2766 itm->setSelected(true); |
|
2767 |
|
2768 itm = new QTreeWidgetItem(); |
|
2769 itm->setText(0, QLatin1String("A")); |
|
2770 w->addTopLevelItem(itm); |
|
2771 |
|
2772 //w->show(); |
|
2773 |
|
2774 QItemSelectionModel *selModel = w->selectionModel(); |
|
2775 QCOMPARE(selModel->hasSelection(), true); |
|
2776 QCOMPARE(selModel->selectedRows().count(), 1); |
|
2777 |
|
2778 QTreeWidgetItem *taken = w->takeTopLevelItem(2); |
|
2779 QCOMPARE(taken->text(0), QLatin1String("C")); |
|
2780 |
|
2781 QCOMPARE(selModel->hasSelection(), false); |
|
2782 QCOMPARE(selModel->selectedRows().count(), 0); |
|
2783 QItemSelection sel = selModel->selection(); |
|
2784 QCOMPARE(selModel->isSelected(w->model()->index(0,0)), false); |
|
2785 |
|
2786 delete w; |
|
2787 } |
|
2788 |
|
2789 class AnotherTreeWidget : public QTreeWidget |
|
2790 { |
|
2791 Q_OBJECT |
|
2792 public: |
|
2793 AnotherTreeWidget(QWidget *parent = 0) : QTreeWidget(parent) {} |
|
2794 void deleteCurrent() { if (currentItem()) delete currentItem(); } |
|
2795 }; |
|
2796 |
|
2797 void tst_QTreeWidget::removeCurrentItem() |
|
2798 { |
|
2799 AnotherTreeWidget widget; |
|
2800 QObject::connect(widget.selectionModel(), |
|
2801 SIGNAL(currentChanged(QModelIndex,QModelIndex)), |
|
2802 &widget, SLOT(clear())); |
|
2803 QTreeWidgetItem *item = new QTreeWidgetItem(&widget); |
|
2804 widget.setCurrentItem(item); |
|
2805 widget.deleteCurrent(); |
|
2806 } |
|
2807 |
|
2808 void tst_QTreeWidget::removeCurrentItem_task186451() |
|
2809 { |
|
2810 AnotherTreeWidget widget; |
|
2811 QTreeWidgetItem *item = new QTreeWidgetItem(&widget, QStringList() << "1"); |
|
2812 QTreeWidgetItem *item2 = new QTreeWidgetItem(&widget, QStringList() << "2"); |
|
2813 widget.setCurrentItem(item); |
|
2814 widget.deleteCurrent(); |
|
2815 |
|
2816 QVERIFY(item2->isSelected()); |
|
2817 QCOMPARE(item2, widget.currentItem()); |
|
2818 } |
|
2819 |
|
2820 |
|
2821 class TreeWidget : QTreeWidget { |
|
2822 |
|
2823 public: |
|
2824 QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const { |
|
2825 return QTreeWidget::indexFromItem(item, column); |
|
2826 } |
|
2827 QTreeWidgetItem *itemFromIndex(const QModelIndex &index) const { |
|
2828 return QTreeWidget::itemFromIndex(index); |
|
2829 } |
|
2830 }; |
|
2831 |
|
2832 |
|
2833 void tst_QTreeWidget::randomExpand() |
|
2834 { |
|
2835 QTreeWidget tree; |
|
2836 QTreeWidgetItem *item1 = new QTreeWidgetItem(&tree); |
|
2837 QTreeWidgetItem *item3 = new QTreeWidgetItem(&tree, item1); |
|
2838 new QTreeWidgetItem(item1); |
|
2839 new QTreeWidgetItem(item3); |
|
2840 |
|
2841 tree.expandAll(); |
|
2842 |
|
2843 /* |
|
2844 item1 |
|
2845 \- item2 |
|
2846 item3 |
|
2847 \- item4 |
|
2848 */ |
|
2849 |
|
2850 QTreeWidgetItem *newItem1 = 0; |
|
2851 for (int i = 0; i < 100; i++) { |
|
2852 newItem1 = new QTreeWidgetItem(&tree, item1); |
|
2853 tree.setItemExpanded(newItem1, true); |
|
2854 QCOMPARE(tree.isItemExpanded(newItem1), true); |
|
2855 |
|
2856 QTreeWidgetItem *x = new QTreeWidgetItem(); |
|
2857 QCOMPARE(tree.isItemExpanded(newItem1), true); |
|
2858 newItem1->addChild(x); |
|
2859 |
|
2860 QCOMPARE(tree.isItemExpanded(newItem1), true); |
|
2861 } |
|
2862 |
|
2863 } |
|
2864 |
|
2865 void tst_QTreeWidget::crashTest() |
|
2866 { |
|
2867 QTreeWidget *tree = new QTreeWidget(); |
|
2868 tree->setColumnCount(1); |
|
2869 tree->show(); |
|
2870 |
|
2871 QTreeWidgetItem *item1 = new QTreeWidgetItem(tree); |
|
2872 item1->setText(0, "item1"); |
|
2873 tree->setItemExpanded(item1, true); |
|
2874 QTreeWidgetItem *item2 = new QTreeWidgetItem(item1); |
|
2875 item2->setText(0, "item2"); |
|
2876 |
|
2877 QTreeWidgetItem *item3 = new QTreeWidgetItem(tree, item1); |
|
2878 item3->setText(0, "item3"); |
|
2879 tree->setItemExpanded(item3, true); |
|
2880 QTreeWidgetItem *item4 = new QTreeWidgetItem(item3); |
|
2881 item4->setText(0, "item4"); |
|
2882 |
|
2883 QTreeWidgetItem *item5 = new QTreeWidgetItem(tree, item3); |
|
2884 item5->setText(0, "item5"); |
|
2885 tree->setItemExpanded(item5, true); |
|
2886 QTreeWidgetItem *item6 = new QTreeWidgetItem(item5); |
|
2887 item6->setText(0, "item6"); |
|
2888 |
|
2889 for (int i = 0; i < 1000; i++) { |
|
2890 QTreeWidgetItem *newItem1 = new QTreeWidgetItem(tree, item1); |
|
2891 newItem1->setText(0, "newItem"); |
|
2892 QTreeWidgetItem *newItem2 = new QTreeWidgetItem(newItem1); |
|
2893 newItem2->setText(0, "subItem1"); |
|
2894 QTreeWidgetItem *newItem3 = new QTreeWidgetItem(newItem1, newItem2); |
|
2895 newItem3->setText(0, "subItem2"); |
|
2896 delete item3; |
|
2897 item3 = newItem1; |
|
2898 } |
|
2899 QApplication::instance()->processEvents(); |
|
2900 |
|
2901 delete tree; |
|
2902 } |
|
2903 |
|
2904 class CrashWidget : public QTreeWidget |
|
2905 { |
|
2906 public: |
|
2907 CrashWidget(QWidget *parent = 0) : QTreeWidget(parent), i(0) { |
|
2908 setSortingEnabled(true); |
|
2909 timerId = startTimer(10); |
|
2910 } |
|
2911 int i; |
|
2912 protected: |
|
2913 void timerEvent(QTimerEvent * event) { |
|
2914 if (event->timerId() == timerId) { |
|
2915 QTreeWidgetItem *newItem = new QTreeWidgetItem((QStringList() << QString::number(i++))); |
|
2916 m_list.append(newItem); |
|
2917 insertTopLevelItem(0, newItem); |
|
2918 while (m_list.count() > 10) |
|
2919 delete m_list.takeFirst(); |
|
2920 } |
|
2921 QTreeWidget::timerEvent(event); |
|
2922 } |
|
2923 private: |
|
2924 int timerId; |
|
2925 QList<QTreeWidgetItem*> m_list; |
|
2926 }; |
|
2927 |
|
2928 void tst_QTreeWidget::sortAndSelect() |
|
2929 { |
|
2930 CrashWidget w; |
|
2931 w.resize(1, 1); |
|
2932 w.show(); |
|
2933 while (w.i < 100) { |
|
2934 QApplication::processEvents(); |
|
2935 if (w.i & 16) { |
|
2936 QPoint pt = w.viewport()->rect().center(); |
|
2937 QTest::mouseClick(w.viewport(), Qt::LeftButton, Qt::NoModifier, pt); |
|
2938 } |
|
2939 } |
|
2940 QVERIFY(true); |
|
2941 } |
|
2942 |
|
2943 void tst_QTreeWidget::defaultRowSizes() |
|
2944 { |
|
2945 QTreeWidget *tw = new QTreeWidget(); |
|
2946 tw->setIconSize(QSize(50, 50)); |
|
2947 tw->setColumnCount(6); |
|
2948 for (int i=0; i<10; ++i) { |
|
2949 QTreeWidgetItem *it = new QTreeWidgetItem(tw); |
|
2950 for (int j=0; j<tw->columnCount() - 1; ++j) { |
|
2951 it->setText(j, "This is a test"); |
|
2952 } |
|
2953 QPixmap icon = tw->style()->standardPixmap((QStyle::StandardPixmap)(i + QStyle::SP_TitleBarMenuButton)); |
|
2954 if (icon.isNull()) |
|
2955 QSKIP("No pixmap found on current style, skipping this test.", SkipSingle); |
|
2956 it->setIcon(tw->columnCount() - 1, |
|
2957 icon.scaled(tw->iconSize())); |
|
2958 } |
|
2959 tw->resize(100,100); |
|
2960 tw->show(); |
|
2961 QApplication::processEvents(); |
|
2962 |
|
2963 QRect visualRect = tw->visualItemRect(tw->topLevelItem(0)); |
|
2964 QVERIFY(visualRect.height() >= 50); |
|
2965 } |
|
2966 |
|
2967 void tst_QTreeWidget::task191552_rtl() |
|
2968 { |
|
2969 Qt::LayoutDirection oldDir = qApp->layoutDirection(); |
|
2970 qApp->setLayoutDirection(Qt::RightToLeft); |
|
2971 |
|
2972 QTreeWidget tw; |
|
2973 tw.setColumnCount(1); |
|
2974 QTreeWidgetItem *item = new QTreeWidgetItem(&tw); |
|
2975 item->setText(0, "item 1"); |
|
2976 item->setCheckState(0, Qt::Checked); |
|
2977 QCOMPARE(item->checkState(0), Qt::Checked); |
|
2978 tw.show(); |
|
2979 QTest::qWait(50); |
|
2980 QStyleOptionViewItem opt; |
|
2981 opt.initFrom(&tw); |
|
2982 opt.rect = tw.visualItemRect(item); |
|
2983 const QRect checkRect = tw.style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt, &tw); |
|
2984 QTest::mouseClick(tw.viewport(), Qt::LeftButton, Qt::NoModifier, checkRect.center()); |
|
2985 QTest::qWait(200); |
|
2986 QCOMPARE(item->checkState(0), Qt::Unchecked); |
|
2987 |
|
2988 qApp->setLayoutDirection(oldDir); |
|
2989 } |
|
2990 |
|
2991 void tst_QTreeWidget::task203673_selection() |
|
2992 { |
|
2993 //we try to change the selection by rightclick + ctrl |
|
2994 //it should do anything when using ExtendedSelection |
|
2995 |
|
2996 QTreeWidget tw; |
|
2997 tw.setColumnCount(1); |
|
2998 QTreeWidgetItem *item1 = new QTreeWidgetItem(&tw); |
|
2999 item1->setText(0, "item 1"); |
|
3000 tw.setSelectionMode(QTreeView::ExtendedSelection); |
|
3001 |
|
3002 QPoint center = tw.visualItemRect(item1).center(); |
|
3003 QCOMPARE(item1->isSelected(), false); |
|
3004 |
|
3005 QTest::mouseClick(tw.viewport(), Qt::RightButton, Qt::ControlModifier, center); |
|
3006 QCOMPARE(item1->isSelected(), false); |
|
3007 |
|
3008 QTest::mouseClick(tw.viewport(), Qt::LeftButton, Qt::ControlModifier, center); |
|
3009 QCOMPARE(item1->isSelected(), true); |
|
3010 |
|
3011 QTest::mouseClick(tw.viewport(), Qt::RightButton, Qt::ControlModifier, center); |
|
3012 QCOMPARE(item1->isSelected(), true); //it shouldn't change |
|
3013 |
|
3014 QTest::mouseClick(tw.viewport(), Qt::LeftButton, Qt::ControlModifier, center); |
|
3015 QCOMPARE(item1->isSelected(), false); |
|
3016 } |
|
3017 |
|
3018 |
|
3019 void tst_QTreeWidget::rootItemFlags() |
|
3020 { |
|
3021 QTreeWidget tw; |
|
3022 tw.setColumnCount(1); |
|
3023 QTreeWidgetItem *item = new QTreeWidgetItem(&tw); |
|
3024 item->setText(0, "item 1"); |
|
3025 |
|
3026 QVERIFY(tw.invisibleRootItem()->flags() & Qt::ItemIsDropEnabled); |
|
3027 |
|
3028 tw.invisibleRootItem()->setFlags(tw.invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled); |
|
3029 |
|
3030 QVERIFY(!(tw.invisibleRootItem()->flags() & Qt::ItemIsDropEnabled)); |
|
3031 } |
|
3032 |
|
3033 void tst_QTreeWidget::task218661_setHeaderData() |
|
3034 { |
|
3035 //We check that setting header data out of bounds returns false |
|
3036 //and doesn't increase the size of the model |
|
3037 QTreeWidget tw; |
|
3038 tw.setColumnCount(1); |
|
3039 QCOMPARE(tw.columnCount(), 1); |
|
3040 |
|
3041 QCOMPARE(tw.model()->setHeaderData(99999, Qt::Horizontal, QVariant()), false); |
|
3042 |
|
3043 QCOMPARE(tw.columnCount(), 1); |
|
3044 } |
|
3045 |
|
3046 void tst_QTreeWidget::task245280_sortChildren() |
|
3047 { |
|
3048 QTreeWidget tw; |
|
3049 tw.setColumnCount(2); |
|
3050 |
|
3051 QTreeWidgetItem top(&tw); |
|
3052 top.setText(0,"Col 0"); |
|
3053 top.setText(1,"Col 1"); |
|
3054 QTreeWidgetItem item1(&top); |
|
3055 item1.setText(0,"X"); |
|
3056 item1.setText(1,"0"); |
|
3057 QTreeWidgetItem item2(&top); |
|
3058 item2.setText(0,"A"); |
|
3059 item2.setText(1,"4"); |
|
3060 QTreeWidgetItem item3(&top); |
|
3061 item3.setText(0,"E"); |
|
3062 item3.setText(1,"1"); |
|
3063 QTreeWidgetItem item4(&top); |
|
3064 item4.setText(0,"Z"); |
|
3065 item4.setText(1,"3"); |
|
3066 QTreeWidgetItem item5(&top); |
|
3067 item5.setText(0,"U"); |
|
3068 item5.setText(1,"2"); |
|
3069 tw.expandAll(); |
|
3070 tw.show(); |
|
3071 top.sortChildren(1,Qt::AscendingOrder); |
|
3072 |
|
3073 for (int i = 0; i < top.childCount(); ++i) |
|
3074 QCOMPARE(top.child(i)->text(1), QString::number(i)); |
|
3075 } |
|
3076 |
|
3077 void tst_QTreeWidget::task253109_itemHeight() |
|
3078 { |
|
3079 QTreeWidget treeWidget; |
|
3080 treeWidget.setColumnCount(1); |
|
3081 treeWidget.show(); |
|
3082 QTest::qWait(200); |
|
3083 |
|
3084 QTreeWidgetItem item(&treeWidget); |
|
3085 class MyWidget : public QWidget |
|
3086 { |
|
3087 virtual QSize sizeHint() const { return QSize(200,100); } |
|
3088 } w; |
|
3089 treeWidget.setItemWidget(&item, 0, &w); |
|
3090 |
|
3091 QTest::qWait(200); |
|
3092 QCOMPARE(w.geometry(), treeWidget.visualItemRect(&item)); |
|
3093 |
|
3094 } |
|
3095 |
|
3096 void tst_QTreeWidget::task206367_duplication() |
|
3097 { |
|
3098 QTreeWidget treeWidget; |
|
3099 treeWidget.show(); |
|
3100 treeWidget.resize(200, 200); |
|
3101 |
|
3102 treeWidget.setSortingEnabled(true); |
|
3103 QTreeWidgetItem* rootItem = new QTreeWidgetItem( &treeWidget, QStringList("root") ); |
|
3104 for (int nFile = 0; nFile < 2; nFile++ ) { |
|
3105 QTreeWidgetItem* itemFile = new QTreeWidgetItem(rootItem, QStringList(QString::number(nFile))); |
|
3106 for (int nRecord = 0; nRecord < 2; nRecord++) |
|
3107 new QTreeWidgetItem(itemFile , QStringList(QString::number(nRecord))); |
|
3108 itemFile->setExpanded(true); |
|
3109 } |
|
3110 rootItem->setExpanded(true); |
|
3111 QTest::qWait(2000); |
|
3112 |
|
3113 //there should be enough room for 2x2 items. If there is a scrollbar, it means the items are duplicated |
|
3114 QVERIFY(!treeWidget.verticalScrollBar()->isVisible()); |
|
3115 |
|
3116 } |
|
3117 |
|
3118 void tst_QTreeWidget::itemSelectionChanged() |
|
3119 { |
|
3120 QVERIFY(testWidget); |
|
3121 if(testWidget->topLevelItem(0)) |
|
3122 QVERIFY(testWidget->topLevelItem(0)->isSelected()); |
|
3123 } |
|
3124 |
|
3125 void tst_QTreeWidget::selectionOrder() |
|
3126 { |
|
3127 testWidget->setColumnCount(1); |
|
3128 QList<QTreeWidgetItem *> items; |
|
3129 for (int i = 0; i < 10; ++i) |
|
3130 items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i)))); |
|
3131 testWidget->insertTopLevelItems(0, items); |
|
3132 |
|
3133 QModelIndex idx = testWidget->indexFromItem(items[0]); |
|
3134 connect(testWidget, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelectionChanged())); |
|
3135 testWidget->selectionModel()->select(idx, QItemSelectionModel::SelectCurrent); |
|
3136 disconnect(testWidget, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelectionChanged())); |
|
3137 } |
|
3138 |
|
3139 void tst_QTreeWidget::setSelectionModel() |
|
3140 { |
|
3141 QTreeWidget tree; |
|
3142 for(int i = 0; i < 3; ++i) |
|
3143 new QTreeWidgetItem(&tree, QStringList(QString::number(i))); |
|
3144 QItemSelectionModel selection(tree.model()); |
|
3145 selection.select(tree.model()->index(1,0), QItemSelectionModel::Select); |
|
3146 tree.setSelectionModel(&selection); |
|
3147 QCOMPARE(tree.topLevelItem(1)->isSelected(), true); |
|
3148 } |
|
3149 |
|
3150 void tst_QTreeWidget::task217309() |
|
3151 { |
|
3152 QTreeWidgetItem item; |
|
3153 item.setFlags(item.flags() | Qt::ItemIsTristate); |
|
3154 QTreeWidgetItem subitem1; |
|
3155 subitem1.setFlags(subitem1.flags() | Qt::ItemIsTristate); |
|
3156 QTreeWidgetItem subitem2; |
|
3157 subitem2.setFlags(subitem2.flags() | Qt::ItemIsTristate); |
|
3158 item.addChild(&subitem1); |
|
3159 item.addChild(&subitem2); |
|
3160 subitem1.setCheckState(0, Qt::Checked); |
|
3161 subitem2.setCheckState(0, Qt::Unchecked); |
|
3162 |
|
3163 QVERIFY(item.data(0, Qt::CheckStateRole) == Qt::PartiallyChecked); |
|
3164 |
|
3165 subitem2.setCheckState(0, Qt::PartiallyChecked); |
|
3166 QVERIFY(item.data(0, Qt::CheckStateRole) == Qt::PartiallyChecked); |
|
3167 |
|
3168 subitem2.setCheckState(0, Qt::Checked); |
|
3169 QVERIFY(item.data(0, Qt::CheckStateRole) == Qt::Checked); |
|
3170 } |
|
3171 |
|
3172 class TreeWidgetItem : public QTreeWidgetItem |
|
3173 { |
|
3174 |
|
3175 public: |
|
3176 void _emitDataChanged() { emitDataChanged(); } |
|
3177 |
|
3178 }; |
|
3179 |
|
3180 void tst_QTreeWidget::emitDataChanged() |
|
3181 { |
|
3182 |
|
3183 QTreeWidget *tree = new QTreeWidget; |
|
3184 QSignalSpy spy(tree, SIGNAL(itemChanged(QTreeWidgetItem *, int))); |
|
3185 TreeWidgetItem *item = new TreeWidgetItem(); |
|
3186 tree->insertTopLevelItem(0, item); |
|
3187 item->_emitDataChanged(); |
|
3188 QCOMPARE(spy.count(), 1); |
|
3189 |
|
3190 } |
|
3191 |
|
3192 void tst_QTreeWidget::setCurrentItemExpandsParent() |
|
3193 { |
|
3194 QTreeWidget w; |
|
3195 w.setColumnCount(1); |
|
3196 QTreeWidgetItem *i1 = new QTreeWidgetItem(&w, QStringList() << "parent"); |
|
3197 QTreeWidgetItem *i2 = new QTreeWidgetItem(i1, QStringList() << "child"); |
|
3198 QVERIFY(!i2->isExpanded()); |
|
3199 QVERIFY(w.currentItem() == 0); |
|
3200 w.setCurrentItem(i2); |
|
3201 QVERIFY(!i2->isExpanded()); |
|
3202 QCOMPARE(w.currentItem(), i2); |
|
3203 } |
|
3204 |
|
3205 void tst_QTreeWidget::task239150_editorWidth() |
|
3206 { |
|
3207 //we check that an item with no text will get an editor with a correct size |
|
3208 QTreeWidget tree; |
|
3209 |
|
3210 QStyleOptionFrameV2 opt; |
|
3211 opt.init(&tree); |
|
3212 const int minWidth = tree.style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(0, 0). |
|
3213 expandedTo(QApplication::globalStrut()), 0).width(); |
|
3214 |
|
3215 { |
|
3216 QTreeWidgetItem item; |
|
3217 item.setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ); |
|
3218 tree.addTopLevelItem(&item); |
|
3219 QVERIFY(tree.itemWidget(&item, 0) == 0); |
|
3220 tree.editItem(&item); |
|
3221 QVERIFY(tree.itemWidget(&item, 0)); |
|
3222 QVERIFY(tree.itemWidget(&item, 0)->width() >= minWidth); |
|
3223 } |
|
3224 |
|
3225 //now let's test it with an item with a lot of text |
|
3226 { |
|
3227 QTreeWidgetItem item; |
|
3228 item.setText(0, "foooooooooooooooooooooooo"); |
|
3229 item.setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ); |
|
3230 tree.addTopLevelItem(&item); |
|
3231 QVERIFY(tree.itemWidget(&item, 0) == 0); |
|
3232 tree.editItem(&item); |
|
3233 QVERIFY(tree.itemWidget(&item, 0)); |
|
3234 QVERIFY(tree.itemWidget(&item, 0)->width() >= minWidth + tree.fontMetrics().width(item.text(0))); |
|
3235 } |
|
3236 } |
|
3237 |
|
3238 |
|
3239 |
|
3240 void tst_QTreeWidget::setTextUpdate() |
|
3241 { |
|
3242 QTreeWidget treeWidget; |
|
3243 treeWidget.setColumnCount(2); |
|
3244 |
|
3245 class MyItemDelegate : public QStyledItemDelegate |
|
3246 { |
|
3247 public: |
|
3248 MyItemDelegate() : numPaints(0) { } |
|
3249 void paint(QPainter *painter, |
|
3250 const QStyleOptionViewItem &option, const QModelIndex &index) const |
|
3251 { |
|
3252 numPaints++; |
|
3253 QStyledItemDelegate::paint(painter, option, index); |
|
3254 } |
|
3255 |
|
3256 mutable int numPaints; |
|
3257 } delegate; |
|
3258 |
|
3259 treeWidget.setItemDelegate(&delegate); |
|
3260 treeWidget.show(); |
|
3261 QStringList strList; |
|
3262 strList << "variable1" << "0"; |
|
3263 QTreeWidgetItem *item = new QTreeWidgetItem(strList); |
|
3264 treeWidget.insertTopLevelItem(0, item); |
|
3265 QTest::qWait(50); |
|
3266 QTRY_VERIFY(delegate.numPaints > 0); |
|
3267 delegate.numPaints = 0; |
|
3268 |
|
3269 item->setText(1, "42"); |
|
3270 QApplication::processEvents(); |
|
3271 QTRY_VERIFY(delegate.numPaints > 0); |
|
3272 } |
|
3273 |
|
3274 |
|
3275 |
|
3276 QTEST_MAIN(tst_QTreeWidget) |
|
3277 #include "tst_qtreewidget.moc" |