#BR127: user can now set the style of the application in the config menu (behaviour...
[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   pageIsLoaded[sp] = false;
69   // TESTING
70   // selectPage(sp->category(), sp->title());
71 }
72
73 void SettingsDlg::selectPage(const QString &cat, const QString &title) {
74   SettingsPage *sp = pages[QString("%1$%2").arg(cat, title)];
75   if(!sp) {
76     _currentPage = 0;
77     ui.settingsStack->setCurrentIndex(0);
78     ui.settingsTree->setCurrentItem(0);
79     return;
80   }
81   if(!pageIsLoaded[sp]) {
82     sp->load();
83     pageIsLoaded[sp] = true;
84   }
85   if(sp != currentPage() && currentPage() != 0 && currentPage()->hasChanged()) {
86     int ret = QMessageBox::warning(this, tr("Save changes"),
87                                   tr("There are unsaved changes on the current configuration page. Would you like to apply your changes now?"),
88                                   QMessageBox::Discard|QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
89     if(ret == QMessageBox::Save) {
90       if(!applyChanges()) sp = currentPage();
91     } else if(ret == QMessageBox::Discard) {
92       undoChanges();
93     } else sp = currentPage();
94   }
95   if(sp != currentPage()) {
96     ui.pageTitle->setText(sp->title());
97     ui.settingsStack->setCurrentWidget(sp);
98     ui.settingsStack->setMinimumSize(sp->minimumSizeHint());  // we don't want our page shrinked, use scrollbars instead...
99     _currentPage = sp;
100   }
101   ui.settingsTree->setCurrentItem(treeItems[sp]);
102   setButtonStates();
103 }
104
105 void SettingsDlg::itemSelected() {
106   QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
107   if(!items.count()) {
108     return;
109   } else {
110     QTreeWidgetItem *parent = items[0]->parent();
111     if(!parent) return;
112     QString cat = parent->text(0);
113     QString title = items[0]->text(0);
114     selectPage(cat, title);
115   }
116 }
117
118 void SettingsDlg::setButtonStates() {
119   SettingsPage *sp = currentPage();
120   ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
121   ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
122   ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
123 }
124
125 void SettingsDlg::buttonClicked(QAbstractButton *button) {
126   switch(ui.buttonBox->standardButton(button)) {
127     case QDialogButtonBox::Ok:
128       if(currentPage() && currentPage()->hasChanged()) {
129         if(applyChanges()) accept();
130       } else accept();
131       break;
132     case QDialogButtonBox::Apply:
133       applyChanges();
134       break;
135     case QDialogButtonBox::Cancel:
136       undoChanges();
137       reject();
138       break;
139     case QDialogButtonBox::Reset:
140       reload();
141       break;
142     case QDialogButtonBox::RestoreDefaults:
143       loadDefaults();
144       break;
145     default:
146       break;
147   }
148 }
149
150 bool SettingsDlg::applyChanges() {
151   if(!currentPage()) return false;
152   if(currentPage()->aboutToSave()) {
153     currentPage()->save();
154     return true;
155   }
156   return false;
157 }
158
159 void SettingsDlg::undoChanges() {
160   if(currentPage()) {
161     currentPage()->load();
162   }
163 }
164
165 void SettingsDlg::reload() {
166   if(!currentPage()) return;
167   int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
168                                   QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
169   if(ret == QMessageBox::Yes) {
170     currentPage()->load();
171   }
172 }
173
174 void SettingsDlg::loadDefaults() {
175   if(!currentPage()) return;
176   int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
177                                   QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
178   if(ret == QMessageBox::RestoreDefaults) {
179     currentPage()->defaults();
180   }
181 }