Debian Codegraph @

src/Gui/PropertyPage.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /****************************************************************************
4 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * Copyright (c) 2023 FreeCAD Project Association *
6 * *
7 * This file is part of FreeCAD. *
8 * *
9 * FreeCAD is free software: you can redistribute it and/or modify it *
10 * under the terms of the GNU Lesser General Public License as *
11 * published by the Free Software Foundation, either version 2.1 of the *
12 * License, or (at your option) any later version. *
13 * *
14 * FreeCAD is distributed in the hope that it will be useful, but *
15 * WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
17 * Lesser General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Lesser General Public *
20 * License along with FreeCAD. If not, see *
21 * <https://www.gnu.org/licenses/>. *
22 * *
23 ***************************************************************************/
24
25
26 #include <QVBoxLayout>
27
28
29 #include <Base/Console.h>
30 #include <App/Application.h>
31
32 #include "PropertyPage.h"
33 #include "PrefWidgets.h"
34 #include "UiLoader.h"
35
36
37 using namespace Gui::Dialog;
38
39 /** Construction */
40 PropertyPage::PropertyPage(QWidget* parent)
41 : QWidget(parent)
42 , bChanged {false}
43 {}
44
45 /** Applies all changes. Reimplement this in your subclasses. */
46 void PropertyPage::apply()
47 {}
48
49 /** Discards all changes. Reimplement this in your subclasses. */
50 void PropertyPage::cancel()
51 {}
52
53 /** Resets to the default values. Reimplement this in your subclasses. */
54 void PropertyPage::reset()
55 {}
56
57 /** Returns whether the page was modified or not. */
58 bool PropertyPage::isModified() const
59 {
60 return bChanged;
61 }
62
63 /** Sets the page to be modified. */
64 void PropertyPage::setModified(bool value)
65 {
66 bChanged = value;
67 }
68
69 /** Applies all changes calling @ref apply() and resets the modified state. */
70 void PropertyPage::onApply()
71 {
72 if (isModified()) {
73 apply();
74 }
75
76 setModified(false);
77 }
78
79 /** Discards all changes calling @ref cancel() and resets the modified state. */
80 void PropertyPage::onCancel()
81 {
82 if (isModified()) {
83 cancel();
84 setModified(false);
85 }
86 }
87
88 /** Resets to the default values. */
89 void PropertyPage::onReset()
90 {
91 reset();
92 }
93
94 // ----------------------------------------------------------------
95
96 /** Construction */
97 PreferencePage::PreferencePage(QWidget* parent)
98 : QWidget(parent)
99 , restartRequired(false)
100 {}
101
102 void PreferencePage::changeEvent(QEvent* event)
103 {
104 QWidget::changeEvent(event);
105 }
106
107 bool PreferencePage::isRestartRequired() const
108 {
109 return restartRequired;
110 }
111
112 void PreferencePage::requireRestart()
113 {
114 restartRequired = true;
115 }
116
117
118 // ----------------------------------------------------------------
119
120 PreferenceUiForm::PreferenceUiForm(const QString& fn, QWidget* parent)
121 : PreferencePage(parent)
122 , _form(nullptr)
123 {
124 auto loader = UiLoader::newInstance();
125 loader->setWorkingDirectory(QFileInfo(fn).absolutePath());
126 QFile file(fn);
127 if (file.open(QFile::ReadOnly)) {
128 _form = loader->load(&file, this);
129 }
130 file.close();
131 if (_form) {
132 this->setWindowTitle(_form->windowTitle());
133 auto layout = new QVBoxLayout;
134 layout->addWidget(_form);
135 setLayout(layout);
136 }
137 else {
138 Base::Console().error("Failed to load UI file from '%s'\n", (const char*)fn.toUtf8());
139 }
140 }
141
142 PreferenceUiForm::~PreferenceUiForm() = default;
143
144 void PreferenceUiForm::changeEvent(QEvent* e)
145 {
146 QWidget::changeEvent(e);
147 }
148
149 template<typename PW>
150 void PreferenceUiForm::loadPrefWidgets()
151 {
152 QList<PW> pw = _form->findChildren<PW>();
153 for (typename QList<PW>::iterator it = pw.begin(); it != pw.end(); ++it) {
154 (*it)->onRestore();
155 }
156 }
157
158 template<typename PW>
159 void PreferenceUiForm::savePrefWidgets()
160 {
161 QList<PW> pw = _form->findChildren<PW>();
162 for (typename QList<PW>::iterator it = pw.begin(); it != pw.end(); ++it) {
163 (*it)->onSave();
164 }
165 }
166
167 void PreferenceUiForm::loadSettings()
168 {
169 if (!_form) {
170 return;
171 }
172
173 // search for all pref widgets to restore their settings
174 loadPrefWidgets<Gui::PrefSpinBox*>();
175 loadPrefWidgets<Gui::PrefDoubleSpinBox*>();
176 loadPrefWidgets<Gui::PrefLineEdit*>();
177 loadPrefWidgets<Gui::PrefTextEdit*>();
178 loadPrefWidgets<Gui::PrefFileChooser*>();
179 loadPrefWidgets<Gui::PrefComboBox*>();
180 loadPrefWidgets<Gui::PrefFontBox*>();
181 loadPrefWidgets<Gui::PrefCheckBox*>();
182 loadPrefWidgets<Gui::PrefRadioButton*>();
183 loadPrefWidgets<Gui::PrefSlider*>();
184 loadPrefWidgets<Gui::PrefColorButton*>();
185 loadPrefWidgets<Gui::PrefUnitSpinBox*>();
186 loadPrefWidgets<Gui::PrefQuantitySpinBox*>();
187 loadPrefWidgets<Gui::PrefCheckableGroupBox*>();
188 }
189
190 void PreferenceUiForm::saveSettings()
191 {
192 if (!_form) {
193 return;
194 }
195
196 // search for all pref widgets to save their settings
197 savePrefWidgets<Gui::PrefSpinBox*>();
198 savePrefWidgets<Gui::PrefDoubleSpinBox*>();
199 savePrefWidgets<Gui::PrefLineEdit*>();
200 savePrefWidgets<Gui::PrefTextEdit*>();
201 savePrefWidgets<Gui::PrefFileChooser*>();
202 savePrefWidgets<Gui::PrefComboBox*>();
203 savePrefWidgets<Gui::PrefFontBox*>();
204 savePrefWidgets<Gui::PrefCheckBox*>();
205 savePrefWidgets<Gui::PrefRadioButton*>();
206 savePrefWidgets<Gui::PrefSlider*>();
207 savePrefWidgets<Gui::PrefColorButton*>();
208 savePrefWidgets<Gui::PrefUnitSpinBox*>();
209 savePrefWidgets<Gui::PrefQuantitySpinBox*>();
210 savePrefWidgets<Gui::PrefCheckableGroupBox*>();
211 }
212
213 QWidget* Gui::Dialog::PreferenceUiForm::form()
214 {
215 return _form;
216 }
217
218 void PreferencePage::resetSettingsToDefaults()
219 {
220 auto prefs = this->findChildren<QObject*>();
221
222 for (const auto& pref : prefs) {
223 if (!pref->property("prefPath").isNull() && !pref->property("prefEntry").isNull()) {
224 std::string path = pref->property("prefPath").toString().toStdString();
225 std::string entry = pref->property("prefEntry").toString().toStdString();
226
227 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
228 std::string("User parameter:BaseApp/Preferences/" + path).c_str()
229 );
230
231 for (const auto& pn : hGrp->GetParameterNames(entry.c_str())) {
232 hGrp->RemoveAttribute(pn.first, pn.second.c_str());
233 }
234 }
235 }
236 }
237 // ----------------------------------------------------------------
238
239 /** Construction */
240 CustomizeActionPage::CustomizeActionPage(QWidget* parent)
241 : QWidget(parent)
242 {}
243
244 /** Destruction */
245 CustomizeActionPage::~CustomizeActionPage() = default;
246
247 bool CustomizeActionPage::event(QEvent* e)
248 {
249 bool ok = QWidget::event(e);
250
251 if (e->type() == QEvent::ParentChange || e->type() == QEvent::ParentAboutToChange) {
252 QWidget* topLevel = this->parentWidget();
253 while (topLevel && !topLevel->inherits("QDialog")) {
254 topLevel = topLevel->parentWidget();
255 }
256 if (topLevel) {
257 int index = topLevel->metaObject()->indexOfSignal(
258 QMetaObject::normalizedSignature("addMacroAction(const QByteArray&)")
259 );
260 if (index >= 0) {
261 if (e->type() == QEvent::ParentChange) {
262 connect(
263 topLevel,
264 SIGNAL(addMacroAction(const QByteArray&)),
265 this,
266 SLOT(onAddMacroAction(const QByteArray&))
267 );
268 connect(
269 topLevel,
270 SIGNAL(removeMacroAction(const QByteArray&)),
271 this,
272 SLOT(onRemoveMacroAction(const QByteArray&))
273 );
274 connect(
275 topLevel,
276 SIGNAL(modifyMacroAction(const QByteArray&)),
277 this,
278 SLOT(onModifyMacroAction(const QByteArray&))
279 );
280 }
281 else {
282 disconnect(
283 topLevel,
284 SIGNAL(addMacroAction(const QByteArray&)),
285 this,
286 SLOT(onAddMacroAction(const QByteArray&))
287 );
288 disconnect(
289 topLevel,
290 SIGNAL(removeMacroAction(const QByteArray&)),
291 this,
292 SLOT(onRemoveMacroAction(const QByteArray&))
293 );
294 disconnect(
295 topLevel,
296 SIGNAL(modifyMacroAction(const QByteArray&)),
297 this,
298 SLOT(onModifyMacroAction(const QByteArray&))
299 );
300 }
301 }
302 }
303 }
304
305 return ok;
306 }
307
308 void CustomizeActionPage::changeEvent(QEvent* e)
309 {
310 QWidget::changeEvent(e);
311 }
312
313 #include "moc_PropertyPage.cpp"