Skip to content

Reset properties properly #28006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 58 additions & 11 deletions src/inspector/models/abstractinspectormodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ void AbstractInspectorModel::setModelType(InspectorModelType modelType)
void AbstractInspectorModel::onPropertyValueChanged(const mu::engraving::Pid pid, const QVariant& newValue)
{
setPropertyValue(m_elementList, pid, newValue);
updateProperties();
}

void AbstractInspectorModel::setPropertyValue(const QList<engraving::EngravingItem*>& items, const mu::engraving::Pid pid,
Expand Down Expand Up @@ -401,6 +402,32 @@ void AbstractInspectorModel::setPropertyValue(const QList<engraving::EngravingIt
endCommand();
}

void AbstractInspectorModel::onPropertyValueReset(const mu::engraving::Pid pid)
{
resetPropertyValue(m_elementList, pid);
updateProperties();
}

void AbstractInspectorModel::resetPropertyValue(const QList<engraving::EngravingItem*>& items, const mu::engraving::Pid pid)
{
if (items.empty()) {
return;
}

beginCommand(TranslatableString("undoableAction", "Reset %1").arg(propertyUserName(pid)));

for (mu::engraving::EngravingItem* item : items) {
IF_ASSERT_FAILED(item) {
continue;
}

item->undoResetProperty(pid);
}

updateNotation();
endCommand();
}

void AbstractInspectorModel::updateProperties()
{
requestElements();
Expand Down Expand Up @@ -580,22 +607,24 @@ PropertyItem* AbstractInspectorModel::buildPropertyItem(const mu::engraving::Pid
std::function<void(const mu::engraving::Pid propertyId,
const QVariant& newValue)> onPropertyChangedCallBack,
std::function<void(const mu::engraving::Sid styleId,
const QVariant& newValue)> onStyleChangedCallBack)
const QVariant& newValue)> onStyleChangedCallBack,
std::function<void(const mu::engraving::Pid propertyId)> onPropertyResetCallBack)
{
PropertyItem* newPropertyItem = new PropertyItem(propertyId, this);

initPropertyItem(newPropertyItem, onPropertyChangedCallBack, onStyleChangedCallBack);
initPropertyItem(newPropertyItem, onPropertyChangedCallBack, onStyleChangedCallBack, onPropertyResetCallBack);

return newPropertyItem;
}

PointFPropertyItem* AbstractInspectorModel::buildPointFPropertyItem(const mu::engraving::Pid& propertyId,
std::function<void(const mu::engraving::Pid propertyId,
const QVariant& newValue)> onPropertyChangedCallBack)
const QVariant& newValue)> onPropertyChangedCallBack,
std::function<void(const mu::engraving::Pid propertyId)> onPropertyResetCallBack)
{
PointFPropertyItem* newPropertyItem = new PointFPropertyItem(propertyId, this);

initPropertyItem(newPropertyItem, onPropertyChangedCallBack);
initPropertyItem(newPropertyItem, onPropertyChangedCallBack, nullptr, onPropertyResetCallBack);

return newPropertyItem;
}
Expand All @@ -604,7 +633,8 @@ void AbstractInspectorModel::initPropertyItem(PropertyItem* propertyItem,
std::function<void(const mu::engraving::Pid propertyId,
const QVariant& newValue)> onPropertyChangedCallBack,
std::function<void(const mu::engraving::Sid styleId,
const QVariant& newValue)> onStyleChangedCallBack)
const QVariant& newValue)> onStyleChangedCallBack,
std::function<void(const mu::engraving::Pid propertyId)> onPropertyResetCallBack)
{
auto propertyCallback = onPropertyChangedCallBack;
if (!propertyCallback) {
Expand All @@ -622,8 +652,16 @@ void AbstractInspectorModel::initPropertyItem(PropertyItem* propertyItem,
};
}

auto resetCallback = onPropertyResetCallBack;
if (!resetCallback) {
resetCallback = [this](const mu::engraving::Pid propertyId) {
onPropertyValueReset(propertyId);
};
}

connect(propertyItem, &PropertyItem::propertyModified, this, propertyCallback);
connect(propertyItem, &PropertyItem::applyToStyleRequested, this, styleCallback);
connect(propertyItem, &PropertyItem::propertyReset, this, resetCallback);
}

