X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fuisupport%2Fsettingspage.cpp;h=98f412336da05850c0aecdb2d7c330b0fe06d9bf;hb=694f9bfbf7f1af19108461c7e00d133e55082bce;hp=37ac416f8b03b34a4fa10eb7f0926750143576ed;hpb=9a6a8478bdd8c7c5bb4ff1fa3de9510863d65a97;p=quassel.git diff --git a/src/uisupport/settingspage.cpp b/src/uisupport/settingspage.cpp index 37ac416f..98f41233 100644 --- a/src/uisupport/settingspage.cpp +++ b/src/uisupport/settingspage.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel Project * + * Copyright (C) 2005-09 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -20,44 +20,229 @@ #include "settingspage.h" -SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent) : QWidget(parent), - _category(category), _title(title) { +#include +#include +#include +#include - _changed = false; +#include "uisettings.h" + +SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent) + : QWidget(parent), + _category(category), + _title(title), + _changed(false), + _autoWidgetsChanged(false) +{ +} + + +void SettingsPage::setChangedState(bool hasChanged_) +{ + if (hasChanged_ != _changed) { + bool old = hasChanged(); + _changed = hasChanged_; + if (hasChanged() != old) + emit changed(hasChanged()); + } +} + + +void SettingsPage::load(QCheckBox *box, bool checked) +{ + box->setProperty("StoredValue", checked); + box->setChecked(checked); +} + + +bool SettingsPage::hasChanged(QCheckBox *box) +{ + return box->property("StoredValue").toBool() != box->isChecked(); +} + + +void SettingsPage::load(QComboBox *box, int index) +{ + box->setProperty("StoredValue", index); + box->setCurrentIndex(index); +} + + +bool SettingsPage::hasChanged(QComboBox *box) +{ + return box->property("StoredValue").toInt() != box->currentIndex(); +} + + +void SettingsPage::load(QSpinBox *box, int value) +{ + box->setProperty("StoredValue", value); + box->setValue(value); +} + + +bool SettingsPage::hasChanged(QSpinBox *box) +{ + return box->property("StoredValue").toInt() != box->value(); +} + + +/*** Auto child widget handling ***/ + +void SettingsPage::initAutoWidgets() +{ + _autoWidgets.clear(); + + // find all descendants that should be considered auto widgets + // we need to climb the QObject tree recursively + findAutoWidgets(this, &_autoWidgets); + + foreach(QObject *widget, _autoWidgets) { + if (widget->inherits("ColorButton")) + connect(widget, SIGNAL(colorChanged(QColor)), SLOT(autoWidgetHasChanged())); + else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox")) + connect(widget, SIGNAL(toggled(bool)), SLOT(autoWidgetHasChanged())); + else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit")) + connect(widget, SIGNAL(textChanged(const QString &)), SLOT(autoWidgetHasChanged())); + else if (widget->inherits("QComboBox")) + connect(widget, SIGNAL(currentIndexChanged(int)), SLOT(autoWidgetHasChanged())); + else if (widget->inherits("QSpinBox")) + connect(widget, SIGNAL(valueChanged(int)), SLOT(autoWidgetHasChanged())); + else if (widget->inherits("FontSelector")) + connect(widget, SIGNAL(fontChanged(QFont)), SLOT(autoWidgetHasChanged())); + else + qWarning() << "SettingsPage::init(): Unknown autoWidget type" << widget->metaObject()->className(); + } } -QString SettingsPage::category() const { - return _category; + +void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const +{ + foreach(QObject *child, parent->children()) { + if (child->property("settingsKey").isValid()) + autoList->append(child); + findAutoWidgets(child, autoList); + } } -QString SettingsPage::title() const { - return _title; + +QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const +{ + QByteArray prop; + if (widget->inherits("ColorButton")) + prop = "color"; + else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox")) + prop = "checked"; + else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit")) + prop = "text"; + else if (widget->inherits("QComboBox")) + prop = "currentIndex"; + else if (widget->inherits("QSpinBox")) + prop = "value"; + else if (widget->inherits("FontSelector")) + prop = "selectedFont"; + else + qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget; + + return prop; } -bool SettingsPage::hasDefaults() const { - return false; + +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 + key.prepend(settingsKey() + '/'); + return key; } -void SettingsPage::defaults() { +void SettingsPage::autoWidgetHasChanged() +{ + bool changed_ = false; + foreach(QObject *widget, _autoWidgets) { + QVariant curValue = widget->property(autoWidgetPropertyName(widget)); + if (!curValue.isValid()) + qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property"; + + if (curValue != widget->property("storedValue")) { + changed_ = true; + break; + } + } + + if (changed_ != _autoWidgetsChanged) { + bool old = hasChanged(); + _autoWidgetsChanged = changed_; + if (hasChanged() != old) + emit changed(hasChanged()); + } } -bool SettingsPage::hasChanged() const { - return _changed; + +void SettingsPage::load() +{ + UiSettings s(""); + foreach(QObject *widget, _autoWidgets) { + 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); + } + bool old = hasChanged(); + _autoWidgetsChanged = _changed = false; + if (hasChanged() != old) + emit changed(hasChanged()); } -bool SettingsPage::aboutToSave() { - return true; + +void SettingsPage::save() +{ + UiSettings s(""); + foreach(QObject *widget, _autoWidgets) { + QString key = autoWidgetSettingsKey(widget); + QVariant val = widget->property(autoWidgetPropertyName(widget)); + widget->setProperty("storedValue", val); + if (key.isEmpty()) + saveAutoWidgetValue(widget->objectName(), val); + else + s.setValue(key, val); + } + bool old = hasChanged(); + _autoWidgetsChanged = _changed = false; + if (hasChanged() != old) + emit changed(hasChanged()); } -void SettingsPage::changed() { - changeState(true); + +void SettingsPage::defaults() +{ + foreach(QObject *widget, _autoWidgets) { + QVariant val = widget->property("defaultValue"); + widget->setProperty(autoWidgetPropertyName(widget), val); + } + autoWidgetHasChanged(); } -void SettingsPage::changeState(bool hasChanged) { - if(hasChanged != _changed) { - _changed = hasChanged; - emit changed(hasChanged); - } + +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; +}