Gui: add enum types to handle background gradients

This fixes some API flaws where booleans were used to distinguish between three different types
This commit is contained in:
wmayer
2023-04-04 17:12:14 +02:00
committed by wwmayer
parent 21b2f97ab0
commit eb3fd5fe47
6 changed files with 97 additions and 52 deletions

View File

@@ -66,7 +66,7 @@ SoFCBackgroundGradient::SoFCBackgroundGradient()
fCol.setValue(0.5f, 0.5f, 0.8f);
tCol.setValue(0.7f, 0.7f, 0.9f);
mCol.setValue(1.0f, 1.0f, 1.0f);
radial = false;
gradient = Gradient::LINEAR;
}
/*!
@@ -96,7 +96,7 @@ void SoFCBackgroundGradient::GLRender (SoGLRenderAction * /*action*/)
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
if (!radial) { // linear gradient
if (gradient == Gradient::LINEAR) {
glBegin(GL_TRIANGLE_STRIP);
if (mCol[0] < 0) {
glColor3f(fCol[0],fCol[1],fCol[2]); glVertex2f(-1, 1);
@@ -152,23 +152,29 @@ void SoFCBackgroundGradient::GLRender (SoGLRenderAction * /*action*/)
glMatrixMode(GL_MODELVIEW);
}
void SoFCBackgroundGradient::setGradient(SoFCBackgroundGradient::Gradient grad)
{
gradient = grad;
}
SoFCBackgroundGradient::Gradient SoFCBackgroundGradient::getGradient() const
{
return gradient;
}
void SoFCBackgroundGradient::setColorGradient(const SbColor& fromColor,
const SbColor& toColor,
bool isRadial)
const SbColor& toColor)
{
fCol = fromColor;
tCol = toColor;
mCol[0] = -1.0f;
radial = isRadial;
}
void SoFCBackgroundGradient::setColorGradient(const SbColor& fromColor,
const SbColor& toColor,
const SbColor& midColor,
bool isRadial)
const SbColor& midColor)
{
fCol = fromColor;
tCol = toColor;
mCol = midColor;
radial = isRadial;
}

View File

@@ -40,21 +40,25 @@ class GuiExport SoFCBackgroundGradient : public SoNode {
SO_NODE_HEADER(Gui::SoFCBackgroundGradient);
public:
enum Gradient {
LINEAR = 0,
RADIAL = 1
};
static void initClass();
static void finish();
SoFCBackgroundGradient();
void GLRender (SoGLRenderAction *action);
void setGradient(Gradient grad);
Gradient getGradient() const;
void setColorGradient(const SbColor& fromColor,
const SbColor& toColor);
void setColorGradient(const SbColor& fromColor,
const SbColor& toColor,
bool isRadial);
void setColorGradient(const SbColor& fromColor,
const SbColor& toColor,
const SbColor& midColor,
bool isRadial);
const SbColor& midColor);
private:
bool radial;
Gradient gradient;
protected:
virtual ~SoFCBackgroundGradient();

View File

