1 | /* |
2 | * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl> |
3 | * |
4 | * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | |
7 | #ifndef LEGENDLAYOUT_H |
8 | #define LEGENDLAYOUT_H |
9 | |
10 | #include <QQuickItem> |
11 | #include <qqmlregistration.h> |
12 | |
13 | class LegendLayoutAttached : public QObject |
14 | { |
15 | Q_OBJECT |
16 | QML_ANONYMOUS |
17 | |
18 | public: |
19 | explicit LegendLayoutAttached(QObject *parent = nullptr); |
20 | |
21 | Q_PROPERTY(qreal minimumWidth READ minimumWidth WRITE setMinimumWidth NOTIFY minimumWidthChanged) |
22 | qreal minimumWidth() const; |
23 | void setMinimumWidth(qreal newMinimumWidth); |
24 | Q_SIGNAL void minimumWidthChanged(); |
25 | bool isMinimumWidthValid() const; |
26 | |
27 | Q_PROPERTY(qreal preferredWidth READ preferredWidth WRITE setPreferredWidth NOTIFY preferredWidthChanged) |
28 | qreal preferredWidth() const; |
29 | void setPreferredWidth(qreal newPreferredWidth); |
30 | Q_SIGNAL void preferredWidthChanged(); |
31 | bool isPreferredWidthValid() const; |
32 | |
33 | Q_PROPERTY(qreal maximumWidth READ maximumWidth WRITE setMaximumWidth NOTIFY maximumWidthChanged) |
34 | qreal maximumWidth() const; |
35 | void setMaximumWidth(qreal newMaximumWidth); |
36 | Q_SIGNAL void maximumWidthChanged(); |
37 | bool isMaximumWidthValid() const; |
38 | |
39 | private: |
40 | std::optional<qreal> m_minimumWidth; |
41 | std::optional<qreal> m_preferredWidth; |
42 | std::optional<qreal> m_maximumWidth; |
43 | }; |
44 | |
45 | class LegendLayout : public QQuickItem |
46 | { |
47 | Q_OBJECT |
48 | QML_ELEMENT |
49 | QML_ATTACHED(LegendLayoutAttached) |
50 | |
51 | public: |
52 | explicit LegendLayout(QQuickItem *parent = nullptr); |
53 | |
54 | Q_PROPERTY(qreal horizontalSpacing READ horizontalSpacing WRITE setHorizontalSpacing NOTIFY horizontalSpacingChanged) |
55 | qreal horizontalSpacing() const; |
56 | void setHorizontalSpacing(qreal newHorizontalSpacing); |
57 | Q_SIGNAL void horizontalSpacingChanged(); |
58 | |
59 | Q_PROPERTY(qreal verticalSpacing READ verticalSpacing WRITE setVerticalSpacing NOTIFY verticalSpacingChanged) |
60 | qreal verticalSpacing() const; |
61 | void setVerticalSpacing(qreal newVerticalSpacing); |
62 | Q_SIGNAL void verticalSpacingChanged(); |
63 | |
64 | Q_PROPERTY(qreal preferredWidth READ preferredWidth NOTIFY preferredWidthChanged) |
65 | qreal preferredWidth() const; |
66 | Q_SIGNAL void preferredWidthChanged(); |
67 | |
68 | static LegendLayoutAttached *qmlAttachedProperties(QObject *object) |
69 | { |
70 | return new LegendLayoutAttached(object); |
71 | } |
72 | |
73 | protected: |
74 | void componentComplete() override; |
75 | void updatePolish() override; |
76 | void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; |
77 | void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) override; |
78 | |
79 | private: |
80 | void updateItem(int index, QQuickItem *item); |
81 | std::tuple<int, int, qreal, qreal> determineColumns(); |
82 | |
83 | qreal m_horizontalSpacing = 0.0; |
84 | qreal m_verticalSpacing = 0.0; |
85 | qreal m_preferredWidth = 0.0; |
86 | |
87 | bool m_completed = false; |
88 | }; |
89 | |
90 | #endif // LEGENDLAYOUT_H |
91 | |