1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "settingsdlg.h"
23 SettingsDlg::SettingsDlg(QWidget *parent) : QDialog(parent) {
27 //ui.settingsFrame->setWidgetResizable(true);
28 //ui.settingsFrame->setWidget(ui.settingsStack);
29 ui.settingsTree->setRootIsDecorated(false);
31 connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
32 connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
35 SettingsPage *SettingsDlg::currentPage() const {
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()));
45 QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
47 cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
48 cat->setExpanded(true);
49 cat->setFlags(Qt::ItemIsEnabled);
51 new QTreeWidgetItem(cat, QStringList(sp->title()));
52 pages[QString("%1$%2").arg(sp->category(), sp->title())] = sp;
55 selectPage(sp->category(), sp->title());
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);
66 void SettingsDlg::itemSelected() {
67 // Check if we have changed anything...
70 QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
74 QTreeWidgetItem *parent = items[0]->parent();
76 QString cat = parent->text(0);
77 QString title = items[0]->text(0);
78 selectPage(cat, title);
79 ui.pageTitle->setText(title);
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());
90 void SettingsDlg::buttonClicked(QAbstractButton *button) {
91 switch(ui.buttonBox->standardButton(button)) {
92 case QDialogButtonBox::Ok:
93 if(applyChanges()) accept();
95 case QDialogButtonBox::Apply:
98 case QDialogButtonBox::Cancel:
101 case QDialogButtonBox::Reset:
104 case QDialogButtonBox::RestoreDefaults:
112 bool SettingsDlg::applyChanges() {
113 if(!currentPage()) return false;
114 if(currentPage()->aboutToSave()) {
115 currentPage()->save();
121 // TODO add messagebox
122 void SettingsDlg::reload() {
123 SettingsPage *page = qobject_cast<SettingsPage *>(ui.settingsStack->currentWidget());
124 if(page) page->load();
127 void SettingsDlg::loadDefaults() {
128 SettingsPage *page = qobject_cast<SettingsPage *>(ui.settingsStack->currentWidget());
129 if(page) page->defaults();