Skip to content

Commit

Permalink
Use an editable combo box for the Type property
Browse files Browse the repository at this point in the history
This makes it easier select one of the predefined object types.

Closes #823
  • Loading branch information
bjorn committed Sep 4, 2019
1 parent 4c9be94 commit a8b0255
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 22 deletions.
80 changes: 58 additions & 22 deletions src/tiled/varianteditorfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
#include "utils.h"
#include "variantpropertymanager.h"

#include <QCompleter>
#include <QComboBox>
#include <QHBoxLayout>
#include <QShortcut>
#include <QToolButton>

#include "qtcompat_p.h"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<QLineEdit*>(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);
Expand Down Expand Up @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -245,6 +255,19 @@ void VariantEditorFactory::textPropertyEditTextChanged(const QString &value)
}
}

void VariantEditorFactory::comboBoxPropertyEditTextChanged(const QString &value)
{
auto comboBox = qobject_cast<QComboBox*>(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
Expand Down Expand Up @@ -285,6 +308,19 @@ void VariantEditorFactory::slotEditorDestroyed(QObject *object)
return;
}
}

// Check if it was a QComboBox
{
QComboBox *comboBox = static_cast<QComboBox*>(object);

if (QtProperty *property = mComboBoxToProperty.value(comboBox)) {
mComboBoxToProperty.remove(comboBox);
mCreatedComboBoxes[property].removeAll(comboBox);
if (mCreatedComboBoxes[property].isEmpty())
mCreatedComboBoxes.remove(property);
return;
}
}
}

} // namespace Tiled
Expand Down
6 changes: 6 additions & 0 deletions src/tiled/varianteditorfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include <QtVariantEditorFactory>

class QComboBox;

namespace Tiled {

class FileEdit;
Expand Down Expand Up @@ -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<QtProperty *, QList<FileEdit *> > mCreatedFileEdits;
Expand All @@ -74,6 +77,9 @@ class VariantEditorFactory : public QtVariantEditorFactory

QMap<QtProperty *, QList<TextPropertyEdit *> > mCreatedTextPropertyEdits;
QMap<TextPropertyEdit *, QtProperty *> mTextPropertyEditToProperty;

QMap<QtProperty *, QList<QComboBox *> > mCreatedComboBoxes;
QMap<QComboBox *, QtProperty *> mComboBoxToProperty;
};

} // namespace Tiled

0 comments on commit a8b0255

Please # to comment.