| 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 | * \qmltype GridLines |
| 61 | * \inqmlmodule org.kde.quickcharts.controls |
| 62 | * \brief An item that renders a set of lines to make a grid for a chart. |
| 63 | */ |
| 64 | class GridLines : public QQuickItem |
| 65 | { |
| 66 | Q_OBJECT |
| 67 | QML_ELEMENT |
| 68 | |
| 69 | public: |
| 70 | /*! |
| 71 | * \value Horizontal |
| 72 | * \value Vertical |
| 73 | */ |
| 74 | enum class Direction { Horizontal, Vertical }; |
| 75 | Q_ENUM(Direction) |
| 76 | |
| 77 | explicit GridLines(QQuickItem *parent = nullptr); |
| 78 | |
| 79 | /*! |
| 80 | * \qmlproperty enumeration GridLines::direction |
| 81 | * \qmlenumeratorsfrom GridLines::Direction |
| 82 | */ |
| 83 | Q_PROPERTY(GridLines::Direction direction READ direction WRITE setDirection NOTIFY directionChanged) |
| 84 | Direction direction() const; |
| 85 | void setDirection(GridLines::Direction newDirection); |
| 86 | Q_SIGNAL void directionChanged(); |
| 87 | |
| 88 | /*! |
| 89 | * \qmlproperty XYChart GridLines::chart |
| 90 | */ |
| 91 | Q_PROPERTY(XYChart *chart READ chart WRITE setChart NOTIFY chartChanged) |
| 92 | XYChart *chart() const; |
| 93 | void setChart(XYChart *newChart); |
| 94 | Q_SIGNAL void chartChanged(); |
| 95 | |
| 96 | /*! |
| 97 | * \qmlproperty real GridLines::spacing |
| 98 | */ |
| 99 | Q_PROPERTY(float spacing READ spacing WRITE setSpacing NOTIFY spacingChanged) |
| 100 | float spacing() const; |
| 101 | void setSpacing(float newSpacing); |
| 102 | Q_SIGNAL void spacingChanged(); |
| 103 | |
| 104 | /*! |
| 105 | * \qmlproperty bool GridLines::major.visible |
| 106 | * \qmlproperty color GridLines::major.color |
| 107 | * \qmlproperty real GridLines::major.lineWidth |
| 108 | * \qmlproperty int GridLines::major.frequency |
| 109 | * \qmlproperty int GridLines::major.count |
| 110 | */ |
| 111 | Q_PROPERTY(LinePropertiesGroup *major READ majorGroup CONSTANT) |
| 112 | LinePropertiesGroup *majorGroup() const; |
| 113 | |
| 114 | /*! |
| 115 | * \qmlproperty bool GridLines::minor.visible |
| 116 | * \qmlproperty color GridLines::minor.color |
| 117 | * \qmlproperty real GridLines::minor.lineWidth |
| 118 | * \qmlproperty int GridLines::minor.frequency |
| 119 | * \qmlproperty int GridLines::minor.count |
| 120 | */ |
| 121 | Q_PROPERTY(LinePropertiesGroup *minor READ minorGroup CONSTANT) |
| 122 | LinePropertiesGroup *minorGroup() const; |
| 123 | |
| 124 | private: |
| 125 | QSGNode *updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *) override; |
| 126 | void updateLines(LineGridNode *node, LinePropertiesGroup *properties); |
| 127 | |
| 128 | GridLines::Direction m_direction = Direction::Horizontal; |
| 129 | XYChart *m_chart = nullptr; |
| 130 | float m_spacing = 10.0; |
| 131 | |
| 132 | std::unique_ptr<LinePropertiesGroup> m_major; |
| 133 | std::unique_ptr<LinePropertiesGroup> m_minor; |
| 134 | }; |
| 135 | |
| 136 | #endif // GRIDLINES_H |
| 137 | |