From 37d2492719f12fc3452f0076aa170492e2c8e0b4 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 30 May 2022 11:12:31 +0200 Subject: [PATCH] Gui: refactor SoFCColorGradient::setViewportSize and SoFCColorLegend::setViewportSize --- src/Gui/SoFCColorBar.cpp | 27 +++++++++++++++++++++++++++ src/Gui/SoFCColorBar.h | 5 +++++ src/Gui/SoFCColorGradient.cpp | 21 ++------------------- src/Gui/SoFCColorLegend.cpp | 21 ++------------------- 4 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/Gui/SoFCColorBar.cpp b/src/Gui/SoFCColorBar.cpp index 435c69a41a..55b982fba5 100644 --- a/src/Gui/SoFCColorBar.cpp +++ b/src/Gui/SoFCColorBar.cpp @@ -106,6 +106,33 @@ float SoFCColorBarBase::getBoundingWidth(const SbVec2s& size) return boxWidth; } +float SoFCColorBarBase::getBounds(const SbVec2s& size, float& fMinX, float&fMinY, float& fMaxX, float& fMaxY) +{ + // ratio of window width / height + float fRatio = static_cast(size[0]) / static_cast(size[1]); + float baseYValue = 4.0f; + float barWidth = 0.5f; + + fMinX = 5.0f * fRatio; // must be scaled with the ratio to assure it stays at the right + fMaxX = fMinX + barWidth; + fMinY = -baseYValue; + fMaxY = baseYValue; // bar has the height of almost whole window height + + if (fRatio < 1.0f) { + // must be adjusted to assure that the height of the bar doesn't shrink + fMinY = -baseYValue / fRatio; + fMaxY = baseYValue / fRatio; + } + + // get the bounding box width of the color bar and labels + float boxWidth = getBoundingWidth(size); + if (fRatio < 1.0f) { + boxWidth *= fRatio; + } + + return boxWidth; +} + // -------------------------------------------------------------------------- namespace Gui { diff --git a/src/Gui/SoFCColorBar.h b/src/Gui/SoFCColorBar.h index c6a148a5f6..66829f632f 100644 --- a/src/Gui/SoFCColorBar.h +++ b/src/Gui/SoFCColorBar.h @@ -112,6 +112,11 @@ public: virtual const char* getColorBarName() const = 0; protected: + /** 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 + */ + float getBounds(const SbVec2s& size, float& fMinX, float&fMinY, float& fMaxX, float& fMaxY); /** Returns the width of the color bar and labels * * Computes the occupied width of the color bar and its labels. diff --git a/src/Gui/SoFCColorGradient.cpp b/src/Gui/SoFCColorGradient.cpp index 84c2920fb2..4897aea7c2 100644 --- a/src/Gui/SoFCColorGradient.cpp +++ b/src/Gui/SoFCColorGradient.cpp @@ -110,25 +110,8 @@ void SoFCColorGradient::setMarkerLabel(const SoMFString& label) void SoFCColorGradient::setViewportSize(const SbVec2s& size) { - // ratio of window height / width - float fRatio = static_cast(size[0]) / static_cast(size[1]); - float baseYValue = 4.0f; - float barWidth = 0.5f; - float fMinX = 5.0f * fRatio; // must be scaled with the ratio to assure it stays at the right - float fMaxX = fMinX + barWidth; - float fMinY = -baseYValue, fMaxY = baseYValue; // bar has the height of almost whole window height - - if (fRatio < 1.0f) { - // height must be adjusted to assure bar stays smaller than window height - fMinY = -baseYValue / fRatio; - fMaxY = baseYValue / fRatio; - } - - // get the bounding box width of the labels - float boxWidth = getBoundingWidth(size); - if (fRatio < 1.0f) { - boxWidth *= fRatio; - } + float fMinX, fMinY, fMaxX, fMaxY; + float boxWidth = getBounds(size, fMinX, fMinY, fMaxX, fMaxY); // search for the labels int num = 0; diff --git a/src/Gui/SoFCColorLegend.cpp b/src/Gui/SoFCColorLegend.cpp index dd7c7d3224..273f95d410 100644 --- a/src/Gui/SoFCColorLegend.cpp +++ b/src/Gui/SoFCColorLegend.cpp @@ -190,25 +190,8 @@ void SoFCColorLegend::setMarkerValue(const SoMFString& value) void SoFCColorLegend::setViewportSize(const SbVec2s& size) { - // ratio of window height / width - float fRatio = static_cast(size[0]) / static_cast(size[1]); - float baseYValue = 4.0f; - float barWidth = 0.5f; - float fMinX = 5.0f * fRatio; // must be scaled with the ratio to assure it stays at the right - float fMaxX = fMinX + barWidth; - float fMinY = -baseYValue, fMaxY = baseYValue; // bar has the height of almost whole window height - - if (fRatio < 1.0f) { - // height must be adjusted to assure bar stays smaller than window height - fMinY = -baseYValue / fRatio; - fMaxY = baseYValue / fRatio; - } - - // get the bounding box width of the labels - float boxWidth = getBoundingWidth(size); - if (fRatio < 1.0f) { - boxWidth *= fRatio; - } + float fMinX, fMinY, fMaxX, fMaxY; + float boxWidth = getBounds(size, fMinX, fMinY, fMaxX, fMaxY); // legend bar is shifted to the left by width of the labels to assure that labels are fully visible _bbox.setBounds(fMinX - boxWidth, fMinY, fMaxX - boxWidth, fMaxY);