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)
28 setAttribute(Qt::WA_DeleteOnClose, true);
33 ui.settingsTree->setRootIsDecorated(false);
35 connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
36 connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
39 void SettingsDlg::registerSettingsPage(SettingsPage *sp) {
40 sp->setParent(ui.settingsStack);
41 ui.settingsStack->addWidget(sp);
43 connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
46 QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
48 cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
49 cat->setExpanded(true);
50 cat->setFlags(Qt::ItemIsEnabled);
54 QTreeWidgetItem *item = new QTreeWidgetItem(cat, QStringList(sp->title()));
55 item->setData(0, SettingsPageRole, qVariantFromValue<QObject *>(sp));
56 pageIsLoaded[sp] = false;
59 void SettingsDlg::selectPage(SettingsPage *sp) {
62 ui.settingsStack->setCurrentIndex(0);
63 ui.pageTitle->setText(tr("Settings"));
67 if(!pageIsLoaded[sp]) {
69 pageIsLoaded[sp] = true;
72 if(sp != currentPage() && currentPage() != 0 && currentPage()->hasChanged()) {
73 int ret = QMessageBox::warning(this, tr("Save changes"),
74 tr("There are unsaved changes on the current configuration page. Would you like to apply your changes now?"),
75 QMessageBox::Discard|QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
76 if(ret == QMessageBox::Save) {
77 if(!applyChanges()) sp = currentPage();
78 } else if(ret == QMessageBox::Discard) {
80 } else sp = currentPage();
83 if(sp != currentPage()) {
84 ui.pageTitle->setText(sp->title());
85 ui.settingsStack->setCurrentWidget(sp);
91 void SettingsDlg::itemSelected() {
92 QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
94 if(!items.isEmpty()) {
95 sp = qobject_cast<SettingsPage *>(items[0]->data(0, SettingsPageRole).value<QObject *>());
100 void SettingsDlg::setButtonStates() {
101 SettingsPage *sp = currentPage();
102 ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
103 ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
104 ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
107 void SettingsDlg::buttonClicked(QAbstractButton *button) {
108 switch(ui.buttonBox->standardButton(button)) {
109 case QDialogButtonBox::Ok:
110 if(currentPage() && currentPage()->hasChanged()) {
111 if(applyChanges()) accept();
114 case QDialogButtonBox::Apply:
117 case QDialogButtonBox::Cancel:
121 case QDialogButtonBox::Reset:
124 case QDialogButtonBox::RestoreDefaults:
132 bool SettingsDlg::applyChanges() {
133 if(!currentPage()) return false;
134 if(currentPage()->aboutToSave()) {
135 currentPage()->save();
141 void SettingsDlg::undoChanges() {
143 currentPage()->load();
147 void SettingsDlg::reload() {
148 if(!currentPage()) return;
149 int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
150 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
151 if(ret == QMessageBox::Yes) {
152 currentPage()->load();
156 void SettingsDlg::loadDefaults() {
157 if(!currentPage()) return;
158 int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
159 QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
160 if(ret == QMessageBox::RestoreDefaults) {
161 currentPage()->defaults();