Gui: refactor View3DInventor and move handling of settings to separate classes

This commit is contained in:
wmayer
2023-02-26 14:48:32 +01:00
committed by wwmayer
parent 07dbe74c34
commit 64ce9ec0a4
6 changed files with 456 additions and 311 deletions

View File

@@ -43,21 +43,19 @@
# include <QUrl>
# include <QWindow>
# include <Inventor/actions/SoGetPrimitiveCountAction.h>
# include <Inventor/fields/SoSFColor.h>
# include <Inventor/fields/SoSFString.h>
# include <Inventor/nodes/SoDirectionalLight.h>
# include <Inventor/nodes/SoOrthographicCamera.h>
# include <Inventor/nodes/SoPerspectiveCamera.h>
# include <Inventor/nodes/SoSeparator.h>
#endif
#include <App/Color.h>
#include <App/Document.h>
#include <Base/Builder3D.h>
#include <Base/Console.h>
#include <Base/Interpreter.h>
#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<ParameterGrp&>(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<QColor>(col));
}
else if (strcmp(Reason, "FrontColor") == 0) {
unsigned long col = rGrp.GetUnsigned("FrontColor", 3806916544);
_viewer->getNavigationCube()->setFrontColor(App::Color::fromPackedRGBA<QColor>(col));
}
else if (strcmp(Reason, "HiliteColor") == 0) {
unsigned long col = rGrp.GetUnsigned("HiliteColor", 2867003391);
_viewer->getNavigationCube()->setHiliteColor(App::Color::fromPackedRGBA<QColor>(col));
}
else if (strcmp(Reason, "ButtonColor") == 0) {
unsigned long col = rGrp.GetUnsigned("ButtonColor", 3806916480);
_viewer->getNavigationCube()->setButtonColor(App::Color::fromPackedRGBA<QColor>(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<QColor>(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<View3DSettings>(App::GetApplication().GetParameterGroupByPath
("User parameter:BaseApp/Preferences/View"), _viewer);
naviSettings = std::make_unique<NaviCubeSettings>(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);