Gui: fixes issue #11113: Adjust Default Main Light Position

This commit is contained in:
wmayer
2023-10-23 17:12:58 +02:00
committed by Chris Hennes
parent 010a62eba0
commit 9708f00157
6 changed files with 514 additions and 1 deletions

View File

@@ -319,6 +319,7 @@ SET(Gui_UIC_SRCS
PreferencePages/DlgSettingsEditor.ui
PreferencePages/DlgSettingsGeneral.ui
DlgSettingsImage.ui
PreferencePages/DlgSettingsLightSources.ui
PreferencePages/DlgSettingsMacro.ui
PreferencePages/DlgSettingsNavigation.ui
PreferencePages/DlgSettingsNotificationArea.ui
@@ -565,6 +566,7 @@ SET(Dialog_Settings_CPP_SRCS
PreferencePages/DlgSettingsEditor.cpp
PreferencePages/DlgSettingsGeneral.cpp
DlgSettingsImageImp.cpp
PreferencePages/DlgSettingsLightSources.cpp
PreferencePages/DlgSettingsMacroImp.cpp
PreferencePages/DlgSettingsNavigation.cpp
PreferencePages/DlgSettingsNotificationArea.cpp
@@ -586,6 +588,7 @@ SET(Dialog_Settings_HPP_SRCS
PreferencePages/DlgSettingsEditor.h
PreferencePages/DlgSettingsGeneral.h
DlgSettingsImageImp.h
PreferencePages/DlgSettingsLightSources.h
PreferencePages/DlgSettingsMacroImp.h
PreferencePages/DlgSettingsNavigation.h
PreferencePages/DlgSettingsNotificationArea.h
@@ -609,6 +612,7 @@ SET(Dialog_Settings_SRCS
PreferencePages/DlgSettingsEditor.ui
PreferencePages/DlgSettingsGeneral.ui
DlgSettingsImage.ui
PreferencePages/DlgSettingsLightSources.ui
PreferencePages/DlgSettingsMacro.ui
PreferencePages/DlgSettingsNavigation.ui
PreferencePages/DlgSettingsNotificationArea.ui

View File

@@ -0,0 +1,220 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#include <QColor>
#include <QEvent>
#include <QGridLayout>
#include <Inventor/draggers/SoDirectionalLightDragger.h>
#include <Inventor/nodes/SoDirectionalLight.h>
#include <Inventor/nodes/SoOrthographicCamera.h>
#include <Inventor/nodes/SoPickStyle.h>
#include <Inventor/nodes/SoSeparator.h>
#endif
#include "DlgSettingsLightSources.h"
#include "ui_DlgSettingsLightSources.h"
#include <Gui/View3DInventorViewer.h>
using namespace Gui::Dialog;
/* TRANSLATOR Gui::Dialog::DlgSettingsLightSources */
DlgSettingsLightSources::DlgSettingsLightSources(QWidget* parent)
: PreferencePage(parent)
, ui(new Ui_DlgSettingsLightSources)
{
ui->setupUi(this);
setupConnection();
}
DlgSettingsLightSources::~DlgSettingsLightSources()
{
delete view;
}
void DlgSettingsLightSources::setupConnection()
{
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);
}
void DlgSettingsLightSources::showEvent(QShowEvent* event)
{
Q_UNUSED(event)
if (!view) {
QGroupBox* box = ui->groupBoxLight;
QWidget* widget = createViewer(box);
auto grid = new QGridLayout(box);
grid->addWidget(widget);
box->setLayout(grid);
loadDirection();
}
}
void DlgSettingsLightSources::dragMotionCallback(void *data, SoDragger *drag)
{
auto lightdrag = static_cast<SoDirectionalLightDragger*>(drag);
auto self = static_cast<DlgSettingsLightSources*>(data);
SbRotation rotation = lightdrag->rotation.getValue();
SbVec3f dir(0, 0, -1);
rotation.multVec(dir, dir);
self->view->getHeadlight()->direction = dir;
}
QWidget* DlgSettingsLightSources::createViewer(QWidget* parent)
{
// NOLINTBEGIN
view = new Gui::View3DInventorViewer(parent);
view->setRedirectToSceneGraph(true);
view->setViewing(true);
view->setPopupMenuEnabled(false);
view->setBackgroundColor(QColor(255, 255, 255));
view->setGradientBackground(Gui::View3DInventorViewer::NoGradient);
view->setEnabledNaviCube(false);
auto root = static_cast<SoSeparator*>(view->getSceneGraph());
root->addChild(createDragger());
view->setCameraType(SoOrthographicCamera::getClassTypeId());
view->setViewDirection(SbVec3f(1, 1, -5));
view->viewAll();
// NOLINTEND
const int size = 250;
view->resize(size, size);
return view;
}
SoDirectionalLightDragger* DlgSettingsLightSources::createDragger()
{
lightDragger = new SoDirectionalLightDragger();
SoDragger * therotator = static_cast<SoDragger *>(lightDragger->getPart("translator", false));
therotator->setPartAsDefault("xTranslator.translatorActive", nullptr);
therotator->setPartAsDefault("yTranslator.translatorActive", nullptr);
therotator->setPartAsDefault("zTranslator.translatorActive", nullptr);
therotator->setPartAsDefault("xTranslator.translator", nullptr);
therotator->setPartAsDefault("yTranslator.translator", nullptr);
therotator->setPartAsDefault("zTranslator.translator", nullptr);
SoNode* node = therotator->getPart("yzTranslator.translator", false);
if (node && node->isOfType(SoGroup::getClassTypeId())) {
auto ps = new SoPickStyle();
ps->style = SoPickStyle::UNPICKABLE;
static_cast<SoGroup*>(node)->insertChild(ps, 0);
}
lightDragger->addMotionCallback(dragMotionCallback, this);
return lightDragger;
}
void DlgSettingsLightSources::saveSettings()
{
ui->checkBoxLight1->onSave();
ui->light1Color->onSave();
ui->sliderIntensity1->onSave();
saveDirection();
}
void DlgSettingsLightSources::loadSettings()
{
ui->checkBoxLight1->onRestore();
ui->light1Color->onRestore();
ui->sliderIntensity1->onRestore();
}
void DlgSettingsLightSources::saveDirection()
{
if (lightDragger) {
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());
}
}
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;
}
void DlgSettingsLightSources::toggleLight(bool on)
{
view->getHeadlight()->on = on;
}
void DlgSettingsLightSources::lightIntensity(int value)
{
float intensity = float(value) / 100.0F;
view->getHeadlight()->intensity = intensity;
}
void DlgSettingsLightSources::lightColor()
{
QColor color = ui->light1Color->color();
double red = color.redF();
double green = color.greenF();
double blue = color.blueF();
view->getHeadlight()->color = SbColor(float(red), float(green), float(blue));
}
void DlgSettingsLightSources::changeEvent(QEvent* event)
{
if (event->type() == QEvent::LanguageChange) {
ui->retranslateUi(this);
}
PreferencePage::changeEvent(event);
}
#include "moc_DlgSettingsLightSources.cpp"

View File

@@ -0,0 +1,79 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#ifndef GUI_DIALOG_DLGSETTINGSLIGHTSOURCES_H
#define GUI_DIALOG_DLGSETTINGSLIGHTSOURCES_H
#include <Gui/PropertyPage.h>
#include <memory>
class SoDragger;
class SoDirectionalLightDragger;
namespace Gui {
class View3DInventorViewer;
namespace Dialog {
class Ui_DlgSettingsLightSources;
/**
* The DlgSettingsLightSources class implements a preference page to change settings
* for the light sources of a 3D view.
* @author Werner Mayer
*/
class DlgSettingsLightSources : public PreferencePage
{
Q_OBJECT
public:
explicit DlgSettingsLightSources(QWidget* parent = nullptr);
~DlgSettingsLightSources() override;
void saveSettings() override;
void loadSettings() override;
protected:
void changeEvent(QEvent* event) override;
void showEvent(QShowEvent* event) override;
private:
void setupConnection();
void toggleLight(bool on);
void lightIntensity(int value);
void lightColor();
void saveDirection();
void loadDirection();
QWidget* createViewer(QWidget* parent);
SoDirectionalLightDragger* createDragger();
static void dragMotionCallback(void *data, SoDragger *drag);
private:
std::unique_ptr<Ui_DlgSettingsLightSources> ui;
View3DInventorViewer* view = nullptr;
SoDirectionalLightDragger* lightDragger = nullptr;
};
} // namespace Dialog
} // namespace Gui
#endif // GUI_DIALOG_DLGSETTINGSLIGHTSOURCES_H

View File

@@ -0,0 +1,199 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Gui::Dialog::DlgSettingsLightSources</class>
<widget class="QWidget" name="Gui::Dialog::DlgSettingsLightSources">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>484</width>
<height>515</height>
</rect>
</property>
<property name="windowTitle">
<string>Light Sources</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Light sources</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="Gui::PrefCheckBox" name="checkBoxLight1">
<property name="text">
<string>Light source</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="prefEntry" stdset="0">
<cstring>EnableHeadlight</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>View</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="Gui::PrefColorButton" name="light1Color">
<property name="color">
<color>
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</property>
<property name="prefEntry" stdset="0">
<cstring>HeadlightColor</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>View</cstring>
</property>
</widget>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>115</width>
<height>13</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="3">
<widget class="QLabel" name="light1Label">
<property name="text">
<string>Intensity</string>
</property>
</widget>
</item>
<item row="0" column="4">
<widget class="Gui::PrefSlider" name="sliderIntensity1">
<property name="maximum">
<number>100</number>
</property>
<property name="value">
<number>100</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="tickPosition">
<enum>QSlider::TicksBelow</enum>
</property>
<property name="tickInterval">
<number>10</number>
</property>
<property name="prefEntry" stdset="0">
<cstring>HeadlightIntensity</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>View</cstring>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QGroupBox" name="groupBoxLight">
<property name="title">
<string>Lights</string>
</property>
</widget>
</item>
<item row="2" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>428</width>
<height>376</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>Gui::ColorButton</class>
<extends>QPushButton</extends>
<header>Gui/Widgets.h</header>
</customwidget>
<customwidget>
<class>Gui::PrefColorButton</class>
<extends>Gui::ColorButton</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
<customwidget>
<class>Gui::PrefSlider</class>
<extends>QSlider</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
<customwidget>
<class>Gui::PrefCheckBox</class>
<extends>QCheckBox</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>checkBoxLight1</sender>
<signal>toggled(bool)</signal>
<receiver>light1Color</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>73</x>
<y>53</y>
</hint>
<hint type="destinationlabel">
<x>150</x>
<y>53</y>
</hint>
</hints>
</connection>
<connection>
<sender>checkBoxLight1</sender>
<signal>toggled(bool)</signal>
<receiver>light1Label</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>73</x>
<y>53</y>
</hint>
<hint type="destinationlabel">
<x>284</x>
<y>53</y>
</hint>
</hints>
</connection>
<connection>
<sender>checkBoxLight1</sender>
<signal>toggled(bool)</signal>
<receiver>sliderIntensity1</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>73</x>
<y>53</y>
</hint>
<hint type="destinationlabel">
<x>357</x>
<y>53</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -87,6 +87,7 @@ void View3DSettings::applySettings()
OnChange(*hGrp,"UseVBO");
OnChange(*hGrp,"RenderCache");
OnChange(*hGrp,"Orthographic");
OnChange(*hGrp,"EnableHeadlight");
OnChange(*hGrp,"HeadlightColor");
OnChange(*hGrp,"HeadlightDirection");
OnChange(*hGrp,"HeadlightIntensity");
@@ -108,7 +109,13 @@ void View3DSettings::applySettings()
void View3DSettings::OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::MessageType Reason)
{
const ParameterGrp& rGrp = static_cast<ParameterGrp&>(rCaller);
if (strcmp(Reason,"HeadlightColor") == 0) {
if (strcmp(Reason,"EnableHeadlight") == 0) {
bool enable = rGrp.GetBool("EnableHeadlight", true);
for (auto _viewer : _viewers) {
_viewer->getHeadlight()->on.setValue(enable);
}
}
else if (strcmp(Reason,"HeadlightColor") == 0) {
unsigned long headlight = rGrp.GetUnsigned("HeadlightColor",ULONG_MAX); // default color (white)
float transparency;
SbColor headlightColor;

View File

@@ -36,6 +36,7 @@
#include "PreferencePages/DlgSettingsEditor.h"
#include "PreferencePages/DlgSettingsGeneral.h"
#include "PreferencePages/DlgSettingsMacroImp.h"
#include "PreferencePages/DlgSettingsLightSources.h"
#include "PreferencePages/DlgSettingsNavigation.h"
#include "PreferencePages/DlgSettingsNotificationArea.h"
#include "PreferencePages/DlgSettingsPythonConsole.h"
@@ -58,6 +59,7 @@
using namespace Gui;
using namespace Gui::Dialog;
// clang-format off
/**
* Registers all preference pages or widgets to create them dynamically at any later time.
*/
@@ -74,6 +76,7 @@ WidgetFactorySupplier::WidgetFactorySupplier()
new PrefPageProducer<DlgSettingsNotificationArea> ( QT_TRANSLATE_NOOP("QObject","General") );
new PrefPageProducer<DlgSettingsReportView> ( QT_TRANSLATE_NOOP("QObject","General") );
new PrefPageProducer<DlgSettings3DViewImp> ( QT_TRANSLATE_NOOP("QObject","Display") );
new PrefPageProducer<DlgSettingsLightSources> ( QT_TRANSLATE_NOOP("QObject","Display") );
new PrefPageProducer<DlgSettingsUI> ( QT_TRANSLATE_NOOP("QObject","Display") );
new PrefPageProducer<DlgSettingsNavigation> ( QT_TRANSLATE_NOOP("QObject","Display") );
new PrefPageProducer<DlgSettingsViewColor> ( QT_TRANSLATE_NOOP("QObject","Display") );
@@ -122,3 +125,4 @@ WidgetFactorySupplier::WidgetFactorySupplier()
new WidgetProducer<Gui::DoubleSpinBox>;
new WidgetProducer<Gui::QuantitySpinBox>;
}
// clang-format on