summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/itemmodels/qitemselectionmodel
diff options
context:
space:
mode:
authorFilippo Cucchetto <f.cucchetto@asem.it>2017-04-26 12:49:36 +0200
committerFilippo Cucchetto <filippocucchetto@gmail.com>2017-05-09 13:46:51 +0000
commitbe0a221ae491b8426b3a4cdf0d5863701e922c1f (patch)
tree880471f6321e1df1b546a34e39bcf8bcae12387f /tests/auto/corelib/itemmodels/qitemselectionmodel
parent16f950c702646b0f07c8c0a473759c20a6313fb2 (diff)
Fix missing handling of columns when merging selection ranges
This commit fixes two bugs: 1) Two ranges should not be merged if they are of different columns. The old code would have merged (0,0) with (1, 1). Tranforming a selection of just two indexes in a rectangle of four indexes. 2) The QItemSelectionRange appended had wrong column and worked only for indexes of the first column. For example if 'tl' was (0, 1) than br was (0, 1) so the QItemSelectionRange would have be ((0,1), (0, 1-1)) so ((0,1), (0,0)). This QItemSelectionRange is invalid because topLeft columns is greater than bottomRight column. The fix take in consideration the bottomRight column. Task-number: QTBUG-58871 Change-Id: I591ef0bcc63926f24a7b1ced002af9b7737a4b6e Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'tests/auto/corelib/itemmodels/qitemselectionmodel')
-rw-r--r--tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp b/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp
index fb3968c838..b05e3968ea 100644
--- a/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp
+++ b/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp
@@ -92,6 +92,9 @@ private slots:
void QTBUG48402_data();
void QTBUG48402();
+ void QTBUG58851_data();
+ void QTBUG58851();
+
private:
QAbstractItemModel *model;
QItemSelectionModel *selection;
@@ -2848,5 +2851,76 @@ void tst_QItemSelectionModel::QTBUG48402()
QCOMPARE(QItemSelectionRange(helper.tl, helper.br), QItemSelectionRange(dtl, dbr));
}
+void tst_QItemSelectionModel::QTBUG58851_data()
+{
+ using IntPair = std::pair<int, int>;
+ using IntPairList = QList<IntPair>;
+ using IntPairPair = std::pair<IntPair, IntPair>;
+ using IntPairPairList = QList<IntPairPair>;
+
+ QTest::addColumn<IntPairPairList>("rangesToSelect");
+ QTest::addColumn<IntPairList>("expectedSelectedIndexesPairs");
+ QTest::newRow("Single index in > 0 column")
+ << (IntPairPairList() << IntPairPair(IntPair(0, 1), IntPair(0, 1)))
+ << (IntPairList() << IntPair(0, 1));
+ QTest::newRow("Rectangle in > 0 column")
+ << (IntPairPairList() << IntPairPair(IntPair(0, 1), IntPair(1, 2)))
+ << (IntPairList() << IntPair(0, 1) << IntPair(0, 2) << IntPair(1, 1) << IntPair(1, 2));
+ QTest::newRow("Diagonal in > 0 column")
+ << (IntPairPairList()
+ << IntPairPair(IntPair(0, 1), IntPair(0, 1))
+ << IntPairPair(IntPair(1, 2), IntPair(1, 2))
+ << IntPairPair(IntPair(2, 3), IntPair(2, 3)))
+ << (IntPairList()
+ << IntPair(0, 1)
+ << IntPair(1, 2)
+ << IntPair(2, 3));
+}
+
+void tst_QItemSelectionModel::QTBUG58851()
+{
+ using IntPair = std::pair<int, int>;
+ using IntPairList = QList<IntPair>;
+ using IntPairPair = std::pair<IntPair, IntPair>;
+ using IntPairPairList = QList<IntPairPair>;
+
+ QFETCH(IntPairPairList, rangesToSelect);
+ QFETCH(IntPairList, expectedSelectedIndexesPairs);
+
+ QStandardItemModel model(4, 4);
+ for (int row = 0; row < model.rowCount(); ++row) {
+ for (int column = 0; column < model.columnCount(); ++column) {
+ QStandardItem *item = new QStandardItem(QString("%0%1").arg(row).arg(column));
+ model.setItem(row, column, item);
+ }
+ }
+
+ QSortFilterProxyModel proxy;
+ proxy.setSourceModel(&model);
+ proxy.setSortRole(Qt::DisplayRole);
+
+ std::vector<QPersistentModelIndex> expectedSelectedIndexes;
+ for (const IntPair &index : expectedSelectedIndexesPairs)
+ expectedSelectedIndexes.emplace_back(proxy.index(index.first, index.second));
+
+ QItemSelectionModel selections(&proxy);
+ for (const IntPairPair &range : rangesToSelect) {
+ const IntPair &tl = range.first;
+ const IntPair &br = range.second;
+ selections.select(QItemSelection(proxy.index(tl.first, tl.second),
+ proxy.index(br.first, br.second)),
+ QItemSelectionModel::Select);
+ }
+
+ for (const QPersistentModelIndex &i : expectedSelectedIndexes) {
+ QVERIFY(selections.isSelected(i));
+ }
+ proxy.sort(1, Qt::DescendingOrder);
+ QCOMPARE(selections.selectedIndexes().count(), (int)expectedSelectedIndexes.size());
+ for (const QPersistentModelIndex &i : expectedSelectedIndexes) {
+ QVERIFY(selections.isSelected(i));
+ }
+}
+
QTEST_MAIN(tst_QItemSelectionModel)
#include "tst_qitemselectionmodel.moc"