Debian Codegraph @

src/Gui/PreferencePages/DlgSettingsLightSources.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 **************************************************************************/
23
24 #include <Inventor/events/SoEvent.h>
25 #include <Inventor/nodes/SoDirectionalLight.h>
26 #include <Inventor/nodes/SoEventCallback.h>
27 #include <Inventor/nodes/SoOrthographicCamera.h>
28 #include <Inventor/nodes/SoComplexity.h>
29 #include <Inventor/nodes/SoSeparator.h>
30 #include <Inventor/nodes/SoMaterial.h>
31 #include <Inventor/nodes/SoSphere.h>
32
33 #include "DlgSettingsLightSources.h"
34 #include "ui_DlgSettingsLightSources.h"
35
36 #include <Utilities.h>
37 #include <Base/Builder3D.h>
38 #include <Base/Tools.h>
39 #include <Gui/View3DInventorViewer.h>
40 #include <Gui/View3DSettings.h>
41
42
43 using namespace Gui::Dialog;
44
45 /* TRANSLATOR Gui::Dialog::DlgSettingsLightSources */
46
47 DlgSettingsLightSources::DlgSettingsLightSources(QWidget* parent)
48 : PreferencePage(parent)
49 , ui(new Ui_DlgSettingsLightSources)
50 {
51 ui->setupUi(this);
52
53 view = ui->viewer;
54
55 configureViewer();
56
57 const auto connectLightEvents = [this](
58 QuantitySpinBox* horizontalAngleSpinBox,
59 QuantitySpinBox* verticalAngleSpinBox,
60 QSpinBox* intensitySpinBox,
61 ColorButton* colorButton,
62 QCheckBox* enabledCheckbox,
63 auto updateLightFunction
64 ) {
65 connect(
66 horizontalAngleSpinBox,
67 qOverload<double>(&QuantitySpinBox::valueChanged),
68 this,
69 updateLightFunction
70 );
71 connect(
72 verticalAngleSpinBox,
73 qOverload<double>(&QuantitySpinBox::valueChanged),
74 this,
75 updateLightFunction
76 );
77 connect(intensitySpinBox, qOverload<int>(&QSpinBox::valueChanged), this, updateLightFunction);
78 connect(colorButton, &ColorButton::changed, this, updateLightFunction);
79 #if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
80 connect(enabledCheckbox, &QCheckBox::checkStateChanged, this, updateLightFunction);
81 #else
82 connect(enabledCheckbox, &QCheckBox::stateChanged, this, updateLightFunction);
83 #endif
84 };
85
86 const auto updateLight = [this](
87 SoDirectionalLight* light,
88 QuantitySpinBox* horizontalAngleSpinBox,
89 QuantitySpinBox* verticalAngleSpinBox,
90 QSpinBox* intensitySpinBox,
91 ColorButton* colorButton,
92 QCheckBox* enabledCheckbox,
93 std::function<void(bool)> setLightEnabled
94 ) {
95 light->color = Base::convertTo<SbColor>(colorButton->color());
96 light->intensity = Base::fromPercent(intensitySpinBox->value());
97 light->direction = Base::convertTo<SbVec3f>(azimuthElevationToDirection(
98 horizontalAngleSpinBox->rawValue(),
99 verticalAngleSpinBox->rawValue()
100 ));
101 setLightEnabled(enabledCheckbox->isChecked());
102 };
103
104 const auto updateHeadLight = [this, updateLight] {
105 updateLight(
106 view->getHeadlight(),
107 ui->mainLightHorizontalAngle,
108 ui->mainLightVerticalAngle,
109 ui->mainLightIntensitySpinBox,
110 ui->mainLightColor,
111 ui->mainLightEnable,
112 [this](bool enabled) { view->setHeadlightEnabled(enabled); }
113 );
114 };
115
116 const auto updateFillLight = [this, updateLight] {
117 updateLight(
118 view->getFillLight(),
119 ui->fillLightHorizontalAngle,
120 ui->fillLightVerticalAngle,
121 ui->fillLightIntensitySpinBox,
122 ui->fillLightColor,
123 ui->fillLightEnable,
124 [this](bool enabled) { view->setFillLightEnabled(enabled); }
125 );
126 };
127
128 const auto updateBackLight = [this, updateLight] {
129 updateLight(
130 view->getBacklight(),
131 ui->backLightHorizontalAngle,
132 ui->backLightVerticalAngle,
133 ui->backLightIntensitySpinBox,
134 ui->backLightColor,
135 ui->backLightEnable,
136 [this](bool enabled) { view->setBacklightEnabled(enabled); }
137 );
138 };
139
140 connectLightEvents(
141 ui->mainLightHorizontalAngle,
142 ui->mainLightVerticalAngle,
143 ui->mainLightIntensitySpinBox,
144 ui->mainLightColor,
145 ui->mainLightEnable,
146 updateHeadLight
147 );
148 connectLightEvents(
149 ui->backLightHorizontalAngle,
150 ui->backLightVerticalAngle,
151 ui->backLightIntensitySpinBox,
152 ui->backLightColor,
153 ui->backLightEnable,
154 updateBackLight
155 );
156 connectLightEvents(
157 ui->fillLightHorizontalAngle,
158 ui->fillLightVerticalAngle,
159 ui->fillLightIntensitySpinBox,
160 ui->fillLightColor,
161 ui->fillLightEnable,
162 updateFillLight
163 );
164
165 const auto updateAmbientLight = [this] {
166 view->getEnvironment()->ambientColor = Base::convertTo<SbColor>(ui->ambientLightColor->color());
167 view->getEnvironment()->ambientIntensity = Base::fromPercent(
168 ui->ambientLightIntensitySpinBox->value()
169 );
170 };
171
172 connect(
173 ui->ambientLightIntensitySpinBox,
174 qOverload<int>(&QSpinBox::valueChanged),
175 this,
176 updateAmbientLight
177 );
178 connect(ui->ambientLightColor, &ColorButton::changed, this, updateAmbientLight);
179
180 connect(ui->zoomInButton, &QToolButton::clicked, this, &DlgSettingsLightSources::zoomIn);
181 connect(ui->zoomOutButton, &QToolButton::clicked, this, &DlgSettingsLightSources::zoomOut);
182
183 DlgSettingsLightSources::loadSettings();
184 }
185
186 static inline SoMaterial* createMaterial(void)
187 {
188 const QColor ambientColor {0xff333333}, diffuseColor {0xffd2d2ff}, emissiveColor {0xff000000},
189 specularColor {0xffcccccc};
190
191 auto material = new SoMaterial();
192
193 material->ambientColor.setValue(Base::convertTo<SbColor>(ambientColor));
194 material->diffuseColor.setValue(Base::convertTo<SbColor>(diffuseColor));
195 material->emissiveColor.setValue(Base::convertTo<SbColor>(emissiveColor));
196 material->specularColor.setValue(Base::convertTo<SbColor>(specularColor));
197
198 material->shininess = 0.9f;
199
200 return material;
201 }
202
203 static inline SoSphere* createSphere(void)
204 {
205 auto sphere = new SoSphere();
206 sphere->radius = 3;
207
208 return sphere;
209 }
210
211 static inline SoComplexity* createGoodComplexity()
212 {
213 auto complexity = new SoComplexity();
214 complexity->value = 1.0;
215
216 return complexity;
217 }
218
219 void DlgSettingsLightSources::configureViewer()
220 {
221 const SbVec3f defaultViewDirection {0.0f, 1.0f, 0.3f};
222
223 View3DSettings(hGrpView, view).applySettings();
224
225 view->setRedirectToSceneGraph(true);
226 view->setViewing(true);
227 view->setPopupMenuEnabled(false);
228 view->setEnabledNaviCube(false);
229
230 const auto root = static_cast<SoSeparator*>(view->getSceneGraph());
231 root->addChild(createGoodComplexity());
232 root->addChild(createMaterial());
233 root->addChild(createSphere());
234
235 const auto callback = new SoEventCallback();
236 root->addChild(callback);
237 callback->addEventCallback(
238 SoEvent::getClassTypeId(),
239 []([[maybe_unused]] void* ud, SoEventCallback* cb) { cb->setHandled(); }
240 );
241
242 view->setCameraType(SoOrthographicCamera::getClassTypeId());
243 view->setViewDirection(defaultViewDirection);
244 view->viewAll();
245
246 camera = dynamic_cast<SoOrthographicCamera*>(view->getCamera());
247 const float cameraHeight = camera->height.getValue() * 2.0f;
248 camera->height = cameraHeight;
249 zoomStep = cameraHeight / 14.0f;
250 }
251
252 Base::Vector3d DlgSettingsLightSources::azimuthElevationToDirection(double azimuth, double elevation)
253 {
254 azimuth = Base::toRadians(azimuth);
255 elevation = Base::toRadians(elevation);
256
257 auto direction = Base::Vector3d {
258 std::sin(azimuth) * std::cos(elevation),
259 std::cos(azimuth) * std::cos(elevation),
260 std::sin(elevation)
261 };
262
263 direction.Normalize();
264
265 return direction;
266 }
267
268 std::pair<double, double> DlgSettingsLightSources::directionToAzimuthElevation(Base::Vector3d direction)
269 {
270 const auto azimuth = std::atan2(direction[0], direction[1]);
271 const auto elevation = std::atan2(
272 direction[2],
273 std::sqrt(direction[1] * direction[1] + direction[0] * direction[0])
274 );
275
276 return {Base::toDegrees(azimuth), Base::toDegrees(elevation)};
277 }
278
279 void DlgSettingsLightSources::saveSettings()
280 {
281 for (const auto& widget : findChildren<QWidget*>()) {
282 if (const auto pref = dynamic_cast<PrefWidget*>(widget)) {
283 pref->onSave();
284 }
285 }
286
287 const auto saveAngles = [this](
288 QuantitySpinBox* horizontalAngleSpinBox,
289 QuantitySpinBox* verticalAngleSpinBox,
290 const char* parameter
291 ) {
292 try {
293 const auto direction = azimuthElevationToDirection(
294 horizontalAngleSpinBox->rawValue(),
295 verticalAngleSpinBox->rawValue()
296 );
297
298 hGrp->SetASCII(parameter, Base::vectorToString(Base::convertTo<Base::Vector3f>(direction)));
299 }
300 catch (...) {
301 }
302 };
303
304 saveAngles(ui->mainLightHorizontalAngle, ui->mainLightVerticalAngle, "HeadlightDirection");
305 saveAngles(ui->backLightHorizontalAngle, ui->backLightVerticalAngle, "BacklightDirection");
306 saveAngles(ui->fillLightHorizontalAngle, ui->fillLightVerticalAngle, "FillLightDirection");
307 }
308
309 void DlgSettingsLightSources::loadSettings()
310 {
311 for (const auto& widget : findChildren<QWidget*>()) {
312 if (const auto pref = dynamic_cast<PrefWidget*>(widget)) {
313 pref->onRestore();
314 }
315 }
316
317 const auto loadAngles = [this](
318 QuantitySpinBox* horizontalAngleSpinBox,
319 QuantitySpinBox* verticalAngleSpinBox,
320 const char* parameter
321 ) {
322 try {
323 const auto direction = Base::stringToVector(hGrp->GetASCII(parameter));
324 const auto [azimuth, elevation] = directionToAzimuthElevation(
325 Base::convertTo<Base::Vector3d>(direction)
326 );
327
328 horizontalAngleSpinBox->setValue(azimuth);
329 verticalAngleSpinBox->setValue(elevation);
330 }
331 catch (...) {
332 }
333 };
334
335 loadAngles(ui->mainLightHorizontalAngle, ui->mainLightVerticalAngle, "HeadlightDirection");
336 loadAngles(ui->backLightHorizontalAngle, ui->backLightVerticalAngle, "BacklightDirection");
337 loadAngles(ui->fillLightHorizontalAngle, ui->fillLightVerticalAngle, "FillLightDirection");
338 }
339
340 void DlgSettingsLightSources::resetSettingsToDefaults()
341 {
342 PreferencePage::resetSettingsToDefaults();
343
344 hGrp->RemoveASCII("HeadlightDirection");
345 hGrp->RemoveASCII("BacklightDirection");
346 hGrp->RemoveASCII("FillLightDirection");
347
348 loadSettings();
349 configureViewer();
350 }
351
352 void DlgSettingsLightSources::zoomIn() const
353 {
354 if (camera == nullptr) {
355 return;
356 }
357
358 camera->height = camera->height.getValue() - zoomStep;
359 }
360
361 void DlgSettingsLightSources::zoomOut() const
362 {
363 if (camera == nullptr) {
364 return;
365 }
366
367 camera->height = camera->height.getValue() + zoomStep;
368 }
369
370 void DlgSettingsLightSources::changeEvent(QEvent* event)
371 {
372 if (event->type() == QEvent::LanguageChange) {
373 ui->retranslateUi(this);
374 }
375 PreferencePage::changeEvent(event);
376 }
377
378 #include "moc_DlgSettingsLightSources.cpp"