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 AXISLABELS_H |
9 | #define AXISLABELS_H |
10 | |
11 | #include <memory> |
12 | |
13 | #include <QQuickItem> |
14 | #include <Qt> |
15 | #include <qqmlregistration.h> |
16 | |
17 | class ChartDataSource; |
18 | class ItemBuilder; |
19 | |
20 | class AxisLabels; |
21 | |
22 | class AxisLabelsAttached : public QObject |
23 | { |
24 | Q_OBJECT |
25 | QML_ANONYMOUS |
26 | |
27 | public: |
28 | explicit AxisLabelsAttached(QObject *parent = nullptr); |
29 | |
30 | Q_PROPERTY(int index READ index NOTIFY indexChanged) |
31 | int index() const; |
32 | void setIndex(int newIndex); |
33 | Q_SIGNAL void indexChanged(); |
34 | |
35 | Q_PROPERTY(QString label READ label NOTIFY labelChanged) |
36 | QString label() const; |
37 | void setLabel(const QString &newLabel); |
38 | Q_SIGNAL void labelChanged(); |
39 | |
40 | private: |
41 | int m_index = -1; |
42 | QString m_label; |
43 | }; |
44 | |
45 | /** |
46 | * An item that uses a delegate to place axis labels on a chart. |
47 | */ |
48 | class AxisLabels : public QQuickItem |
49 | { |
50 | Q_OBJECT |
51 | QML_ELEMENT |
52 | QML_ATTACHED(AxisLabelsAttached) |
53 | |
54 | public: |
55 | enum class Direction { HorizontalLeftRight, HorizontalRightLeft, VerticalTopBottom, VerticalBottomTop }; |
56 | Q_ENUM(Direction) |
57 | |
58 | explicit AxisLabels(QQuickItem *parent = nullptr); |
59 | ~AxisLabels() override; |
60 | |
61 | Q_PROPERTY(AxisLabels::Direction direction READ direction WRITE setDirection NOTIFY directionChanged) |
62 | AxisLabels::Direction direction() const; |
63 | Q_SLOT void setDirection(AxisLabels::Direction newDirection); |
64 | Q_SIGNAL void directionChanged(); |
65 | |
66 | Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged) |
67 | QQmlComponent *delegate() const; |
68 | Q_SLOT void setDelegate(QQmlComponent *newDelegate); |
69 | Q_SIGNAL void delegateChanged(); |
70 | |
71 | Q_PROPERTY(ChartDataSource *source READ source WRITE setSource NOTIFY sourceChanged) |
72 | ChartDataSource *source() const; |
73 | Q_SLOT void setSource(ChartDataSource *newSource); |
74 | Q_SIGNAL void sourceChanged(); |
75 | |
76 | Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged) |
77 | Qt::Alignment alignment() const; |
78 | Q_SLOT void setAlignment(Qt::Alignment newAlignment); |
79 | Q_SIGNAL void alignmentChanged(); |
80 | |
81 | Q_PROPERTY(bool constrainToBounds READ constrainToBounds WRITE setConstrainToBounds NOTIFY constrainToBoundsChanged) |
82 | bool constrainToBounds() const; |
83 | Q_SLOT void setConstrainToBounds(bool newConstrainToBounds); |
84 | Q_SIGNAL void constrainToBoundsChanged(); |
85 | |
86 | static AxisLabelsAttached *qmlAttachedProperties(QObject *object) |
87 | { |
88 | return new AxisLabelsAttached(object); |
89 | } |
90 | |
91 | protected: |
92 | void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; |
93 | |
94 | private: |
95 | void scheduleLayout(); |
96 | bool isHorizontal(); |
97 | void updateLabels(); |
98 | void layout(); |
99 | void onBeginCreate(int index, QQuickItem *item); |
100 | |
101 | Direction m_direction = Direction::HorizontalLeftRight; |
102 | ChartDataSource *m_source = nullptr; |
103 | Qt::Alignment m_alignment = Qt::AlignHCenter | Qt::AlignVCenter; |
104 | bool m_constrainToBounds = true; |
105 | |
106 | std::unique_ptr<ItemBuilder> m_itemBuilder; |
107 | bool m_layoutScheduled = false; |
108 | }; |
109 | |
110 | #endif // AXISLABELS_H |
111 | |