ProjectExplorer: Use separate class for aspect collection

Better encapsulation.

Change-Id: Ia77acf71493f8c7fe1436b2123eaa3adca6917ee
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2018-11-05 17:20:54 +01:00
parent fd40b445a1
commit 01d19b4f70
2 changed files with 77 additions and 22 deletions

View File

@@ -52,6 +52,33 @@ QWidget *ProjectConfigurationAspect::createConfigWidget() const
} }
// ProjectConfigurationAspects
ProjectConfigurationAspects::ProjectConfigurationAspects() = default;
ProjectConfigurationAspects::~ProjectConfigurationAspects()
{
qDeleteAll(base());
}
ProjectConfigurationAspect *ProjectConfigurationAspects::aspect(Core::Id id) const
{
return Utils::findOrDefault(base(), Utils::equal(&ProjectConfigurationAspect::id, id));
}
void ProjectConfigurationAspects::fromMap(const QVariantMap &map) const
{
for (ProjectConfigurationAspect *aspect : *this)
aspect->fromMap(map);
}
void ProjectConfigurationAspects::toMap(QVariantMap &map) const
{
for (ProjectConfigurationAspect *aspect : *this)
aspect->toMap(map);
}
// ProjectConfiguration // ProjectConfiguration
ProjectConfiguration::ProjectConfiguration(QObject *parent, Core::Id id) ProjectConfiguration::ProjectConfiguration(QObject *parent, Core::Id id)
@@ -61,10 +88,7 @@ ProjectConfiguration::ProjectConfiguration(QObject *parent, Core::Id id)
setObjectName(id.toString()); setObjectName(id.toString());
} }
ProjectConfiguration::~ProjectConfiguration() ProjectConfiguration::~ProjectConfiguration() = default;
{
qDeleteAll(m_aspects);
}
Core::Id ProjectConfiguration::id() const Core::Id ProjectConfiguration::id() const
{ {
@@ -130,8 +154,7 @@ QVariantMap ProjectConfiguration::toMap() const
map.insert(QLatin1String(DISPLAY_NAME_KEY), m_displayName); map.insert(QLatin1String(DISPLAY_NAME_KEY), m_displayName);
map.insert(QLatin1String(DEFAULT_DISPLAY_NAME_KEY), m_defaultDisplayName); map.insert(QLatin1String(DEFAULT_DISPLAY_NAME_KEY), m_defaultDisplayName);
for (const auto &aspect : m_aspects) m_aspects.toMap(map);
aspect->toMap(map);
return map; return map;
} }
@@ -148,15 +171,14 @@ bool ProjectConfiguration::fromMap(const QVariantMap &map)
m_defaultDisplayName.isEmpty() ? m_defaultDisplayName.isEmpty() ?
m_displayName : m_defaultDisplayName).toString(); m_displayName : m_defaultDisplayName).toString();
for (const auto &aspect : qAsConst(m_aspects)) m_aspects.fromMap(map);
aspect->fromMap(map);
return true; return true;
} }
ProjectConfigurationAspect *ProjectConfiguration::aspect(Core::Id id) const ProjectConfigurationAspect *ProjectConfiguration::aspect(Core::Id id) const
{ {
return Utils::findOrDefault(m_aspects, Utils::equal(&ProjectConfigurationAspect::id, id)); return m_aspects.aspect(id);
} }
Core::Id ProjectExplorer::idFromMap(const QVariantMap &map) Core::Id ProjectExplorer::idFromMap(const QVariantMap &map)

View File

@@ -80,6 +80,48 @@ protected:
ConfigWidgetCreator m_configWidgetCreator; ConfigWidgetCreator m_configWidgetCreator;
}; };
class PROJECTEXPLORER_EXPORT ProjectConfigurationAspects
: private QList<ProjectConfigurationAspect *>
{
using Base = QList<ProjectConfigurationAspect *>;
public:
ProjectConfigurationAspects();
~ProjectConfigurationAspects();
template <class Aspect, typename ...Args>
Aspect *addAspect(Args && ...args)
{
auto aspect = new Aspect(args...);
append(aspect);
return aspect;
}
ProjectConfigurationAspect *aspect(Core::Id id) const;
template <typename T> T *aspect() const
{
for (ProjectConfigurationAspect *aspect : *this)
if (T *result = qobject_cast<T *>(aspect))
return result;
return nullptr;
}
void fromMap(const QVariantMap &map) const;
void toMap(QVariantMap &map) const;
using Base::append;
using Base::begin;
using Base::end;
private:
Base &base() { return *this; }
const Base &base() const { return *this; }
ProjectConfigurationAspects(const ProjectConfigurationAspects &) = delete;
ProjectConfigurationAspects &operator=(const ProjectConfigurationAspects &) = delete;
};
class PROJECTEXPLORER_EXPORT ProjectConfiguration : public QObject class PROJECTEXPLORER_EXPORT ProjectConfiguration : public QObject
{ {
Q_OBJECT Q_OBJECT
@@ -119,29 +161,20 @@ public:
template<class Aspect, typename ...Args> template<class Aspect, typename ...Args>
Aspect *addAspect(Args && ...args) Aspect *addAspect(Args && ...args)
{ {
auto aspect = new Aspect(args...); return m_aspects.addAspect<Aspect>(std::forward<Args>(args)...);
m_aspects.append(aspect);
return aspect;
} }
const QList<ProjectConfigurationAspect *> aspects() const { return m_aspects; } const ProjectConfigurationAspects &aspects() const { return m_aspects; }
ProjectConfigurationAspect *aspect(Core::Id id) const; ProjectConfigurationAspect *aspect(Core::Id id) const;
template <typename T> T *aspect() const { return m_aspects.aspect<T>(); }
template <typename T> T *aspect() const
{
for (ProjectConfigurationAspect *aspect : m_aspects)
if (T *result = qobject_cast<T *>(aspect))
return result;
return nullptr;
}
signals: signals:
void displayNameChanged(); void displayNameChanged();
void toolTipChanged(); void toolTipChanged();
protected: protected:
QList<ProjectConfigurationAspect *> m_aspects; ProjectConfigurationAspects m_aspects;
private: private:
const Core::Id m_id; const Core::Id m_id;