1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "settingspage.h"
28 #include "uisettings.h"
30 SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent)
35 _autoWidgetsChanged(false)
39 void SettingsPage::setChangedState(bool hasChanged_) {
40 if(hasChanged_ != _changed) {
41 bool old = hasChanged();
42 _changed = hasChanged_;
43 if(hasChanged() != old)
44 emit changed(hasChanged());
48 void SettingsPage::load(QCheckBox *box, bool checked) {
49 box->setProperty("StoredValue", checked);
50 box->setChecked(checked);
53 bool SettingsPage::hasChanged(QCheckBox *box) {
54 return box->property("StoredValue").toBool() != box->isChecked();
58 void SettingsPage::load(QComboBox *box, int index) {
59 box->setProperty("StoredValue", index);
60 box->setCurrentIndex(index);
63 bool SettingsPage::hasChanged(QComboBox *box) {
64 return box->property("StoredValue").toInt() != box->currentIndex();
67 void SettingsPage::load(QSpinBox *box, int value) {
68 box->setProperty("StoredValue", value);
72 bool SettingsPage::hasChanged(QSpinBox *box) {
73 return box->property("StoredValue").toInt() != box->value();
76 /*** Auto child widget handling ***/
78 void SettingsPage::initAutoWidgets() {
81 // find all descendants that should be considered auto widgets
82 // we need to climb the QObject tree recursively
83 findAutoWidgets(this, &_autoWidgets);
85 foreach(QObject *widget, _autoWidgets) {
86 if(widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
87 connect(widget, SIGNAL(toggled(bool)), SLOT(autoWidgetHasChanged()));
88 else if(widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
89 connect(widget, SIGNAL(textChanged(const QString &)), SLOT(autoWidgetHasChanged()));
90 else if(widget->inherits("QComboBox"))
91 connect(widget, SIGNAL(currentIndexChanged(int)), SLOT(autoWidgetHasChanged()));
92 else if(widget->inherits("QSpinBox"))
93 connect(widget, SIGNAL(valueChanged(int)), SLOT(autoWidgetHasChanged()));
95 qWarning() << "SettingsPage::init(): Unknown autoWidget type" << widget->metaObject()->className();
99 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const {
100 foreach(QObject *child, parent->children()) {
101 if(child->property("settingsKey").isValid())
102 autoList->append(child);
103 findAutoWidgets(child, autoList);
107 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const {
109 if(widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
111 else if(widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
113 else if(widget->inherits("QComboBox"))
114 prop = "currentIndex";
115 else if(widget->inherits("QSpinBox"))
118 qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
123 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const {
124 QString key = widget->property("settingsKey").toString();
127 if(key.startsWith('/'))
130 key.prepend(settingsKey() + '/');
134 void SettingsPage::autoWidgetHasChanged() {
135 bool changed_ = false;
136 foreach(QObject *widget, _autoWidgets) {
137 QVariant curValue = widget->property(autoWidgetPropertyName(widget));
138 if(!curValue.isValid())
139 qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
141 if(curValue != widget->property("storedValue")) {
147 if(changed_ != _autoWidgetsChanged) {
148 bool old = hasChanged();
149 _autoWidgetsChanged = changed_;
150 if(hasChanged() != old)
151 emit changed(hasChanged());
155 void SettingsPage::load() {
157 foreach(QObject *widget, _autoWidgets) {
158 QString key = autoWidgetSettingsKey(widget);
161 val = loadAutoWidgetValue(widget->objectName());
163 val = s.value(key, QVariant());
165 val = widget->property("defaultValue");
166 widget->setProperty(autoWidgetPropertyName(widget), val);
167 widget->setProperty("storedValue", val);
169 bool old = hasChanged();
170 _autoWidgetsChanged = _changed = false;
171 if(hasChanged() != old)
172 emit changed(hasChanged());
175 void SettingsPage::save() {
177 foreach(QObject *widget, _autoWidgets) {
178 QString key = autoWidgetSettingsKey(widget);
179 QVariant val = widget->property(autoWidgetPropertyName(widget));
180 widget->setProperty("storedValue", val);
182 saveAutoWidgetValue(widget->objectName(), val);
184 s.setValue(key, val);
186 bool old = hasChanged();
187 _autoWidgetsChanged = _changed = false;
188 if(hasChanged() != old)
189 emit changed(hasChanged());
192 void SettingsPage::defaults() {
193 foreach(QObject *widget, _autoWidgets) {
194 QVariant val = widget->property("defaultValue");
195 widget->setProperty(autoWidgetPropertyName(widget), val);
197 autoWidgetHasChanged();
200 QVariant SettingsPage::loadAutoWidgetValue(const QString &widgetName) {
201 qWarning() << "Could not load value for SettingsPage widget" << widgetName;
205 void SettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &) {
206 qWarning() << "Could not save value for SettingsPage widget" << widgetName;