@@ -467,7 +467,7 @@ void View3DInventorViewer::init()
setViewing(false);
setBackgroundColor(QColor(25, 25, 25));
setGradientBackground(true);
setGradientBackground(Background::LinearGradient);
// set some callback functions for user interaction
addStartCallback(interactionStartCB);
@@ -1070,32 +1070,53 @@ void View3DInventorViewer::handleEventCB(void* ud, SoEventCallback* n)
SoGLWidgetElement::set(action->getState(), qobject_cast<QtGLWidget*>(that->getGLWidget()));
}
void View3DInventorViewer::setGradientBackground(bool on)
void View3DInventorViewer::setGradientBackground(View3DInventorViewer::Background grad)
{
if (on && backgroundroot->findChild(pcBackGround) == -1)
backgroundroot->addChild(pcBackGround);
else if (!on && backgroundroot->findChild(pcBackGround) != -1)
backgroundroot->removeChild(pcBackGround);
switch (grad) {
case Background::NoGradient:
if (backgroundroot->findChild(pcBackGround) != -1) {
backgroundroot->removeChild(pcBackGround);
}
break;
case Background::LinearGradient:
pcBackGround->setGradient(SoFCBackgroundGradient::LINEAR);
if (backgroundroot->findChild(pcBackGround) == -1) {
backgroundroot->addChild(pcBackGround);
}
break;
case Background::RadialGradient:
pcBackGround->setGradient(SoFCBackgroundGradient::RADIAL);
if (backgroundroot->findChild(pcBackGround) == -1) {
backgroundroot->addChild(pcBackGround);
}
break;
}
}
bool View3DInventorViewer::hasGradientBackground() const
View3DInventorViewer::Background View3DInventorViewer::getGradientBackground() const
{
return (backgroundroot->findChild(pcBackGround) != -1);
if (backgroundroot->findChild(pcBackGround) == -1) {
return Background::NoGradient;
}
if (pcBackGround->getGradient() == SoFCBackgroundGradient::LINEAR) {
return Background::LinearGradient;
}
return Background::RadialGradient;
}
void View3DInventorViewer::setGradientBackgroundColor(const SbColor& fromColor,
const SbColor& toColor)
{
pcBackGround->setColorGradient(fromColor, toColor);
}
void View3DInventorViewer::setGradientBackgroundColor(const SbColor& fromColor,
const SbColor& toColor,
bool isRadial)
const SbColor& midColor)
{
pcBackGround->setColorGradient(fromColor, toColor, isRadial);
}
void View3DInventorViewer::setGradientBackgroundColor(const SbColor& fromColor,
const SbColor& toColor,
const SbColor& midColor,
bool isRadial)
{
pcBackGround->setColorGradient(fromColor, toColor, midColor, isRadial);
pcBackGround->setColorGradient(fromColor, toColor, midColor);
}
void View3DInventorViewer::setEnabledFPSCounter(bool on)
@@ -1880,7 +1901,7 @@ void View3DInventorViewer::imageFromFramebuffer(int width, int height, int sampl
QtGLFramebufferObject fbo(width, height, fboFormat);
const QColor col = backgroundColor();
bool on = hasGradientBackground();
auto grad = getGradientBackground();
int alpha = 255;
QColor bgopaque = bgcolor;
@@ -1890,12 +1911,12 @@ void View3DInventorViewer::imageFromFramebuffer(int width, int height, int sampl
if (alpha < 255)
bgopaque.setRgb(255,255,255);
setBackgroundColor(bgopaque);
setGradientBackground(false);
setGradientBackground(Background::NoGradient);
}
renderToFramebuffer(&fbo);
setBackgroundColor(col);
setGradientBackground(on);
setGradientBackground(grad);
img = fbo.toImage();
// if background color isn't opaque manipulate the image

View File

@@ -133,6 +133,16 @@ public:
};
//@}
/** @name Background
*/
//@{
enum Background {
NoGradient,
LinearGradient,
RadialGradient
};
//@}
explicit View3DInventorViewer (QWidget *parent, const QtGLWidget* sharewidget = nullptr);
View3DInventorViewer (const QtGLFormat& format, QWidget *parent, const QtGLWidget* sharewidget = nullptr);
~View3DInventorViewer() override;
@@ -370,15 +380,13 @@ public:
*/
void viewSelection();
void setGradientBackground(bool b);
bool hasGradientBackground() const;
void setGradientBackground(Background);
Background getGradientBackground() const;
void setGradientBackgroundColor(const SbColor& fromColor,
const SbColor& toColor);
void setGradientBackgroundColor(const SbColor& fromColor,
const SbColor& toColor,
bool isRadial);
void setGradientBackgroundColor(const SbColor& fromColor,
const SbColor& toColor,
const SbColor& midColor,
bool isRadial);
const SbColor& midColor);
void setNavigationType(Base::Type);
void setAxisCross(bool b);

View File

@@ -293,9 +293,15 @@ void View3DSettings::OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::M
}
}
else if (strcmp(Reason,"Gradient") == 0 || strcmp(Reason,"RadialGradient") == 0) {
View3DInventorViewer::Background background = View3DInventorViewer::Background::NoGradient;
if (rGrp.GetBool("Gradient", true)) {
background = View3DInventorViewer::Background::LinearGradient;
}
else if (rGrp.GetBool("RadialGradient", false)) {
background = View3DInventorViewer::Background::RadialGradient;
}
for (auto _viewer : _viewers) {
_viewer->setGradientBackground(rGrp.GetBool("Gradient", true) ||
rGrp.GetBool("RadialGradient", false));
_viewer->setGradientBackground(background);
}
}
else if (strcmp(Reason,"ShowFPS") == 0) {
@@ -412,15 +418,15 @@ void View3DSettings::OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::M
r4 = ((col4 >> 24) & 0xff) / 255.0; g4 = ((col4 >> 16) & 0xff) / 255.0; b4 = ((col4 >> 8) & 0xff) / 255.0;
for (auto _viewer : _viewers) {
_viewer->setBackgroundColor(QColor::fromRgbF(r1, g1, b1));
if (!rGrp.GetBool("UseBackgroundColorMid",false))
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),
rGrp.GetBool("RadialGradient", false));
else
_viewer->setGradientBackgroundColor(SbColor(r2, g2, b2),
SbColor(r3, g3, b3),
SbColor(r4, g4, b4),
rGrp.GetBool("RadialGradient", false));
SbColor(r4, g4, b4));
}
}
}
}

View File

@@ -437,7 +437,7 @@ Py::Object View3DInventorViewerPy::setBackgroundColor(const Py::Tuple& args)
}
try {
SbColor col(r,g,b);
_viewer->setGradientBackgroundColor(col, col, false);
_viewer->setGradientBackgroundColor(col, col);
return Py::None();
}
catch (const Base::Exception& e) {