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 | /*! |
31 | * \qmlattachedproperty int AxisLabels::index |
32 | */ |
33 | Q_PROPERTY(int index READ index NOTIFY indexChanged) |
34 | int index() const; |
35 | void setIndex(int newIndex); |
36 | Q_SIGNAL void indexChanged(); |
37 | |
38 | /*! |
39 | * \qmlattachedproperty string AxisLabels::label |
40 | */ |
41 | Q_PROPERTY(QString label READ label NOTIFY labelChanged) |
42 | QString label() const; |
43 | void setLabel(const QString &newLabel); |
44 | Q_SIGNAL void labelChanged(); |
45 | |
46 | private: |
47 | int m_index = -1; |
48 | QString m_label; |
49 | }; |
50 | |
51 | /*! |
52 | * \qmltype AxisLabels |
53 | * \inqmlmodule org.kde.quickcharts.controls |
54 | * \brief An item that uses a delegate to place axis labels on a chart. |
55 | * \inherits Item |
56 | */ |
57 | class AxisLabels : public QQuickItem |
58 | { |
59 | Q_OBJECT |
60 | QML_ELEMENT |
61 | QML_ATTACHED(AxisLabelsAttached) |
62 | |
63 | public: |
64 | /*! |
65 | * \value HorizontalLeftRight |
66 | * \value HorizontalRightLeft |
67 | * \value VerticalTopBottom |
68 | * \value VerticalBottomTop |
69 | */ |
70 | enum class Direction { HorizontalLeftRight, HorizontalRightLeft, VerticalTopBottom, VerticalBottomTop }; |
71 | Q_ENUM(Direction) |
72 | |
73 | explicit AxisLabels(QQuickItem *parent = nullptr); |
74 | ~AxisLabels() override; |
75 | |
76 | /*! |
77 | * \qmlproperty enumeration AxisLabels::direction |
78 | * \qmlenumeratorsfrom AxisLabels::Direction |
79 | */ |
80 | Q_PROPERTY(AxisLabels::Direction direction READ direction WRITE setDirection NOTIFY directionChanged) |
81 | AxisLabels::Direction direction() const; |
82 | Q_SLOT void setDirection(AxisLabels::Direction newDirection); |
83 | Q_SIGNAL void directionChanged(); |
84 | |
85 | /*! |
86 | * \qmlproperty Component AxisLabels::delegate |
87 | */ |
88 | Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged) |
89 | QQmlComponent *delegate() const; |
90 | Q_SLOT void setDelegate(QQmlComponent *newDelegate); |
91 | Q_SIGNAL void delegateChanged(); |
92 | |
93 | /*! |
94 | * \qmlproperty ChartDataSource AxisLabels::source |
95 | */ |
96 | Q_PROPERTY(ChartDataSource *source READ source WRITE setSource NOTIFY sourceChanged) |
97 | ChartDataSource *source() const; |
98 | Q_SLOT void setSource(ChartDataSource *newSource); |
99 | Q_SIGNAL void sourceChanged(); |
100 | |
101 | /*! |
102 | * \qmlproperty Qt.Alignment AxisLabels::alignment |
103 | */ |
104 | Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged) |
105 | Qt::Alignment alignment() const; |
106 | Q_SLOT void setAlignment(Qt::Alignment newAlignment); |
107 | Q_SIGNAL void alignmentChanged(); |
108 | |
109 | /*! |
110 | * \qmlproperty bool AxisLabels::constrainToBounds |
111 | */ |
112 | Q_PROPERTY(bool constrainToBounds READ constrainToBounds WRITE setConstrainToBounds NOTIFY constrainToBoundsChanged) |
113 | bool constrainToBounds() const; |
114 | Q_SLOT void setConstrainToBounds(bool newConstrainToBounds); |
115 | Q_SIGNAL void constrainToBoundsChanged(); |
116 | |
117 | static AxisLabelsAttached *qmlAttachedProperties(QObject *object) |
118 | { |
119 | return new AxisLabelsAttached(object); |
120 | } |
121 | |
122 | protected: |
123 | void updatePolish() override; |
124 | void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; |
125 | |
126 | private: |
127 | bool isHorizontal(); |
128 | void updateLabels(); |
129 | void onBeginCreate(int index, QQuickItem *item); |
130 | |
131 | Direction m_direction = Direction::HorizontalLeftRight; |
132 | ChartDataSource *m_source = nullptr; |
133 | Qt::Alignment m_alignment = Qt::AlignHCenter | Qt::AlignVCenter; |
134 | bool m_constrainToBounds = true; |
135 | |
136 | std::unique_ptr<ItemBuilder> m_itemBuilder; |
137 | bool m_layoutScheduled = false; |
138 | }; |
139 | |
140 | #endif // AXISLABELS_H |
141 | |