Merge branch 'seezer'
[quassel.git] / src / qtui / settingsdlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "settingsdlg.h"
22
23 SettingsDlg::SettingsDlg(QWidget *parent)
24   : QDialog(parent),
25     _currentPage(0)
26 {
27   ui.setupUi(this);
28
29   updateGeometry();
30
31   ui.settingsTree->setRootIsDecorated(false);
32
33   connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
34   connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
35 }
36
37 void SettingsDlg::registerSettingsPage(SettingsPage *sp) {
38   sp->setParent(ui.settingsStack);
39   ui.settingsStack->addWidget(sp);
40   
41   connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
42
43   QTreeWidgetItem *cat;
44   QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
45   if(!cats.count()) {
46     cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
47     cat->setExpanded(true);
48     cat->setFlags(Qt::ItemIsEnabled);
49   } else {
50     cat = cats[0];
51   }
52   QTreeWidgetItem *item = new QTreeWidgetItem(cat, QStringList(sp->title()));
53   item->setData(0, SettingsPageRole, qVariantFromValue<QObject *>(sp));
54   pageIsLoaded[sp] = false;
55 }
56
57 void SettingsDlg::selectPage(SettingsPage *sp) {
58   if(!sp) {
59     _currentPage = 0;
60     ui.settingsStack->setCurrentIndex(0);
61     ui.pageTitle->setText(tr("Settings"));
62     return;
63   }
64
65   if(!pageIsLoaded[sp]) {
66     sp->load();
67     pageIsLoaded[sp] = true;
68   }
69
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) {
77       undoChanges();
78     } else sp = currentPage();
79   }
80
81   if(sp != currentPage()) {
82     ui.pageTitle->setText(sp->title());
83     ui.settingsStack->setCurrentWidget(sp);
84     _currentPage = sp;
85   }
86   setButtonStates();
87 }
88
89 void SettingsDlg::itemSelected() {
90   QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
91   SettingsPage *sp = 0;
92   if(!items.isEmpty()) {
93     sp = qobject_cast<SettingsPage *>(items[0]->data(0, SettingsPageRole).value<QObject *>());
94   }
95   selectPage(sp);
96 }
97
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());
103 }
104
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();
110       } else accept();
111       break;
112     case QDialogButtonBox::Apply:
113       applyChanges();
114       break;
115     case QDialogButtonBox::Cancel:
116       undoChanges();
117       reject();
118       break;
119     case QDialogButtonBox::Reset:
120       reload();
121       break;
122     case QDialogButtonBox::RestoreDefaults:
123       loadDefaults();
124       break;
125     default:
126       break;
127   }
128 }
129
130 bool SettingsDlg::applyChanges() {
131   if(!currentPage()) return false;
132   if(currentPage()->aboutToSave()) {
133     currentPage()->save();
134     return true;
135   }
136   return false;
137 }
138
139 void SettingsDlg::undoChanges() {
140   if(currentPage()) {
141     currentPage()->load();
142   }
143 }
144
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();
151   }
152 }
153
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();
160   }
161 }