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.

Pick-to: 6.3
Change-Id: Ifbdf0903158b65c50d0e36e98ab7e48efaa3e3ab
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Richard Moe Gustavsen 2022-02-07 11:10:06 +01:00
parent a2a2734bff
commit 2014101583
3 changed files with 69 additions and 6 deletions

View File

@ -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());

View File

@ -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");

View File

@ -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)
}
}
}
}