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