From 1545449bce16fa939869d73ed5d4bbd8416d4512 Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Wed, 22 Jul 2009 00:25:01 +0200 Subject: [PATCH] Allow custom saving/loading of SettingsPage autowidgets SettingsPages can automatically handle child widgets' contents (e.g. track changes, load and save) if those have the properties "settingsKey" and "defaultValue" set. However, sometimes widgets need to be saved to something else than a Settings object (e.g. a core config object). You can now leave the settingsKey property empty for your autowidgets and reimplement {save|load}AutoWidgetValue() in your derived SettingsPage to handle custom saving and loading. --- src/uisupport/settingspage.cpp | 34 ++++++++++++++++++++++++++-------- src/uisupport/settingspage.h | 5 +++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/uisupport/settingspage.cpp b/src/uisupport/settingspage.cpp index a1b6f8db..0476f967 100644 --- a/src/uisupport/settingspage.cpp +++ b/src/uisupport/settingspage.cpp @@ -25,8 +25,6 @@ #include #include -#include - #include "uisettings.h" SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent) @@ -80,9 +78,6 @@ bool SettingsPage::hasChanged(QSpinBox *box) { void SettingsPage::initAutoWidgets() { _autoWidgets.clear(); - if(settingsKey().isNull()) - return; - // find all descendants that should be considered auto widgets // we need to climb the QObject tree recursively findAutoWidgets(this, &_autoWidgets); @@ -103,7 +98,7 @@ void SettingsPage::initAutoWidgets() { void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const { foreach(QObject *child, parent->children()) { - if(!child->property("settingsKey").toString().isEmpty()) + if(child->property("settingsKey").isValid()) autoList->append(child); findAutoWidgets(child, autoList); } @@ -127,6 +122,8 @@ QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const { QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const { QString key = widget->property("settingsKey").toString(); + if(key.isEmpty()) + return QString(""); if(key.startsWith('/')) key.remove(0, 1); else @@ -158,7 +155,14 @@ void SettingsPage::autoWidgetHasChanged() { void SettingsPage::load() { UiSettings s(""); foreach(QObject *widget, _autoWidgets) { - QVariant val = s.value(autoWidgetSettingsKey(widget), widget->property("defaultValue")); + QString key = autoWidgetSettingsKey(widget); + QVariant val; + if(key.isEmpty()) + val = loadAutoWidgetValue(widget->objectName()); + else + val = s.value(key, QVariant()); + if(!val.isValid()) + val = widget->property("defaultValue"); widget->setProperty(autoWidgetPropertyName(widget), val); widget->setProperty("storedValue", val); } @@ -171,9 +175,13 @@ void SettingsPage::load() { void SettingsPage::save() { UiSettings s(""); foreach(QObject *widget, _autoWidgets) { + QString key = autoWidgetSettingsKey(widget); QVariant val = widget->property(autoWidgetPropertyName(widget)); widget->setProperty("storedValue", val); - s.setValue(autoWidgetSettingsKey(widget), val); + if(key.isEmpty()) + saveAutoWidgetValue(widget->objectName(), val); + else + s.setValue(key, val); } bool old = hasChanged(); _autoWidgetsChanged = _changed = false; @@ -188,3 +196,13 @@ void SettingsPage::defaults() { } autoWidgetHasChanged(); } + +QVariant SettingsPage::loadAutoWidgetValue(const QString &widgetName) { + qWarning() << "Could not load value for SettingsPage widget" << widgetName; + return QVariant(); +} + +void SettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &) { + qWarning() << "Could not save value for SettingsPage widget" << widgetName; +} + diff --git a/src/uisupport/settingspage.h b/src/uisupport/settingspage.h index 4a106aa1..3ef6d2cc 100644 --- a/src/uisupport/settingspage.h +++ b/src/uisupport/settingspage.h @@ -40,6 +40,9 @@ class QSpinBox; * its key is treated as a global path starting from the root, rather than from settingsKey(). * A second dynamic property \c defaultValue can be defined in child widgets as well. * + * For widgets requiring special ways for storing and saving, define the property settingsKey and leave it empty. In this + * case, the methods saveAutoWidgetValue() and loadAutoWidgetValue() will be called with the widget's objectName as parameter. + * * SettingsPage manages loading, saving, setting to default and setting the changed state for all automatic child widgets. * Derived classes must be sure to call initAutoWidgets() *after* setupUi(); they also need to call the baseclass implementations * of load(), save() and defaults() (preferably at the end of the derived function, since they call setChangedState(false)). @@ -127,6 +130,8 @@ protected slots: protected: void initAutoWidgets(); + virtual QVariant loadAutoWidgetValue(const QString &widgetName); + virtual void saveAutoWidgetValue(const QString &widgetName, const QVariant &value); signals: //! Emitted whenever the widget state changes. -- 2.20.1