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