Skip to content

Commit

Permalink
Focus new custom properties or class members when adding them
Browse files Browse the repository at this point in the history
When adding custom class properties, they are expanded with the first
member focused.
  • Loading branch information
bjorn committed Nov 11, 2024
1 parent 116cf5f commit 1373099
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/tiled/propertieswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2538,7 +2538,8 @@ void PropertiesWidget::addProperty(const QString &name, const QVariant &value)
name, value));
}

// mPropertyBrowser->editCustomProperty(name);
if (auto property = mCustomProperties->property(name))
mPropertyBrowser->focusProperty(property);
}

void PropertiesWidget::removeProperties()
Expand Down
18 changes: 4 additions & 14 deletions src/tiled/propertytypeseditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,8 @@ void PropertyTypesEditor::openAddMemberDialog()

if (dialog.exec() == AddPropertyDialog::Accepted)
addMember(dialog.propertyName(), QVariant(dialog.propertyValue()));

activateWindow();
}

void PropertyTypesEditor::addMember(const QString &name, const QVariant &value)
Expand All @@ -593,21 +595,9 @@ void PropertyTypesEditor::addMember(const QString &name, const QVariant &value)

applyMemberToSelectedType(name, value);
updateDetails();
editMember(name);
}

void PropertyTypesEditor::editMember(const QString &name)
{
// todo: VariantEditor needs to support focusing the widget of a specific property
#if 0
QtVariantProperty *property = mPropertiesHelper->property(name);
if (!property)
return;

const QList<QtBrowserItem*> propertyItems = mMembersView->items(property);
if (!propertyItems.isEmpty())
mMembersView->editItem(propertyItems.first());
#endif
if (auto property = mMembersProperty->property(name))
mMembersEditor->focusProperty(property);
}

void PropertyTypesEditor::removeMember()
Expand Down
1 change: 0 additions & 1 deletion src/tiled/propertytypeseditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ class PropertyTypesEditor : public QDialog
void openClassOfPopup();
void openAddMemberDialog();
void addMember(const QString &name, const QVariant &value = QVariant());
void editMember(const QString &name);
void removeMember();
void renameMember(const QString &name);
void renameMemberTo(const QString &oldName, const QString &name);
Expand Down
37 changes: 37 additions & 0 deletions src/tiled/varianteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,38 @@ void VariantEditor::removeProperty(Property *property)
property->disconnect(this);
}

/**
* Focuses the editor for the given property. Makes sure any parent group
* properties are expanded.
*
* When the given property is a group property, the group property is expanded
* and the first child property is focused.
*/
bool VariantEditor::focusProperty(Property *property)
{
for (auto it = m_propertyWidgets.constBegin(); it != m_propertyWidgets.constEnd(); ++it) {
auto &widgets = it.value();

if (it.key() == property) {
if (widgets.editor)
widgets.editor->setFocus();
else if (auto groupProperty = qobject_cast<GroupProperty *>(it.key())) {
groupProperty->setExpanded(true);
if (widgets.children && !groupProperty->subProperties().isEmpty())
widgets.children->focusProperty(groupProperty->subProperties().first());
}
return true;
} else if (auto groupProperty = qobject_cast<GroupProperty *>(it.key())) {
if (widgets.children && widgets.children->focusProperty(property)) {
groupProperty->setExpanded(true);
return true;
}
}
}

return false;
}

void VariantEditor::setLevel(int level)
{
m_level = level;
Expand Down Expand Up @@ -1008,6 +1040,11 @@ VariantEditorView::VariantEditorView(QWidget *parent)
setWidget(scrollWidget);
}

void VariantEditorView::focusProperty(Property *property)
{
m_editor->focusProperty(property);
}

} // namespace Tiled

#include "moc_varianteditor.cpp"
4 changes: 4 additions & 0 deletions src/tiled/varianteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ class VariantEditor : public QWidget
void insertProperty(int index, Property *property);
void removeProperty(Property *property);

bool focusProperty(Property *property);

void setLevel(int level);

private:
Expand Down Expand Up @@ -553,6 +555,8 @@ class VariantEditorView : public QScrollArea
void addProperty(Property *property)
{ m_editor->addProperty(property); }

void focusProperty(Property *property);

private:
VariantEditor *m_editor;
};
Expand Down
6 changes: 6 additions & 0 deletions src/tiled/variantmapproperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class VariantMapProperty : public GroupProperty

const QVariantMap &value() const { return mValue; }

Property *property(const QString &name) const;

signals:
void memberValueChanged(const QStringList &path, const QVariant &value);
void renameRequested(const QString &name);
Expand Down Expand Up @@ -85,5 +87,9 @@ class VariantMapProperty : public GroupProperty
QSet<QStringList> mExpandedProperties;
};

inline Property *VariantMapProperty::property(const QString &name) const
{
return mPropertyMap.value(name);
}

} // namespace Tiled

0 comments on commit 1373099

Please sign in to comment.