Core: Update color bar when changing preferences

This solves one part of issue #10717
This commit is contained in:
wmayer
2024-06-30 22:32:44 +02:00
committed by Chris Hennes
parent 0a39ab952d
commit d64bf15f8a
10 changed files with 202 additions and 8 deletions

View File

@@ -1012,6 +1012,7 @@ SET(Inventor_CPP_SRCS
SoFCBackgroundGradient.cpp
SoFCBoundingBox.cpp
SoFCColorBar.cpp
SoFCColorBarNotifier.cpp
SoFCColorGradient.cpp
SoFCColorLegend.cpp
SoFCDB.cpp
@@ -1042,6 +1043,7 @@ SET(Inventor_SRCS
SoFCBackgroundGradient.h
SoFCBoundingBox.h
SoFCColorBar.h
SoFCColorBarNotifier.h
SoFCColorGradient.h
SoFCColorLegend.h
SoFCDB.h

View File

@@ -83,6 +83,22 @@ void SoFCColorBarBase::setModified()
_boxWidth = -1.0f;
}
void SoFCColorBarBase::setFormat(const SoLabelTextFormat& fmt)
{
format = fmt;
applyFormat(fmt);
}
SoLabelTextFormat SoFCColorBarBase::getFormat() const
{
return format;
}
void SoFCColorBarBase::applyFormat(const SoLabelTextFormat& fmt)
{
boost::ignore_unused(fmt);
}
float SoFCColorBarBase::getBoundingWidth(const SbVec2s& size)
{
float fRatio = static_cast<float>(size[0]) / static_cast<float>(size[1]);
@@ -222,6 +238,12 @@ SoFCColorBarBase* SoFCColorBar::getActiveBar() const
return _colorBars[child];
}
void SoFCColorBar::setFormat(const SoLabelTextFormat& fmt)
{
for (auto it : _colorBars)
it->setFormat(fmt);
}
void SoFCColorBar::setViewportSize( const SbVec2s& size )
{
boost::ignore_unused(size);

View File

@@ -41,6 +41,14 @@ class SoGLRenderAction;
namespace Gui {
class SoFCColorGradient;
struct SoLabelTextFormat
{
// NOLINTBEGIN
int textSize = 13;
uint32_t textColor = 0xffffffff;
// NOLINTEND
};
/**
* The abstract color bar base class to get most important information on how to convert a scalar to an RGB color.
* @author Werner Mayer
@@ -110,8 +118,16 @@ public:
* This method must be implemented in subclasses.
*/
virtual const char* getColorBarName() const = 0;
/** Sets the format for the label text.
*/
virtual void setFormat(const SoLabelTextFormat& fmt);
/** Returns the format for the label text.
*/
SoLabelTextFormat getFormat() const;
protected:
/** Applies the format to the label text */
virtual void applyFormat(const SoLabelTextFormat& fmt);
/** Computes the dimensions of the color bar and labels in coordinates with
* respect to the defined height of the camera.
* Returns the width of the bounding box
@@ -140,6 +156,7 @@ protected:
private:
float _boxWidth{-1.0F};
SbVec2s _windowSize;
SoLabelTextFormat format;
};
// --------------------------------------------------------------------------
@@ -202,6 +219,7 @@ public:
/** Returns the name of the color bar.
*/
const char* getColorBarName() const override { return "Color Bar"; }
void setFormat(const SoLabelTextFormat& fmt) override;
protected:
/**

View File

@@ -0,0 +1,74 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2024 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include "PreCompiled.h"
#include "SoFCColorBarNotifier.h"
#include "SoFCColorBar.h"
#include "Window.h"
using namespace Gui;
SoFCColorBarNotifier& SoFCColorBarNotifier::instance()
{
static SoFCColorBarNotifier instance;
return instance;
}
SoFCColorBarNotifier::SoFCColorBarNotifier()
{
group = Gui::WindowParameter::getDefaultParameter()->GetGroup("View");
group->Attach(this);
}
void SoFCColorBarNotifier::attach(SoFCColorBarBase* bar)
{
if (bars.insert(bar).second) {
bar->ref();
group->Notify("CbLabelTextSize");
}
}
void SoFCColorBarNotifier::detach(SoFCColorBarBase* bar)
{
auto pos = bars.find(bar);
if (pos != bars.end()) {
bars.erase(pos);
bar->unref();
}
}
void SoFCColorBarNotifier::OnChange(ParameterGrp::SubjectType& caller,
ParameterGrp::MessageType reason)
{
const ParameterGrp& grp = dynamic_cast<ParameterGrp&>(caller);
if (strcmp(reason, "CbLabelTextSize") == 0 || strcmp(reason, "CbLabelColor") == 0) {
SoLabelTextFormat format;
format.textSize = static_cast<int>(grp.GetInt("CbLabelTextSize", format.textSize));
format.textColor = static_cast<uint32_t>(grp.GetUnsigned("CbLabelColor", format.textColor));
for (auto bar : bars) {
bar->setFormat(format);
}
}
}

View File

@@ -0,0 +1,56 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2024 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#ifndef GUI_SOFCCOLORBARNOTIFIER_H
#define GUI_SOFCCOLORBARNOTIFIER_H
#include <Base/Parameter.h>
#include <set>
namespace Gui {
class SoFCColorBarBase;
class GuiExport SoFCColorBarNotifier: public ParameterGrp::ObserverType // NOLINT
{
public:
static SoFCColorBarNotifier& instance();
void attach(SoFCColorBarBase* bar);
void detach(SoFCColorBarBase* bar);
void OnChange(ParameterGrp::SubjectType& caller, ParameterGrp::MessageType reason) override;
private:
SoFCColorBarNotifier();
~SoFCColorBarNotifier() override = default;
FC_DISABLE_COPY_MOVE(SoFCColorBarNotifier)
private:
std::set<SoFCColorBarBase*> bars;
ParameterGrp::handle group;
};
} // namespace Gui
#endif // GUI_SOFCCOLORBARNOTIFIER_H

View File

@@ -53,8 +53,6 @@ constexpr const float yMin = -4.0F;
constexpr const float yMax = 4.0F;
constexpr const float spaceX = 0.1F;
constexpr const float spaceY = 0.05F;
constexpr const int defaultNumLabels = 13;
constexpr const unsigned long defaultColor = 0xffffffff;
constexpr const float defaultMin = -0.5F;
constexpr const float defaultMax = 0.5F;
constexpr const float upperLimit = 10000.0F;
@@ -105,6 +103,22 @@ const char* SoFCColorGradient::getColorBarName() const
return QT_TRANSLATE_NOOP("QObject", "Color Gradient");
}
void SoFCColorGradient::applyFormat(const SoLabelTextFormat& fmt)
{
auto textColor = App::Color(fmt.textColor);
for (int j = 0; j < labels->getNumChildren(); j++) {
if (labels->getChild(j)->getTypeId() == SoBaseColor::getClassTypeId()) {
auto baseColor = static_cast<SoBaseColor*>(labels->getChild(j)); // NOLINT
baseColor->rgb.setValue(textColor.r, textColor.g, textColor.b);
}
else if (labels->getChild(j)->getTypeId() == SoFont::getClassTypeId()) {
auto font = static_cast<SoFont*>(labels->getChild(j)); // NOLINT
font->size.setValue(static_cast<float>(fmt.textSize));
}
}
}
void SoFCColorGradient::setMarkerLabel(const SoMFString& label)
{
coinRemoveAllChildren(labels);
@@ -116,16 +130,14 @@ void SoFCColorGradient::setMarkerLabel(const SoMFString& label)
float fStep = (maxPt[1] - minPt[1]) / ((float)num - 1);
auto trans = new SoTransform;
ParameterGrp::handle hGrp = Gui::WindowParameter::getDefaultParameter()->GetGroup("View");
auto LabelTextSize = hGrp->GetInt("CbLabelTextSize", defaultNumLabels);
auto LabelTextColor =
App::Color((uint32_t)hGrp->GetUnsigned("CbLabelColor", defaultColor));
SoLabelTextFormat fmt = getFormat();
auto textColor = App::Color(fmt.textColor);
auto textFont = new SoFont;
auto color = new SoBaseColor;
textFont->name.setValue("Helvetica,Arial,Times New Roman");
textFont->size.setValue(static_cast<float>(LabelTextSize));
textFont->size.setValue(static_cast<float>(fmt.textSize));
trans->translation.setValue(maxPt[0] + spaceX, maxPt[1] - spaceY + fStep, 0.0F);
color->rgb.setValue(LabelTextColor.r, LabelTextColor.g, LabelTextColor.b);
color->rgb.setValue(textColor.r, textColor.g, textColor.b);
labels->addChild(trans);
labels->addChild(color);
labels->addChild(textFont);

View File

@@ -78,6 +78,7 @@ public:
const char* getColorBarName() const override;
protected:
void applyFormat(const SoLabelTextFormat& fmt) override;
/**
* Sets the current viewer size this color gradient is embedded into, to recalculate its new position.
*/

View File

@@ -58,6 +58,7 @@
#include <Gui/Selection.h>
#include <Gui/SelectionObject.h>
#include <Gui/SoFCColorBar.h>
#include <Gui/SoFCColorBarNotifier.h>
#include <Gui/TaskView/TaskDialog.h>
#include <Gui/View3DInventor.h>
#include <Gui/View3DInventorViewer.h>
@@ -232,6 +233,7 @@ ViewProviderFemPostObject::ViewProviderFemPostObject()
m_colorRoot->addChild(m_colorStyle);
m_colorBar = new Gui::SoFCColorBar;
m_colorBar->Attach(this);
Gui::SoFCColorBarNotifier::instance().attach(m_colorBar);
m_colorBar->ref();
// create the vtk algorithms we use for visualisation
@@ -274,6 +276,7 @@ ViewProviderFemPostObject::~ViewProviderFemPostObject()
m_material->unref();
m_matPlainEdges->unref();
m_switchMatEdges->unref();
Gui::SoFCColorBarNotifier::instance().detach(m_colorBar);
m_colorBar->Detach(this);
m_colorBar->unref();
m_colorStyle->unref();

View File

@@ -52,6 +52,7 @@
#include <Gui/Flag.h>
#include <Gui/MainWindow.h>
#include <Gui/SoFCColorBar.h>
#include <Gui/SoFCColorBarNotifier.h>
#include <Gui/View3DInventorViewer.h>
#include <Gui/Widgets.h>
#include <Mod/Inspection/App/InspectionFeature.h>
@@ -95,6 +96,7 @@ ViewProviderInspection::ViewProviderInspection()
// simple color bar
pcColorBar = new Gui::SoFCColorBar;
pcColorBar->Attach(this);
Gui::SoFCColorBarNotifier::instance().attach(pcColorBar);
pcColorBar->ref();
pcColorBar->setRange(-0.1f, 0.1f, 3);
pcLinkRoot = new SoGroup;
@@ -113,6 +115,7 @@ ViewProviderInspection::~ViewProviderInspection()
pcCoords->unref();
pcMatBinding->unref();
pcColorMat->unref();
Gui::SoFCColorBarNotifier::instance().detach(pcColorBar);
pcColorBar->Detach(this);
pcColorBar->unref();
pcLinkRoot->unref();

View File

@@ -53,6 +53,7 @@
#include <Gui/MainWindow.h>
#include <Gui/Selection.h>
#include <Gui/SoFCColorBar.h>
#include <Gui/SoFCColorBarNotifier.h>
#include <Gui/SoFCSelection.h>
#include <Gui/View3DInventorViewer.h>
#include <Gui/Widgets.h>
@@ -83,6 +84,7 @@ ViewProviderMeshCurvature::ViewProviderMeshCurvature()
// simple color bar
pcColorBar = new Gui::SoFCColorBar;
pcColorBar->Attach(this);
Gui::SoFCColorBarNotifier::instance().attach(pcColorBar);
pcColorBar->ref();
pcColorBar->setRange(-0.5f, 0.5f, 3);
pcLinkRoot = new SoGroup;
@@ -124,6 +126,7 @@ ViewProviderMeshCurvature::~ViewProviderMeshCurvature()
{
pcColorRoot->unref();
pcColorMat->unref();
Gui::SoFCColorBarNotifier::instance().detach(pcColorBar);
pcColorBar->Detach(this);
pcColorBar->unref();
pcLinkRoot->unref();