1 /***************************************************************************
2 * Copyright (C) 2005-08 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) version 3. *
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 "settingsdlg.h"
23 SettingsDlg::SettingsDlg(QWidget *parent)
31 ui.settingsTree->setRootIsDecorated(false);
33 connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
34 connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
37 void SettingsDlg::registerSettingsPage(SettingsPage *sp) {
38 sp->setParent(ui.settingsStack);
39 ui.settingsStack->addWidget(sp);
41 connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
44 QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
46 cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
47 cat->setExpanded(true);
48 cat->setFlags(Qt::ItemIsEnabled);
52 QTreeWidgetItem *item = new QTreeWidgetItem(cat, QStringList(sp->title()));
53 item->setData(0, SettingsPageRole, qVariantFromValue<QObject *>(sp));
54 pageIsLoaded[sp] = false;
57 void SettingsDlg::selectPage(SettingsPage *sp) {
60 ui.settingsStack->setCurrentIndex(0);
61 ui.pageTitle->setText(tr("Settings"));
65 if(!pageIsLoaded[sp]) {
67 pageIsLoaded[sp] = true;
70 if(sp != currentPage() && currentPage() != 0 && currentPage()->hasChanged()) {
71 int ret = QMessageBox::warning(this, tr("Save changes"),
72 tr("There are unsaved changes on the current configuration page. Would you like to apply your changes now?"),
73 QMessageBox::Discard|QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
74 if(ret == QMessageBox::Save) {
75 if(!applyChanges()) sp = currentPage();
76 } else if(ret == QMessageBox::Discard) {
78 } else sp = currentPage();
81 if(sp != currentPage()) {
82 ui.pageTitle->setText(sp->title());
83 ui.settingsStack->setCurrentWidget(sp);
89 void SettingsDlg::itemSelected() {
90 QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
92 if(!items.isEmpty()) {
93 sp = qobject_cast<SettingsPage *>(items[0]->data(0, SettingsPageRole).value<QObject *>());
98 void SettingsDlg::setButtonStates() {
99 SettingsPage *sp = currentPage();
100 ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
101 ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
102 ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
105 void SettingsDlg::buttonClicked(QAbstractButton *button) {
106 switch(ui.buttonBox->standardButton(button)) {
107 case QDialogButtonBox::Ok:
108 if(currentPage() && currentPage()->hasChanged()) {
109 if(applyChanges()) accept();
112 case QDialogButtonBox::Apply:
115 case QDialogButtonBox::Cancel:
119 case QDialogButtonBox::Reset:
122 case QDialogButtonBox::RestoreDefaults:
130 bool SettingsDlg::applyChanges() {
131 if(!currentPage()) return false;
132 if(currentPage()->aboutToSave()) {
133 currentPage()->save();
139 void SettingsDlg::undoChanges() {
141 currentPage()->load();
145 void SettingsDlg::reload() {
146 if(!currentPage()) return;
147 int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
148 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
149 if(ret == QMessageBox::Yes) {
150 currentPage()->load();
154 void SettingsDlg::loadDefaults() {
155 if(!currentPage()) return;
156 int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
157 QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
158 if(ret == QMessageBox::RestoreDefaults) {
159 currentPage()->defaults();