From b9ca537fc72daafb91d11fbb7ec0fbfc8723c7a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 26 Sep 2024 18:02:32 +0200 Subject: [PATCH] Show inherited class name as placeholder text For map objects, which can inherit their class from their tile or their template. --- src/tiled/propertieswidget.cpp | 21 +++++++++++++++++++-- src/tiled/textpropertyedit.h | 2 ++ src/tiled/varianteditor.cpp | 15 +++++++++++++++ src/tiled/varianteditor.h | 16 ++++++++++++++-- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/tiled/propertieswidget.cpp b/src/tiled/propertieswidget.cpp index e65ab82234..2b6d695ed1 100644 --- a/src/tiled/propertieswidget.cpp +++ b/src/tiled/propertieswidget.cpp @@ -556,6 +556,8 @@ class ClassNameProperty : public StringProperty , mDocument(document) , mObject(object) { + updatePlaceholderText(); + connect(mDocument, &Document::changed, this, &ClassNameProperty::onChanged); } @@ -564,6 +566,7 @@ class ClassNameProperty : public StringProperty { auto editor = new QComboBox(parent); editor->setEditable(true); + editor->lineEdit()->setPlaceholderText(placeholderText()); editor->addItems(classNamesFor(*mObject)); auto syncEditor = [this, editor] { const QSignalBlocker blocker(editor); @@ -571,11 +574,15 @@ class ClassNameProperty : public StringProperty }; syncEditor(); connect(this, &Property::valueChanged, editor, syncEditor); + connect(this, &StringProperty::placeholderTextChanged, + editor->lineEdit(), &QLineEdit::setPlaceholderText); connect(editor, &QComboBox::currentTextChanged, this, &StringProperty::setValue); connect(Preferences::instance(), &Preferences::propertyTypesChanged, - editor, [this,editor] { + editor, [=] { + const QSignalBlocker blocker(editor); editor->clear(); editor->addItems(classNamesFor(*mObject)); + syncEditor(); }); return editor; } @@ -590,8 +597,18 @@ class ClassNameProperty : public StringProperty if (!objectsEvent.objects.contains(mObject)) return; - if (objectsEvent.properties & ObjectsChangeEvent::ClassProperty) + if (objectsEvent.properties & ObjectsChangeEvent::ClassProperty) { + updatePlaceholderText(); emit valueChanged(); + } + } + + void updatePlaceholderText() + { + if (mObject->typeId() == Object::MapObjectType && mObject->className().isEmpty()) + setPlaceholderText(static_cast(mObject)->effectiveClassName()); + else + setPlaceholderText(QString()); } Document *mDocument; diff --git a/src/tiled/textpropertyedit.h b/src/tiled/textpropertyedit.h index 4ef2c7360f..a509ab38fc 100644 --- a/src/tiled/textpropertyedit.h +++ b/src/tiled/textpropertyedit.h @@ -45,6 +45,8 @@ class TextPropertyEdit : public QWidget QString text() const; + QLineEdit *lineEdit() const { return mLineEdit; } + public slots: void setText(const QString &text); diff --git a/src/tiled/varianteditor.cpp b/src/tiled/varianteditor.cpp index 580efa507f..d09e163f25 100644 --- a/src/tiled/varianteditor.cpp +++ b/src/tiled/varianteditor.cpp @@ -57,15 +57,26 @@ void Property::setEnabled(bool enabled) } } +void StringProperty::setPlaceholderText(const QString &placeholderText) +{ + if (m_placeholderText != placeholderText) { + m_placeholderText = placeholderText; + emit placeholderTextChanged(placeholderText); + } +} + QWidget *StringProperty::createEditor(QWidget *parent) { auto editor = new QLineEdit(parent); + editor->setPlaceholderText(m_placeholderText); + auto syncEditor = [=] { editor->setText(value()); }; syncEditor(); connect(this, &Property::valueChanged, editor, syncEditor); + connect(this, &StringProperty::placeholderTextChanged, editor, &QLineEdit::setPlaceholderText); connect(editor, &QLineEdit::textEdited, this, &StringProperty::setValue); return editor; @@ -74,6 +85,8 @@ QWidget *StringProperty::createEditor(QWidget *parent) QWidget *MultilineStringProperty::createEditor(QWidget *parent) { auto editor = new TextPropertyEdit(parent); + editor->lineEdit()->setPlaceholderText(placeholderText()); + auto syncEditor = [=] { const QSignalBlocker blocker(editor); editor->setText(value()); @@ -81,6 +94,8 @@ QWidget *MultilineStringProperty::createEditor(QWidget *parent) syncEditor(); connect(this, &StringProperty::valueChanged, editor, syncEditor); + connect(this, &StringProperty::placeholderTextChanged, + editor->lineEdit(), &QLineEdit::setPlaceholderText); connect(editor, &TextPropertyEdit::textChanged, this, &StringProperty::setValue); return editor; diff --git a/src/tiled/varianteditor.h b/src/tiled/varianteditor.h index ce9098114b..a4e72d4130 100644 --- a/src/tiled/varianteditor.h +++ b/src/tiled/varianteditor.h @@ -168,13 +168,25 @@ class PropertyTemplate : public Property struct StringProperty : PropertyTemplate { + Q_OBJECT + +public: using PropertyTemplate::PropertyTemplate; QWidget *createEditor(QWidget *parent) override; + + void setPlaceholderText(const QString &placeholderText); + const QString &placeholderText() const { return m_placeholderText; } + +signals: + void placeholderTextChanged(const QString &placeholderText); + +private: + QString m_placeholderText; }; -struct MultilineStringProperty : PropertyTemplate +struct MultilineStringProperty : StringProperty { - using PropertyTemplate::PropertyTemplate; + using StringProperty::StringProperty; QWidget *createEditor(QWidget *parent) override; };