1 | /* |
2 | * SPDX-FileCopyrightText: 2018 Marco Martin <mart@kde.org> |
3 | * |
4 | * SPDX-License-Identifier: LGPL-2.0-or-later |
5 | */ |
6 | |
7 | #ifndef SCENEPOSITIONATTACHED_H |
8 | #define SCENEPOSITIONATTACHED_H |
9 | |
10 | #include <QObject> |
11 | #include <QQmlEngine> |
12 | |
13 | class QQuickItem; |
14 | |
15 | /*! |
16 | * \qmltype ScenePosition |
17 | * \inqmlmodule org.kde.kirigami |
18 | * |
19 | * \brief An attached property that contains the information about the scene position of the item. |
20 | * |
21 | * Its global x and y coordinates will update automatically and can be binded. |
22 | * \code |
23 | * import org.kde.kirigami as Kirigami |
24 | * Text { |
25 | * text: ScenePosition.x |
26 | * } |
27 | * \endcode |
28 | */ |
29 | class ScenePositionAttached : public QObject |
30 | { |
31 | Q_OBJECT |
32 | QML_ELEMENT |
33 | QML_ATTACHED(ScenePositionAttached) |
34 | QML_NAMED_ELEMENT(ScenePosition) |
35 | QML_UNCREATABLE("" ) |
36 | |
37 | /*! \qmlattachedproperty double org.kde.kirigami::ScenePosition::x |
38 | * |
39 | * The global scene X position |
40 | * |
41 | */ |
42 | Q_PROPERTY(qreal x READ x NOTIFY xChanged FINAL) |
43 | |
44 | /*! \qmlattachedproperty double org.kde.kirigami::ScenePosition::y |
45 | * |
46 | * The global scene Y position |
47 | * |
48 | */ |
49 | Q_PROPERTY(qreal y READ y NOTIFY yChanged FINAL) |
50 | |
51 | public: |
52 | explicit ScenePositionAttached(QObject *parent = nullptr); |
53 | ~ScenePositionAttached() override; |
54 | |
55 | qreal x() const; |
56 | qreal y() const; |
57 | |
58 | // QML attached property |
59 | static ScenePositionAttached *qmlAttachedProperties(QObject *object); |
60 | |
61 | Q_SIGNALS: |
62 | void xChanged(); |
63 | void yChanged(); |
64 | |
65 | private: |
66 | void connectAncestors(QQuickItem *item); |
67 | |
68 | QQuickItem *m_item = nullptr; |
69 | QList<QQuickItem *> m_ancestors; |
70 | }; |
71 | |
72 | QML_DECLARE_TYPEINFO(ScenePositionAttached, QML_HAS_ATTACHED_PROPERTIES) |
73 | |
74 | #endif // SCENEPOSITIONATTACHED_H |
75 | |