From 73e6373ce34433f222f5e5956ed69be1c4ffed2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 5 Dec 2024 11:41:42 +0100 Subject: [PATCH] Add custom property when pressing Enter with type combo focused This change also disables Enter for opening the combo box popup, fixing the rather broken behavior (at least on Linux) that pressing Enter to select a type will immediately re-open the popup. That only applies to combo boxes in the Properties view, though. --- src/tiled/propertyeditorwidgets.cpp | 12 ++++++++++++ src/tiled/propertyeditorwidgets.h | 5 +++++ src/tiled/variantmapproperty.cpp | 4 ++++ 3 files changed, 21 insertions(+) diff --git a/src/tiled/propertyeditorwidgets.cpp b/src/tiled/propertyeditorwidgets.cpp index b4c9880a71..625bb33a1f 100644 --- a/src/tiled/propertyeditorwidgets.cpp +++ b/src/tiled/propertyeditorwidgets.cpp @@ -288,6 +288,18 @@ bool ComboBox::event(QEvent *event) return QComboBox::event(event); } +void ComboBox::keyPressEvent(QKeyEvent *event) +{ + switch (event->key()) { + case Qt::Key_Enter: + case Qt::Key_Return: + emit returnPressed(); + return; + } + + QComboBox::keyPressEvent(event); +} + void ComboBox::wheelEvent(QWheelEvent *event) { if (!hasFocus()) diff --git a/src/tiled/propertyeditorwidgets.h b/src/tiled/propertyeditorwidgets.h index c08b86cc7e..bfe3c1cc69 100644 --- a/src/tiled/propertyeditorwidgets.h +++ b/src/tiled/propertyeditorwidgets.h @@ -70,8 +70,13 @@ class ComboBox : public QComboBox public: ComboBox(QWidget *parent = nullptr); +signals: + /** This signal is emitted when the Return or Enter key is pressed. */ + void returnPressed(); + protected: bool event(QEvent *event) override; + void keyPressEvent(QKeyEvent *event) override; void wheelEvent(QWheelEvent *event) override; }; diff --git a/src/tiled/variantmapproperty.cpp b/src/tiled/variantmapproperty.cpp index 5fbe6f0a5a..b7b1a41263 100644 --- a/src/tiled/variantmapproperty.cpp +++ b/src/tiled/variantmapproperty.cpp @@ -586,6 +586,10 @@ QWidget *AddValueProperty::createEditor(QWidget *parent) m_value = typeBox->itemData(index); session::propertyType = typeBox->currentText(); }); + connect(typeBox, &ComboBox::returnPressed, this, [this] { + if (!name().isEmpty()) + emit addRequested(); + }); typeBox->installEventFilter(this);