Committing my local state. Mostly still invisible part on the not yet enabled network...
[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) : QDialog(parent) {
24   ui.setupUi(this);
25   _currentPage = 0;
26
27   //recommendedSize = layout()->minimumSize();
28
29   // make the scrollarea behave sanely
30   ui.settingsFrame->setWidgetResizable(true);
31   ui.settingsFrame->setWidget(ui.settingsStack);
32
33   updateGeometry();
34
35   ui.settingsTree->setRootIsDecorated(false);
36
37   connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
38   connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
39 }
40
41 /*
42 QSize SettingsDlg::sizeHint() const {
43   return recommendedSize;
44 }
45 */
46
47 SettingsPage *SettingsDlg::currentPage() const {
48   return _currentPage;
49 }
50
51 void SettingsDlg::registerSettingsPage(SettingsPage *sp) {
52   sp->setParent(ui.settingsStack);
53   ui.settingsStack->addWidget(sp);
54   //recommendedSize = recommendedSize.expandedTo(sp->sizeHint());
55   //updateGeometry();
56   connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
57
58   QTreeWidgetItem *cat;
59   QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
60   if(!cats.count()) {
61     cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
62     cat->setExpanded(true);
63     cat->setFlags(Qt::ItemIsEnabled);
64   } else cat = cats[0];
65   QTreeWidgetItem *item = new QTreeWidgetItem(cat, QStringList(sp->title()));
66   treeItems[sp] = item;
67   pages[QString("%1$%2").arg(sp->category(), sp->title())] = sp;
68   // TESTING
69   selectPage(sp->category(), sp->title());
70 }
71
72 void SettingsDlg::selectPage(const QString &cat, const QString &title) {
73   SettingsPage *sp = pages[QString("%1$%2").arg(cat, title)];
74   if(!sp) {
75     _currentPage = 0;
76     ui.settingsStack->setCurrentIndex(0);
77     ui.settingsTree->setCurrentItem(0);
78     return;
79   }
80   if(sp != currentPage() && currentPage() != 0 && currentPage()->hasChanged()) {
81     int ret = QMessageBox::warning(this, tr("Save changes"),
82                                   tr("There are unsaved changes on the current configuration page. Would you like to apply your changes now?"),
83                                   QMessageBox::Discard|QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
84     if(ret == QMessageBox::Save) {
85       if(!applyChanges()) sp = currentPage();
86     } else if(ret == QMessageBox::Discard) {
87       undoChanges();
88     } else sp = currentPage();
89   }
90   if(sp != currentPage()) {
91     ui.pageTitle->setText(sp->title());
92     ui.settingsStack->setCurrentWidget(sp);
93     ui.settingsStack->setMinimumSize(sp->minimumSizeHint());  // we don't want our page shrinked, use scrollbars instead...
94     _currentPage = sp;
95   }
96   ui.settingsTree->setCurrentItem(treeItems[sp]);
97   setButtonStates();
98 }
99
100 void SettingsDlg::itemSelected() {
101   QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
102   if(!items.count()) {
103     return;
104   } else {
105     QTreeWidgetItem *parent = items[0]->parent();
106     if(!parent) return;
107     QString cat = parent->text(0);
108     QString title = items[0]->text(0);
109     selectPage(cat, title);
110   }
111 }
112
113 void SettingsDlg::setButtonStates() {
114   SettingsPage *sp = currentPage();
115   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(sp && sp->hasChanged());
116   ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
117   ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
118   ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
119 }
120
121 void SettingsDlg::buttonClicked(QAbstractButton *button) {
122   switch(ui.buttonBox->standardButton(button)) {
123     case QDialogButtonBox::Ok:
124       if(applyChanges()) accept();
125       break;
126     case QDialogButtonBox::Apply:
127       applyChanges();
128       break;
129     case QDialogButtonBox::Cancel:
130       undoChanges();
131       reject();
132       break;
133     case QDialogButtonBox::Reset:
134       reload();
135       break;
136     case QDialogButtonBox::RestoreDefaults:
137       loadDefaults();
138       break;
139     default:
140       break;
141   }
142 }
143
144 bool SettingsDlg::applyChanges() {
145   if(!currentPage()) return false;
146   if(currentPage()->aboutToSave()) {
147     currentPage()->save();
148     return true;
149   }
150   return false;
151 }
152
153 void SettingsDlg::undoChanges() {
154   if(currentPage()) {
155     currentPage()->load();
156   }
157 }
158
159 void SettingsDlg::reload() {
160   if(!currentPage()) return;
161   int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
162                                   QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
163   if(ret == QMessageBox::Yes) {
164     currentPage()->load();
165   }
166 }
167
168 void SettingsDlg::loadDefaults() {
169   if(!currentPage()) return;
170   int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
171                                   QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
172   if(ret == QMessageBox::Yes) {
173     currentPage()->defaults();
174   }
175 }