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 //recommendedSize = layout()->minimumSize();
29 // make the scrollarea behave sanely
30 ui.settingsFrame->setWidgetResizable(true);
31 ui.settingsFrame->setWidget(ui.settingsStack);
35 ui.settingsTree->setRootIsDecorated(false);
37 connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
38 connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
42 QSize SettingsDlg::sizeHint() const {
43 return recommendedSize;
47 SettingsPage *SettingsDlg::currentPage() const {
51 void SettingsDlg::registerSettingsPage(SettingsPage *sp) {
52 sp->setParent(ui.settingsStack);
53 ui.settingsStack->addWidget(sp);
54 //recommendedSize = recommendedSize.expandedTo(sp->sizeHint());
56 connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
59 QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
61 cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
62 cat->setExpanded(true);
63 cat->setFlags(Qt::ItemIsEnabled);
65 QTreeWidgetItem *item = new QTreeWidgetItem(cat, QStringList(sp->title()));
67 pages[QString("%1$%2").arg(sp->category(), sp->title())] = sp;
70 // selectPage(sp->category(), sp->title());
73 void SettingsDlg::selectPage(const QString &cat, const QString &title) {
74 SettingsPage *sp = pages[QString("%1$%2").arg(cat, title)];
77 ui.settingsStack->setCurrentIndex(0);
78 ui.settingsTree->setCurrentItem(0);
81 if(sp != currentPage() && currentPage() != 0 && currentPage()->hasChanged()) {
82 int ret = QMessageBox::warning(this, tr("Save changes"),
83 tr("There are unsaved changes on the current configuration page. Would you like to apply your changes now?"),
84 QMessageBox::Discard|QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
85 if(ret == QMessageBox::Save) {
86 if(!applyChanges()) sp = currentPage();
87 } else if(ret == QMessageBox::Discard) {
89 } else sp = currentPage();
91 if(sp != currentPage()) {
92 ui.pageTitle->setText(sp->title());
93 ui.settingsStack->setCurrentWidget(sp);
94 ui.settingsStack->setMinimumSize(sp->minimumSizeHint()); // we don't want our page shrinked, use scrollbars instead...
97 ui.settingsTree->setCurrentItem(treeItems[sp]);
101 void SettingsDlg::itemSelected() {
102 QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
106 QTreeWidgetItem *parent = items[0]->parent();
108 QString cat = parent->text(0);
109 QString title = items[0]->text(0);
110 selectPage(cat, title);
114 void SettingsDlg::setButtonStates() {
115 SettingsPage *sp = currentPage();
116 ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
117 ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
118 ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
121 void SettingsDlg::buttonClicked(QAbstractButton *button) {
122 switch(ui.buttonBox->standardButton(button)) {
123 case QDialogButtonBox::Ok:
124 if(currentPage() && currentPage()->hasChanged()) {
125 if(applyChanges()) accept();
128 case QDialogButtonBox::Apply:
131 case QDialogButtonBox::Cancel:
135 case QDialogButtonBox::Reset:
138 case QDialogButtonBox::RestoreDefaults:
146 bool SettingsDlg::applyChanges() {
147 if(!currentPage()) return false;
148 if(currentPage()->aboutToSave()) {
149 currentPage()->save();
155 void SettingsDlg::undoChanges() {
157 currentPage()->load();
161 void SettingsDlg::reload() {
162 if(!currentPage()) return;
163 int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
164 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
165 if(ret == QMessageBox::Yes) {
166 currentPage()->load();
170 void SettingsDlg::loadDefaults() {
171 if(!currentPage()) return;
172 int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
173 QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
174 if(ret == QMessageBox::RestoreDefaults) {
175 currentPage()->defaults();