void AbstractInspectorModel::loadPropertyItem(PropertyItem* propertyItem, ConvertPropertyValueFunc convertElementPropertyValueFunc)
Expand All @@ -649,9 +687,9 @@ void AbstractInspectorModel::loadPropertyItem(PropertyItem* propertyItem, const
propertyItem->setStyleId(styleId);

QVariant propertyValue;
QVariant defaultPropertyValue;

bool isUndefined = false;
bool isModified = false;

for (const mu::engraving::EngravingItem* element : elements) {
IF_ASSERT_FAILED(element) {
Expand All @@ -672,14 +710,22 @@ void AbstractInspectorModel::loadPropertyItem(PropertyItem* propertyItem, const
elementDefaultValue = convertElementPropertyValueFunc(elementDefaultValue);
}

if (!(propertyValue.isValid() && defaultPropertyValue.isValid())) {
if (!propertyValue.isValid()) {
propertyValue = elementCurrentValue;
defaultPropertyValue = elementDefaultValue;
}

isUndefined = propertyValue != elementCurrentValue;
if (!isUndefined && propertyValue != elementCurrentValue) {
isUndefined = true;
}

if (!isModified) {
PropertyFlags f = element->propertyFlags(pid);
if (f == PropertyFlags::UNSTYLED || elementCurrentValue != elementDefaultValue) {
isModified = true;
}
}

if (isUndefined) {
if (isUndefined && isModified) {
break;
}
}
Expand All @@ -692,7 +738,8 @@ void AbstractInspectorModel::loadPropertyItem(PropertyItem* propertyItem, const
propertyValue = QVariant();
}

propertyItem->fillValues(propertyValue, defaultPropertyValue);
propertyItem->fillValues(propertyValue);
propertyItem->setIsModified(isModified);
}

bool AbstractInspectorModel::isNotationExisting() const
Expand Down
18 changes: 14 additions & 4 deletions src/inspector/models/abstractinspectormodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,15 @@ public slots:
void setElementType(mu::engraving::ElementType type);

PropertyItem* buildPropertyItem(const mu::engraving::Pid& pid, std::function<void(const mu::engraving::Pid propertyId,
const QVariant& newValue)> onPropertyChangedCallBack = nullptr, std::function<void(const mu::engraving::Sid styleId,
const QVariant& newValue)> onStyleChangedCallBack = nullptr);
const QVariant& newValue)> onPropertyChangedCallBack =
nullptr, std::function<void(const mu::engraving::Sid styleId,
const
QVariant& newValue)> onStyleChangedCallBack = nullptr,
std::function<void(const mu::engraving::Pid propertyId)> onPropertyResetCallBack = nullptr);
PointFPropertyItem* buildPointFPropertyItem(const mu::engraving::Pid& pid, std::function<void(const mu::engraving::Pid propertyId,
const QVariant& newValue)> onPropertyChangedCallBack = nullptr);
const QVariant& newValue)>
onPropertyChangedCallBack = nullptr,
std::function<void(const mu::engraving::Pid propertyId)> onPropertyResetCallBack = nullptr);

using ConvertPropertyValueFunc = std::function<QVariant (const QVariant&)>;
void loadPropertyItem(PropertyItem* propertyItem, ConvertPropertyValueFunc convertElementPropertyValueFunc = nullptr);
Expand Down Expand Up @@ -233,13 +238,18 @@ public slots:
protected slots:
void onPropertyValueChanged(const mu::engraving::Pid pid, const QVariant& newValue);
void setPropertyValue(const QList<mu::engraving::EngravingItem*>& items, const mu::engraving::Pid pid, const QVariant& newValue);
void onPropertyValueReset(const mu::engraving::Pid pid);
void resetPropertyValue(const QList<mu::engraving::EngravingItem*>& items, const mu::engraving::Pid pid);
void updateProperties();

private:
void initPropertyItem(PropertyItem* propertyItem, std::function<void(const mu::engraving::Pid propertyId,
const QVariant& newValue)> onPropertyChangedCallBack = nullptr,
std::function<void(const mu::engraving::Sid styleId,
const QVariant& newValue)> onStyleChangedCallBack = nullptr);
const
QVariant
& newValue)> onStyleChangedCallBack = nullptr,
std::function<void(const mu::engraving::Pid propertyId)> onPropertyResetCallBack = nullptr);

mu::engraving::Sid styleIdByPropertyId(const mu::engraving::Pid pid) const;
mu::engraving::PropertyIdSet propertyIdSetFromStyleIdSet(const mu::engraving::StyleIdSet& styleIdSet) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ void AppearanceSettingsModel::createProperties()
m_offset = buildPointFPropertyItem(Pid::OFFSET, [this](const mu::engraving::Pid, const QVariant& newValue) {
setPropertyValue(m_elementsForOffsetProperty, Pid::OFFSET, newValue);
loadProperties();
}, [this](const mu::engraving::Pid) {
resetPropertyValue(m_elementsForOffsetProperty, Pid::OFFSET);
loadProperties();
});
}

Expand Down
37 changes: 16 additions & 21 deletions src/inspector/models/propertyitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ PropertyItem::PropertyItem(const mu::engraving::Pid propertyId, QObject* parent)
m_propertyId = propertyId;
}

