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