diff options
author | Frank Reininghaus <frank78ac@googlemail.com> | 2016-05-18 19:25:36 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2016-10-07 10:38:58 +0000 |
commit | 5cc0d92c2493aea8397a51eea80167abdd6a1fa6 (patch) | |
tree | 3232507f9850e3431eb5c645d31edb4fb0f66954 | |
parent | c6f5e4b47c3f27c80768e735bb4109a8dc83d2e5 (diff) |
QAbstractItemView: use only a 1x1 QRect for selecting on mouse press
Before commit f1e90768095be37419ba4bf3c69ec5c860bdbcb6, mousePressEvent
called the virtual method setSelection(const QRect&, SelectionFlags)
with the 1x1 rectangle which contains only the clicked QPoint, unless
the SelectionFlag "Current" was set because Shift was pressed during
the mouse press.
Since that commit, the behavior has been changed such that the
rectangle is the one that is spanned by the center of the clicked item
and the clicked pixel. In theory, the result should be the same (i.e.,
only the clicked item should be selected), but
* the code path in QListView::setSelection for 1x1 QRects is more
efficient, and
* using a larger QRect can cause problems with custom views, see the
comments in QTBUG-18009
This commit ensures that the 1x1 QRect is used again, unless the
SelectionFlag "Current" is used.
Change-Id: I70dd70c083c20a3af6cd6095aa89a489756b505f
Task-number: QTBUG-18009
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Reviewed-by: Thorbjørn Lund Martsum <tmartsum@gmail.com>
-rw-r--r-- | src/widgets/itemviews/qabstractitemview.cpp | 9 | ||||
-rw-r--r-- | tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp | 52 |
2 files changed, 59 insertions, 2 deletions
diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp index d36431c716..4dd8ad0215 100644 --- a/src/widgets/itemviews/qabstractitemview.cpp +++ b/src/widgets/itemviews/qabstractitemview.cpp @@ -1735,13 +1735,18 @@ void QAbstractItemView::mousePressEvent(QMouseEvent *event) d->autoScroll = false; d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate); d->autoScroll = autoScroll; - QRect rect(visualRect(d->currentSelectionStartIndex).center(), pos); if (command.testFlag(QItemSelectionModel::Toggle)) { command &= ~QItemSelectionModel::Toggle; d->ctrlDragSelectionFlag = d->selectionModel->isSelected(index) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select; command |= d->ctrlDragSelectionFlag; } - setSelection(rect, command); + + if ((command & QItemSelectionModel::Current) == 0) { + setSelection(QRect(pos, QSize(1, 1)), command); + } else { + QRect rect(visualRect(d->currentSelectionStartIndex).center(), pos); + setSelection(rect, command); + } // signal handlers may change the model emit pressed(index); diff --git a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp index 27c9e07037..90ea6f3ef7 100644 --- a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp +++ b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp @@ -152,6 +152,7 @@ private slots: void QTBUG48968_reentrant_updateEditorGeometries(); void QTBUG50535_update_on_new_selection_model(); void testSelectionModelInSyncWithView(); + void testClickToSelect(); }; class MyAbstractItemDelegate : public QAbstractItemDelegate @@ -2074,5 +2075,56 @@ void tst_QAbstractItemView::testSelectionModelInSyncWithView() QCOMPARE(view.selectionModel()->selection().indexes(), QModelIndexList() << model.index(0, 0)); } +class SetSelectionTestView : public QListView +{ + Q_OBJECT +public: + SetSelectionTestView() : QListView() {} + +signals: + void setSelectionCalled(const QRect &rect); + +protected: + void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags) Q_DECL_OVERRIDE + { + emit setSelectionCalled(rect); + QListView::setSelection(rect, flags); + } +}; + +void tst_QAbstractItemView::testClickToSelect() +{ + // This test verifies that the QRect that is passed from QAbstractItemView::mousePressEvent + // to the virtual method QAbstractItemView::setSelection(const QRect &, SelectionFlags) + // is the 1x1 rect which conains exactly the clicked pixel if no modifiers are pressed. + + QStringList list; + list << "A" << "B" << "C"; + QStringListModel model(list); + + SetSelectionTestView view; + view.setModel(&model); + view.show(); + QTest::qWaitForWindowExposed(&view); + + QSignalSpy spy(&view, &SetSelectionTestView::setSelectionCalled); + + const QModelIndex indexA(model.index(0, 0)); + const QRect visualRectA = view.visualRect(indexA); + const QPoint centerA = visualRectA.center(); + + // Click the center of the visualRect of item "A" + QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, centerA); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.back().front().value<QRect>(), QRect(centerA, QSize(1, 1))); + + // Click a point slightly away from the center + const QPoint nearCenterA = centerA + QPoint(1, 1); + QVERIFY(visualRectA.contains(nearCenterA)); + QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, nearCenterA); + QCOMPARE(spy.count(), 2); + QCOMPARE(spy.back().front().value<QRect>(), QRect(nearCenterA, QSize(1, 1))); +} + QTEST_MAIN(tst_QAbstractItemView) #include "tst_qabstractitemview.moc" |