void PropertyItem::fillValues(const QVariant& currentValue, const QVariant& defaultValue)
void PropertyItem::fillValues(const QVariant& currentValue)
{
updateCurrentValue(currentValue);

setDefaultValue(defaultValue);

emit isModifiedChanged(isModified());
}

Expand All @@ -55,12 +53,15 @@ void PropertyItem::updateCurrentValue(const QVariant& currentValue)

void PropertyItem::resetToDefault()
{
setValue(m_defaultValue);
emit propertyReset(m_propertyId);
emit isModifiedChanged(isModified());
}

void PropertyItem::applyToStyle()
{
emit applyToStyleRequested(m_styleId, m_currentValue);

resetToDefault();
}

mu::engraving::Pid PropertyItem::propertyId() const
Expand All @@ -73,11 +74,6 @@ QVariant PropertyItem::value() const
return m_currentValue;
}

QVariant PropertyItem::defaultValue() const
{
return m_defaultValue;
}

bool PropertyItem::isUndefined() const
{
return !m_currentValue.isValid();
Expand All @@ -100,7 +96,7 @@ bool PropertyItem::isStyled() const

bool PropertyItem::isModified() const
{
return m_currentValue != m_defaultValue;
return m_isModified;
}

void PropertyItem::setStyleId(const mu::engraving::Sid styleId)
Expand All @@ -122,17 +118,6 @@ void PropertyItem::setValue(const QVariant& value)
updateCurrentValue(value);

emit propertyModified(m_propertyId, m_currentValue);
emit isModifiedChanged(isModified());
}

void PropertyItem::setDefaultValue(const QVariant& defaultValue)
{
if (m_defaultValue == defaultValue) {
return;
}

m_defaultValue = defaultValue;
emit defaultValueChanged(m_defaultValue);
}

void PropertyItem::setIsEnabled(bool isEnabled)
Expand All @@ -154,3 +139,13 @@ void PropertyItem::setIsVisible(bool isVisible)
m_isVisible = isVisible;
emit isVisibleChanged(isVisible);
}

void PropertyItem::setIsModified(bool isModified)
{
if (m_isModified == isModified) {
return;
}

m_isModified = isModified;
emit isModifiedChanged(isModified);
}
10 changes: 4 additions & 6 deletions src/inspector/models/propertyitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class PropertyItem : public QObject
Q_OBJECT

Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
Q_PROPERTY(QVariant defaultValue READ defaultValue NOTIFY defaultValueChanged)
Q_PROPERTY(bool isUndefined READ isUndefined NOTIFY isUndefinedChanged)
Q_PROPERTY(bool isVisible READ isVisible NOTIFY isVisibleChanged)
Q_PROPERTY(bool isEnabled READ isEnabled NOTIFY isEnabledChanged)
Expand All @@ -44,15 +43,14 @@ class PropertyItem : public QObject
public:
explicit PropertyItem(const mu::engraving::Pid propertyId, QObject* parent = nullptr);

void fillValues(const QVariant& currentValue, const QVariant& defaultValue);
void fillValues(const QVariant& currentValue);
void updateCurrentValue(const QVariant& currentValue);

Q_INVOKABLE void resetToDefault();
Q_INVOKABLE void applyToStyle();

mu::engraving::Pid propertyId() const;
QVariant value() const;
QVariant defaultValue() const;
bool isUndefined() const;
bool isEnabled() const;
bool isVisible() const;
Expand All @@ -63,30 +61,30 @@ class PropertyItem : public QObject

public slots:
void setValue(const QVariant& value);
void setDefaultValue(const QVariant& defaultValue);
void setIsEnabled(bool isEnabled);
void setIsVisible(bool isVisible);
void setIsModified(bool isModified);

signals:
void valueChanged();
void defaultValueChanged(QVariant defaultValue);
void isUndefinedChanged(bool isUndefined);
void isEnabledChanged(bool isEnabled);
void isStyledChanged();
void isVisibleChanged(bool isVisible);
void isModifiedChanged(bool isModified);

void propertyModified(mu::engraving::Pid propertyId, QVariant newValue);
void propertyReset(mu::engraving::Pid propertyId);
void applyToStyleRequested(mu::engraving::Sid styledId, QVariant newStyleValue);

private:
mu::engraving::Pid m_propertyId = mu::engraving::Pid::END;
mu::engraving::Sid m_styleId = mu::engraving::Sid::NOSTYLE;

QVariant m_defaultValue;
QVariant m_currentValue;
bool m_isEnabled = false;
bool m_isVisible = false;
bool m_isModified = false;
};
}

Expand Down
Loading