Allow custom saving/loading of SettingsPage autowidgets
authorManuel Nickschas <sputnick@quassel-irc.org>
Tue, 21 Jul 2009 22:25:01 +0000 (00:25 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Tue, 21 Jul 2009 22:25:01 +0000 (00:25 +0200)
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
src/uisupport/settingspage.h

index a1b6f8d..0476f96 100644 (file)
@@ -25,8 +25,6 @@
 #include <QSpinBox>
 #include <QVariant>
 
-#include <QDebug>
-
 #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;
+}
+
index 4a106aa..3ef6d2c 100644 (file)
@@ -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.