forked from qt-creator/qt-creator
Utils: Allow aspects to modify values for loading and saving
In some cases it is useful when the persistent value is not the internally used actual value, e.g. Having the displayed string of a SelectionAspect instead of a numerical index makes the settings more readable for a human. Change-Id: I11ecb8e75ab041ace2358cc45972ce9ee965b24d Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -60,6 +60,8 @@ public:
|
|||||||
Utils::Id m_id;
|
Utils::Id m_id;
|
||||||
QVariant m_value;
|
QVariant m_value;
|
||||||
QVariant m_defaultValue;
|
QVariant m_defaultValue;
|
||||||
|
std::function<QVariant(const QVariant &)> m_toSettings;
|
||||||
|
std::function<QVariant(const QVariant &)> m_fromSettings;
|
||||||
|
|
||||||
QString m_displayName;
|
QString m_displayName;
|
||||||
QString m_settingsKey; // Name of data in settings.
|
QString m_settingsKey; // Name of data in settings.
|
||||||
@@ -488,7 +490,8 @@ void BaseAspect::saveToMap(QVariantMap &data, const QVariant &value,
|
|||||||
*/
|
*/
|
||||||
void BaseAspect::fromMap(const QVariantMap &map)
|
void BaseAspect::fromMap(const QVariantMap &map)
|
||||||
{
|
{
|
||||||
setValue(map.value(settingsKey(), defaultValue()));
|
const QVariant val = map.value(settingsKey(), toSettingsValue(defaultValue()));
|
||||||
|
setValue(fromSettingsValue(val));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -496,21 +499,45 @@ void BaseAspect::fromMap(const QVariantMap &map)
|
|||||||
*/
|
*/
|
||||||
void BaseAspect::toMap(QVariantMap &map) const
|
void BaseAspect::toMap(QVariantMap &map) const
|
||||||
{
|
{
|
||||||
saveToMap(map, d->m_value, d->m_defaultValue, settingsKey());
|
saveToMap(map, toSettingsValue(d->m_value), toSettingsValue(d->m_defaultValue), settingsKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseAspect::readSettings(const QSettings *settings)
|
void BaseAspect::readSettings(const QSettings *settings)
|
||||||
{
|
{
|
||||||
if (settingsKey().isEmpty())
|
if (settingsKey().isEmpty())
|
||||||
return;
|
return;
|
||||||
setValue(settings->value(settingsKey(), defaultValue()));
|
const QVariant val = settings->value(settingsKey(), toSettingsValue(defaultValue()));
|
||||||
|
setValue(fromSettingsValue(val));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseAspect::writeSettings(QSettings *settings) const
|
void BaseAspect::writeSettings(QSettings *settings) const
|
||||||
{
|
{
|
||||||
if (settingsKey().isEmpty())
|
if (settingsKey().isEmpty())
|
||||||
return;
|
return;
|
||||||
QtcSettings::setValueWithDefault(settings, settingsKey(), value(), defaultValue());
|
QtcSettings::setValueWithDefault(settings,
|
||||||
|
settingsKey(),
|
||||||
|
toSettingsValue(value()),
|
||||||
|
toSettingsValue(defaultValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
void BaseAspect::setFromSettingsTransformation(const SavedValueTransformation &transform)
|
||||||
|
{
|
||||||
|
d->m_fromSettings = transform;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BaseAspect::setToSettingsTransformation(const SavedValueTransformation &transform)
|
||||||
|
{
|
||||||
|
d->m_toSettings = transform;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant BaseAspect::toSettingsValue(const QVariant &val) const
|
||||||
|
{
|
||||||
|
return d->m_toSettings ? d->m_toSettings(val) : val;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant BaseAspect::fromSettingsValue(const QVariant &val) const
|
||||||
|
{
|
||||||
|
return d->m_fromSettings ? d->m_fromSettings(val) : val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -1436,11 +1463,24 @@ void SelectionAspect::setValue(int value)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SelectionAspect::setStringValue(const QString &val)
|
||||||
|
{
|
||||||
|
const int index = indexForDisplay(val);
|
||||||
|
QTC_ASSERT(index >= 0, return);
|
||||||
|
setValue(index);
|
||||||
|
}
|
||||||
|
|
||||||
void SelectionAspect::setDefaultValue(int val)
|
void SelectionAspect::setDefaultValue(int val)
|
||||||
{
|
{
|
||||||
BaseAspect::setDefaultValue(val);
|
BaseAspect::setDefaultValue(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Note: This needs to be set after all options are added.
|
||||||
|
void SelectionAspect::setDefaultValue(const QString &val)
|
||||||
|
{
|
||||||
|
BaseAspect::setDefaultValue(indexForDisplay(val));
|
||||||
|
}
|
||||||
|
|
||||||
QString SelectionAspect::stringValue() const
|
QString SelectionAspect::stringValue() const
|
||||||
{
|
{
|
||||||
return d->m_options.at(value()).displayName;
|
return d->m_options.at(value()).displayName;
|
||||||
@@ -1451,6 +1491,20 @@ void SelectionAspect::addOption(const QString &displayName, const QString &toolT
|
|||||||
d->m_options.append({displayName, toolTip});
|
d->m_options.append({displayName, toolTip});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int SelectionAspect::indexForDisplay(const QString &displayName) const
|
||||||
|
{
|
||||||
|
for (int i = 0, n = d->m_options.size(); i < n; ++i) {
|
||||||
|
if (d->m_options.at(i).displayName == displayName)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString SelectionAspect::displayForIndex(int index) const
|
||||||
|
{
|
||||||
|
return d->m_options.at(index).displayName;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\class Utils::MultiSelectionAspect
|
\class Utils::MultiSelectionAspect
|
||||||
\inmodule QtCreator
|
\inmodule QtCreator
|
||||||
|
|||||||
@@ -120,6 +120,12 @@ public:
|
|||||||
virtual void readSettings(const QSettings *settings);
|
virtual void readSettings(const QSettings *settings);
|
||||||
virtual void writeSettings(QSettings *settings) const;
|
virtual void writeSettings(QSettings *settings) const;
|
||||||
|
|
||||||
|
using SavedValueTransformation = std::function<QVariant(const QVariant &)>;
|
||||||
|
void setFromSettingsTransformation(const SavedValueTransformation &transform);
|
||||||
|
void setToSettingsTransformation(const SavedValueTransformation &transform);
|
||||||
|
QVariant toSettingsValue(const QVariant &val) const;
|
||||||
|
QVariant fromSettingsValue(const QVariant &val) const;
|
||||||
|
|
||||||
virtual void apply();
|
virtual void apply();
|
||||||
virtual void cancel();
|
virtual void cancel();
|
||||||
virtual void finish();
|
virtual void finish();
|
||||||
@@ -242,7 +248,9 @@ public:
|
|||||||
|
|
||||||
int value() const;
|
int value() const;
|
||||||
void setValue(int val);
|
void setValue(int val);
|
||||||
|
void setStringValue(const QString &val);
|
||||||
void setDefaultValue(int val);
|
void setDefaultValue(int val);
|
||||||
|
void setDefaultValue(const QString &val);
|
||||||
|
|
||||||
QString stringValue() const;
|
QString stringValue() const;
|
||||||
|
|
||||||
@@ -250,6 +258,8 @@ public:
|
|||||||
void setDisplayStyle(DisplayStyle style);
|
void setDisplayStyle(DisplayStyle style);
|
||||||
|
|
||||||
void addOption(const QString &displayName, const QString &toolTip = {});
|
void addOption(const QString &displayName, const QString &toolTip = {});
|
||||||
|
int indexForDisplay(const QString &displayName) const;
|
||||||
|
QString displayForIndex(int index) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Internal::SelectionAspectPrivate> d;
|
std::unique_ptr<Internal::SelectionAspectPrivate> d;
|
||||||
|
|||||||
Reference in New Issue
Block a user