From 3c463d8b784b71e51161f5a6d145e71d76be68ab Mon Sep 17 00:00:00 2001 From: Chris Hennes Date: Wed, 6 Oct 2021 11:43:06 -0500 Subject: [PATCH] [Gui] Add ability for stylesheet to override prefs --- src/Gui/Widgets.cpp | 13 ++++++++++++- src/Gui/Widgets.h | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Gui/Widgets.cpp b/src/Gui/Widgets.cpp index 632feb4866..2db9575fd1 100644 --- a/src/Gui/Widgets.cpp +++ b/src/Gui/Widgets.cpp @@ -893,6 +893,7 @@ void UrlLabel::setUrl(const QString& u) StatefulLabel::StatefulLabel(QWidget* parent) : QLabel(parent) + , _overridePreference(false) { // Always attach to the parameter group that stores the main FreeCAD stylesheet _stylesheetGroup = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General"); @@ -957,10 +958,20 @@ void StatefulLabel::OnChange(Base::Subject& rCaller, const char* rc } } +void StatefulLabel::setOverridePreference(bool overridePreference) +{ + _overridePreference = overridePreference; +} + void StatefulLabel::setState(QString state) { _state = state; - std::string stateIn = state.toStdString(); + this->ensurePolished(); + + // If the stylesheet insists, ignore all other logic and let it do its thing. This + // property is *only* set by the stylesheet. + if (_overridePreference) + return; // Check the cache first: if (auto style = _styleCache.find(_state); style != _styleCache.end()) { diff --git a/src/Gui/Widgets.h b/src/Gui/Widgets.h index a59ac1896c..93e9d22bd1 100644 --- a/src/Gui/Widgets.h +++ b/src/Gui/Widgets.h @@ -290,12 +290,27 @@ private: * "error" might be colored differently than one that is a "warning" or a "message". * * In order of style precedence for a given state: User preference > Stylesheet > Default + * unless the stylesheet sets the overridePreference, in which case the stylesheet will + * take precedence. If a stylesheet sets styles for this widgets states, it should also + * set the "handledByStyle" property to ensure the style values are used, rather than the + * defaults. + * + * For example, the .qss might contain: + * Gui--StatefulLabel { + * qproperty-overridePreference: true; + * } + * Gui--StatefulLabel[state="special_state"] { + * color: red; + * } + * In this case, StatefulLabels with state "special_state" will be colored red, regardless of any + * entry in preferences. Use the "overridePreference" stylesheet option with care! * * @author Chris Hennes */ class GuiExport StatefulLabel : public QLabel, public Base::Observer { Q_OBJECT + Q_PROPERTY( bool overridePreference MEMBER _overridePreference WRITE setOverridePreference) Q_PROPERTY( QString state MEMBER _state WRITE setState ) public: @@ -328,9 +343,11 @@ public: public Q_SLOTS: void setState(QString state); + void setOverridePreference(bool overridePreference); private: QString _state; + bool _overridePreference; ParameterGrp::handle _parameterGroup; ParameterGrp::handle _stylesheetGroup;