summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
84b0f5f)
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.
#include <QSpinBox>
#include <QVariant>
#include <QSpinBox>
#include <QVariant>
#include "uisettings.h"
SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent)
#include "uisettings.h"
SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent)
void SettingsPage::initAutoWidgets() {
_autoWidgets.clear();
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);
// find all descendants that should be considered auto widgets
// we need to climb the QObject tree recursively
findAutoWidgets(this, &_autoWidgets);
void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const {
foreach(QObject *child, parent->children()) {
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);
}
autoList->append(child);
findAutoWidgets(child, autoList);
}
QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const {
QString key = widget->property("settingsKey").toString();
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
if(key.startsWith('/'))
key.remove(0, 1);
else
void SettingsPage::load() {
UiSettings s("");
foreach(QObject *widget, _autoWidgets) {
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);
}
widget->setProperty(autoWidgetPropertyName(widget), val);
widget->setProperty("storedValue", val);
}
void SettingsPage::save() {
UiSettings s("");
foreach(QObject *widget, _autoWidgets) {
void SettingsPage::save() {
UiSettings s("");
foreach(QObject *widget, _autoWidgets) {
+ QString key = autoWidgetSettingsKey(widget);
QVariant val = widget->property(autoWidgetPropertyName(widget));
widget->setProperty("storedValue", val);
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;
}
bool old = hasChanged();
_autoWidgetsChanged = _changed = false;
}
autoWidgetHasChanged();
}
}
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;
+}
+
* 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.
*
* 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)).
* 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)).
protected:
void initAutoWidgets();
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.
signals:
//! Emitted whenever the widget state changes.