Fix license headers: Quassel IRC Team -> Quassel Project, 2007 -> 2008
[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
26   //ui.settingsFrame->setWidgetResizable(true);
27   //ui.settingsFrame->setWidget(ui.settingsStack);
28   ui.settingsTree->setRootIsDecorated(false);
29
30   connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
31   connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
32 }
33
34
35 void SettingsDlg::registerSettingsPage(SettingsPage *sp) {
36   sp->setParent(ui.settingsStack);
37   ui.settingsStack->addWidget(sp);
38
39   QTreeWidgetItem *cat;
40   QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
41   if(!cats.count()) {
42     cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
43     cat->setExpanded(true);
44     cat->setFlags(Qt::ItemIsEnabled);
45   } else cat = cats[0];
46   new QTreeWidgetItem(cat, QStringList(sp->title()));
47   pages[QString("%1$%2").arg(sp->category(), sp->title())] = sp;
48   updateGeometry();
49   // TESTING
50   selectPage(sp->category(), sp->title());
51 }
52
53 void SettingsDlg::selectPage(const QString &cat, const QString &title) {
54   QWidget *w = pages[QString("%1$%2").arg(cat, title)];
55   Q_ASSERT(w);
56   ui.settingsStack->setCurrentWidget(w);
57 }
58
59 void SettingsDlg::itemSelected() {
60   QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
61   if(!items.count()) {
62     return;
63   } else {
64     QTreeWidgetItem *parent = items[0]->parent();
65     if(!parent) return;
66     QString cat = parent->text(0);
67     QString title = items[0]->text(0);
68     selectPage(cat, title);
69     ui.pageTitle->setText(title);
70   }
71 }
72
73 void SettingsDlg::buttonClicked(QAbstractButton *button) {
74   switch(ui.buttonBox->standardButton(button)) {
75     case QDialogButtonBox::Ok:
76       applyChanges();
77       accept();
78       break;
79     case QDialogButtonBox::Apply:
80       applyChanges();
81       break;
82     case QDialogButtonBox::Cancel:
83       reject();
84       break;
85     case QDialogButtonBox::Reset:
86       reload();
87       break;
88     case QDialogButtonBox::RestoreDefaults:
89       loadDefaults();
90       break;
91     default:
92       break;
93   }
94 }
95
96 void SettingsDlg::applyChanges() {
97   foreach(SettingsPage *page, pages.values()) {
98     page->save();
99   }
100 }
101
102 void SettingsDlg::reload() {
103   SettingsPage *page = qobject_cast<SettingsPage *>(ui.settingsStack->currentWidget());
104   if(page) page->load();
105 }
106
107 void SettingsDlg::loadDefaults() {
108   SettingsPage *page = qobject_cast<SettingsPage *>(ui.settingsStack->currentWidget());
109   if(page) page->defaults();
110 }