1 /***************************************************************************
2 * Copyright (C) 2005-09 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 #include "iconloader.h"
25 SettingsDlg::SettingsDlg(QWidget *parent)
31 setAttribute(Qt::WA_DeleteOnClose, true);
32 setWindowIcon(SmallIcon("configure"));
36 ui.settingsTree->setRootIsDecorated(false);
38 connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
39 connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
44 void SettingsDlg::registerSettingsPage(SettingsPage *sp) {
45 sp->setParent(ui.settingsStack);
46 ui.settingsStack->addWidget(sp);
48 connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
51 QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
53 cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
54 cat->setExpanded(true);
55 cat->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
60 QTreeWidgetItem *item;
61 if(sp->title().isEmpty())
64 item = new QTreeWidgetItem(cat, QStringList(sp->title()));
66 item->setData(0, SettingsPageRole, QVariant::fromValue<QObject *>(sp));
67 ui.settingsTree->setMinimumWidth(ui.settingsTree->header()->sectionSizeHint(0) + 5);
68 pageIsLoaded[sp] = false;
69 if(!ui.settingsTree->selectedItems().count())
70 ui.settingsTree->setCurrentItem(item);
73 void SettingsDlg::selectPage(SettingsPage *sp) {
76 ui.settingsStack->setCurrentIndex(0);
77 ui.pageTitle->setText(tr("Settings"));
81 if(!pageIsLoaded[sp]) {
83 pageIsLoaded[sp] = true;
86 if(sp != currentPage() && currentPage() != 0 && currentPage()->hasChanged()) {
87 int ret = QMessageBox::warning(this, tr("Save changes"),
88 tr("There are unsaved changes on the current configuration page. Would you like to apply your changes now?"),
89 QMessageBox::Discard|QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
90 if(ret == QMessageBox::Save) {
91 if(!applyChanges()) sp = currentPage();
92 } else if(ret == QMessageBox::Discard) {
94 } else sp = currentPage();
97 if(sp != currentPage()) {
98 if(sp->title().isEmpty()) {
99 ui.pageTitle->setText(sp->category());
100 setWindowTitle(tr("Configure %1").arg(sp->category()));
103 ui.pageTitle->setText(sp->title());
104 setWindowTitle(tr("Configure %1").arg(sp->title()));
107 ui.settingsStack->setCurrentWidget(sp);
113 void SettingsDlg::itemSelected() {
114 QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
115 SettingsPage *sp = 0;
116 if(!items.isEmpty()) {
117 sp = qobject_cast<SettingsPage *>(items[0]->data(0, SettingsPageRole).value<QObject *>());
122 void SettingsDlg::setButtonStates() {
123 SettingsPage *sp = currentPage();
124 ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
125 ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
126 ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
129 void SettingsDlg::buttonClicked(QAbstractButton *button) {
130 switch(ui.buttonBox->standardButton(button)) {
131 case QDialogButtonBox::Ok:
132 if(currentPage() && currentPage()->hasChanged()) {
133 if(applyChanges()) accept();
136 case QDialogButtonBox::Apply:
139 case QDialogButtonBox::Cancel:
143 case QDialogButtonBox::Reset:
146 case QDialogButtonBox::RestoreDefaults:
154 bool SettingsDlg::applyChanges() {
155 if(!currentPage()) return false;
156 if(currentPage()->aboutToSave()) {
157 currentPage()->save();
163 void SettingsDlg::undoChanges() {
165 currentPage()->load();
169 void SettingsDlg::reload() {
170 if(!currentPage()) return;
171 int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
172 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
173 if(ret == QMessageBox::Yes) {
174 currentPage()->load();
178 void SettingsDlg::loadDefaults() {
179 if(!currentPage()) return;
180 int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
181 QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
182 if(ret == QMessageBox::RestoreDefaults) {
183 currentPage()->defaults();