From be0a221ae491b8426b3a4cdf0d5863701e922c1f Mon Sep 17 00:00:00 2001 From: Filippo Cucchetto Date: Wed, 26 Apr 2017 12:49:36 +0200 Subject: 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) Reviewed-by: Qt CI Bot --- .../tst_qitemselectionmodel.cpp | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) (limited to 'tests/auto/corelib/itemmodels/qitemselectionmodel') 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; + using IntPairList = QList; + using IntPairPair = std::pair; + using IntPairPairList = QList; + + QTest::addColumn("rangesToSelect"); + QTest::addColumn("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; + using IntPairList = QList; + using IntPairPair = std::pair; + using IntPairPairList = QList; + + 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 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" -- cgit v1.2.3