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 *)));
41 void SettingsDlg::registerSettingsPage(SettingsPage *sp) {
42 sp->setParent(ui.settingsStack);
43 ui.settingsStack->addWidget(sp);
45 connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
48 QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
50 cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
51 cat->setExpanded(true);
52 cat->setFlags(Qt::ItemIsEnabled);
56 QTreeWidgetItem *item = new QTreeWidgetItem(cat, QStringList(sp->title()));
57 item->setData(0, SettingsPageRole, qVariantFromValue<QObject *>(sp));
58 pageIsLoaded[sp] = false;
61 void SettingsDlg::selectPage(SettingsPage *sp) {
64 ui.settingsStack->setCurrentIndex(0);
65 ui.pageTitle->setText(tr("Settings"));
69 if(!pageIsLoaded[sp]) {
71 pageIsLoaded[sp] = true;
74 if(sp != currentPage() && currentPage() != 0 && currentPage()->hasChanged()) {
75 int ret = QMessageBox::warning(this, tr("Save changes"),
76 tr("There are unsaved changes on the current configuration page. Would you like to apply your changes now?"),
77 QMessageBox::Discard|QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
78 if(ret == QMessageBox::Save) {
79 if(!applyChanges()) sp = currentPage();
80 } else if(ret == QMessageBox::Discard) {
82 } else sp = currentPage();
85 if(sp != currentPage()) {
86 ui.pageTitle->setText(sp->title());
87 ui.settingsStack->setCurrentWidget(sp);
93 void SettingsDlg::itemSelected() {
94 QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
96 if(!items.isEmpty()) {
97 sp = qobject_cast<SettingsPage *>(items[0]->data(0, SettingsPageRole).value<QObject *>());
102 void SettingsDlg::setButtonStates() {
103 SettingsPage *sp = currentPage();
104 ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
105 ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
106 ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
109 void SettingsDlg::buttonClicked(QAbstractButton *button) {
110 switch(ui.buttonBox->standardButton(button)) {
111 case QDialogButtonBox::Ok:
112 if(currentPage() && currentPage()->hasChanged()) {
113 if(applyChanges()) accept();
116 case QDialogButtonBox::Apply:
119 case QDialogButtonBox::Cancel:
123 case QDialogButtonBox::Reset:
126 case QDialogButtonBox::RestoreDefaults:
134 bool SettingsDlg::applyChanges() {
135 if(!currentPage()) return false;
136 if(currentPage()->aboutToSave()) {
137 currentPage()->save();
143 void SettingsDlg::undoChanges() {
145 currentPage()->load();
149 void SettingsDlg::reload() {
150 if(!currentPage()) return;
151 int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
152 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
153 if(ret == QMessageBox::Yes) {
154 currentPage()->load();
158 void SettingsDlg::loadDefaults() {
159 if(!currentPage()) return;
160 int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
161 QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
162 if(ret == QMessageBox::RestoreDefaults) {
163 currentPage()->defaults();