Skip to content

Commit

Permalink
Added context menu to custom properties
Browse files Browse the repository at this point in the history
Currently providing access to the existing Add, Remove and Reset actions
as well as "Expand All" and "Collapse All" for custom classes.
  • Loading branch information
bjorn committed Oct 17, 2024
1 parent 1a5e492 commit 5055d9a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 10 deletions.
74 changes: 65 additions & 9 deletions src/tiled/propertieswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ class VariantMapProperty : public GroupProperty

void removeMember(const QString &name);
void addMember(const QString &name, const QVariant &value);
void setClassMember(const QStringList &path, const QVariant &value);

void updateModifiedRecursively(Property *property, const QVariant &value);
void emitValueChangedRecursively(Property *property);
Expand All @@ -512,6 +513,8 @@ class VariantMapProperty : public GroupProperty

void emitMemberValueChanged(const QStringList &path, const QVariant &value);

void memberContextMenuRequested(Property *property, const QStringList &path, const QPoint &globalPos);

bool mEmittingValueChanged = false;
bool mPropertyTypesChanged = false;
QVariantMap mValue;
Expand Down Expand Up @@ -2620,6 +2623,10 @@ Property *VariantMapProperty::createProperty(const QStringList &path,

property->setToolTip(QStringLiteral("%1&nbsp;<span style=\"color: gray;\">:&nbsp;%2<span>")
.arg(property->name(), typeName));

connect(property, &Property::contextMenuRequested, this, [=](const QPoint &globalPos) {
memberContextMenuRequested(property, path, globalPos);
});
}

return property;
Expand All @@ -2644,13 +2651,7 @@ void VariantMapProperty::createClassMembers(const QStringList &path,
return get().value<PropertyValue>().value.toMap().value(name, def);
};
auto setMember = [=] (const QVariant &value) {
if (setPropertyMemberValue(mValue, childPath, value)) {
const auto &topLevelName = childPath.first();
updateModifiedRecursively(mPropertyMap.value(topLevelName),
mValue.value(topLevelName));

emitMemberValueChanged(childPath, value);
}
setClassMember(childPath, value);
};

if (auto childProperty = createProperty(childPath, std::move(getMember), setMember)) {
Expand Down Expand Up @@ -2698,10 +2699,22 @@ void VariantMapProperty::addMember(const QString &name, const QVariant &value)
emitMemberValueChanged({ name }, value);
}

void VariantMapProperty::setClassMember(const QStringList &path, const QVariant &value)
{
if (!setPropertyMemberValue(mValue, path, value))
return;

const auto &topLevelName = path.first();
updateModifiedRecursively(mPropertyMap.value(topLevelName),
mValue.value(topLevelName));

emitMemberValueChanged(path, value);
}

void VariantMapProperty::updateModifiedRecursively(Property *property,
const QVariant &value)
{
auto groupProperty = dynamic_cast<GroupProperty*>(property);
auto groupProperty = qobject_cast<GroupProperty*>(property);
if (!groupProperty)
return;

Expand All @@ -2722,7 +2735,7 @@ void VariantMapProperty::emitValueChangedRecursively(Property *property)
{
emit property->valueChanged();

if (auto groupProperty = dynamic_cast<GroupProperty*>(property))
if (auto groupProperty = qobject_cast<GroupProperty*>(property))
for (auto subProperty : groupProperty->subProperties())
emitValueChangedRecursively(subProperty);
}
Expand All @@ -2746,6 +2759,49 @@ void VariantMapProperty::emitMemberValueChanged(const QStringList &path, const Q
emit valueChanged();
}

void VariantMapProperty::memberContextMenuRequested(Property *property, const QStringList &path, const QPoint &globalPos)
{
QMenu menu;

// Add Expand All and Collapse All actions to group properties
if (auto groupProperty = qobject_cast<GroupProperty*>(property)) {
menu.addAction(tr("Expand All"), groupProperty, &GroupProperty::expandAll);
menu.addAction(tr("Collapse All"), groupProperty, &GroupProperty::collapseAll);
}

// Provide the Add, Remove and Reset actions also here
if (property->actions()) {
menu.addSeparator();

if (property->actions() & Property::Action::Add) {
QAction *add = menu.addAction(tr("Add Property"), this, [this, name = path.first()] {
addMember(name, mSuggestions.value(name));
});
Utils::setThemeIcon(add, "add");
}
if (property->actions() & Property::Action::Remove) {
QAction *remove = menu.addAction(tr("Remove Property"), this, [this, name = path.first()] {
removeMember(name);
});
Utils::setThemeIcon(remove, "remove");
}
if (property->actions() & Property::Action::Reset) {
QAction *reset = menu.addAction(tr("Reset Member"), this, [=] {
setClassMember(path, QVariant());
emitValueChangedRecursively(property);
});
reset->setEnabled(property->isModified());
Utils::setThemeIcon(reset, "edit-clear");
}
}

// todo: Add "Convert" sub-menu
// todo: Add "Copy" and "Paste" actions

if (!menu.isEmpty())
menu.exec(globalPos);
}


void CustomProperties::setDocument(Document *document)
{
Expand Down
5 changes: 5 additions & 0 deletions src/tiled/propertyeditorwidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ bool PropertyLabel::event(QEvent *event)
}
}

