From 94f3a476a94c2b5164acef97ea29c584667c06e6 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 26 Feb 2023 14:48:32 +0100 Subject: [PATCH] Gui: refactor View3DInventor and move handling of settings to separate classes --- src/Gui/CMakeLists.txt | 2 + src/Gui/NaviCube.h | 2 + src/Gui/View3DInventor.cpp | 317 ++----------------------------- src/Gui/View3DInventor.h | 13 +- src/Gui/View3DSettings.cpp | 370 +++++++++++++++++++++++++++++++++++++ src/Gui/View3DSettings.h | 63 +++++++ 6 files changed, 456 insertions(+), 311 deletions(-) create mode 100644 src/Gui/View3DSettings.cpp create mode 100644 src/Gui/View3DSettings.h diff --git a/src/Gui/CMakeLists.txt b/src/Gui/CMakeLists.txt index b60dc4849e..30addf3efa 100644 --- a/src/Gui/CMakeLists.txt +++ b/src/Gui/CMakeLists.txt @@ -812,6 +812,7 @@ SET(View3D_CPP_SRCS View3DInventorSelection.cpp View3DInventorViewer.cpp View3DInventorRiftViewer.cpp + View3DSettings.cpp CoinRiftWidget.cpp View3DPy.cpp View3DViewerPy.cpp @@ -833,6 +834,7 @@ SET(View3D_SRCS View3DInventorViewer.h View3DPy.h View3DInventorRiftViewer.h + View3DSettings.h CoinRiftWidget.h View3DViewerPy.h NaviCube.h diff --git a/src/Gui/NaviCube.h b/src/Gui/NaviCube.h index 05bdb8d273..dff5af5a5a 100644 --- a/src/Gui/NaviCube.h +++ b/src/Gui/NaviCube.h @@ -24,6 +24,8 @@ #define SRC_GUI_NAVICUBE_H_ #include +#include +#include class SoEvent; diff --git a/src/Gui/View3DInventor.cpp b/src/Gui/View3DInventor.cpp index 413218967f..8c9774798d 100644 --- a/src/Gui/View3DInventor.cpp +++ b/src/Gui/View3DInventor.cpp @@ -43,21 +43,19 @@ # include # include # include -# include # include -# include # include # include # include #endif -#include #include #include #include #include #include "View3DInventor.h" +#include "View3DSettings.h" #include "Application.h" #include "Document.h" #include "FileDialog.h" @@ -97,14 +95,6 @@ View3DInventor::View3DInventor(Gui::Document* pcDocument, QWidget* parent, // accept drops on the window, get handled in dropEvent, dragEnterEvent setAcceptDrops(true); - // attach parameter Observer - hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View"); - hGrp->Attach(this); - - hGrpNavi = - App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/NaviCube"); - hGrpNavi->Attach(this); - //anti-aliasing settings bool smoothing = false; bool glformat = false; @@ -152,8 +142,9 @@ View3DInventor::~View3DInventor() if (Cam) _pcDocument->saveCameraSettings(SoFCDB::writeNodesToString(Cam).c_str()); } - hGrp->Detach(this); - hGrpNavi->Detach(this); + + viewSettings.reset(); + naviSettings.reset(); //If we destroy this viewer by calling 'delete' directly the focus proxy widget which is defined //by a widget in SoQtViewer isn't reset. This widget becomes a dangling pointer and makes @@ -199,294 +190,12 @@ PyObject *View3DInventor::getPyObject() void View3DInventor::applySettings() { - // apply the user settings - View3DInventor::OnChange(*hGrp,"EyeDistance"); - View3DInventor::OnChange(*hGrp,"CornerCoordSystem"); - View3DInventor::OnChange(*hGrp,"CornerCoordSystemSize"); - View3DInventor::OnChange(*hGrp,"ShowAxisCross"); - View3DInventor::OnChange(*hGrp,"UseAutoRotation"); - View3DInventor::OnChange(*hGrp,"Gradient"); - View3DInventor::OnChange(*hGrp,"BackgroundColor"); - View3DInventor::OnChange(*hGrp,"BackgroundColor2"); - View3DInventor::OnChange(*hGrp,"BackgroundColor3"); - View3DInventor::OnChange(*hGrp,"BackgroundColor4"); - View3DInventor::OnChange(*hGrp,"UseBackgroundColorMid"); - View3DInventor::OnChange(*hGrp,"ShowFPS"); - View3DInventor::OnChange(*hGrp,"ShowNaviCube"); - View3DInventor::OnChange(*hGrpNavi, "CornerNaviCube"); - View3DInventor::OnChange(*hGrp,"UseVBO"); - View3DInventor::OnChange(*hGrp,"RenderCache"); - View3DInventor::OnChange(*hGrp,"Orthographic"); - View3DInventor::OnChange(*hGrp,"HeadlightColor"); - View3DInventor::OnChange(*hGrp,"HeadlightDirection"); - View3DInventor::OnChange(*hGrp,"HeadlightIntensity"); - View3DInventor::OnChange(*hGrp,"EnableBacklight"); - View3DInventor::OnChange(*hGrp,"BacklightColor"); - View3DInventor::OnChange(*hGrp,"BacklightDirection"); - View3DInventor::OnChange(*hGrp,"BacklightIntensity"); - View3DInventor::OnChange(*hGrp,"NavigationStyle"); - View3DInventor::OnChange(*hGrp,"OrbitStyle"); - View3DInventor::OnChange(*hGrp,"Sensitivity"); - View3DInventor::OnChange(*hGrp,"ResetCursorPosition"); - View3DInventor::OnChange(*hGrp,"DimensionsVisible"); - View3DInventor::OnChange(*hGrp,"Dimensions3dVisible"); - View3DInventor::OnChange(*hGrp,"DimensionsDeltaVisible"); - View3DInventor::OnChange(*hGrp,"PickRadius"); - View3DInventor::OnChange(*hGrp,"TransparentObjectRenderType"); -} - -void View3DInventor::OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::MessageType Reason) -{ - const ParameterGrp& rGrp = static_cast(rCaller); - if (strcmp(Reason,"HeadlightColor") == 0) { - unsigned long headlight = rGrp.GetUnsigned("HeadlightColor",ULONG_MAX); // default color (white) - float transparency; - SbColor headlightColor; - headlightColor.setPackedValue((uint32_t)headlight, transparency); - _viewer->getHeadlight()->color.setValue(headlightColor); - } - else if (strcmp(Reason,"HeadlightDirection") == 0) { - try { - std::string pos = rGrp.GetASCII("HeadlightDirection"); - Base::Vector3f dir = Base::to_vector(pos); - _viewer->getHeadlight()->direction.setValue(dir.x, dir.y, dir.z); - } - catch (const std::exception&) { - // ignore exception - } - } - else if (strcmp(Reason,"HeadlightIntensity") == 0) { - long value = rGrp.GetInt("HeadlightIntensity", 100); - _viewer->getHeadlight()->intensity.setValue((float)value/100.0f); - } - else if (strcmp(Reason,"EnableBacklight") == 0) { - _viewer->setBacklight(rGrp.GetBool("EnableBacklight", false)); - } - else if (strcmp(Reason,"BacklightColor") == 0) { - unsigned long backlight = rGrp.GetUnsigned("BacklightColor",ULONG_MAX); // default color (white) - float transparency; - SbColor backlightColor; - backlightColor.setPackedValue((uint32_t)backlight, transparency); - _viewer->getBacklight()->color.setValue(backlightColor); - } - else if (strcmp(Reason,"BacklightDirection") == 0) { - try { - std::string pos = rGrp.GetASCII("BacklightDirection"); - Base::Vector3f dir = Base::to_vector(pos); - _viewer->getBacklight()->direction.setValue(dir.x, dir.y, dir.z); - } - catch (const std::exception&) { - // ignore exception - } - } - else if (strcmp(Reason,"BacklightIntensity") == 0) { - long value = rGrp.GetInt("BacklightIntensity", 100); - _viewer->getBacklight()->intensity.setValue((float)value/100.0f); - } - else if (strcmp(Reason,"EnablePreselection") == 0) { - const ParameterGrp& rclGrp = ((ParameterGrp&)rCaller); - SoFCEnableHighlightAction cAct(rclGrp.GetBool("EnablePreselection", true)); - cAct.apply(_viewer->getSceneGraph()); - } - else if (strcmp(Reason,"EnableSelection") == 0) { - const ParameterGrp& rclGrp = ((ParameterGrp&)rCaller); - SoFCEnableSelectionAction cAct(rclGrp.GetBool("EnableSelection", true)); - cAct.apply(_viewer->getSceneGraph()); - } - else if (strcmp(Reason,"HighlightColor") == 0) { - float transparency; - SbColor highlightColor(0.8f, 0.1f, 0.1f); - auto highlight = (unsigned long)(highlightColor.getPackedValue()); - highlight = rGrp.GetUnsigned("HighlightColor", highlight); - highlightColor.setPackedValue((uint32_t)highlight, transparency); - SoSFColor col; col.setValue(highlightColor); - SoFCHighlightColorAction cAct(col); - cAct.apply(_viewer->getSceneGraph()); - } - else if (strcmp(Reason,"SelectionColor") == 0) { - float transparency; - SbColor selectionColor(0.1f, 0.8f, 0.1f); - auto selection = (unsigned long)(selectionColor.getPackedValue()); - selection = rGrp.GetUnsigned("SelectionColor", selection); - selectionColor.setPackedValue((uint32_t)selection, transparency); - SoSFColor col; col.setValue(selectionColor); - SoFCSelectionColorAction cAct(col); - cAct.apply(_viewer->getSceneGraph()); - } - else if (strcmp(Reason,"NavigationStyle") == 0) { - // check whether the simple or the full mouse model is used - std::string model = rGrp.GetASCII("NavigationStyle",CADNavigationStyle::getClassTypeId().getName()); - Base::Type type = Base::Type::fromName(model.c_str()); - _viewer->setNavigationType(type); - } - else if (strcmp(Reason,"OrbitStyle") == 0) { - int style = rGrp.GetInt("OrbitStyle",1); - _viewer->navigationStyle()->setOrbitStyle(NavigationStyle::OrbitStyle(style)); - } - else if (strcmp(Reason,"Sensitivity") == 0) { - float val = rGrp.GetFloat("Sensitivity",2.0f); - _viewer->navigationStyle()->setSensitivity(val); - } - else if (strcmp(Reason,"ResetCursorPosition") == 0) { - bool on = rGrp.GetBool("ResetCursorPosition",false); - _viewer->navigationStyle()->setResetCursorPosition(on); - } - else if (strcmp(Reason,"InvertZoom") == 0) { - bool on = rGrp.GetBool("InvertZoom", true); - _viewer->navigationStyle()->setZoomInverted(on); - } - else if (strcmp(Reason,"ZoomAtCursor") == 0) { - bool on = rGrp.GetBool("ZoomAtCursor", true); - _viewer->navigationStyle()->setZoomAtCursor(on); - } - else if (strcmp(Reason,"ZoomStep") == 0) { - float val = rGrp.GetFloat("ZoomStep", 0.0f); - _viewer->navigationStyle()->setZoomStep(val); - } - else if (strcmp(Reason,"RotationMode") == 0) { - long mode = rGrp.GetInt("RotationMode", 1); - if (mode == 0) { - _viewer->navigationStyle()->setRotationCenterMode(NavigationStyle::RotationCenterMode::WindowCenter); - } - else if (mode == 1) { - _viewer->navigationStyle()->setRotationCenterMode(NavigationStyle::RotationCenterMode::ScenePointAtCursor | - NavigationStyle::RotationCenterMode::FocalPointAtCursor); - } - else if (mode == 2) { - _viewer->navigationStyle()->setRotationCenterMode(NavigationStyle::RotationCenterMode::ScenePointAtCursor | - NavigationStyle::RotationCenterMode::BoundingBoxCenter); - } - } - else if (strcmp(Reason,"EyeDistance") == 0) { - _viewer->getSoRenderManager()->setStereoOffset(rGrp.GetFloat("EyeDistance", 5.0)); - } - else if (strcmp(Reason,"CornerCoordSystem") == 0) { - _viewer->setFeedbackVisibility(rGrp.GetBool("CornerCoordSystem", true)); - } - else if (strcmp(Reason,"CornerCoordSystemSize") == 0) { - _viewer->setFeedbackSize(rGrp.GetInt("CornerCoordSystemSize", 10)); - } - else if (strcmp(Reason,"ShowAxisCross") == 0) { - _viewer->setAxisCross(rGrp.GetBool("ShowAxisCross", false)); - } - else if (strcmp(Reason,"UseAutoRotation") == 0) { - _viewer->setAnimationEnabled(rGrp.GetBool("UseAutoRotation", false)); - } - else if (strcmp(Reason,"Gradient") == 0) { - _viewer->setGradientBackground((rGrp.GetBool("Gradient", true))); - } - else if (strcmp(Reason,"ShowFPS") == 0) { - _viewer->setEnabledFPSCounter(rGrp.GetBool("ShowFPS", false)); - } - else if (strcmp(Reason,"ShowNaviCube") == 0) { - _viewer->setEnabledNaviCube(rGrp.GetBool("ShowNaviCube", true)); - } - else if (strcmp(Reason, "CornerNaviCube") == 0) { - _viewer->setNaviCubeCorner(rGrp.GetInt("CornerNaviCube", 1)); - } - else if (strcmp(Reason, "CubeSize") == 0) { - _viewer->getNavigationCube()->setSize(rGrp.GetInt("CubeSize", 132)); - } - else if (strcmp(Reason, "NaviRotateToNearest") == 0) { - _viewer->getNavigationCube()->setNaviRotateToNearest( - rGrp.GetBool("NaviRotateToNearest", true)); - } - else if (strcmp(Reason, "NaviStepByTurn") == 0) { - _viewer->getNavigationCube()->setNaviStepByTurn(rGrp.GetInt("NaviStepByTurn", 8)); - } - else if (strcmp(Reason, "FontSize") == 0) { - _viewer->getNavigationCube()->setFontSize( - rGrp.GetInt("FontSize", _viewer->getNavigationCube()->getDefaultFontSize())); - } - else if (strcmp(Reason, "FontString") == 0) { - std::string font = rGrp.GetASCII( - "FontString", NaviCube::getDefaultSansserifFont().toStdString().c_str()); - _viewer->getNavigationCube()->setFont(font); - } - else if (strcmp(Reason, "TextColor") == 0) { - unsigned long col = rGrp.GetUnsigned("TextColor", 255); - _viewer->getNavigationCube()->setTextColor(App::Color::fromPackedRGBA(col)); - } - else if (strcmp(Reason, "FrontColor") == 0) { - unsigned long col = rGrp.GetUnsigned("FrontColor", 3806916544); - _viewer->getNavigationCube()->setFrontColor(App::Color::fromPackedRGBA(col)); - } - else if (strcmp(Reason, "HiliteColor") == 0) { - unsigned long col = rGrp.GetUnsigned("HiliteColor", 2867003391); - _viewer->getNavigationCube()->setHiliteColor(App::Color::fromPackedRGBA(col)); - } - else if (strcmp(Reason, "ButtonColor") == 0) { - unsigned long col = rGrp.GetUnsigned("ButtonColor", 3806916480); - _viewer->getNavigationCube()->setButtonColor(App::Color::fromPackedRGBA(col)); - } - else if (strcmp(Reason, "BorderWidth") == 0) { - _viewer->getNavigationCube()->setBorderWidth(rGrp.GetFloat("BorderWidth", 1.1)); - } - else if (strcmp(Reason, "BorderColor") == 0) { - unsigned long col = rGrp.GetUnsigned("BorderColor", 842150655); - _viewer->getNavigationCube()->setBorderColor(App::Color::fromPackedRGBA(col)); - } - else if (strcmp(Reason,"UseVBO") == 0) { - _viewer->setEnabledVBO(rGrp.GetBool("UseVBO", false)); - } - else if (strcmp(Reason,"RenderCache") == 0) { - _viewer->setRenderCache(rGrp.GetInt("RenderCache", 0)); - } - else if (strcmp(Reason,"Orthographic") == 0) { - // check whether a perspective or orthogrphic camera should be set - if (rGrp.GetBool("Orthographic", true)) - _viewer->setCameraType(SoOrthographicCamera::getClassTypeId()); - else - _viewer->setCameraType(SoPerspectiveCamera::getClassTypeId()); - } - else if (strcmp(Reason, "DimensionsVisible") == 0) { - if (rGrp.GetBool("DimensionsVisible", true)) - _viewer->turnAllDimensionsOn(); - else - _viewer->turnAllDimensionsOff(); - } - else if (strcmp(Reason, "Dimensions3dVisible") == 0) { - if (rGrp.GetBool("Dimensions3dVisible", true)) - _viewer->turn3dDimensionsOn(); - else - _viewer->turn3dDimensionsOff(); - } - else if (strcmp(Reason, "DimensionsDeltaVisible") == 0) { - if (rGrp.GetBool("DimensionsDeltaVisible", true)) - _viewer->turnDeltaDimensionsOn(); - else - _viewer->turnDeltaDimensionsOff(); - } - else if (strcmp(Reason, "PickRadius") == 0) { - _viewer->setPickRadius(rGrp.GetFloat("PickRadius", 5.0f)); - } - else if (strcmp(Reason, "TransparentObjectRenderType") == 0) { - long renderType = rGrp.GetInt("TransparentObjectRenderType", 0); - if (renderType == 0) { - _viewer->getSoRenderManager()->getGLRenderAction() - ->setTransparentDelayedObjectRenderType(SoGLRenderAction::ONE_PASS); - } - else if (renderType == 1) { - _viewer->getSoRenderManager()->getGLRenderAction() - ->setTransparentDelayedObjectRenderType(SoGLRenderAction::NONSOLID_SEPARATE_BACKFACE_PASS); - } - } - else { - unsigned long col1 = rGrp.GetUnsigned("BackgroundColor",3940932863UL); - unsigned long col2 = rGrp.GetUnsigned("BackgroundColor2",859006463UL); // default color (dark blue) - unsigned long col3 = rGrp.GetUnsigned("BackgroundColor3",2880160255UL); // default color (blue/grey) - unsigned long col4 = rGrp.GetUnsigned("BackgroundColor4",1869583359UL); // default color (blue/grey) - float r1,g1,b1,r2,g2,b2,r3,g3,b3,r4,g4,b4; - r1 = ((col1 >> 24) & 0xff) / 255.0; g1 = ((col1 >> 16) & 0xff) / 255.0; b1 = ((col1 >> 8) & 0xff) / 255.0; - r2 = ((col2 >> 24) & 0xff) / 255.0; g2 = ((col2 >> 16) & 0xff) / 255.0; b2 = ((col2 >> 8) & 0xff) / 255.0; - r3 = ((col3 >> 24) & 0xff) / 255.0; g3 = ((col3 >> 16) & 0xff) / 255.0; b3 = ((col3 >> 8) & 0xff) / 255.0; - r4 = ((col4 >> 24) & 0xff) / 255.0; g4 = ((col4 >> 16) & 0xff) / 255.0; b4 = ((col4 >> 8) & 0xff) / 255.0; - _viewer->setBackgroundColor(QColor::fromRgbF(r1, g1, b1)); - if (!rGrp.GetBool("UseBackgroundColorMid",false)) - _viewer->setGradientBackgroundColor(SbColor(r2, g2, b2), SbColor(r3, g3, b3)); - else - _viewer->setGradientBackgroundColor(SbColor(r2, g2, b2), SbColor(r3, g3, b3), SbColor(r4, g4, b4)); - } + viewSettings = std::make_unique(App::GetApplication().GetParameterGroupByPath + ("User parameter:BaseApp/Preferences/View"), _viewer); + naviSettings = std::make_unique(App::GetApplication().GetParameterGroupByPath + ("User parameter:BaseApp/Preferences/NaviCube"), _viewer); + viewSettings->applySettings(); + naviSettings->applySettings(); } void View3DInventor::onRename(Gui::Document *pDoc) @@ -713,8 +422,8 @@ bool View3DInventor::onMsg(const char* pMsg, const char** ppReturn) getGuiDocument()->saveCopy(); return true; } - else - return false; + + return false; } bool View3DInventor::onHasMsg(const char* pMsg) const @@ -930,7 +639,7 @@ void View3DInventor::windowStateChanged(MDIView* view) if (canStartTimer) { // do a single shot event (maybe insert a checkbox in viewer settings) - int msecs = hGrp->GetInt("stopAnimatingIfDeactivated", 3000); + int msecs = 3000; //hGrp->GetInt("stopAnimatingIfDeactivated", 3000); if (!stopSpinTimer->isActive() && msecs >= 0) { // if < 0 do not stop rotation stopSpinTimer->setSingleShot(true); stopSpinTimer->start(msecs); diff --git a/src/Gui/View3DInventor.h b/src/Gui/View3DInventor.h index 793a1d97a1..209a7f9c7d 100644 --- a/src/Gui/View3DInventor.h +++ b/src/Gui/View3DInventor.h @@ -23,6 +23,7 @@ #ifndef GUI_VIEW3DINVENTOR_H #define GUI_VIEW3DINVENTOR_H +#include #include #include @@ -39,6 +40,8 @@ namespace Gui { class Document; class View3DInventorViewer; class View3DPy; +class View3DSettings; +class NaviCubeSettings; class GuiExport GLOverlayWidget : public QWidget { @@ -61,7 +64,7 @@ protected: * It consists out of the 3D view * \author Juergen Riegel */ -class GuiExport View3DInventor : public MDIView, public ParameterGrp::ObserverType +class GuiExport View3DInventor : public MDIView { Q_OBJECT @@ -75,8 +78,6 @@ public: bool onMsg(const char* pMsg, const char** ppReturn) override; bool onHasMsg(const char* pMsg) const override; void deleteSelf() override; - /// Observer message from the ParameterGrp - void OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::MessageType Reason) override; /// get called when the document is updated void onRename(Gui::Document *pDoc) override; void onUpdate() override; @@ -134,15 +135,13 @@ protected: void customEvent (QEvent * e) override; void contextMenuEvent (QContextMenuEvent*e) override; - /// handle to the viewer parameter group - ParameterGrp::handle hGrp; - ParameterGrp::handle hGrpNavi; - private: View3DInventorViewer * _viewer; PyObject *_viewerPy; QTimer * stopSpinTimer; QStackedWidget* stack; + std::unique_ptr viewSettings; + std::unique_ptr naviSettings; // friends friend class View3DPy; diff --git a/src/Gui/View3DSettings.cpp b/src/Gui/View3DSettings.cpp new file mode 100644 index 0000000000..72dd689be4 --- /dev/null +++ b/src/Gui/View3DSettings.cpp @@ -0,0 +1,370 @@ +/*************************************************************************** + * Copyright (c) 2023 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library 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 Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + +#include "PreCompiled.h" + +#ifndef _PreComp_ +# include +# include +# include +# include +#endif + +#include +#include + +#include "NaviCube.h" +#include "NavigationStyle.h" +#include "SoFCSelectionAction.h" +#include "View3DSettings.h" +#include "View3DInventorViewer.h" + +using namespace Gui; + +View3DSettings::View3DSettings(ParameterGrp::handle hGrp, + View3DInventorViewer* view) + : hGrp(hGrp) + , _viewer(view) +{ + hGrp->Attach(this); +} + +View3DSettings::~View3DSettings() +{ + hGrp->Detach(this); +} + +void View3DSettings::applySettings() +{ + // apply the user settings + OnChange(*hGrp,"EyeDistance"); + OnChange(*hGrp,"CornerCoordSystem"); + OnChange(*hGrp,"CornerCoordSystemSize"); + OnChange(*hGrp,"ShowAxisCross"); + OnChange(*hGrp,"UseAutoRotation"); + OnChange(*hGrp,"Gradient"); + OnChange(*hGrp,"BackgroundColor"); + OnChange(*hGrp,"BackgroundColor2"); + OnChange(*hGrp,"BackgroundColor3"); + OnChange(*hGrp,"BackgroundColor4"); + OnChange(*hGrp,"UseBackgroundColorMid"); + OnChange(*hGrp,"ShowFPS"); + OnChange(*hGrp,"ShowNaviCube"); + OnChange(*hGrp,"UseVBO"); + OnChange(*hGrp,"RenderCache"); + OnChange(*hGrp,"Orthographic"); + OnChange(*hGrp,"HeadlightColor"); + OnChange(*hGrp,"HeadlightDirection"); + OnChange(*hGrp,"HeadlightIntensity"); + OnChange(*hGrp,"EnableBacklight"); + OnChange(*hGrp,"BacklightColor"); + OnChange(*hGrp,"BacklightDirection"); + OnChange(*hGrp,"BacklightIntensity"); + OnChange(*hGrp,"NavigationStyle"); + OnChange(*hGrp,"OrbitStyle"); + OnChange(*hGrp,"Sensitivity"); + OnChange(*hGrp,"ResetCursorPosition"); + OnChange(*hGrp,"DimensionsVisible"); + OnChange(*hGrp,"Dimensions3dVisible"); + OnChange(*hGrp,"DimensionsDeltaVisible"); + OnChange(*hGrp,"PickRadius"); + OnChange(*hGrp,"TransparentObjectRenderType"); +} + +void View3DSettings::OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::MessageType Reason) +{ + const ParameterGrp& rGrp = static_cast(rCaller); + if (strcmp(Reason,"HeadlightColor") == 0) { + unsigned long headlight = rGrp.GetUnsigned("HeadlightColor",ULONG_MAX); // default color (white) + float transparency; + SbColor headlightColor; + headlightColor.setPackedValue((uint32_t)headlight, transparency); + _viewer->getHeadlight()->color.setValue(headlightColor); + } + else if (strcmp(Reason,"HeadlightDirection") == 0) { + try { + std::string pos = rGrp.GetASCII("HeadlightDirection"); + Base::Vector3f dir = Base::to_vector(pos); + _viewer->getHeadlight()->direction.setValue(dir.x, dir.y, dir.z); + } + catch (const std::exception&) { + // ignore exception + } + } + else if (strcmp(Reason,"HeadlightIntensity") == 0) { + long value = rGrp.GetInt("HeadlightIntensity", 100); + _viewer->getHeadlight()->intensity.setValue((float)value/100.0f); + } + else if (strcmp(Reason,"EnableBacklight") == 0) { + _viewer->setBacklight(rGrp.GetBool("EnableBacklight", false)); + } + else if (strcmp(Reason,"BacklightColor") == 0) { + unsigned long backlight = rGrp.GetUnsigned("BacklightColor",ULONG_MAX); // default color (white) + float transparency; + SbColor backlightColor; + backlightColor.setPackedValue((uint32_t)backlight, transparency); + _viewer->getBacklight()->color.setValue(backlightColor); + } + else if (strcmp(Reason,"BacklightDirection") == 0) { + try { + std::string pos = rGrp.GetASCII("BacklightDirection"); + Base::Vector3f dir = Base::to_vector(pos); + _viewer->getBacklight()->direction.setValue(dir.x, dir.y, dir.z); + } + catch (const std::exception&) { + // ignore exception + } + } + else if (strcmp(Reason,"BacklightIntensity") == 0) { + long value = rGrp.GetInt("BacklightIntensity", 100); + _viewer->getBacklight()->intensity.setValue((float)value/100.0f); + } + else if (strcmp(Reason,"EnablePreselection") == 0) { + const ParameterGrp& rclGrp = ((ParameterGrp&)rCaller); + SoFCEnableHighlightAction cAct(rclGrp.GetBool("EnablePreselection", true)); + cAct.apply(_viewer->getSceneGraph()); + } + else if (strcmp(Reason,"EnableSelection") == 0) { + const ParameterGrp& rclGrp = ((ParameterGrp&)rCaller); + SoFCEnableSelectionAction cAct(rclGrp.GetBool("EnableSelection", true)); + cAct.apply(_viewer->getSceneGraph()); + } + else if (strcmp(Reason,"HighlightColor") == 0) { + float transparency; + SbColor highlightColor(0.8f, 0.1f, 0.1f); + auto highlight = (unsigned long)(highlightColor.getPackedValue()); + highlight = rGrp.GetUnsigned("HighlightColor", highlight); + highlightColor.setPackedValue((uint32_t)highlight, transparency); + SoSFColor col; col.setValue(highlightColor); + SoFCHighlightColorAction cAct(col); + cAct.apply(_viewer->getSceneGraph()); + } + else if (strcmp(Reason,"SelectionColor") == 0) { + float transparency; + SbColor selectionColor(0.1f, 0.8f, 0.1f); + auto selection = (unsigned long)(selectionColor.getPackedValue()); + selection = rGrp.GetUnsigned("SelectionColor", selection); + selectionColor.setPackedValue((uint32_t)selection, transparency); + SoSFColor col; col.setValue(selectionColor); + SoFCSelectionColorAction cAct(col); + cAct.apply(_viewer->getSceneGraph()); + } + else if (strcmp(Reason,"NavigationStyle") == 0) { + // check whether the simple or the full mouse model is used + std::string model = rGrp.GetASCII("NavigationStyle",CADNavigationStyle::getClassTypeId().getName()); + Base::Type type = Base::Type::fromName(model.c_str()); + _viewer->setNavigationType(type); + } + else if (strcmp(Reason,"OrbitStyle") == 0) { + int style = rGrp.GetInt("OrbitStyle",1); + _viewer->navigationStyle()->setOrbitStyle(NavigationStyle::OrbitStyle(style)); + } + else if (strcmp(Reason,"Sensitivity") == 0) { + float val = rGrp.GetFloat("Sensitivity",2.0f); + _viewer->navigationStyle()->setSensitivity(val); + } + else if (strcmp(Reason,"ResetCursorPosition") == 0) { + bool on = rGrp.GetBool("ResetCursorPosition",false); + _viewer->navigationStyle()->setResetCursorPosition(on); + } + else if (strcmp(Reason,"InvertZoom") == 0) { + bool on = rGrp.GetBool("InvertZoom", true); + _viewer->navigationStyle()->setZoomInverted(on); + } + else if (strcmp(Reason,"ZoomAtCursor") == 0) { + bool on = rGrp.GetBool("ZoomAtCursor", true); + _viewer->navigationStyle()->setZoomAtCursor(on); + } + else if (strcmp(Reason,"ZoomStep") == 0) { + float val = rGrp.GetFloat("ZoomStep", 0.0f); + _viewer->navigationStyle()->setZoomStep(val); + } + else if (strcmp(Reason,"RotationMode") == 0) { + long mode = rGrp.GetInt("RotationMode", 1); + if (mode == 0) { + _viewer->navigationStyle()->setRotationCenterMode(NavigationStyle::RotationCenterMode::WindowCenter); + } + else if (mode == 1) { + _viewer->navigationStyle()->setRotationCenterMode(NavigationStyle::RotationCenterMode::ScenePointAtCursor | + NavigationStyle::RotationCenterMode::FocalPointAtCursor); + } + else if (mode == 2) { + _viewer->navigationStyle()->setRotationCenterMode(NavigationStyle::RotationCenterMode::ScenePointAtCursor | + NavigationStyle::RotationCenterMode::BoundingBoxCenter); + } + } + else if (strcmp(Reason,"EyeDistance") == 0) { + _viewer->getSoRenderManager()->setStereoOffset(rGrp.GetFloat("EyeDistance", 5.0)); + } + else if (strcmp(Reason,"CornerCoordSystem") == 0) { + _viewer->setFeedbackVisibility(rGrp.GetBool("CornerCoordSystem", true)); + } + else if (strcmp(Reason,"CornerCoordSystemSize") == 0) { + _viewer->setFeedbackSize(rGrp.GetInt("CornerCoordSystemSize", 10)); + } + else if (strcmp(Reason,"ShowAxisCross") == 0) { + _viewer->setAxisCross(rGrp.GetBool("ShowAxisCross", false)); + } + else if (strcmp(Reason,"UseAutoRotation") == 0) { + _viewer->setAnimationEnabled(rGrp.GetBool("UseAutoRotation", false)); + } + else if (strcmp(Reason,"Gradient") == 0) { + _viewer->setGradientBackground((rGrp.GetBool("Gradient", true))); + } + else if (strcmp(Reason,"ShowFPS") == 0) { + _viewer->setEnabledFPSCounter(rGrp.GetBool("ShowFPS", false)); + } + else if (strcmp(Reason,"ShowNaviCube") == 0) { + _viewer->setEnabledNaviCube(rGrp.GetBool("ShowNaviCube", true)); + } + else if (strcmp(Reason,"UseVBO") == 0) { + _viewer->setEnabledVBO(rGrp.GetBool("UseVBO", false)); + } + else if (strcmp(Reason,"RenderCache") == 0) { + _viewer->setRenderCache(rGrp.GetInt("RenderCache", 0)); + } + else if (strcmp(Reason,"Orthographic") == 0) { + // check whether a perspective or orthogrphic camera should be set + if (rGrp.GetBool("Orthographic", true)) + _viewer->setCameraType(SoOrthographicCamera::getClassTypeId()); + else + _viewer->setCameraType(SoPerspectiveCamera::getClassTypeId()); + } + else if (strcmp(Reason, "DimensionsVisible") == 0) { + if (rGrp.GetBool("DimensionsVisible", true)) + _viewer->turnAllDimensionsOn(); + else + _viewer->turnAllDimensionsOff(); + } + else if (strcmp(Reason, "Dimensions3dVisible") == 0) { + if (rGrp.GetBool("Dimensions3dVisible", true)) + _viewer->turn3dDimensionsOn(); + else + _viewer->turn3dDimensionsOff(); + } + else if (strcmp(Reason, "DimensionsDeltaVisible") == 0) { + if (rGrp.GetBool("DimensionsDeltaVisible", true)) + _viewer->turnDeltaDimensionsOn(); + else + _viewer->turnDeltaDimensionsOff(); + } + else if (strcmp(Reason, "PickRadius") == 0) { + _viewer->setPickRadius(rGrp.GetFloat("PickRadius", 5.0f)); + } + else if (strcmp(Reason, "TransparentObjectRenderType") == 0) { + long renderType = rGrp.GetInt("TransparentObjectRenderType", 0); + if (renderType == 0) { + _viewer->getSoRenderManager()->getGLRenderAction() + ->setTransparentDelayedObjectRenderType(SoGLRenderAction::ONE_PASS); + } + else if (renderType == 1) { + _viewer->getSoRenderManager()->getGLRenderAction() + ->setTransparentDelayedObjectRenderType(SoGLRenderAction::NONSOLID_SEPARATE_BACKFACE_PASS); + } + } + else { + unsigned long col1 = rGrp.GetUnsigned("BackgroundColor",3940932863UL); + unsigned long col2 = rGrp.GetUnsigned("BackgroundColor2",859006463UL); // default color (dark blue) + unsigned long col3 = rGrp.GetUnsigned("BackgroundColor3",2880160255UL); // default color (blue/grey) + unsigned long col4 = rGrp.GetUnsigned("BackgroundColor4",1869583359UL); // default color (blue/grey) + float r1,g1,b1,r2,g2,b2,r3,g3,b3,r4,g4,b4; + r1 = ((col1 >> 24) & 0xff) / 255.0; g1 = ((col1 >> 16) & 0xff) / 255.0; b1 = ((col1 >> 8) & 0xff) / 255.0; + r2 = ((col2 >> 24) & 0xff) / 255.0; g2 = ((col2 >> 16) & 0xff) / 255.0; b2 = ((col2 >> 8) & 0xff) / 255.0; + r3 = ((col3 >> 24) & 0xff) / 255.0; g3 = ((col3 >> 16) & 0xff) / 255.0; b3 = ((col3 >> 8) & 0xff) / 255.0; + r4 = ((col4 >> 24) & 0xff) / 255.0; g4 = ((col4 >> 16) & 0xff) / 255.0; b4 = ((col4 >> 8) & 0xff) / 255.0; + _viewer->setBackgroundColor(QColor::fromRgbF(r1, g1, b1)); + if (!rGrp.GetBool("UseBackgroundColorMid",false)) + _viewer->setGradientBackgroundColor(SbColor(r2, g2, b2), SbColor(r3, g3, b3)); + else + _viewer->setGradientBackgroundColor(SbColor(r2, g2, b2), SbColor(r3, g3, b3), SbColor(r4, g4, b4)); + } +} + +// ---------------------------------------------------------------------------- + +NaviCubeSettings::NaviCubeSettings(ParameterGrp::handle hGrp, + View3DInventorViewer* view) + : hGrp(hGrp) + , _viewer(view) +{ + hGrp->Attach(this); +} + +NaviCubeSettings::~NaviCubeSettings() +{ + hGrp->Detach(this); +} + +void NaviCubeSettings::applySettings() +{ + OnChange(*hGrp, "CornerNaviCube"); +} + +void NaviCubeSettings::OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::MessageType Reason) +{ + const ParameterGrp& rGrp = static_cast(rCaller); + if (strcmp(Reason, "CornerNaviCube") == 0) { + _viewer->setNaviCubeCorner(rGrp.GetInt("CornerNaviCube", 1)); + } + else if (strcmp(Reason, "CubeSize") == 0) { + _viewer->getNavigationCube()->setSize(rGrp.GetInt("CubeSize", 132)); + } + else if (strcmp(Reason, "NaviRotateToNearest") == 0) { + _viewer->getNavigationCube()->setNaviRotateToNearest( + rGrp.GetBool("NaviRotateToNearest", true)); + } + else if (strcmp(Reason, "NaviStepByTurn") == 0) { + _viewer->getNavigationCube()->setNaviStepByTurn(rGrp.GetInt("NaviStepByTurn", 8)); + } + else if (strcmp(Reason, "FontSize") == 0) { + _viewer->getNavigationCube()->setFontSize( + rGrp.GetInt("FontSize", _viewer->getNavigationCube()->getDefaultFontSize())); + } + else if (strcmp(Reason, "FontString") == 0) { + std::string font = rGrp.GetASCII( + "FontString", NaviCube::getDefaultSansserifFont().toStdString().c_str()); + _viewer->getNavigationCube()->setFont(font); + } + else if (strcmp(Reason, "TextColor") == 0) { + unsigned long col = rGrp.GetUnsigned("TextColor", 255); + _viewer->getNavigationCube()->setTextColor(App::Color::fromPackedRGBA(col)); + } + else if (strcmp(Reason, "FrontColor") == 0) { + unsigned long col = rGrp.GetUnsigned("FrontColor", 3806916544); + _viewer->getNavigationCube()->setFrontColor(App::Color::fromPackedRGBA(col)); + } + else if (strcmp(Reason, "HiliteColor") == 0) { + unsigned long col = rGrp.GetUnsigned("HiliteColor", 2867003391); + _viewer->getNavigationCube()->setHiliteColor(App::Color::fromPackedRGBA(col)); + } + else if (strcmp(Reason, "ButtonColor") == 0) { + unsigned long col = rGrp.GetUnsigned("ButtonColor", 3806916480); + _viewer->getNavigationCube()->setButtonColor(App::Color::fromPackedRGBA(col)); + } + else if (strcmp(Reason, "BorderWidth") == 0) { + _viewer->getNavigationCube()->setBorderWidth(rGrp.GetFloat("BorderWidth", 1.1)); + } + else if (strcmp(Reason, "BorderColor") == 0) { + unsigned long col = rGrp.GetUnsigned("BorderColor", 842150655); + _viewer->getNavigationCube()->setBorderColor(App::Color::fromPackedRGBA(col)); + } +} diff --git a/src/Gui/View3DSettings.h b/src/Gui/View3DSettings.h new file mode 100644 index 0000000000..19125db358 --- /dev/null +++ b/src/Gui/View3DSettings.h @@ -0,0 +1,63 @@ +/*************************************************************************** + * Copyright (c) 2023 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library 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 Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + +#ifndef GUI_VIEW3DSETTINGS_H +#define GUI_VIEW3DSETTINGS_H + +#include + +namespace Gui { +class View3DInventorViewer; + +class View3DSettings : public ParameterGrp::ObserverType +{ +public: + View3DSettings(ParameterGrp::handle hGrp, View3DInventorViewer *); + ~View3DSettings() override; + + /// Observer message from the ParameterGrp + void OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::MessageType Reason) override; + void applySettings(); + +private: + ParameterGrp::handle hGrp; + View3DInventorViewer * _viewer; +}; + +class NaviCubeSettings : public ParameterGrp::ObserverType +{ +public: + NaviCubeSettings(ParameterGrp::handle hGrp, View3DInventorViewer *); + ~NaviCubeSettings() override; + + /// Observer message from the ParameterGrp + void OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::MessageType Reason) override; + void applySettings(); + +private: + ParameterGrp::handle hGrp; + View3DInventorViewer * _viewer; +}; + +} // namespace Gui + +#endif // GUI_VIEW3DSETTINGS_H