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

source code of qtgraphs/src/common/theme/qgraphstheme.h