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