aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-02-07 11:10:06 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-02-10 13:49:08 +0000
commit9dd1968924f88996d78e3e0d8fa2c82d38ccf37e (patch)
tree6d0b8f58a53071ee0a7006852b47fb6492af357f
parent4a32230409c21a5a2534cbe022c4a631eaeda30c (diff)
TreeViewDelegate: allow app to add custom pointer handlers
As it stood, it was not possible to add custom pointer handlers to a TreeViewDelegate, to e.g do an expandRecursive() if using the right mouse button, or holding down ctrl during a click. This patch will change this, so that we only perform the default collapse/expand operations when using a plain left (double) click, and ignore the event otherwise. Change-Id: Ifbdf0903158b65c50d0e36e98ab7e48efaa3e3ab Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io> (cherry picked from commit 2014101583b89128960f645e57192d1d475767e6) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/quicktemplates2/qquicktreeviewdelegate.cpp7
-rw-r--r--tests/auto/quickcontrols2/qquicktreeviewdelegate/tst_qquicktreeviewdelegate.cpp53
-rw-r--r--tests/manual/treeview/sidebyside/data/treeview.qml15
3 files changed, 69 insertions, 6 deletions
diff --git a/src/quicktemplates2/qquicktreeviewdelegate.cpp b/src/quicktemplates2/qquicktreeviewdelegate.cpp
index b233020702..1636afbfa1 100644
--- a/src/quicktemplates2/qquicktreeviewdelegate.cpp
+++ b/src/quicktemplates2/qquicktreeviewdelegate.cpp
@@ -216,6 +216,13 @@ void QQuickTreeViewDelegate::mousePressEvent(QMouseEvent *event)
{
QQuickAbstractButton::mousePressEvent(event);
+ if (event->buttons() != Qt::LeftButton || event->modifiers() != Qt::NoModifier) {
+ // Allow application to add its own pointer handlers that does something
+ // other than plain expand/collapse if e.g holding down modifier keys.
+ event->ignore();
+ return;
+ }
+
const auto indicator = QQuickAbstractButton::indicator();
if (indicator && indicator->isVisible()) {
const auto posInIndicator = mapToItem(indicator, event->position());
diff --git a/tests/auto/quickcontrols2/qquicktreeviewdelegate/tst_qquicktreeviewdelegate.cpp b/tests/auto/quickcontrols2/qquicktreeviewdelegate/tst_qquicktreeviewdelegate.cpp
index 021628709c..0478c97431 100644
--- a/tests/auto/quickcontrols2/qquicktreeviewdelegate/tst_qquicktreeviewdelegate.cpp
+++ b/tests/auto/quickcontrols2/qquicktreeviewdelegate/tst_qquicktreeviewdelegate.cpp
@@ -89,6 +89,8 @@ private slots:
void showTreeView();
void expandAndCollapsUsingDoubleClick();
void expandAndCollapseClickOnIndicator();
+ void expandAndCollapsUsingNonSupportedButtonAndModifers_data();
+ void expandAndCollapsUsingNonSupportedButtonAndModifers();
void checkPropertiesRoot();
void checkPropertiesChildren();
};
@@ -163,6 +165,57 @@ void tst_qquicktreeviewdelegate::expandAndCollapseClickOnIndicator()
QCOMPARE(treeViewPrivate->loadedRows.count(), 1);
}
+void tst_qquicktreeviewdelegate::expandAndCollapsUsingNonSupportedButtonAndModifers_data()
+{
+ QTest::addColumn<Qt::MouseButton>("button");
+ QTest::addColumn<Qt::KeyboardModifiers>("modifiers");
+
+ QTest::newRow("left + Qt::ControlModifier") << Qt::LeftButton << Qt::KeyboardModifiers(Qt::ControlModifier);
+ QTest::newRow("left + Qt::ShiftModifier") << Qt::LeftButton << Qt::KeyboardModifiers(Qt::ShiftModifier);
+ QTest::newRow("left + Qt::AltModifier") << Qt::LeftButton << Qt::KeyboardModifiers(Qt::AltModifier);
+ QTest::newRow("left + Qt::MetaModifier") << Qt::LeftButton << Qt::KeyboardModifiers(Qt::MetaModifier);
+ QTest::newRow("left + Qt::ControlModifier + Qt::ShiftModifier") << Qt::LeftButton << (Qt::ShiftModifier | Qt::ControlModifier);
+
+ QTest::newRow("right + Qt::NoModifier") << Qt::RightButton << Qt::KeyboardModifiers(Qt::ControlModifier);
+ QTest::newRow("right + Qt::ControlModifier") << Qt::RightButton << Qt::KeyboardModifiers(Qt::ShiftModifier);
+}
+
+void tst_qquicktreeviewdelegate::expandAndCollapsUsingNonSupportedButtonAndModifers()
+{
+ QFETCH(Qt::MouseButton, button);
+ QFETCH(Qt::KeyboardModifiers, modifiers);
+ // Ensure that we don't expand or collapse the tree if the user is using the right mouse
+ // button, or holding down modifier keys. This "space" is reserved for application specific actions.
+ LOAD_TREEVIEW("unmodified.qml");
+
+ QCOMPARE(treeViewPrivate->loadedRows.count(), 1);
+ const auto item = treeView->itemAtCell(0, 0);
+ QVERIFY(item);
+ const QPoint localPos = QPoint(item->width() / 2, item->height() / 2);
+ const QPoint pos = item->window()->contentItem()->mapFromItem(item, localPos).toPoint();
+ QTest::mouseDClick(item->window(), button, modifiers, pos);
+
+ WAIT_UNTIL_POLISHED;
+
+ QCOMPARE(treeViewPrivate->loadedRows.count(), 1);
+
+ // Expand first row, and ensure we don't collapse it again
+ // if doing a double click together with Qt::CTRL.
+ QTest::mouseDClick(item->window(), Qt::LeftButton, Qt::NoModifier, pos);
+
+ WAIT_UNTIL_POLISHED;
+
+ // We now expect 5 rows, the root pluss it's 4 children
+ QCOMPARE(treeViewPrivate->loadedRows.count(), 5);
+
+ QTest::mouseDClick(item->window(), button, modifiers, pos);
+
+ WAIT_UNTIL_POLISHED;
+
+ // We still expect 5 rows, the root pluss it's 4 children
+ QCOMPARE(treeViewPrivate->loadedRows.count(), 5);
+}
+
void tst_qquicktreeviewdelegate::checkPropertiesRoot()
{
LOAD_TREEVIEW("unmodified.qml");
diff --git a/tests/manual/treeview/sidebyside/data/treeview.qml b/tests/manual/treeview/sidebyside/data/treeview.qml
index 6587e11f6e..0e97e23a96 100644
--- a/tests/manual/treeview/sidebyside/data/treeview.qml
+++ b/tests/manual/treeview/sidebyside/data/treeview.qml
@@ -114,15 +114,18 @@ ApplicationWindow {
id: testModel
}
- Rectangle {
- anchors.fill: parent
- color: "white"
- z: -1
- }
-
Component {
id: treeViewDelegate
TreeViewDelegate {
+ TapHandler {
+ acceptedModifiers: Qt.ControlModifier
+ onTapped: {
+ if (treeView.isExpanded(row))
+ treeView.collapseRecursively(row)
+ else
+ treeView.expandRecursively(row)
+ }
+ }
}
}