1 | /*************************************************************************** |
2 | * Copyright (C) 2005-2014 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 <QMessageBox> |
22 | #include <QPushButton> |
23 | |
24 | #include "settingspagedlg.h" |
25 | |
26 | #include "iconloader.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(SmallIcon("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, SIGNAL(changed(bool)), this, SLOT(setButtonStates())); |
49 | connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *))); |
50 | page->load(); |
51 | setButtonStates(); |
52 | } |
53 | |
54 | |
55 | SettingsPage *SettingsPageDlg::currentPage() const |
56 | { |
57 | return _currentPage; |
58 | } |
59 | |
60 | |
61 | void SettingsPageDlg::setButtonStates() |
62 | { |
63 | SettingsPage *sp = currentPage(); |
64 | ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged()); |
65 | ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged()); |
66 | ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults()); |
67 | } |
68 | |
69 | |
70 | void SettingsPageDlg::buttonClicked(QAbstractButton *button) |
71 | { |
72 | switch (ui.buttonBox->standardButton(button)) { |
73 | case QDialogButtonBox::Ok: |
74 | if (currentPage() && currentPage()->hasChanged()) { |
75 | if (applyChanges()) accept(); |
76 | } |
77 | else accept(); |
78 | break; |
79 | case QDialogButtonBox::Apply: |
80 | applyChanges(); |
81 | break; |
82 | case QDialogButtonBox::Cancel: |
83 | undoChanges(); |
84 | reject(); |
85 | break; |
86 | case QDialogButtonBox::Reset: |
87 | reload(); |
88 | break; |
89 | case QDialogButtonBox::RestoreDefaults: |
90 | loadDefaults(); |
91 | break; |
92 | default: |
93 | break; |
94 | } |
95 | } |
96 | |
97 | |
98 | bool SettingsPageDlg::applyChanges() |
99 | { |
100 | if (!currentPage()) return false; |
101 | if (currentPage()->aboutToSave()) { |
102 | currentPage()->save(); |
103 | return true; |
104 | } |
105 | return false; |
106 | } |
107 | |
108 | |
109 | void SettingsPageDlg::undoChanges() |
110 | { |
111 | if (currentPage()) { |
112 | currentPage()->load(); |
113 | } |
114 | } |
115 | |
116 | |
117 | void SettingsPageDlg::reload() |
118 | { |
119 | if (!currentPage()) return; |
120 | int ret = QMessageBox::question(this, tr("Reload Settings" ), tr("Do you like to reload the settings, undoing your changes on this page?" ), |
121 | QMessageBox::Yes|QMessageBox::No, QMessageBox::No); |
122 | if (ret == QMessageBox::Yes) { |
123 | currentPage()->load(); |
124 | } |
125 | } |
126 | |
127 | |
128 | void SettingsPageDlg::loadDefaults() |
129 | { |
130 | if (!currentPage()) return; |
131 | int ret = QMessageBox::question(this, tr("Restore Defaults" ), tr("Do you like to restore the default values for this page?" ), |
132 | QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel); |
133 | if (ret == QMessageBox::RestoreDefaults) { |
134 | currentPage()->defaults(); |
135 | } |
136 | } |
137 | |