f260fb09145a7fa5a2ba9d34fdb3415d273b3489
[quassel.git] / src / qtui / settingsdlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 #include "iconloader.h"
24
25 SettingsDlg::SettingsDlg(QWidget *parent)
26   : QDialog(parent),
27     _currentPage(0)
28 {
29   ui.setupUi(this);
30   setModal(true);
31   setAttribute(Qt::WA_DeleteOnClose, true);
32   setWindowIcon(SmallIcon("configure"));
33
34   updateGeometry();
35
36   ui.settingsTree->setRootIsDecorated(false);
37
38   connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
39   connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
40
41   setButtonStates();
42 }
43
44 void SettingsDlg::registerSettingsPage(SettingsPage *sp) {
45   sp->setParent(ui.settingsStack);
46   ui.settingsStack->addWidget(sp);
47
48   connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
49
50   QTreeWidgetItem *cat;
51   QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
52   if(!cats.count()) {
53     cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
54     cat->setExpanded(true);
55     cat->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
56   } else {
57     cat = cats[0];
58   }
59
60   QTreeWidgetItem *item;
61   if(sp->title().isEmpty())
62     item = cat;
63   else
64     item = new QTreeWidgetItem(cat, QStringList(sp->title()));
65
66   item->setData(0, SettingsPageRole, QVariant::fromValue<QObject *>(sp));
67   ui.settingsTree->setMinimumWidth(ui.settingsTree->header()->sectionSizeHint(0) + 5);
68   pageIsLoaded[sp] = false;
69   if(!ui.settingsTree->selectedItems().count())
70     ui.settingsTree->setCurrentItem(item);
71 }
72
73 void SettingsDlg::selectPage(SettingsPage *sp) {
74   if(!sp) {
75     _currentPage = 0;
76     ui.settingsStack->setCurrentIndex(0);
77     ui.pageTitle->setText(tr("Settings"));
78     return;
79   }
80
81   if(!pageIsLoaded[sp]) {
82     sp->load();
83     pageIsLoaded[sp] = true;
84   }
85
86   if(sp != currentPage() && currentPage() != 0 && currentPage()->hasChanged()) {
87     int ret = QMessageBox::warning(this, tr("Save changes"),
88                                   tr("There are unsaved changes on the current configuration page. Would you like to apply your changes now?"),
89                                   QMessageBox::Discard|QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
90     if(ret == QMessageBox::Save) {
91       if(!applyChanges()) sp = currentPage();
92     } else if(ret == QMessageBox::Discard) {
93       undoChanges();
94     } else sp = currentPage();
95   }
96
97   if(sp != currentPage()) {
98     ui.pageTitle->setText(sp->title());
99     setWindowTitle(tr("Configure %1").arg(sp->title()));
100     ui.settingsStack->setCurrentWidget(sp);
101     _currentPage = sp;
102   }
103   setButtonStates();
104 }
105
106 void SettingsDlg::itemSelected() {
107   QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
108   SettingsPage *sp = 0;
109   if(!items.isEmpty()) {
110     sp = qobject_cast<SettingsPage *>(items[0]->data(0, SettingsPageRole).value<QObject *>());
111   }
112   selectPage(sp);
113 }
114
115 void SettingsDlg::setButtonStates() {
116   SettingsPage *sp = currentPage();
117   ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
118   ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
119   ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
120 }
121
122 void SettingsDlg::buttonClicked(QAbstractButton *button) {
123   switch(ui.buttonBox->standardButton(button)) {
124     case QDialogButtonBox::Ok:
125       if(currentPage() && currentPage()->hasChanged()) {
126         if(applyChanges()) accept();
127       } else accept();
128       break;
129     case QDialogButtonBox::Apply:
130       applyChanges();
131       break;
132     case QDialogButtonBox::Cancel:
133       undoChanges();
134       reject();
135       break;
136     case QDialogButtonBox::Reset:
137       reload();
138       break;
139     case QDialogButtonBox::RestoreDefaults:
140       loadDefaults();
141       break;
142     default:
143       break;
144   }
145 }
146
147 bool SettingsDlg::applyChanges() {
148   if(!currentPage()) return false;
149   if(currentPage()->aboutToSave()) {
150     currentPage()->save();
151     return true;
152   }
153   return false;
154 }
155
156 void SettingsDlg::undoChanges() {
157   if(currentPage()) {
158     currentPage()->load();
159   }
160 }
161
162 void SettingsDlg::reload() {
163   if(!currentPage()) return;
164   int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
165                                   QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
166   if(ret == QMessageBox::Yes) {
167     currentPage()->load();
168   }
169 }
170
171 void SettingsDlg::loadDefaults() {
172   if(!currentPage()) return;
173   int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
174                                   QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
175   if(ret == QMessageBox::RestoreDefaults) {
176     currentPage()->defaults();
177   }
178 }