Gui: allow to change background color and gradient from Python

This commit is contained in:
wmayer
2023-04-04 18:02:16 +02:00
committed by wwmayer
parent eb3fd5fe47
commit 1f3e35e95c
2 changed files with 94 additions and 4 deletions

View File

@@ -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<float>(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) {

View File

@@ -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);