Utils: Introduce a few AspectContainer convenience functions

Change-Id: I89832ab1971d263e4db9b8be5e89d581fd598b41
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2021-03-01 16:33:57 +01:00
parent 46402f2712
commit 2252b22b10
2 changed files with 40 additions and 0 deletions

View File

@@ -2078,6 +2078,40 @@ void AspectContainer::finish()
aspect->finish();
}
void AspectContainer::reset()
{
for (BaseAspect *aspect : qAsConst(d->m_items))
aspect->setValueQuietly(aspect->defaultValue());
}
void AspectContainer::fromMap(const QString &prefix, const QVariantMap &map)
{
for (BaseAspect *aspect : qAsConst(d->m_items))
aspect->setValue(map.value(prefix + aspect->settingsKey()));
}
void AspectContainer::toMap(const QString &prefix, QVariantMap &map) const
{
for (BaseAspect *aspect : qAsConst(d->m_items))
map.insert(prefix + aspect->settingsKey(), aspect->value());
}
bool AspectContainer::equals(const AspectContainer &other) const
{
// FIXME: Expensive, but should not really be needed in a fully aspectified world.
QVariantMap thisMap, thatMap;
toMap(thisMap);
other.toMap(thatMap);
return thisMap == thatMap;
}
void AspectContainer::copyFrom(const AspectContainer &other)
{
QVariantMap map;
other.toMap(map);
fromMap(map);
}
void AspectContainer::forEachAspect(const std::function<void(BaseAspect *)> &run) const
{
for (BaseAspect *aspect : qAsConst(d->m_items)) {

View File

@@ -533,6 +533,12 @@ public:
void cancel() override;
void finish() override;
void reset();
void fromMap(const QString &prefix, const QVariantMap &map);
void toMap(const QString &prefix, QVariantMap &map) const;
bool equals(const AspectContainer &other) const;
void copyFrom(const AspectContainer &other);
void forEachAspect(const std::function<void(BaseAspect *)> &run) const;
private: