1 | // Copyright (C) 2024 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #ifndef QGRAPHSTHEME_H |
5 | #define QGRAPHSTHEME_H |
6 | |
7 | #include <QtCore/qobject.h> |
8 | #include <QtGraphs/qgraphsglobal.h> |
9 | #include <QtGui/qbrush.h> |
10 | #include <QtGui/qcolor.h> |
11 | #include <QtGui/qfont.h> |
12 | #include <QtQml/qqmlengine.h> |
13 | #include <QtQml/qqmlparserstatus.h> |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | class QQuickGraphsColor; |
18 | class QQuickGradient; |
19 | class QGraphsThemePrivate; |
20 | struct QGraphsLinePrivate; |
21 | |
22 | QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QGraphsLinePrivate, Q_GRAPHS_EXPORT) |
23 | |
24 | struct QGraphsThemeDirtyBitField |
25 | { |
26 | bool plotAreaBackgroundColorDirty : 1; |
27 | bool plotAreaBackgroundVisibilityDirty : 1; |
28 | bool seriesColorsDirty : 1; |
29 | bool seriesGradientDirty : 1; |
30 | bool colorSchemeDirty : 1; |
31 | bool colorStyleDirty : 1; |
32 | bool labelFontDirty : 1; |
33 | bool gridVisibilityDirty : 1; |
34 | bool gridDirty : 1; |
35 | bool labelBackgroundColorDirty : 1; |
36 | bool labelBackgroundVisibilityDirty : 1; |
37 | bool labelBorderVisibilityDirty : 1; |
38 | bool labelTextColorDirty : 1; |
39 | bool axisXDirty : 1; |
40 | bool axisYDirty : 1; |
41 | bool axisZDirty : 1; |
42 | bool labelsVisibilityDirty : 1; |
43 | bool multiHighlightColorDirty : 1; |
44 | bool multiHighlightGradientDirty : 1; |
45 | bool singleHighlightColorDirty : 1; |
46 | bool singleHighlightGradientDirty : 1; |
47 | bool themeDirty : 1; |
48 | bool backgroundColorDirty : 1; |
49 | bool backgroundVisibilityDirty : 1; |
50 | |
51 | QGraphsThemeDirtyBitField() |
52 | : plotAreaBackgroundColorDirty(false) |
53 | , plotAreaBackgroundVisibilityDirty(false) |
54 | , seriesColorsDirty(false) |
55 | , seriesGradientDirty(false) |
56 | , colorSchemeDirty(false) |
57 | , colorStyleDirty(false) |
58 | , labelFontDirty(false) |
59 | , gridVisibilityDirty(false) |
60 | , gridDirty(false) |
61 | , labelBackgroundColorDirty(false) |
62 | , labelBackgroundVisibilityDirty(false) |
63 | , labelBorderVisibilityDirty(false) |
64 | , labelTextColorDirty(false) |
65 | , axisXDirty(false) |
66 | , axisYDirty(false) |
67 | , axisZDirty(false) |
68 | , labelsVisibilityDirty(false) |
69 | , multiHighlightColorDirty(false) |
70 | , multiHighlightGradientDirty(false) |
71 | , singleHighlightColorDirty(false) |
72 | , singleHighlightGradientDirty(false) |
73 | , themeDirty(false) |
74 | , backgroundColorDirty(false) |
75 | , backgroundVisibilityDirty(false) |
76 | {} |
77 | }; |
78 | |
79 | class QGraphsLine |
80 | { |
81 | Q_GADGET_EXPORT(Q_GRAPHS_EXPORT) |
82 | QML_VALUE_TYPE(graphsline) |
83 | QML_STRUCTURED_VALUE |
84 | |
85 | Q_PROPERTY(QColor mainColor READ mainColor WRITE setMainColor FINAL) |
86 | Q_PROPERTY(QColor subColor READ subColor WRITE setSubColor FINAL) |
87 | Q_PROPERTY(qreal mainWidth READ mainWidth WRITE setMainWidth FINAL) |
88 | Q_PROPERTY(qreal subWidth READ subWidth WRITE setSubWidth FINAL) |
89 | Q_PROPERTY(QColor labelTextColor READ labelTextColor WRITE setLabelTextColor FINAL) |
90 | |
91 | public: |
92 | Q_GRAPHS_EXPORT static QVariant create(const QJSValue ¶ms); |
93 | Q_GRAPHS_EXPORT QGraphsLine(); |
94 | Q_GRAPHS_EXPORT QGraphsLine(const QGraphsLine &other); |
95 | QGraphsLine(QGraphsLine &&other) noexcept = default; |
96 | Q_GRAPHS_EXPORT ~QGraphsLine(); |
97 | Q_GRAPHS_EXPORT QGraphsLine &operator=(const QGraphsLine &other); |
98 | void swap(QGraphsLine &other) noexcept { d.swap(other&: other.d); } |
99 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QGraphsLine) |
100 | |
101 | Q_GRAPHS_EXPORT QColor mainColor() const; |
102 | Q_GRAPHS_EXPORT void setMainColor(QColor newColor); |
103 | Q_GRAPHS_EXPORT QColor subColor() const; |
104 | Q_GRAPHS_EXPORT void setSubColor(QColor newColor); |
105 | Q_GRAPHS_EXPORT qreal mainWidth() const; |
106 | Q_GRAPHS_EXPORT void setMainWidth(qreal newWidth); |
107 | Q_GRAPHS_EXPORT qreal subWidth() const; |
108 | Q_GRAPHS_EXPORT void setSubWidth(qreal newWidth); |
109 | Q_GRAPHS_EXPORT QColor labelTextColor() const; |
110 | Q_GRAPHS_EXPORT void setLabelTextColor(QColor newColor); |
111 | Q_GRAPHS_EXPORT void detach(); |
112 | |
113 | Q_GRAPHS_EXPORT operator QVariant() const; |
114 | |
115 | private: |
116 | QExplicitlySharedDataPointer<QGraphsLinePrivate> d; |
117 | |
118 | friend Q_GRAPHS_EXPORT bool comparesEqual(const QGraphsLine &lhs, |
119 | const QGraphsLine &rhs) noexcept; |
120 | Q_DECLARE_EQUALITY_COMPARABLE(QGraphsLine) |
121 | |
122 | friend class QGraphsTheme; |
123 | }; |
124 | Q_DECLARE_SHARED(QGraphsLine) |
125 | |
126 | class Q_GRAPHS_EXPORT QGraphsTheme : public QObject, public QQmlParserStatus |
127 | { |
128 | Q_OBJECT |
129 | |
130 | // For QQuickGradient |
131 | Q_MOC_INCLUDE(<QtQuick/private/qquickrectangle_p.h>) |
132 | |
133 | Q_INTERFACES(QQmlParserStatus) |
134 | Q_CLASSINFO("RegisterEnumClassesUnscoped" , "false" ) |
135 | Q_PROPERTY(QGraphsTheme::ColorScheme colorScheme READ colorScheme WRITE setColorScheme NOTIFY |
136 | colorSchemeChanged FINAL) |
137 | Q_PROPERTY(QGraphsTheme::Theme theme READ theme WRITE setTheme NOTIFY themeChanged FINAL) |
138 | Q_PROPERTY(QGraphsTheme::ColorStyle colorStyle READ colorStyle WRITE setColorStyle NOTIFY |
139 | colorStyleChanged FINAL) |
140 | |
141 | Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY |
142 | backgroundColorChanged FINAL) |
143 | Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY |
144 | backgroundVisibleChanged FINAL) |
145 | |
146 | Q_PROPERTY(QColor plotAreaBackgroundColor READ plotAreaBackgroundColor WRITE |
147 | setPlotAreaBackgroundColor NOTIFY plotAreaBackgroundColorChanged FINAL) |
148 | Q_PROPERTY(bool plotAreaBackgroundVisible READ isPlotAreaBackgroundVisible WRITE |
149 | setPlotAreaBackgroundVisible NOTIFY plotAreaBackgroundVisibleChanged FINAL) |
150 | |
151 | Q_PROPERTY( |
152 | bool gridVisible READ isGridVisible WRITE setGridVisible NOTIFY gridVisibleChanged FINAL) |
153 | |
154 | Q_PROPERTY(QFont axisXLabelFont READ axisXLabelFont WRITE setAxisXLabelFont NOTIFY axisXLabelFontChanged FINAL) |
155 | Q_PROPERTY(QFont axisYLabelFont READ axisYLabelFont WRITE setAxisYLabelFont NOTIFY axisYLabelFontChanged FINAL) |
156 | Q_PROPERTY(QFont axisZLabelFont READ axisZLabelFont WRITE setAxisZLabelFont NOTIFY axisZLabelFontChanged FINAL) |
157 | |
158 | Q_PROPERTY(QGraphsLine grid READ grid WRITE setGrid NOTIFY gridChanged FINAL) |
159 | Q_PROPERTY(QGraphsLine axisX READ axisX WRITE setAxisX NOTIFY axisXChanged FINAL) |
160 | Q_PROPERTY(QGraphsLine axisY READ axisY WRITE setAxisY NOTIFY axisYChanged FINAL) |
161 | Q_PROPERTY(QGraphsLine axisZ READ axisZ WRITE setAxisZ NOTIFY axisZChanged FINAL) |
162 | |
163 | Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont NOTIFY labelFontChanged FINAL) |
164 | Q_PROPERTY(bool labelsVisible READ labelsVisible WRITE setLabelsVisible NOTIFY |
165 | labelsVisibleChanged FINAL) |
166 | Q_PROPERTY(QColor labelBackgroundColor READ labelBackgroundColor WRITE setLabelBackgroundColor |
167 | NOTIFY labelBackgroundColorChanged FINAL) |
168 | Q_PROPERTY(QColor labelTextColor READ labelTextColor WRITE setLabelTextColor NOTIFY |
169 | labelTextColorChanged FINAL) |
170 | Q_PROPERTY(bool labelBackgroundVisible READ isLabelBackgroundVisible WRITE |
171 | setLabelBackgroundVisible NOTIFY labelBackgroundVisibleChanged FINAL) |
172 | Q_PROPERTY(bool labelBorderVisible READ isLabelBorderVisible WRITE setLabelBorderVisible NOTIFY |
173 | labelBorderVisibleChanged FINAL) |
174 | |
175 | Q_PROPERTY(QList<QColor> seriesColors READ seriesColors WRITE setSeriesColors NOTIFY |
176 | seriesColorsChanged FINAL) |
177 | Q_PROPERTY(QList<QColor> borderColors READ borderColors WRITE setBorderColors NOTIFY borderColorsChanged FINAL) |
178 | Q_PROPERTY(qreal borderWidth READ borderWidth WRITE setBorderWidth NOTIFY borderWidthChanged FINAL) |
179 | |
180 | Q_PROPERTY(QQmlListProperty<QQuickGraphsColor> baseColors READ baseColorsQML CONSTANT FINAL) |
181 | Q_PROPERTY(QQmlListProperty<QQuickGradient> baseGradients READ baseGradientsQML CONSTANT FINAL) |
182 | Q_PROPERTY(QQmlListProperty<QObject> themeChildren READ themeChildren CONSTANT FINAL) |
183 | |
184 | Q_PROPERTY(QColor singleHighlightColor READ singleHighlightColor WRITE setSingleHighlightColor |
185 | NOTIFY singleHighlightColorChanged FINAL) |
186 | Q_PROPERTY(QColor multiHighlightColor READ multiHighlightColor WRITE setMultiHighlightColor |
187 | NOTIFY multiHighlightColorChanged FINAL) |
188 | Q_PROPERTY(QQuickGradient *singleHighlightGradient READ singleHighlightGradientQML WRITE |
189 | setSingleHighlightGradientQML NOTIFY singleHighlightGradientQMLChanged FINAL) |
190 | Q_PROPERTY(QQuickGradient *multiHighlightGradient READ multiHighlightGradientQML WRITE |
191 | setMultiHighlightGradientQML NOTIFY multiHighlightGradientQMLChanged FINAL) |
192 | Q_CLASSINFO("DefaultProperty" , "themeChildren" ) |
193 | QML_NAMED_ELEMENT(GraphsTheme) |
194 | public: |
195 | enum class Theme { |
196 | QtGreen = 0, |
197 | QtGreenNeon, |
198 | MixSeries, |
199 | OrangeSeries, |
200 | YellowSeries, |
201 | BlueSeries, |
202 | PurpleSeries, |
203 | GreySeries, |
204 | UserDefined, |
205 | }; |
206 | Q_ENUM(Theme) |
207 | |
208 | enum class ColorStyle { |
209 | Uniform, |
210 | ObjectGradient, |
211 | RangeGradient, |
212 | }; |
213 | Q_ENUM(ColorStyle) |
214 | |
215 | enum class ForceTheme { No, Yes }; |
216 | Q_ENUM(ForceTheme); |
217 | |
218 | enum class ColorScheme { Automatic, Light, Dark }; |
219 | Q_ENUM(ColorScheme); |
220 | |
221 | explicit QGraphsTheme(QObject *parent = nullptr); |
222 | |
223 | ~QGraphsTheme() override; |
224 | |
225 | bool themeDirty() const; |
226 | void resetThemeDirty(); |
227 | void resetColorTheme(); |
228 | |
229 | QGraphsThemeDirtyBitField *dirtyBits(); |
230 | void resetDirtyBits(); |
231 | |
232 | ColorScheme colorScheme() const; |
233 | void setColorScheme(ColorScheme newColorScheme); |
234 | |
235 | Theme theme() const; |
236 | void setTheme(Theme newTheme, ForceTheme force = ForceTheme::No); |
237 | |
238 | ColorStyle colorStyle() const; |
239 | void setColorStyle(ColorStyle newColorStyle); |
240 | |
241 | QColor backgroundColor() const; |
242 | void setBackgroundColor(QColor newBackgroundColor); |
243 | bool isBackgroundVisible() const; |
244 | void setBackgroundVisible(bool newBackgroundVisible); |
245 | |
246 | QColor plotAreaBackgroundColor() const; |
247 | void setPlotAreaBackgroundColor(QColor newBackgroundColor); |
248 | bool isPlotAreaBackgroundVisible() const; |
249 | void setPlotAreaBackgroundVisible(bool newBackgroundVisibility); |
250 | |
251 | bool isGridVisible() const; |
252 | void setGridVisible(bool newGridVisibility); |
253 | |
254 | bool labelsVisible() const; |
255 | void setLabelsVisible(bool newLabelsVisibility); |
256 | QColor labelBackgroundColor() const; |
257 | void setLabelBackgroundColor(QColor newLabelBackgroundColor); |
258 | QColor labelTextColor() const; |
259 | void setLabelTextColor(QColor newLabelTextColor); |
260 | |
261 | QColor singleHighlightColor() const; |
262 | void setSingleHighlightColor(QColor newSingleHighlightColor); |
263 | QColor multiHighlightColor() const; |
264 | void setMultiHighlightColor(QColor newMultiHighlightColor); |
265 | void setSingleHighlightGradient(const QLinearGradient &gradient); |
266 | QLinearGradient singleHighlightGradient() const; |
267 | void setMultiHighlightGradient(const QLinearGradient &gradient); |
268 | QLinearGradient multiHighlightGradient() const; |
269 | |
270 | QFont labelFont() const; |
271 | void setLabelFont(const QFont &newFont); |
272 | |
273 | QList<QColor> seriesColors() const; |
274 | void setSeriesColors(const QList<QColor> &newSeriesColors); |
275 | QList<QColor> borderColors() const; |
276 | void setBorderColors(const QList<QColor> &newBorderColors); |
277 | QList<QLinearGradient> seriesGradients() const; |
278 | void setSeriesGradients(const QList<QLinearGradient> &newSeriesGradients); |
279 | |
280 | bool isLabelBackgroundVisible() const; |
281 | void setLabelBackgroundVisible(bool newLabelBackgroundVisibility); |
282 | |
283 | bool isLabelBorderVisible() const; |
284 | void setLabelBorderVisible(bool newLabelBorderVisibility); |
285 | |
286 | qreal borderWidth() const; |
287 | void setBorderWidth(qreal newBorderWidth); |
288 | |
289 | QFont axisXLabelFont() const; |
290 | void setAxisXLabelFont(const QFont &newAxisXLabelFont); |
291 | QFont axisYLabelFont() const; |
292 | void setAxisYLabelFont(const QFont &newAxisYLabelFont); |
293 | QFont axisZLabelFont() const; |
294 | void setAxisZLabelFont(const QFont &newAxisZLabelFont); |
295 | |
296 | QGraphsLine grid() const; |
297 | void setGrid(const QGraphsLine &newGrid); |
298 | QGraphsLine axisX() const; |
299 | void setAxisX(const QGraphsLine &newAxisX); |
300 | QGraphsLine axisY() const; |
301 | void setAxisY(const QGraphsLine &newAxisY); |
302 | QGraphsLine axisZ() const; |
303 | void setAxisZ(const QGraphsLine &newAxisZ); |
304 | |
305 | Q_SIGNALS: |
306 | void update(); |
307 | |
308 | void colorSchemeChanged(); |
309 | void themeChanged(QGraphsTheme::Theme theme); |
310 | void colorStyleChanged(QGraphsTheme::ColorStyle type); |
311 | |
312 | void backgroundColorChanged(); |
313 | void backgroundVisibleChanged(); |
314 | |
315 | void plotAreaBackgroundColorChanged(); |
316 | void plotAreaBackgroundVisibleChanged(); |
317 | |
318 | void gridVisibleChanged(); |
319 | |
320 | void labelsVisibleChanged(); |
321 | void labelBackgroundColorChanged(); |
322 | void labelTextColorChanged(); |
323 | |
324 | void singleHighlightColorChanged(QColor color); |
325 | void multiHighlightColorChanged(QColor color); |
326 | void singleHighlightGradientChanged(const QLinearGradient &gradient); |
327 | void multiHighlightGradientChanged(const QLinearGradient &gradient); |
328 | |
329 | void labelFontChanged(); |
330 | |
331 | void labelBackgroundVisibleChanged(); |
332 | void labelBorderVisibleChanged(); |
333 | |
334 | void seriesColorsChanged(const QList<QColor> &list); |
335 | void seriesGradientsChanged(const QList<QLinearGradient> &list); |
336 | void borderColorsChanged(); |
337 | void borderWidthChanged(); |
338 | |
339 | void singleHighlightGradientQMLChanged(); |
340 | void multiHighlightGradientQMLChanged(); |
341 | |
342 | void axisXLabelFontChanged(); |
343 | void axisYLabelFontChanged(); |
344 | void axisZLabelFontChanged(); |
345 | |
346 | void gridChanged(); |
347 | void axisXChanged(); |
348 | void axisYChanged(); |
349 | void axisZChanged(); |
350 | |
351 | public Q_SLOTS: |
352 | void handleBaseColorUpdate(); |
353 | void handleBaseGradientUpdate(); |
354 | |
355 | protected: |
356 | explicit QGraphsTheme(QGraphsThemePrivate &dd, QObject *parent = nullptr); |
357 | // from QDeclarativeParserStatus |
358 | void classBegin() override; |
359 | void componentComplete() override; |
360 | |
361 | private: |
362 | enum class GradientQMLStyle { |
363 | Base, |
364 | SingleHL, |
365 | MultiHL, |
366 | }; |
367 | |
368 | void updateAutomaticColorScheme(); |
369 | void setColorSchemePalette(); |
370 | void setThemePalette(); |
371 | QLinearGradient createGradient(QColor color, float colorLevel); |
372 | |
373 | void setThemeGradient(QQuickGradient *gradient, GradientQMLStyle type); |
374 | QLinearGradient convertGradient(QQuickGradient *gradient); |
375 | |
376 | QQmlListProperty<QQuickGraphsColor> baseColorsQML(); |
377 | static void appendBaseColorsFunc(QQmlListProperty<QQuickGraphsColor> *list, |
378 | QQuickGraphsColor *color); |
379 | static qsizetype countBaseColorsFunc(QQmlListProperty<QQuickGraphsColor> *list); |
380 | static QQuickGraphsColor *atBaseColorsFunc(QQmlListProperty<QQuickGraphsColor> *list, |
381 | qsizetype index); |
382 | static void clearBaseColorsFunc(QQmlListProperty<QQuickGraphsColor> *list); |
383 | |
384 | QQmlListProperty<QQuickGradient> baseGradientsQML(); |
385 | static void appendBaseGradientsFunc(QQmlListProperty<QQuickGradient> *list, |
386 | QQuickGradient *gradient); |
387 | static qsizetype countBaseGradientsFunc(QQmlListProperty<QQuickGradient> *list); |
388 | static QQuickGradient *atBaseGradientsFunc(QQmlListProperty<QQuickGradient> *list, |
389 | qsizetype index); |
390 | static void clearBaseGradientsFunc(QQmlListProperty<QQuickGradient> *list); |
391 | |
392 | QQmlListProperty<QObject> themeChildren(); |
393 | static void appendThemeChildren(QQmlListProperty<QObject> *list, QObject *element); |
394 | |
395 | void addColor(QQuickGraphsColor *color); |
396 | QList<QQuickGraphsColor *> colorList(); |
397 | void clearColors(); |
398 | void clearDummyColors(); |
399 | void clearGradients(); |
400 | QList<QQuickGradient *> gradientList(); |
401 | void addGradient(QQuickGradient *gradient); |
402 | |
403 | void setSingleHighlightGradientQML(QQuickGradient *gradient); |
404 | QQuickGradient *singleHighlightGradientQML() const; |
405 | |
406 | void setMultiHighlightGradientQML(QQuickGradient *gradient); |
407 | QQuickGradient *multiHighlightGradientQML() const; |
408 | |
409 | Q_DISABLE_COPY_MOVE(QGraphsTheme) |
410 | Q_DECLARE_PRIVATE(QGraphsTheme) |
411 | }; |
412 | |
413 | QT_END_NAMESPACE |
414 | |
415 | #endif |
416 | |