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