1 | /* |
2 | * This file is part of KQuickCharts |
3 | * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl> |
4 | * |
5 | * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
6 | */ |
7 | |
8 | #ifndef GRIDLINES_H |
9 | #define GRIDLINES_H |
10 | |
11 | #include <memory> |
12 | |
13 | #include <QQuickItem> |
14 | |
15 | class GridLines; |
16 | class LineGridNode; |
17 | class XYChart; |
18 | |
19 | class LinePropertiesGroup : public QObject |
20 | { |
21 | Q_OBJECT |
22 | QML_ELEMENT |
23 | QML_UNCREATABLE("Grouped Property" ) |
24 | |
25 | public: |
26 | explicit LinePropertiesGroup(GridLines *parent); |
27 | |
28 | Q_PROPERTY(bool visible READ visible WRITE setVisible NOTIFY propertiesChanged) |
29 | bool visible() const; |
30 | void setVisible(bool newVisible); |
31 | |
32 | Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY propertiesChanged) |
33 | QColor color() const; |
34 | void setColor(const QColor &newColor); |
35 | |
36 | Q_PROPERTY(float lineWidth READ lineWidth WRITE setLineWidth NOTIFY propertiesChanged) |
37 | float lineWidth() const; |
38 | void setLineWidth(float newLineWidth); |
39 | |
40 | Q_PROPERTY(int frequency READ frequency WRITE setFrequency NOTIFY propertiesChanged) |
41 | int frequency() const; |
42 | void setFrequency(int newFrequency); |
43 | |
44 | Q_PROPERTY(int count READ count WRITE setCount NOTIFY propertiesChanged) |
45 | int count() const; |
46 | void setCount(int newCount); |
47 | |
48 | Q_SIGNAL void propertiesChanged(); |
49 | |
50 | private: |
51 | GridLines *m_parent = nullptr; |
52 | bool m_visible = true; |
53 | QColor m_color = Qt::black; |
54 | float m_lineWidth = 1.0; |
55 | int m_frequency = 2; |
56 | int m_count = -1; |
57 | }; |
58 | |
59 | /** |
60 | * An item that renders a set of lines to make a grid for a chart. |
61 | */ |
62 | class GridLines : public QQuickItem |
63 | { |
64 | Q_OBJECT |
65 | QML_ELEMENT |
66 | |
67 | public: |
68 | enum class Direction { Horizontal, Vertical }; |
69 | Q_ENUM(Direction) |
70 | /** |
71 | * Default constructor |
72 | */ |
73 | explicit GridLines(QQuickItem *parent = nullptr); |
74 | |
75 | Q_PROPERTY(GridLines::Direction direction READ direction WRITE setDirection NOTIFY directionChanged) |
76 | Direction direction() const; |
77 | void setDirection(GridLines::Direction newDirection); |
78 | Q_SIGNAL void directionChanged(); |
79 | |
80 | Q_PROPERTY(XYChart *chart READ chart WRITE setChart NOTIFY chartChanged) |
81 | XYChart *chart() const; |
82 | void setChart(XYChart *newChart); |
83 | Q_SIGNAL void chartChanged(); |
84 | |
85 | Q_PROPERTY(float spacing READ spacing WRITE setSpacing NOTIFY spacingChanged) |
86 | float spacing() const; |
87 | void setSpacing(float newSpacing); |
88 | Q_SIGNAL void spacingChanged(); |
89 | |
90 | Q_PROPERTY(LinePropertiesGroup *major READ majorGroup CONSTANT) |
91 | LinePropertiesGroup *majorGroup() const; |
92 | |
93 | Q_PROPERTY(LinePropertiesGroup *minor READ minorGroup CONSTANT) |
94 | LinePropertiesGroup *minorGroup() const; |
95 | |
96 | private: |
97 | QSGNode *updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *) override; |
98 | void updateLines(LineGridNode *node, LinePropertiesGroup *properties); |
99 | |
100 | GridLines::Direction m_direction = Direction::Horizontal; |
101 | XYChart *m_chart = nullptr; |
102 | float m_spacing = 10.0; |
103 | |
104 | std::unique_ptr<LinePropertiesGroup> m_major; |
105 | std::unique_ptr<LinePropertiesGroup> m_minor; |
106 | }; |
107 | |
108 | #endif // GRIDLINES_H |
109 | |