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 setWindowFlags(Qt::Sheet);
29 setAttribute(Qt::WA_DeleteOnClose, true);
34 ui.settingsTree->setRootIsDecorated(false);
36 connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
37 connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
40 void SettingsDlg::registerSettingsPage(SettingsPage *sp) {
41 sp->setParent(ui.settingsStack);
42 ui.settingsStack->addWidget(sp);
44 connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
47 QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
49 cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
50 cat->setExpanded(true);
51 cat->setFlags(Qt::ItemIsEnabled);
55 QTreeWidgetItem *item = new QTreeWidgetItem(cat, QStringList(sp->title()));
56 item->setData(0, SettingsPageRole, qVariantFromValue<QObject *>(sp));
57 pageIsLoaded[sp] = false;
60 void SettingsDlg::selectPage(SettingsPage *sp) {
63 ui.settingsStack->setCurrentIndex(0);
64 ui.pageTitle->setText(tr("Settings"));
68 if(!pageIsLoaded[sp]) {
70 pageIsLoaded[sp] = true;
73 if(sp != currentPage() && currentPage() != 0 && currentPage()->hasChanged()) {
74 int ret = QMessageBox::warning(this, tr("Save changes"),
75 tr("There are unsaved changes on the current configuration page. Would you like to apply your changes now?"),
76 QMessageBox::Discard|QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
77 if(ret == QMessageBox::Save) {
78 if(!applyChanges()) sp = currentPage();
79 } else if(ret == QMessageBox::Discard) {
81 } else sp = currentPage();
84 if(sp != currentPage()) {
85 ui.pageTitle->setText(sp->title());
86 ui.settingsStack->setCurrentWidget(sp);
92 void SettingsDlg::itemSelected() {
93 QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
95 if(!items.isEmpty()) {
96 sp = qobject_cast<SettingsPage *>(items[0]->data(0, SettingsPageRole).value<QObject *>());
101 void SettingsDlg::setButtonStates() {
102 SettingsPage *sp = currentPage();
103 ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
104 ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
105 ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
108 void SettingsDlg::buttonClicked(QAbstractButton *button) {
109 switch(ui.buttonBox->standardButton(button)) {
110 case QDialogButtonBox::Ok:
111 if(currentPage() && currentPage()->hasChanged()) {
112 if(applyChanges()) accept();
115 case QDialogButtonBox::Apply:
118 case QDialogButtonBox::Cancel:
122 case QDialogButtonBox::Reset:
125 case QDialogButtonBox::RestoreDefaults:
133 bool SettingsDlg::applyChanges() {
134 if(!currentPage()) return false;
135 if(currentPage()->aboutToSave()) {
136 currentPage()->save();
142 void SettingsDlg::undoChanges() {
144 currentPage()->load();
148 void SettingsDlg::reload() {
149 if(!currentPage()) return;
150 int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
151 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
152 if(ret == QMessageBox::Yes) {
153 currentPage()->load();
157 void SettingsDlg::loadDefaults() {
158 if(!currentPage()) return;
159 int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
160 QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
161 if(ret == QMessageBox::RestoreDefaults) {
162 currentPage()->defaults();