SQL-Queries are now stored in a resource. The bashscript
[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) : QDialog(parent) {
24   ui.setupUi(this);
25   _currentPage = 0;
26
27   //ui.settingsFrame->setWidgetResizable(true);
28   //ui.settingsFrame->setWidget(ui.settingsStack);
29   ui.settingsTree->setRootIsDecorated(false);
30
31   connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
32   connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
33 }
34
35 SettingsPage *SettingsDlg::currentPage() const {
36   return _currentPage;
37 }
38
39 void SettingsDlg::registerSettingsPage(SettingsPage *sp) {
40   sp->setParent(ui.settingsStack);
41   ui.settingsStack->addWidget(sp);
42   connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
43
44   QTreeWidgetItem *cat;
45   QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
46   if(!cats.count()) {
47     cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
48     cat->setExpanded(true);
49     cat->setFlags(Qt::ItemIsEnabled);
50   } else cat = cats[0];
51   new QTreeWidgetItem(cat, QStringList(sp->title()));
52   pages[QString("%1$%2").arg(sp->category(), sp->title())] = sp;
53   updateGeometry();
54   // TESTING
55   selectPage(sp->category(), sp->title());
56 }
57
58 void SettingsDlg::selectPage(const QString &cat, const QString &title) {
59   SettingsPage *sp = pages[QString("%1$%2").arg(cat, title)];
60   Q_ASSERT(sp); // FIXME allow for invalid settings pages
61   ui.settingsStack->setCurrentWidget(sp);
62   _currentPage = sp;
63   setButtonStates();
64 }
65
66 void SettingsDlg::itemSelected() {
67   // Check if we have changed anything...
68   // TODO
69
70   QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
71   if(!items.count()) {
72     return;
73   } else {
74     QTreeWidgetItem *parent = items[0]->parent();
75     if(!parent) return;
76     QString cat = parent->text(0);
77     QString title = items[0]->text(0);
78     selectPage(cat, title);
79     ui.pageTitle->setText(title);
80   }
81 }
82
83 void SettingsDlg::setButtonStates() {
84   SettingsPage *sp = currentPage();
85   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(sp && sp->hasChanged());
86   ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
87   ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
88 }
89
90 void SettingsDlg::buttonClicked(QAbstractButton *button) {
91   switch(ui.buttonBox->standardButton(button)) {
92     case QDialogButtonBox::Ok:
93       if(applyChanges()) accept();
94       break;
95     case QDialogButtonBox::Apply:
96       applyChanges();
97       break;
98     case QDialogButtonBox::Cancel:
99       reject();
100       break;
101     case QDialogButtonBox::Reset:
102       reload();
103       break;
104     case QDialogButtonBox::RestoreDefaults:
105       loadDefaults();
106       break;
107     default:
108       break;
109   }
110 }
111
112 bool SettingsDlg::applyChanges() {
113   if(!currentPage()) return false;
114   if(currentPage()->aboutToSave()) {
115     currentPage()->save();
116     return true;
117   }
118   return false;
119 }
120
121 // TODO add messagebox
122 void SettingsDlg::reload() {
123   SettingsPage *page = qobject_cast<SettingsPage *>(ui.settingsStack->currentWidget());
124   if(page) page->load();
125 }
126
127 void SettingsDlg::loadDefaults() {
128   SettingsPage *page = qobject_cast<SettingsPage *>(ui.settingsStack->currentWidget());
129   if(page) page->defaults();
130 }