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