File indexing completed on 2024-04-28 15:27:44

0001 /*
0002  *  SPDX-FileCopyrightText: 2018 Marco Martin <mart@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef SCENEPOSITIONATTACHED_H
0008 #define SCENEPOSITIONATTACHED_H
0009 
0010 #include <QObject>
0011 #include <QQmlEngine>
0012 
0013 class QQuickItem;
0014 
0015 /**
0016  * @brief This attached property contains items window-local x & y coordinates.
0017  *
0018  * Note that an Item's X and Y coordinates are relative to its parent, meaning
0019  * that when a list of Items has the same parent, the top-most Item will have
0020  * coordinates 0x0.
0021  *
0022  * Use this attached property to get the X and Y coordinates of an Item that are
0023  * relative to their QML scene instead of their parent. This is useful when
0024  * implementing custom views and animations.
0025  *
0026  * Typically the 0x0 coordinate of the QML scene starts at the top left corner
0027  * of the window, below the titlebar.
0028  *
0029  * Example usage:
0030  * @include scenepositionattached.qml
0031  *
0032  * @since org.kde.kirigami 2.3
0033  */
0034 class ScenePositionAttached : public QObject
0035 {
0036     Q_OBJECT
0037     /**
0038      * The global scene X position
0039      */
0040     Q_PROPERTY(int x READ x NOTIFY xChanged)
0041 
0042     /**
0043      * The global scene Y position
0044      */
0045     Q_PROPERTY(int y READ y NOTIFY yChanged)
0046 
0047 public:
0048     explicit ScenePositionAttached(QObject *parent = nullptr);
0049     ~ScenePositionAttached() override;
0050 
0051     int x() const;
0052     int y() const;
0053 
0054     // QML attached property
0055     static ScenePositionAttached *qmlAttachedProperties(QObject *object);
0056 
0057 Q_SIGNALS:
0058     void xChanged();
0059     void yChanged();
0060 
0061 private:
0062     void connectAncestors(QQuickItem *item);
0063 
0064     QQuickItem *m_item = nullptr;
0065     QList<QQuickItem *> m_ancestors;
0066 };
0067 
0068 QML_DECLARE_TYPEINFO(ScenePositionAttached, QML_HAS_ATTACHED_PROPERTIES)
0069 
0070 #endif // SCENEPOSITIONATTACHED_H