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