some minor smoothifications to the nick selector
[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);
56   } else {
57     cat = cats[0];
58   }
59   QTreeWidgetItem *item = new QTreeWidgetItem(cat, QStringList(sp->title()));
60   item->setData(0, SettingsPageRole, qVariantFromValue<QObject *>(sp));
61   ui.settingsTree->setMinimumWidth(ui.settingsTree->header()->sectionSizeHint(0) + 5);
62   pageIsLoaded[sp] = false;
63 }
64
65 void SettingsDlg::selectPage(SettingsPage *sp) {
66   if(!sp) {
67     _currentPage = 0;
68     ui.settingsStack->setCurrentIndex(0);
69     ui.pageTitle->setText(tr("Settings"));
70     return;
71   }
72
73   if(!pageIsLoaded[sp]) {
74     sp->load();
75     pageIsLoaded[sp] = true;
76   }
77
78   if(sp != currentPage() && currentPage() != 0 && currentPage()->hasChanged()) {
79     int ret = QMessageBox::warning(this, tr("Save changes"),
80                                   tr("There are unsaved changes on the current configuration page. Would you like to apply your changes now?"),
81                                   QMessageBox::Discard|QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
82     if(ret == QMessageBox::Save) {
83       if(!applyChanges()) sp = currentPage();
84     } else if(ret == QMessageBox::Discard) {
85       undoChanges();
86     } else sp = currentPage();
87   }
88
89   if(sp != currentPage()) {
90     ui.pageTitle->setText(sp->title());
91     setWindowTitle(tr("Configure %1").arg(sp->title()));
92     ui.settingsStack->setCurrentWidget(sp);
93     _currentPage = sp;
94   }
95   setButtonStates();
96 }
97
98 void SettingsDlg::itemSelected() {
99   QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
100   SettingsPage *sp = 0;
101   if(!items.isEmpty()) {
102     sp = qobject_cast<SettingsPage *>(items[0]->data(0, SettingsPageRole).value<QObject *>());
103   }
104   selectPage(sp);
105 }
106
107 void SettingsDlg::setButtonStates() {
108   SettingsPage *sp = currentPage();
109   ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
110   ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
111   ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
112 }
113
114 void SettingsDlg::buttonClicked(QAbstractButton *button) {
115   switch(ui.buttonBox->standardButton(button)) {
116     case QDialogButtonBox::Ok:
117       if(currentPage() && currentPage()->hasChanged()) {
118         if(applyChanges()) accept();
119       } else accept();
120       break;
121     case QDialogButtonBox::Apply:
122       applyChanges();
123       break;
124     case QDialogButtonBox::Cancel:
125       undoChanges();
126       reject();
127       break;
128     case QDialogButtonBox::Reset:
129       reload();
130       break;
131     case QDialogButtonBox::RestoreDefaults:
132       loadDefaults();
133       break;
134     default:
135       break;
136   }
137 }
138
139 bool SettingsDlg::applyChanges() {
140   if(!currentPage()) return false;
141   if(currentPage()->aboutToSave()) {
142     currentPage()->save();
143     return true;
144   }
145   return false;
146 }
147
148 void SettingsDlg::undoChanges() {
149   if(currentPage()) {
150     currentPage()->load();
151   }
152 }
153
154 void SettingsDlg::reload() {
155   if(!currentPage()) return;
156   int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
157                                   QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
158   if(ret == QMessageBox::Yes) {
159     currentPage()->load();
160   }
161 }
162
163 void SettingsDlg::loadDefaults() {
164   if(!currentPage()) return;
165   int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
166                                   QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
167   if(ret == QMessageBox::RestoreDefaults) {
168     currentPage()->defaults();
169   }
170 }