Skip to content

Commit

Permalink
Show inherited class name as placeholder text
Browse files Browse the repository at this point in the history
For map objects, which can inherit their class from their tile or their
template.
  • Loading branch information
bjorn committed Sep 26, 2024
1 parent 2d18a43 commit b9ca537
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
21 changes: 19 additions & 2 deletions src/tiled/propertieswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ class ClassNameProperty : public StringProperty
, mDocument(document)
, mObject(object)
{
updatePlaceholderText();

connect(mDocument, &Document::changed,
this, &ClassNameProperty::onChanged);
}
Expand All @@ -564,18 +566,23 @@ 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);
editor->setCurrentText(value());
};
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;
}
Expand All @@ -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<MapObject*>(mObject)->effectiveClassName());
else
setPlaceholderText(QString());
}

Document *mDocument;
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/textpropertyedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class TextPropertyEdit : public QWidget

QString text() const;

QLineEdit *lineEdit() const { return mLineEdit; }

public slots:
void setText(const QString &text);

Expand Down
15 changes: 15 additions & 0 deletions src/tiled/varianteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -74,13 +85,17 @@ 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());
};
syncEditor();

connect(this, &StringProperty::valueChanged, editor, syncEditor);
connect(this, &StringProperty::placeholderTextChanged,
editor->lineEdit(), &QLineEdit::setPlaceholderText);
connect(editor, &TextPropertyEdit::textChanged, this, &StringProperty::setValue);

return editor;
Expand Down
16 changes: 14 additions & 2 deletions src/tiled/varianteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,25 @@ class PropertyTemplate : public Property

struct StringProperty : PropertyTemplate<QString>
{
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<QString>
struct MultilineStringProperty : StringProperty
{
using PropertyTemplate::PropertyTemplate;
using StringProperty::StringProperty;
QWidget *createEditor(QWidget *parent) override;
};

Expand Down

0 comments on commit b9ca537

Please sign in to comment.