KanoopGuiQt 1.3.0
Qt GUI utility library
Loading...
Searching...
No Matches
stylesheet.h
1#ifndef STYLESHEET_H
2#define STYLESHEET_H
3
4#include <Kanoop/kanoopcommon.h>
5#include <Kanoop/gui/utility/stylesheettypes.h>
6#include <Kanoop/gui/libkanoopgui.h>
7
8#include <QColor>
9#include <QGradient>
10#include <QWidget>
11
12/**
13 * @brief Template helper for building typed Qt stylesheet strings.
14 *
15 * StyleSheet<T> is parameterized on a QWidget subclass. It captures the
16 * widget's class name at construction and accumulates property/value pairs
17 * that are rendered into a complete stylesheet rule by toString().
18 *
19 * An optional pseudo-state (e.g., PS_Hover) and sub-control selector can
20 * narrow the rule to a specific widget state or part.
21 *
22 * @tparam T QWidget subclass whose class name is used as the selector
23 */
24template <typename T>
25class LIBKANOOPGUI_EXPORT StyleSheet
26{
27public:
28 static_assert(std::is_base_of<QWidget, T>::value, "Templatized class type must be subclass of QWidget");
29
30 /** @brief Construct and capture the widget class name as the CSS selector. */
32 {
33 T* t = new T();
34 _typeName = t->metaObject()->className();
35 delete t;
36 }
37
38 /**
39 * @brief Set a stylesheet property to a string value.
40 * @param property Property identifier
41 * @param value String value for the property
42 */
43 void setProperty(StyleSheetProperty property, const QString& value);
44
45 /**
46 * @brief Set a stylesheet property to a color value.
47 * @param property Property identifier
48 * @param value Color to convert to a CSS color string
49 */
50 void setProperty(StyleSheetProperty property, const QColor& value);
51
52 /**
53 * @brief Set a stylesheet property to a pixel integer value.
54 * @param property Property identifier
55 * @param value Size in pixels
56 */
57 void setPropertyPixels(StyleSheetProperty property, int value);
58
59 /**
60 * @brief Set the background to a gradient string (qradialgradient / qlineargradient).
61 * @param gradient Full CSS gradient value string
62 */
63 void setGradient(const QString& gradient) { setProperty(SP_Background, gradient); }
64
65 /**
66 * @brief Set the background to a radial gradient specified by its geometric parameters.
67 *
68 * Generates a CSS qradialgradient() value from the given parameters and passes it to
69 * setGradient(). Colors are converted to rgba() notation so alpha is always preserved.
70 *
71 * @param cx Centre x (0–1)
72 * @param cy Centre y (0–1)
73 * @param radius Radius (0–1)
74 * @param fx Focal-point x (0–1)
75 * @param fy Focal-point y (0–1)
76 * @param stops Gradient stops (position in 0–1 and color, including alpha)
77 */
78 void setRadialGradient(double cx, double cy, double radius, double fx, double fy,
79 const QGradientStops& stops);
80
81 /**
82 * @brief Set all four border properties at once using the dome highlight/shadow convention.
83 *
84 * Top and left edges receive @p topLeft (highlight); bottom and right receive
85 * @p bottomRight (shadow). This matches the standard glassy-dome button style
86 * used throughout the UI.
87 *
88 * @param topLeft CSS border value for the top and left edges
89 * @param bottomRight CSS border value for the bottom and right edges
90 */
91 void setBorder(const QString& topLeft, const QString& bottomRight);
92
93 /**
94 * @brief Set all four border properties using a pixel width and two colors.
95 *
96 * Generates "Npx solid rgba(r,g,b,a)" strings for top/left and bottom/right edges,
97 * then delegates to the string overload.
98 *
99 * @param widthPx Border thickness in pixels
100 * @param topLeft Color for top and left edges (highlight)
101 * @param bottomRight Color for bottom and right edges (shadow)
102 */
103 void setBorder(int widthPx, const QColor& topLeft, const QColor& bottomRight);
104
105 /**
106 * @brief Set the pseudo-state selector (e.g., PS_Hover, PS_Checked).
107 * @param value Pseudo-state enum value
108 */
109 void setPseudoState(StyleSheetPseudoState value) { _pseudoState = value; }
110
111 /**
112 * @brief Set the sub-control selector string (e.g., "::handle").
113 * @param value Sub-control string
114 */
115 void setSubControl(const QString& value) { _subControl = value; }
116
117 /**
118 * @brief Render all accumulated properties into a complete stylesheet rule.
119 * @return Stylesheet string suitable for QWidget::setStyleSheet()
120 */
121 QString toString() const;
122
123
124 /** @brief CSS selector class name (set from T::metaObject()->className()). */
125 QString _typeName;
126
127 /** @brief Accumulated property/value pairs for the stylesheet rule. */
128 QMap<StyleSheetProperty, QString> _properties;
129 /** @brief Pseudo-state selector applied to the rule (PS_Invalid = none). */
130 StyleSheetPseudoState _pseudoState = PS_Invalid;
131 /** @brief Sub-control selector string (empty = none). */
132 QString _subControl;
133};
134
135#endif // STYLESHEET_H
Template helper for building typed Qt stylesheet strings.
Definition stylesheet.h:26
StyleSheet()
Construct and capture the widget class name as the CSS selector.
Definition stylesheet.h:31
QString toString() const
Render all accumulated properties into a complete stylesheet rule.
QString _subControl
Sub-control selector string (empty = none).
Definition stylesheet.h:132
QMap< StyleSheetProperty, QString > _properties
Accumulated property/value pairs for the stylesheet rule.
Definition stylesheet.h:128
void setBorder(int widthPx, const QColor &topLeft, const QColor &bottomRight)
Set all four border properties using a pixel width and two colors.
void setSubControl(const QString &value)
Set the sub-control selector string (e.g., "::handle").
Definition stylesheet.h:115
QString _typeName
CSS selector class name (set from T::metaObject()->className()).
Definition stylesheet.h:125
void setRadialGradient(double cx, double cy, double radius, double fx, double fy, const QGradientStops &stops)
Set the background to a radial gradient specified by its geometric parameters.
void setProperty(StyleSheetProperty property, const QString &value)
Set a stylesheet property to a string value.
void setBorder(const QString &topLeft, const QString &bottomRight)
Set all four border properties at once using the dome highlight/shadow convention.
void setGradient(const QString &gradient)
Set the background to a gradient string (qradialgradient / qlineargradient).
Definition stylesheet.h:63
void setPropertyPixels(StyleSheetProperty property, int value)
Set a stylesheet property to a pixel integer value.
void setPseudoState(StyleSheetPseudoState value)
Set the pseudo-state selector (e.g., PS_Hover, PS_Checked).
Definition stylesheet.h:109
void setProperty(StyleSheetProperty property, const QColor &value)
Set a stylesheet property to a color value.