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 <QMessageBox>
22 #include <QPushButton>
24 #include "settingsdlg.h"
27 #include "iconloader.h"
29 SettingsDlg::SettingsDlg(QWidget *parent)
35 setAttribute(Qt::WA_DeleteOnClose, true);
36 setWindowIcon(SmallIcon("configure"));
40 ui.settingsTree->setRootIsDecorated(false);
42 connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
43 connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
45 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), SLOT(coreConnectionStateChanged()));
50 void SettingsDlg::coreConnectionStateChanged() {
51 for(int i = 0; i < ui.settingsTree->topLevelItemCount(); i++) {
52 QTreeWidgetItem *catItem = ui.settingsTree->topLevelItem(i);
53 for(int j = 0; j < catItem->childCount(); j++) {
54 QTreeWidgetItem *item = catItem->child(j);
57 setItemState(catItem);
61 void SettingsDlg::setItemState(QTreeWidgetItem *item) {
62 SettingsPage *sp = qobject_cast<SettingsPage *>(item->data(0, SettingsPageRole).value<QObject *>());
64 item->setDisabled(!Client::isConnected() && sp->needsCoreConnection());
67 void SettingsDlg::registerSettingsPage(SettingsPage *sp) {
68 sp->setParent(ui.settingsStack);
69 ui.settingsStack->addWidget(sp);
71 connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
74 QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
76 cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
77 cat->setExpanded(true);
78 cat->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
83 QTreeWidgetItem *item;
84 if(sp->title().isEmpty())
87 item = new QTreeWidgetItem(cat, QStringList(sp->title()));
89 item->setData(0, SettingsPageRole, QVariant::fromValue<QObject *>(sp));
90 ui.settingsTree->setMinimumWidth(ui.settingsTree->header()->sectionSizeHint(0) + 5);
91 pageIsLoaded[sp] = false;
92 if(!ui.settingsTree->selectedItems().count())
93 ui.settingsTree->setCurrentItem(item);
98 void SettingsDlg::selectPage(SettingsPage *sp) {
101 ui.settingsStack->setCurrentIndex(0);
102 ui.pageTitle->setText(tr("Settings"));
106 if(!pageIsLoaded[sp]) {
108 pageIsLoaded[sp] = true;
111 if(sp != currentPage() && currentPage() != 0 && currentPage()->hasChanged()) {
112 int ret = QMessageBox::warning(this, tr("Save changes"),
113 tr("There are unsaved changes on the current configuration page. Would you like to apply your changes now?"),
114 QMessageBox::Discard|QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
115 if(ret == QMessageBox::Save) {
116 if(!applyChanges()) sp = currentPage();
117 } else if(ret == QMessageBox::Discard) {
119 } else sp = currentPage();
122 if(sp != currentPage()) {
123 if(sp->title().isEmpty()) {
124 ui.pageTitle->setText(sp->category());
125 setWindowTitle(tr("Configure %1").arg(sp->category()));
128 ui.pageTitle->setText(sp->title());
129 setWindowTitle(tr("Configure %1").arg(sp->title()));
132 ui.settingsStack->setCurrentWidget(sp);
138 void SettingsDlg::itemSelected() {
139 QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
140 SettingsPage *sp = 0;
141 if(!items.isEmpty()) {
142 sp = qobject_cast<SettingsPage *>(items[0]->data(0, SettingsPageRole).value<QObject *>());
147 void SettingsDlg::setButtonStates() {
148 SettingsPage *sp = currentPage();
149 ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
150 ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
151 ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
154 void SettingsDlg::buttonClicked(QAbstractButton *button) {
155 switch(ui.buttonBox->standardButton(button)) {
156 case QDialogButtonBox::Ok:
157 if(currentPage() && currentPage()->hasChanged()) {
158 if(applyChanges()) accept();
161 case QDialogButtonBox::Apply:
164 case QDialogButtonBox::Cancel:
168 case QDialogButtonBox::Reset:
171 case QDialogButtonBox::RestoreDefaults:
179 bool SettingsDlg::applyChanges() {
180 if(!currentPage()) return false;
181 if(currentPage()->aboutToSave()) {
182 currentPage()->save();
188 void SettingsDlg::undoChanges() {
190 currentPage()->load();
194 void SettingsDlg::reload() {
195 if(!currentPage()) return;
196 int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
197 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
198 if(ret == QMessageBox::Yes) {
199 currentPage()->load();
203 void SettingsDlg::loadDefaults() {
204 if(!currentPage()) return;
205 int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
206 QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
207 if(ret == QMessageBox::RestoreDefaults) {
208 currentPage()->defaults();