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"
30 #include "uisettings.h"
32 SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent)
37 _autoWidgetsChanged(false)
41 void SettingsPage::setChangedState(bool hasChanged_) {
42 if(hasChanged_ != _changed) {
43 bool old = hasChanged();
44 _changed = hasChanged_;
45 if(hasChanged() != old)
46 emit changed(hasChanged());
50 void SettingsPage::load(QCheckBox *box, bool checked) {
51 box->setProperty("StoredValue", checked);
52 box->setChecked(checked);
55 bool SettingsPage::hasChanged(QCheckBox *box) {
56 return box->property("StoredValue").toBool() == box->isChecked();
60 void SettingsPage::load(QComboBox *box, int index) {
61 box->setProperty("StoredValue", index);
62 box->setCurrentIndex(index);
65 bool SettingsPage::hasChanged(QComboBox *box) {
66 return box->property("StoredValue").toInt() == box->currentIndex();
69 void SettingsPage::load(QSpinBox *box, int value) {
70 box->setProperty("StoredValue", value);
74 bool SettingsPage::hasChanged(QSpinBox *box) {
75 return box->property("StoredValue").toInt() == box->value();
78 /*** Auto child widget handling ***/
80 void SettingsPage::initAutoWidgets() {
83 if(settingsKey().isNull())
86 // find all descendants that should be considered auto widgets
87 // we need to climb the QObject tree recursively
88 findAutoWidgets(this, &_autoWidgets);
90 foreach(QObject *widget, _autoWidgets) {
91 if(widget->inherits("QAbstractButton"))
92 connect(widget, SIGNAL(toggled(bool)), SLOT(autoWidgetHasChanged()));
93 else if(widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
94 connect(widget, SIGNAL(textChanged(const QString &)), SLOT(autoWidgetHasChanged()));
95 else if(widget->inherits("QComboBox"))
96 connect(widget, SIGNAL(currentIndexChanged(int)), SLOT(autoWidgetHasChanged()));
97 else if(widget->inherits("QSpinBox"))
98 connect(widget, SIGNAL(valueChanged(int)), SLOT(autoWidgetHasChanged()));
100 qWarning() << "SettingsPage::init(): Unknown autoWidget type" << widget->metaObject()->className();
104 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const {
105 foreach(QObject *child, parent->children()) {
106 if(!child->property("settingsKey").toString().isEmpty())
107 autoList->append(child);
108 findAutoWidgets(child, autoList);
112 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const {
114 if(widget->inherits("QAbstractButton"))
116 else if(widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
118 else if(widget->inherits("QComboBox"))
119 prop = "currentIndex";
120 else if(widget->inherits("QSpinBox"))
123 qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
128 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const {
129 QString key = widget->property("settingsKey").toString();
130 if(key.startsWith('/'))
133 key.prepend(settingsKey() + '/');
137 void SettingsPage::autoWidgetHasChanged() {
138 bool changed_ = false;
139 foreach(QObject *widget, _autoWidgets) {
140 QVariant curValue = widget->property(autoWidgetPropertyName(widget));
141 if(!curValue.isValid())
142 qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
144 if(curValue != widget->property("storedValue")) {
150 if(changed_ != _autoWidgetsChanged) {
151 bool old = hasChanged();
152 _autoWidgetsChanged = changed_;
153 if(hasChanged() != old)
154 emit changed(hasChanged());
158 void SettingsPage::load() {
160 foreach(QObject *widget, _autoWidgets) {
161 QVariant val = s.value(autoWidgetSettingsKey(widget), widget->property("defaultValue"));
162 widget->setProperty(autoWidgetPropertyName(widget), val);
163 widget->setProperty("storedValue", val);
165 bool old = hasChanged();
166 _autoWidgetsChanged = _changed = false;
167 if(hasChanged() != old)
168 emit changed(hasChanged());
171 void SettingsPage::save() {
173 foreach(QObject *widget, _autoWidgets) {
174 QVariant val = widget->property(autoWidgetPropertyName(widget));
175 widget->setProperty("storedValue", val);
176 s.setValue(autoWidgetSettingsKey(widget), val);
178 bool old = hasChanged();
179 _autoWidgetsChanged = _changed = false;
180 if(hasChanged() != old)
181 emit changed(hasChanged());
184 void SettingsPage::defaults() {
185 foreach(QObject *widget, _autoWidgets) {
186 QVariant val = widget->property("defaultValue");
187 widget->setProperty(autoWidgetPropertyName(widget), val);
189 autoWidgetHasChanged();