Core: Simplify use of settings based on aspect containers

This effectively introduces a third parallel system for handling
IOptionPage apply() setup besides setWidgetCreator/IOptionsPageWidget
::apply() and the few remaining completely manual implementations of
IOptionPage::apply(), but there is some hope that this one can actually
replace all in a not so distant future.

Change-Id: I0c90a3a484216de9d7ad0f2f60c044c4e84dadb7
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2021-03-24 05:44:52 +01:00
parent 3bd490acdb
commit 3deba648cf
2 changed files with 46 additions and 12 deletions

View File

@@ -28,6 +28,9 @@
#include "ioptionspage.h" #include "ioptionspage.h"
#include <coreplugin/icore.h>
#include <utils/aspects.h>
#include <utils/stringutils.h> #include <utils/stringutils.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
@@ -121,9 +124,16 @@ void Core::IOptionsPage::setWidgetCreator(const WidgetCreator &widgetCreator)
QWidget *Core::IOptionsPage::widget() QWidget *Core::IOptionsPage::widget()
{ {
QTC_ASSERT(m_widgetCreator, return nullptr); if (!m_widget) {
if (!m_widget) if (m_widgetCreator) {
m_widget = m_widgetCreator(); m_widget = m_widgetCreator();
} else if (m_layouter) {
m_widget = new QWidget;
m_layouter(m_widget);
} else {
QTC_CHECK(false);
}
}
return m_widget; return m_widget;
} }
@@ -138,9 +148,14 @@ QWidget *Core::IOptionsPage::widget()
void Core::IOptionsPage::apply() void Core::IOptionsPage::apply()
{ {
QTC_ASSERT(m_widgetCreator, return); if (auto widget = qobject_cast<IOptionsPageWidget *>(m_widget)) {
if (m_widget) widget->apply();
m_widget->apply(); } else if (m_settings) {
if (m_settings->isDirty()) {
m_settings->apply();
m_settings->writeSettings(Core::ICore::settings());
}
}
} }
/*! /*!
@@ -154,11 +169,12 @@ void Core::IOptionsPage::apply()
void Core::IOptionsPage::finish() void Core::IOptionsPage::finish()
{ {
QTC_ASSERT(m_widgetCreator, return); if (auto widget = qobject_cast<IOptionsPageWidget *>(m_widget))
if (m_widget) { widget->finish();
m_widget->finish(); else if (m_settings)
delete m_widget; m_settings->finish();
}
delete m_widget;
} }
/*! /*!
@@ -170,6 +186,16 @@ void Core::IOptionsPage::setCategoryIconPath(const QString &categoryIconPath)
m_categoryIcon = Icon({{categoryIconPath, Theme::PanelTextColorDark}}, Icon::Tint); m_categoryIcon = Icon({{categoryIconPath, Theme::PanelTextColorDark}}, Icon::Tint);
} }
void Core::IOptionsPage::setSettings(AspectContainer *settings)
{
m_settings = settings;
}
void Core::IOptionsPage::setLayouter(const std::function<void(QWidget *w)> &layouter)
{
m_layouter = layouter;
}
/*! /*!
\fn void Core::IOptionsPage::setId(Utils::Id id) \fn void Core::IOptionsPage::setId(Utils::Id id)

View File

@@ -37,10 +37,13 @@
#include <functional> #include <functional>
namespace Utils { class AspectContainer; };
namespace Core { namespace Core {
class CORE_EXPORT IOptionsPageWidget : public QWidget class CORE_EXPORT IOptionsPageWidget : public QWidget
{ {
Q_OBJECT
public: public:
virtual void apply() = 0; virtual void apply() = 0;
virtual void finish() {} virtual void finish() {}
@@ -77,6 +80,8 @@ protected:
void setDisplayCategory(const QString &displayCategory) { m_displayCategory = displayCategory; } void setDisplayCategory(const QString &displayCategory) { m_displayCategory = displayCategory; }
void setCategoryIcon(const Utils::Icon &categoryIcon) { m_categoryIcon = categoryIcon; } void setCategoryIcon(const Utils::Icon &categoryIcon) { m_categoryIcon = categoryIcon; }
void setCategoryIconPath(const QString &categoryIconPath); void setCategoryIconPath(const QString &categoryIconPath);
void setSettings(Utils::AspectContainer *settings);
void setLayouter(const std::function<void(QWidget *w)> &layouter);
Utils::Id m_id; Utils::Id m_id;
Utils::Id m_category; Utils::Id m_category;
@@ -84,10 +89,13 @@ protected:
QString m_displayCategory; QString m_displayCategory;
Utils::Icon m_categoryIcon; Utils::Icon m_categoryIcon;
WidgetCreator m_widgetCreator; WidgetCreator m_widgetCreator;
QPointer<IOptionsPageWidget> m_widget; // Used in conjunction with m_widgetCreator QPointer<QWidget> m_widget; // Used in conjunction with m_widgetCreator
mutable bool m_keywordsInitialized = false; mutable bool m_keywordsInitialized = false;
mutable QStringList m_keywords; mutable QStringList m_keywords;
Utils::AspectContainer *m_settings = nullptr;
std::function<void(QWidget *w)> m_layouter;
}; };
/* /*