From 4284942fabd5ba8bb6299bff1f788ff566ff3fc2 Mon Sep 17 00:00:00 2001 From: Pierre Rossi Date: Mon, 23 Aug 2010 23:10:16 +0200 Subject: Don't toggle selected state when checking/unchecking an item When selecting items with the mouse (and a keyboard modifier), clicking in the check rect of a checkable item shouldn't toggle the selected state but only check/uncheck it. Task-number: QTBUG-4435 Reviewed-by: Thierry --- src/gui/itemviews/qitemdelegate.cpp | 6 ++++-- src/gui/itemviews/qstyleditemdelegate.cpp | 7 ++++--- tests/auto/qitemdelegate/tst_qitemdelegate.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/gui/itemviews/qitemdelegate.cpp b/src/gui/itemviews/qitemdelegate.cpp index bd2b401..6556a3e 100644 --- a/src/gui/itemviews/qitemdelegate.cpp +++ b/src/gui/itemviews/qitemdelegate.cpp @@ -1278,7 +1278,8 @@ bool QItemDelegate::editorEvent(QEvent *event, // make sure that we have the right event type if ((event->type() == QEvent::MouseButtonRelease) - || (event->type() == QEvent::MouseButtonDblClick)) { + || (event->type() == QEvent::MouseButtonDblClick) + || (event->type() == QEvent::MouseButtonPress)) { QRect checkRect = check(option, option.rect, Qt::Checked); QRect emptyRect; doLayout(option, &checkRect, &emptyRect, &emptyRect, false); @@ -1287,7 +1288,8 @@ bool QItemDelegate::editorEvent(QEvent *event, return false; // eat the double click events inside the check rect - if (event->type() == QEvent::MouseButtonDblClick) + if ((event->type() == QEvent::MouseButtonPress) + || (event->type() == QEvent::MouseButtonDblClick)) return true; } else if (event->type() == QEvent::KeyPress) { diff --git a/src/gui/itemviews/qstyleditemdelegate.cpp b/src/gui/itemviews/qstyleditemdelegate.cpp index 115c734..9ccfb76 100644 --- a/src/gui/itemviews/qstyleditemdelegate.cpp +++ b/src/gui/itemviews/qstyleditemdelegate.cpp @@ -727,7 +727,8 @@ bool QStyledItemDelegate::editorEvent(QEvent *event, // make sure that we have the right event type if ((event->type() == QEvent::MouseButtonRelease) - || (event->type() == QEvent::MouseButtonDblClick)) { + || (event->type() == QEvent::MouseButtonDblClick) + || (event->type() == QEvent::MouseButtonPress)) { QStyleOptionViewItemV4 viewOpt(option); initStyleOption(&viewOpt, index); QRect checkRect = style->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &viewOpt, widget); @@ -735,8 +736,8 @@ bool QStyledItemDelegate::editorEvent(QEvent *event, if (me->button() != Qt::LeftButton || !checkRect.contains(me->pos())) return false; - // eat the double click events inside the check rect - if (event->type() == QEvent::MouseButtonDblClick) + if ((event->type() == QEvent::MouseButtonPress) + || (event->type() == QEvent::MouseButtonDblClick)) return true; } else if (event->type() == QEvent::KeyPress) { diff --git a/tests/auto/qitemdelegate/tst_qitemdelegate.cpp b/tests/auto/qitemdelegate/tst_qitemdelegate.cpp index 2a378fe..97a9b82 100644 --- a/tests/auto/qitemdelegate/tst_qitemdelegate.cpp +++ b/tests/auto/qitemdelegate/tst_qitemdelegate.cpp @@ -235,6 +235,7 @@ private slots: void enterKey(); void task257859_finalizeEdit(); + void QTBUG4435_keepSelectionOnCheck(); }; @@ -1168,6 +1169,31 @@ void tst_QItemDelegate::task257859_finalizeEdit() QTRY_VERIFY(!editor); } +void tst_QItemDelegate::QTBUG4435_keepSelectionOnCheck() +{ + QStandardItemModel model(3, 1); + for (int i = 0; i < 3; ++i) { + QStandardItem *item = new QStandardItem(QLatin1String("Item ") + QString::number(i)); + item->setCheckable(true); + item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); + model.setItem(i, item); + } + QTableView view; + view.setModel(&model); + view.setItemDelegate(new TestItemDelegate); + view.show(); + view.selectAll(); + QTest::qWaitForWindowShown(&view); + QStyleOptionViewItem option; + option.rect = view.visualRect(model.index(0, 0)); + const int checkMargin = qApp->style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, 0) + 1; + QPoint pos = qApp->style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &option, 0).center() + + QPoint(checkMargin, 0); + QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ControlModifier, pos); + QTRY_VERIFY(view.selectionModel()->isColumnSelected(0, QModelIndex())); + QCOMPARE(model.item(0)->checkState(), Qt::Checked); +} + // ### _not_ covered: -- cgit v0.12