if (event->type() == QEvent::ContextMenu) {
emit contextMenuRequested(static_cast<QContextMenuEvent *>(event)->globalPos());
return true;
}

if (event->type() == QEvent::LayoutDirectionChange)
updateContentMargins();

Expand Down
1 change: 1 addition & 0 deletions src/tiled/propertyeditorwidgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ class PropertyLabel : public ElidingLabel

signals:
void toggled(bool expanded);
void contextMenuRequested(const QPoint &globalPos);

protected:
bool event(QEvent *event) override;
Expand Down
21 changes: 20 additions & 1 deletion src/tiled/varianteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ void GroupProperty::setExpanded(bool expanded)
}
}

void GroupProperty::expandAll()
{
setExpanded(true);
for (auto property : m_subProperties)
if (auto groupProperty = qobject_cast<GroupProperty *>(property))
groupProperty->expandAll();
}

void GroupProperty::collapseAll()
{
setExpanded(false);
for (auto property : m_subProperties)
if (auto groupProperty = qobject_cast<GroupProperty *>(property))
groupProperty->collapseAll();
}

void StringProperty::setPlaceholderText(const QString &placeholderText)
{
if (m_placeholderText != placeholderText) {
Expand Down Expand Up @@ -618,6 +634,9 @@ QLayout *VariantEditor::createPropertyLayout(Property *property)

widgets.label = new PropertyLabel(m_level, this);

connect(widgets.label, &PropertyLabel::contextMenuRequested,
property, &Property::contextMenuRequested);

if (displayMode != Property::DisplayMode::NoLabel) {
widgets.label->setText(property->name());
widgets.label->setModified(property->isModified());
Expand Down Expand Up @@ -667,7 +686,7 @@ QLayout *VariantEditor::createPropertyLayout(Property *property)
widgets.editorLayout->addWidget(widgets.addButton, 0, Qt::AlignTop);
connect(widgets.addButton, &QAbstractButton::clicked, property, &Property::addRequested);

if (auto groupProperty = dynamic_cast<GroupProperty *>(property)) {
if (auto groupProperty = qobject_cast<GroupProperty *>(property)) {
widgets.childrenLayout = new QVBoxLayout;
widgets.childrenLayout->addLayout(rowLayout);
widgets.layout = widgets.childrenLayout;
Expand Down
4 changes: 4 additions & 0 deletions src/tiled/varianteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class Property : public QObject
void removeRequested();
void addRequested();

void contextMenuRequested(const QPoint &globalPos);

private:
friend class GroupProperty;

Expand Down Expand Up @@ -146,6 +148,8 @@ class GroupProperty : public Property

bool isExpanded() const { return m_expanded; }
void setExpanded(bool expanded);
void expandAll();
void collapseAll();

void clear()
{
Expand Down

0 comments on commit 5055d9a

Please sign in to comment.