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