From 1f3e35e95c52cf94dfde6a5aab7d6f2acc984e01 Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 4 Apr 2023 18:02:16 +0200 Subject: [PATCH] Gui: allow to change background color and gradient from Python --- src/Gui/View3DViewerPy.cpp | 96 ++++++++++++++++++++++++++++++++++++-- src/Gui/View3DViewerPy.h | 2 + 2 files changed, 94 insertions(+), 4 deletions(-) diff --git a/src/Gui/View3DViewerPy.cpp b/src/Gui/View3DViewerPy.cpp index d24a2ab063..2fa740d948 100644 --- a/src/Gui/View3DViewerPy.cpp +++ b/src/Gui/View3DViewerPy.cpp @@ -85,6 +85,10 @@ void View3DInventorViewerPy::init_type() "resetEditingRoot(updateLinks=True): restore the editing ViewProvider's root node"); add_varargs_method("setBackgroundColor", &View3DInventorViewerPy::setBackgroundColor, "setBackgroundColor(r,g,b): sets the background color of the current viewer."); + add_varargs_method("setGradientBackground", &View3DInventorViewerPy::setGradientBackground, + "setGradientBackground(str): sets the background gradient of the current viewer."); + add_varargs_method("setGradientBackgroundColor", &View3DInventorViewerPy::setGradientBackgroundColor, + "setGradientBackgroundColor(tuple,tuple,[tuple]): sets the gradient colors of the current viewer."); add_varargs_method("setRedirectToSceneGraph", &View3DInventorViewerPy::setRedirectToSceneGraph, "setRedirectToSceneGraph(bool): enables or disables to redirect events directly to the scene graph."); add_varargs_method("isRedirectedToSceneGraph", &View3DInventorViewerPy::isRedirectedToSceneGraph, @@ -429,15 +433,99 @@ Py::Object View3DInventorViewerPy::resetEditingRoot(const Py::Tuple& args) } } -Py::Object View3DInventorViewerPy::setBackgroundColor(const Py::Tuple& args) +Py::Object View3DInventorViewerPy::setGradientBackground(const Py::Tuple& args) { - float r,g,b = 0.0; - if (!PyArg_ParseTuple(args.ptr(), "fff", &r, &g, &b)) { + const char* background; + if (!PyArg_ParseTuple(args.ptr(), "s", &background)) { throw Py::Exception(); } try { - SbColor col(r,g,b); + View3DInventorViewer::Background gradient = View3DInventorViewer::Background::NoGradient; + if (strcmp(background, "LINEAR") == 0) { + gradient = View3DInventorViewer::Background::LinearGradient; + } + else if (strcmp(background, "RADIAL") == 0) { + gradient = View3DInventorViewer::Background::RadialGradient; + } + + _viewer->setGradientBackground(gradient); + _viewer->redraw(); + return Py::None(); + } + catch (const Base::Exception& e) { + throw Py::RuntimeError(e.what()); + } + catch (const std::exception& e) { + throw Py::RuntimeError(e.what()); + } + catch(...) { + throw Py::RuntimeError("Unknown C++ exception"); + } +} + +Py::Object View3DInventorViewerPy::setGradientBackgroundColor(const Py::Tuple& args) +{ + PyObject* col1; + PyObject* col2; + PyObject* col3 = nullptr; + if (!PyArg_ParseTuple(args.ptr(), "O!O!|O!", + &PyTuple_Type, &col1, + &PyTuple_Type, &col2, + &PyTuple_Type, &col3)) { + throw Py::Exception(); + } + + auto tupleToColor = [](PyObject* col) { + SbColor color; + Py::Tuple tuple(col); + for (int i=0; i<3; i++) { + color[i] = static_cast(Py::Float(tuple[i])); + } + + return color; + }; + + try { + SbColor midColor(-1, -1, -1); + SbColor fromColor = tupleToColor(col1); + SbColor toColor = tupleToColor(col2); + if (col3) { + midColor = tupleToColor(col3); + } + + _viewer->setGradientBackgroundColor(fromColor, toColor, midColor); + _viewer->redraw(); + return Py::None(); + } + catch (const Base::Exception& e) { + throw Py::RuntimeError(e.what()); + } + catch (const std::exception& e) { + throw Py::RuntimeError(e.what()); + } + catch(...) { + throw Py::RuntimeError("Unknown C++ exception"); + } +} + +Py::Object View3DInventorViewerPy::setBackgroundColor(const Py::Tuple& args) +{ + float red, green, blue = 0.0; + if (!PyArg_ParseTuple(args.ptr(), "fff", &red, &green, &blue)) { + throw Py::Exception(); + } + try { + SbColor col(red, green, blue); _viewer->setGradientBackgroundColor(col, col); + + QColor qtcolor; + qtcolor.setRedF(red); + qtcolor.setGreenF(green); + qtcolor.setBlueF(blue); + if (qtcolor.isValid()) { + _viewer->setBackgroundColor(qtcolor); + } + _viewer->redraw(); return Py::None(); } catch (const Base::Exception& e) { diff --git a/src/Gui/View3DViewerPy.h b/src/Gui/View3DViewerPy.h index b70d9243fd..e157c30be8 100644 --- a/src/Gui/View3DViewerPy.h +++ b/src/Gui/View3DViewerPy.h @@ -67,6 +67,8 @@ public: Py::Object setupEditingRoot(const Py::Tuple &args); Py::Object resetEditingRoot(const Py::Tuple &args); + Py::Object setGradientBackground(const Py::Tuple& args); + Py::Object setGradientBackgroundColor(const Py::Tuple& args); Py::Object setBackgroundColor(const Py::Tuple& args); Py::Object setRedirectToSceneGraph(const Py::Tuple& args); Py::Object isRedirectedToSceneGraph(const Py::Tuple& args);