From a8b0255cf1adb975b9a8f6811ef222e6a51e87cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Wed, 4 Sep 2019 11:54:32 +0200 Subject: [PATCH] Use an editable combo box for the Type property This makes it easier select one of the predefined object types. Closes #823 --- src/tiled/varianteditorfactory.cpp | 80 ++++++++++++++++++++++-------- src/tiled/varianteditorfactory.h | 6 +++ 2 files changed, 64 insertions(+), 22 deletions(-) diff --git a/src/tiled/varianteditorfactory.cpp b/src/tiled/varianteditorfactory.cpp index 3ff7b8d596..456710cbbe 100644 --- a/src/tiled/varianteditorfactory.cpp +++ b/src/tiled/varianteditorfactory.cpp @@ -28,9 +28,8 @@ #include "utils.h" #include "variantpropertymanager.h" -#include +#include #include -#include #include #include "qtcompat_p.h" @@ -81,9 +80,10 @@ void ResetWidget::buttonClicked() VariantEditorFactory::~VariantEditorFactory() { - qDeleteAll(mFileEditToProperty.keys()); - qDeleteAll(mTilesetEditToProperty.keys()); - qDeleteAll(mTextPropertyEditToProperty.keys()); + qDeleteAll(mFileEditToProperty.keyBegin(), mFileEditToProperty.keyEnd()); + qDeleteAll(mTilesetEditToProperty.keyBegin(), mTilesetEditToProperty.keyEnd()); + qDeleteAll(mTextPropertyEditToProperty.keyBegin(), mTextPropertyEditToProperty.keyEnd()); + qDeleteAll(mComboBoxToProperty.keyBegin(), mComboBoxToProperty.keyEnd()); } void VariantEditorFactory::connectPropertyManager(QtVariantPropertyManager *manager) @@ -144,29 +144,27 @@ QWidget *VariantEditorFactory::createEditor(QtVariantPropertyManager *manager, return editor; } - } - - QWidget *editor = QtVariantEditorFactory::createEditor(manager, property, parent); - if (type == QVariant::String) { - // Add support for "suggestions" attribute that adds a QCompleter to the QLineEdit QStringList suggestions = manager->attributeValue(property, QLatin1String("suggestions")).toStringList(); if (!suggestions.isEmpty()) { - if (QLineEdit *lineEdit = qobject_cast(editor)) { - QCompleter *completer = new QCompleter(suggestions, lineEdit); - completer->setCaseSensitivity(Qt::CaseInsensitive); - lineEdit->setCompleter(completer); - - QShortcut *completionShortcut = new QShortcut(tr("Ctrl+Space"), lineEdit); - completionShortcut->setContext(Qt::WidgetShortcut); - connect(completionShortcut, &QShortcut::activated, lineEdit, [=] { - completer->setCompletionPrefix(lineEdit->text().left(lineEdit->cursorPosition())); - completer->complete(); - }); - } + auto editor = new QComboBox(parent); + editor->setEditable(true); + editor->addItems(suggestions); + editor->setCurrentText(manager->value(property).toString()); + mCreatedComboBoxes[property].append(editor); + mComboBoxToProperty[editor] = property; + + connect(editor, &QComboBox::currentTextChanged, + this, &VariantEditorFactory::comboBoxPropertyEditTextChanged); + connect(editor, &QObject::destroyed, + this, &VariantEditorFactory::slotEditorDestroyed); + + return editor; } } + QWidget *editor = QtVariantEditorFactory::createEditor(manager, property, parent); + if (type == QVariant::Color) { // Allow resetting a color property to the invalid color ResetWidget *resetWidget = new ResetWidget(property, editor, parent); @@ -204,6 +202,10 @@ void VariantEditorFactory::slotPropertyChanged(QtProperty *property, for (TextPropertyEdit *edit : qAsConst(mCreatedTextPropertyEdits)[property]) edit->setText(value.toString()); } + else if (mCreatedComboBoxes.contains(property)) { + for (QComboBox *comboBox: qAsConst(mCreatedComboBoxes)[property]) + comboBox->setCurrentText(value.toString()); + } } void VariantEditorFactory::slotPropertyAttributeChanged(QtProperty *property, @@ -216,6 +218,14 @@ void VariantEditorFactory::slotPropertyAttributeChanged(QtProperty *property, edit->setFilter(value.toString()); } } + else if (mCreatedComboBoxes.contains(property)) { + if (attribute == QLatin1String("suggestions")) { + for (QComboBox *comboBox: qAsConst(mCreatedComboBoxes)[property]) { + comboBox->clear(); + comboBox->addItems(value.toStringList()); + } + } + } // changing of "multiline" attribute currently not supported } @@ -245,6 +255,19 @@ void VariantEditorFactory::textPropertyEditTextChanged(const QString &value) } } +void VariantEditorFactory::comboBoxPropertyEditTextChanged(const QString &value) +{ + auto comboBox = qobject_cast(sender()); + Q_ASSERT(comboBox); + + if (QtProperty *property = mComboBoxToProperty.value(comboBox)) { + QtVariantPropertyManager *manager = propertyManager(property); + if (!manager) + return; + manager->setValue(property, value); + } +} + void VariantEditorFactory::slotEditorDestroyed(QObject *object) { // Check if it was a FileEdit @@ -285,6 +308,19 @@ void VariantEditorFactory::slotEditorDestroyed(QObject *object) return; } } + + // Check if it was a QComboBox + { + QComboBox *comboBox = static_cast(object); + + if (QtProperty *property = mComboBoxToProperty.value(comboBox)) { + mComboBoxToProperty.remove(comboBox); + mCreatedComboBoxes[property].removeAll(comboBox); + if (mCreatedComboBoxes[property].isEmpty()) + mCreatedComboBoxes.remove(property); + return; + } + } } } // namespace Tiled diff --git a/src/tiled/varianteditorfactory.h b/src/tiled/varianteditorfactory.h index 43a8d4fa46..08eee159ee 100644 --- a/src/tiled/varianteditorfactory.h +++ b/src/tiled/varianteditorfactory.h @@ -23,6 +23,8 @@ #include +class QComboBox; + namespace Tiled { class FileEdit; @@ -64,6 +66,7 @@ class VariantEditorFactory : public QtVariantEditorFactory const QVariant &value); void fileEditFileUrlChanged(const QUrl &value); void textPropertyEditTextChanged(const QString &value); + void comboBoxPropertyEditTextChanged(const QString &value); void slotEditorDestroyed(QObject *object); QMap > mCreatedFileEdits; @@ -74,6 +77,9 @@ class VariantEditorFactory : public QtVariantEditorFactory QMap > mCreatedTextPropertyEdits; QMap mTextPropertyEditToProperty; + + QMap > mCreatedComboBoxes; + QMap mComboBoxToProperty; }; } // namespace Tiled