1 /***************************************************************************
2 * Copyright (C) 2010 by Frank Reininghaus (frank78ac@googlemail.com) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include <qtest_kde.h>
22 #include "views/dolphintreeview.h"
24 #include <qtestkeyboard.h>
25 #include <QtGui/QStringListModel>
27 class DolphinTreeViewTest
: public QObject
33 void bug218114_visualRegionForSelection();
38 * TestView is a simple view class derived from DolphinTreeView.
39 * It makes sure that the visualRect for each index contains only the item text as
40 * returned by QAbstractItemModel::data(...) for the role Qt::DisplayRole.
42 * We have to check that DolphinTreeView handles the case of visualRects with different widths
43 * correctly because this is the case in DolphinDetailsView which is derived from DolphinTreeView.
46 class TestView
: public DolphinTreeView
52 TestView(QWidget
* parent
= 0) : DolphinTreeView(parent
) {};
55 QRect
visualRect(const QModelIndex
& index
) const {
56 QRect rect
= DolphinTreeView::visualRect(index
);
58 const QStyleOptionViewItem option
= viewOptions();
59 const QFontMetrics
fontMetrics(option
.font
);
60 int width
= option
.decorationSize
.width() + fontMetrics
.width(model()->data(index
).toString());
69 * QTreeView assumes implicitly that the width of each item's visualRect is the same. This leads to painting
70 * problems in Dolphin if items with different widths are in one QItemSelectionRange, see
72 * https://bugs.kde.org/show_bug.cgi?id=218114
74 * To fix this, DolphinTreeView has a custom implementation of visualRegionForSelection(). The following
75 * unit test checks that.
78 void DolphinTreeViewTest::bug218114_visualRegionForSelection()
81 items
<< "a" << "an item with a long name" << "a";
82 QStringListModel
model(items
);
84 QModelIndex index0
= model
.index(0, 0);
85 QModelIndex index1
= model
.index(1, 0);
86 QModelIndex index2
= model
.index(2, 0);
89 view
.setModel(&model
);
90 view
.setSelectionMode(QAbstractItemView::ExtendedSelection
);
91 view
.resize(400, 400);
93 QTest::qWaitForWindowShown(&view
);
95 // First check that the width of index1 is larger than that of index0 and index2 (this triggers the bug).
97 QVERIFY(view
.visualRect(index0
).width() < view
.visualRect(index1
).width());
98 QVERIFY(view
.visualRect(index2
).width() < view
.visualRect(index1
).width());
100 // Select all items in one go.
103 const QItemSelection selection
= view
.selectionModel()->selection();
104 QCOMPARE(selection
.count(), 1);
105 QCOMPARE(selection
.indexes().count(), 3);
107 // Verify that the visualRegionForSelection contains all visualRects.
108 // We do this indirectly using QRegion::boundingRect() because
109 // QRegion::contains(const QRect&) returns true even if the QRect is not
110 // entirely inside the QRegion.
112 const QRegion region
= view
.visualRegionForSelection(selection
);
113 const QRect boundingRect
= region
.boundingRect();
115 QVERIFY(boundingRect
.contains(view
.visualRect(index0
)));
116 QVERIFY(boundingRect
.contains(view
.visualRect(index1
)));
117 QVERIFY(boundingRect
.contains(view
.visualRect(index2
)));
120 QTEST_KDEMAIN(DolphinTreeViewTest
, GUI
)
122 #include "dolphintreeviewtest.moc"