From ebf02483af225b502fccfa7111499dfde33999fd Mon Sep 17 00:00:00 2001 From: xtemp09 Date: Fri, 23 Aug 2024 06:58:14 +0700 Subject: [PATCH] Reimplementation of Light source dialog (#15877) * Reimplementation of Light source dialog Closes #15793. * Gui: Remove redundant void arguments --------- Co-authored-by: Chris Hennes --- .../DlgSettingsLightSources.cpp | 206 ++++++---- .../PreferencePages/DlgSettingsLightSources.h | 22 +- .../DlgSettingsLightSources.ui | 360 +++++++++++++++++- 3 files changed, 493 insertions(+), 95 deletions(-) diff --git a/src/Gui/PreferencePages/DlgSettingsLightSources.cpp b/src/Gui/PreferencePages/DlgSettingsLightSources.cpp index 934d5d4bb0..23231b506c 100644 --- a/src/Gui/PreferencePages/DlgSettingsLightSources.cpp +++ b/src/Gui/PreferencePages/DlgSettingsLightSources.cpp @@ -24,9 +24,6 @@ #include "PreCompiled.h" #ifndef _PreComp_ -#include -#include -#include #include #include #include @@ -52,58 +49,79 @@ DlgSettingsLightSources::DlgSettingsLightSources(QWidget* parent) , ui(new Ui_DlgSettingsLightSources) { ui->setupUi(this); - setupConnection(); + + view = ui->viewer; + createViewer(); + + QSizePolicy sp {QSizePolicy::Expanding, QSizePolicy::Expanding}; + sp.setHeightForWidth(true); + sp.setHorizontalStretch(1); + sp.setVerticalStretch(1); + view->setSizePolicy(sp); } -DlgSettingsLightSources::~DlgSettingsLightSources() +static inline +SbVec3f getDirectionVector(const SbRotation &rotation) { + SbVec3f dir {0.0f, 0.0f, -1.0f}; + rotation.multVec(dir, dir); + return dir; } -void DlgSettingsLightSources::setupConnection() +static inline +void setLightDirection(const SbRotation &rotation, Gui::View3DInventorViewer *viewer) { - connect(ui->checkBoxLight1, &QCheckBox::toggled, - this, &DlgSettingsLightSources::toggleLight); - connect(ui->sliderIntensity1, &QSlider::valueChanged, - this, &DlgSettingsLightSources::lightIntensity); - connect(ui->light1Color, &Gui::ColorButton::changed, - this, &DlgSettingsLightSources::lightColor); + viewer->getHeadlight()->direction = getDirectionVector(rotation); } -void DlgSettingsLightSources::showEvent(QShowEvent* event) +static inline +void setLightDraggerDirection(const SbRotation &rotation, SoDirectionalLightDragger *light_dragger) { - Q_UNUSED(event) - static bool underConstruction = false; // Prevent recursion - if (!underConstruction && !view) { - underConstruction = true; - QGroupBox* box = ui->groupBoxLight; - QWidget* widget = createViewer(box); - auto grid = new QGridLayout(box); - grid->addWidget(widget); + light_dragger->rotation = rotation; +} - loadDirection(); - underConstruction = false; - } +static inline +void setValueSilently(QDoubleSpinBox *spn, const float val) +{ + Q_ASSERT_X(spn, "setValueSilently", "QDoubleSpinBox has been deleted"); + + spn->blockSignals(true); + spn->setValue(val); + spn->blockSignals(false); } void DlgSettingsLightSources::dragMotionCallback(void *data, SoDragger *drag) { - auto lightdrag = static_cast(drag); // NOLINT + auto lightdrag = dynamic_cast (drag); auto self = static_cast(data); - SbRotation rotation = lightdrag->rotation.getValue(); - SbVec3f dir(0, 0, -1); - rotation.multVec(dir, dir); - self->view->getHeadlight()->direction = dir; + + const SbRotation rotation = lightdrag->rotation.getValue(); + + setLightDirection(rotation, self->view); + + setValueSilently(self->ui->q0_spnBox, rotation[0]); + setValueSilently(self->ui->q1_spnBox, rotation[1]); + setValueSilently(self->ui->q2_spnBox, rotation[2]); + setValueSilently(self->ui->q3_spnBox, rotation[3]); + + const SbVec3f dir = getDirectionVector(rotation); + + setValueSilently(self->ui->x_spnBox, dir[0]); + setValueSilently(self->ui->y_spnBox, dir[1]); + setValueSilently(self->ui->z_spnBox, dir[2]); } -QWidget* DlgSettingsLightSources::createViewer(QWidget* parent) +void DlgSettingsLightSources::createViewer() { + const QColor default_bg_color {200, 200, 200}; + const SbVec3f default_view_direction {1.0f, 1.0f, -5.0f}; + // NOLINTBEGIN - view = new Gui::View3DInventorViewer(parent); view->setRedirectToSceneGraph(true); view->setViewing(true); view->setPopupMenuEnabled(false); - view->setBackgroundColor(QColor(255, 255, 255)); + view->setBackgroundColor(default_bg_color); view->setGradientBackground(Gui::View3DInventorViewer::NoGradient); view->setEnabledNaviCube(false); @@ -119,20 +137,11 @@ QWidget* DlgSettingsLightSources::createViewer(QWidget* parent) }); view->setCameraType(SoOrthographicCamera::getClassTypeId()); - view->setViewDirection(SbVec3f(1, 1, -5)); + view->setViewDirection(default_view_direction); view->viewAll(); - float height = static_cast(view->getCamera())->height.getValue(); - static_cast(view->getCamera())->height.setValue(height / 2.0F); + auto cam = dynamic_cast (view->getCamera()); + cam->height = cam->height.getValue() * 2.0f; // NOLINTEND - - ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View"); - View3DSettings viewSettings(hGrp, view); - viewSettings.OnChange(*hGrp,"BackgroundColor"); - - const int size = 250; - view->resize(size, size); - - return view; } SoDirectionalLightDragger* DlgSettingsLightSources::createDragger() @@ -172,23 +181,39 @@ void DlgSettingsLightSources::loadSettings() ui->checkBoxLight1->onRestore(); ui->light1Color->onRestore(); ui->sliderIntensity1->onRestore(); + loadDirection(); + lightColor(); +} + +void DlgSettingsLightSources::resetSettingsToDefaults() +{ + ParameterGrp::handle grp = ui->sliderIntensity1->getWindowParameter(); + + grp->SetFloat("HeadlightRotationX", 0.0); + grp->SetFloat("HeadlightRotationY", 0.0); + grp->SetFloat("HeadlightRotationZ", 0.0); + grp->SetFloat("HeadlightRotationW", 1.0); + + grp->SetASCII("HeadlightDirection", "(0.0,0.0,-1.0)"); + + PreferencePage::resetSettingsToDefaults(); } void DlgSettingsLightSources::saveDirection() { if (lightDragger) { + const SbRotation rotation = lightDragger->rotation.getValue(); + const SbVec3f dir = getDirectionVector(rotation); + const QString headlightDir = QString::fromLatin1("(%1,%2,%3)").arg(dir[0]).arg(dir[1]).arg(dir[2]); + ParameterGrp::handle grp = ui->sliderIntensity1->getWindowParameter(); - SbRotation rotation = lightDragger->rotation.getValue(); + grp->SetFloat("HeadlightRotationX", rotation[0]); grp->SetFloat("HeadlightRotationY", rotation[1]); grp->SetFloat("HeadlightRotationZ", rotation[2]); grp->SetFloat("HeadlightRotationW", rotation[3]); - SbVec3f dir(0, 0, -1); - rotation.multVec(dir, dir); - - QString headlightDir = QString::fromLatin1("(%1,%2,%3)").arg(dir[0]).arg(dir[1]).arg(dir[2]); - grp->SetASCII("HeadlightDirection", headlightDir.toLatin1()); + grp->SetASCII("HeadlightDirection", qPrintable(headlightDir)); } } @@ -196,18 +221,29 @@ void DlgSettingsLightSources::loadDirection() { ParameterGrp::handle grp = ui->sliderIntensity1->getWindowParameter(); SbRotation rotation = lightDragger->rotation.getValue(); - // NOLINTBEGIN - float q1 = float(grp->GetFloat("HeadlightRotationX", rotation[0])); - float q2 = float(grp->GetFloat("HeadlightRotationY", rotation[1])); - float q3 = float(grp->GetFloat("HeadlightRotationZ", rotation[2])); - float q4 = float(grp->GetFloat("HeadlightRotationW", rotation[3])); - // NOLINTEND - rotation.setValue(q1, q2, q3, q4); - lightDragger->rotation.setValue(rotation); - SbVec3f direction(0, 0, -1); - rotation.multVec(direction, direction); - view->getHeadlight()->direction = direction; + auto get_q = [&grp](const char *name, const float def){return static_cast (grp->GetFloat(name, def));}; + + const float q0 = get_q("HeadlightRotationX", rotation[0]), + q1 = get_q("HeadlightRotationY", rotation[1]), + q2 = get_q("HeadlightRotationZ", rotation[2]), + q3 = get_q("HeadlightRotationW", rotation[3]); + + rotation.setValue(q0, q1, q2, q3); + + setLightDirection(rotation, ui->viewer); + setLightDraggerDirection(rotation, lightDragger); + + setValueSilently(ui->q0_spnBox, rotation[0]); + setValueSilently(ui->q1_spnBox, rotation[1]); + setValueSilently(ui->q2_spnBox, rotation[2]); + setValueSilently(ui->q3_spnBox, rotation[3]); + + const SbVec3f dir = getDirectionVector(rotation); + + setValueSilently(ui->x_spnBox, dir[0]); + setValueSilently(ui->y_spnBox, dir[1]); + setValueSilently(ui->z_spnBox, dir[2]); } void DlgSettingsLightSources::toggleLight(bool on) @@ -220,19 +256,17 @@ void DlgSettingsLightSources::toggleLight(bool on) void DlgSettingsLightSources::lightIntensity(int value) { if (view) { - float intensity = float(value) / 100.0F; - view->getHeadlight()->intensity = intensity; + view->getHeadlight()->intensity = static_cast (value) / 100.0f; } } void DlgSettingsLightSources::lightColor() { if (view) { - QColor color = ui->light1Color->color(); - float red = float(color.redF()); - float green = float(color.greenF()); - float blue = float(color.blueF()); - view->getHeadlight()->color = SbColor(red, green, blue); + const QColor color = ui->light1Color->color(); + view->getHeadlight()->color.setValue(color.redF(), + color.greenF(), + color.blueF()); } } @@ -244,5 +278,41 @@ void DlgSettingsLightSources::changeEvent(QEvent* event) PreferencePage::changeEvent(event); } +void DlgSettingsLightSources::updateDraggerQS() +{ + const float q0 = ui->q0_spnBox->value(), + q1 = ui->q1_spnBox->value(), + q2 = ui->q2_spnBox->value(), + q3 = ui->q3_spnBox->value(); + + const SbRotation rotation {q0, q1, q2, q3}; + + setLightDirection(rotation, view); + setLightDraggerDirection(rotation, lightDragger); + + const SbVec3f dir = getDirectionVector(rotation); + + setValueSilently(ui->x_spnBox, dir[0]); + setValueSilently(ui->y_spnBox, dir[1]); + setValueSilently(ui->z_spnBox, dir[2]); +} + +void DlgSettingsLightSources::updateDraggerXYZ() +{ + const float x = ui->x_spnBox->value(), + y = ui->y_spnBox->value(), + z = ui->z_spnBox->value(); + + const SbRotation rotation {SbVec3f{0.0f, 0.0f, -1.0f}, SbVec3f{x, y, z}}; + + setLightDirection(rotation, view); + setLightDraggerDirection(rotation, lightDragger); + + setValueSilently(ui->q0_spnBox, rotation[0]); + setValueSilently(ui->q1_spnBox, rotation[1]); + setValueSilently(ui->q2_spnBox, rotation[2]); + setValueSilently(ui->q3_spnBox, rotation[3]); +} + #include "moc_DlgSettingsLightSources.cpp" diff --git a/src/Gui/PreferencePages/DlgSettingsLightSources.h b/src/Gui/PreferencePages/DlgSettingsLightSources.h index 6b7dfc15c1..e3d36901cc 100644 --- a/src/Gui/PreferencePages/DlgSettingsLightSources.h +++ b/src/Gui/PreferencePages/DlgSettingsLightSources.h @@ -27,6 +27,7 @@ #include #include +#include class SoDragger; class SoDirectionalLightDragger; @@ -47,29 +48,32 @@ class DlgSettingsLightSources : public PreferencePage public: explicit DlgSettingsLightSources(QWidget* parent = nullptr); - ~DlgSettingsLightSources() override; + ~DlgSettingsLightSources() override = default; void saveSettings() override; void loadSettings() override; + void resetSettingsToDefaults() override; -protected: - void changeEvent(QEvent* event) override; - void showEvent(QShowEvent* event) override; - -private: - void setupConnection(); +public Q_SLOTS: + void updateDraggerQS (); + void updateDraggerXYZ(); void toggleLight(bool on); void lightIntensity(int value); void lightColor(); + +protected: + void changeEvent(QEvent* event) override; + +private: void saveDirection(); void loadDirection(); - QWidget* createViewer(QWidget* parent); + void createViewer(); SoDirectionalLightDragger* createDragger(); static void dragMotionCallback(void *data, SoDragger *drag); private: std::unique_ptr ui; - View3DInventorViewer* view = nullptr; + QPointer view; SoDirectionalLightDragger* lightDragger = nullptr; }; diff --git a/src/Gui/PreferencePages/DlgSettingsLightSources.ui b/src/Gui/PreferencePages/DlgSettingsLightSources.ui index adbe2c86c9..fbd1b5e40f 100644 --- a/src/Gui/PreferencePages/DlgSettingsLightSources.ui +++ b/src/Gui/PreferencePages/DlgSettingsLightSources.ui @@ -14,6 +14,19 @@ Light Sources + + + + Qt::Vertical + + + + 428 + 376 + + + + @@ -38,7 +51,7 @@ - + 255 255 @@ -102,25 +115,155 @@ - + + + Adjust the orientation of the directional light source by dragging the handle with the mouse or use the spin boxes for fine tuning. + - Lights + Direction + + + + + Qt::NoFocus + + + + + + + -100.000000000000000 + + + 0.100000000000000 + + + + + + + 2 + + + -100.000000000000000 + + + 0.100000000000000 + + + + + + + q1 + + + + + + + 2 + + + -100.000000000000000 + + + 0.100000000000000 + + + + + + + <html><head/><body><p>z</p></body></html> + + + + + + + q2 + + + + + + + q3 + + + + + + + -100.000000000000000 + + + 0.100000000000000 + + + + + + + y + + + + + + + 2 + + + -100.000000000000000 + + + 0.100000000000000 + + + + + + + 2 + + + -100.000000000000000 + + + 0.100000000000000 + + + + + + + -100.000000000000000 + + + 0.100000000000000 + + + + + + + q0 + + + + + + + x + + + + - - - - Qt::Vertical - - - - 428 - 376 - - - - @@ -144,7 +287,24 @@ QCheckBox
Gui/PrefWidgets.h
+ + Gui::View3DInventorViewer + QWidget +
Gui/View3DInventorViewer.h
+
+ + checkBoxLight1 + light1Color + sliderIntensity1 + x_spnBox + y_spnBox + z_spnBox + q0_spnBox + q1_spnBox + q2_spnBox + q3_spnBox + @@ -174,8 +334,8 @@ 53 - 284 - 53 + 339 + 65 @@ -195,5 +355,169 @@ + + q1_spnBox + valueChanged(double) + Gui::Dialog::DlgSettingsLightSources + updateDraggerQS(void) + + + 399 + 168 + + + 20 + 20 + + + + + q2_spnBox + valueChanged(double) + Gui::Dialog::DlgSettingsLightSources + updateDraggerQS(void) + + + 399 + 200 + + + 20 + 20 + + + + + q3_spnBox + valueChanged(double) + Gui::Dialog::DlgSettingsLightSources + updateDraggerQS(void) + + + 399 + 232 + + + 20 + 20 + + + + + q0_spnBox + valueChanged(double) + Gui::Dialog::DlgSettingsLightSources + updateDraggerQS(void) + + + 422 + 128 + + + 241 + 257 + + + + + checkBoxLight1 + toggled(bool) + Gui::Dialog::DlgSettingsLightSources + toggleLight(bool) + + + 68 + 53 + + + 241 + 257 + + + + + sliderIntensity1 + valueChanged(int) + Gui::Dialog::DlgSettingsLightSources + lightIntensity(int) + + + 404 + 52 + + + 241 + 257 + + + + + light1Color + changed(void) + Gui::Dialog::DlgSettingsLightSources + lightColor(void) + + + 140 + 53 + + + 241 + 257 + + + + + x_spnBox + valueChanged(double) + Gui::Dialog::DlgSettingsLightSources + updateDraggerXYZ(void) + + + 186 + 141 + + + 241 + 257 + + + + + y_spnBox + valueChanged(double) + Gui::Dialog::DlgSettingsLightSources + updateDraggerXYZ(void) + + + 186 + 173 + + + 241 + 257 + + + + + z_spnBox + valueChanged(double) + Gui::Dialog::DlgSettingsLightSources + updateDraggerXYZ(void) + + + 186 + 205 + + + 241 + 257 + + + + + updateDraggerQS(void) + updateDraggerXYZ(void) +