7bac0ef745dfca9127b2da711cf88c725dc84727
[quassel.git] / src / qtui / settingspagedlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
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 "settingspagedlg.h"
22
23 SettingsPageDlg::SettingsPageDlg(SettingsPage *page, QWidget *parent)
24   : QDialog(parent)
25 {
26   ui.setupUi(this);
27   _currentPage = page;
28   page->setParent(this);
29
30   // make it look more native under Mac OS X:
31   setWindowFlags(Qt::Sheet);
32
33   ui.pageTitle->setText(page->title());
34
35   // make the scrollarea behave sanely
36   ui.settingsFrame->setWidgetResizable(true);
37   ui.settingsFrame->setWidget(page);
38
39   updateGeometry();
40
41   connect(page, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
42   connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
43   page->load();
44   setButtonStates();
45 }
46
47 SettingsPage *SettingsPageDlg::currentPage() const {
48   return _currentPage;
49 }
50
51 void SettingsPageDlg::setButtonStates() {
52   SettingsPage *sp = currentPage();
53   ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
54   ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
55   ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
56 }
57
58 void SettingsPageDlg::buttonClicked(QAbstractButton *button) {
59   switch(ui.buttonBox->standardButton(button)) {
60     case QDialogButtonBox::Ok:
61       if(currentPage() && currentPage()->hasChanged()) {
62         if(applyChanges()) accept();
63       } else accept();
64       break;
65     case QDialogButtonBox::Apply:
66       applyChanges();
67       break;
68     case QDialogButtonBox::Cancel:
69       undoChanges();
70       reject();
71       break;
72     case QDialogButtonBox::Reset:
73       reload();
74       break;
75     case QDialogButtonBox::RestoreDefaults:
76       loadDefaults();
77       break;
78     default:
79       break;
80   }
81 }
82
83 bool SettingsPageDlg::applyChanges() {
84   if(!currentPage()) return false;
85   if(currentPage()->aboutToSave()) {
86     currentPage()->save();
87     return true;
88   }
89   return false;
90 }
91
92 void SettingsPageDlg::undoChanges() {
93   if(currentPage()) {
94     currentPage()->load();
95   }
96 }
97
98 void SettingsPageDlg::reload() {
99   if(!currentPage()) return;
100   int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
101                                   QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
102   if(ret == QMessageBox::Yes) {
103     currentPage()->load();
104   }
105 }
106
107 void SettingsPageDlg::loadDefaults() {
108   if(!currentPage()) return;
109   int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
110                                   QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
111   if(ret == QMessageBox::Yes) {
112     currentPage()->defaults();
113   }
114 }