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>
This commit is contained in:
parent
16f950c702
commit
be0a221ae4
@ -942,13 +942,14 @@ static QItemSelection mergeRowLengths(const QVector<QPair<QPersistentModelIndex,
|
||||
const uint nextLength = rowLengths.at(i).second;
|
||||
if ((nextLength == length)
|
||||
&& (next.row() == br.row() + 1)
|
||||
&& (next.column() == br.column())
|
||||
&& (next.parent() == br.parent())) {
|
||||
br = next;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
result.append(QItemSelectionRange(tl, br.sibling(br.row(), length - 1)));
|
||||
result.append(QItemSelectionRange(tl, br.sibling(br.row(), br.column() + length - 1)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -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"
|
||||
|
Loading…
x
Reference in New Issue
Block a user