From 26e38309c8a70d3c7430df6257b881dd0caa3a17 Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Thu, 24 Nov 2022 07:13:06 +0800 Subject: [PATCH] Gui: fix transparency saving in PrefColorButton --- src/Gui/PrefWidgets.cpp | 19 ++++++++++++------- src/Gui/PrefWidgets.h | 3 +++ src/Gui/Widgets.cpp | 25 +++++++++++++++++++++++++ src/Gui/Widgets.h | 3 +++ 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/Gui/PrefWidgets.cpp b/src/Gui/PrefWidgets.cpp index 7e6c6a26a2..cfa45ba6fc 100644 --- a/src/Gui/PrefWidgets.cpp +++ b/src/Gui/PrefWidgets.cpp @@ -552,18 +552,23 @@ void PrefColorButton::restorePreferences() return; } - QColor col = color(); + if (!m_Restored) + m_Default = color(); - unsigned int icol = (col.red() << 24) | (col.green() << 16) | (col.blue() << 8); + const QColor &col = m_Default; - auto lcol = static_cast(icol); + unsigned int icol = (col.red() << 24) | (col.green() << 16) | (col.blue() << 8) | col.alpha(); + + unsigned long lcol = static_cast(icol); lcol = getWindowParameter()->GetUnsigned( entryName(), lcol ); icol = static_cast(lcol); int r = (icol >> 24)&0xff; int g = (icol >> 16)&0xff; int b = (icol >> 8)&0xff; - - setColor(QColor(r,g,b)); + int a = (icol )&0xff; + if (!this->allowTransparency()) + a = 0xff; + setColor(QColor(r,g,b,a)); } void PrefColorButton::savePreferences() @@ -576,8 +581,8 @@ void PrefColorButton::savePreferences() QColor col = color(); // (r,g,b,a) with a = 255 (opaque) - unsigned int icol = (col.red() << 24) | (col.green() << 16) | (col.blue() << 8) | 255; - auto lcol = static_cast(icol); + unsigned int icol = (col.red() << 24) | (col.green() << 16) | (col.blue() << 8) | col.alpha(); + unsigned long lcol = static_cast(icol); getWindowParameter()->SetUnsigned( entryName(), lcol ); } diff --git a/src/Gui/PrefWidgets.h b/src/Gui/PrefWidgets.h index 7552afbcc7..37850e4024 100644 --- a/src/Gui/PrefWidgets.h +++ b/src/Gui/PrefWidgets.h @@ -329,6 +329,9 @@ protected: // restore from/save to parameters void restorePreferences() override; void savePreferences() override; + +private: + QColor m_Default; }; /** The PrefUnitSpinBox class. diff --git a/src/Gui/Widgets.cpp b/src/Gui/Widgets.cpp index 1b7975afd5..bd9845f85f 100644 --- a/src/Gui/Widgets.cpp +++ b/src/Gui/Widgets.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include "Widgets.h" #include "Action.h" @@ -685,6 +686,30 @@ QColor ColorButton::color() const return d->col; } +/** + * Sets the packed color \a c to the button. + */ +void ColorButton::setPackedColor(uint32_t c) +{ + App::Color color; + color.setPackedValue(c); + d->col.setRedF(color.r); + d->col.setGreenF(color.g); + d->col.setBlueF(color.b); + d->col.setAlphaF(color.a); + d->dirty = true; + update(); +} + +/** + * Returns the current packed color of the button. + */ +uint32_t ColorButton::packedColor() const +{ + App::Color color(d->col.redF(), d->col.greenF(), d->col.blueF(), d->col.alphaF()); + return color.getPackedValue(); +} + void ColorButton::setAllowChangeColor(bool ok) { d->allowChange = ok; diff --git a/src/Gui/Widgets.h b/src/Gui/Widgets.h index 987f970e9f..954d59350a 100644 --- a/src/Gui/Widgets.h +++ b/src/Gui/Widgets.h @@ -238,6 +238,9 @@ public: void setColor(const QColor&); QColor color() const; + void setPackedColor(uint32_t); + uint32_t packedColor() const; + void setAllowChangeColor(bool); bool allowChangeColor() const;