Kirigami2

scenepositionattached.cpp
1/*
2 * SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
3 * SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8#include "scenepositionattached.h"
9#include <QDebug>
10#include <QQuickItem>
11
12ScenePositionAttached::ScenePositionAttached(QObject *parent)
13 : QObject(parent)
14{
15 m_item = qobject_cast<QQuickItem *>(parent);
16 connectAncestors(m_item);
17}
18
19ScenePositionAttached::~ScenePositionAttached()
20{
21}
22
23qreal ScenePositionAttached::x() const
24{
25 qreal x = 0.0;
26 QQuickItem *item = m_item;
27
28 while (item) {
29 x += item->x();
30 item = item->parentItem();
31 }
32
33 return x;
34}
35
36qreal ScenePositionAttached::y() const
37{
38 qreal y = 0.0;
39 QQuickItem *item = m_item;
40
41 while (item) {
42 y += item->y();
43 item = item->parentItem();
44 }
45
46 return y;
47}
48
49void ScenePositionAttached::connectAncestors(QQuickItem *item)
50{
51 if (!item) {
52 return;
53 }
54
55 QQuickItem *ancestor = item;
56 while (ancestor) {
57 m_ancestors << ancestor;
58
59 connect(ancestor, &QQuickItem::xChanged, this, &ScenePositionAttached::xChanged);
60 connect(ancestor, &QQuickItem::yChanged, this, &ScenePositionAttached::yChanged);
61 connect(ancestor, &QQuickItem::parentChanged, this, [this, ancestor]() {
62 while (!m_ancestors.isEmpty()) {
63 QQuickItem *last = m_ancestors.takeLast();
64 // Disconnect the item which had its parent changed too,
65 // because connectAncestors() would reconnect it next.
66 disconnect(last, nullptr, this, nullptr);
67 if (last == ancestor) {
68 break;
69 }
70 }
71
72 connectAncestors(ancestor);
73
74 Q_EMIT xChanged();
75 Q_EMIT yChanged();
76 });
77
78 ancestor = ancestor->parentItem();
79 }
80}
81
82ScenePositionAttached *ScenePositionAttached::qmlAttachedProperties(QObject *object)
83{
84 return new ScenePositionAttached(object);
85}
86
87#include "moc_scenepositionattached.cpp"
This attached property contains the information about the scene position of the item: Its global x an...
qreal y
The global scene Y position.
QML_ELEMENTqreal x
The global scene X position.
bool isEmpty() const const
value_type takeLast()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QQuickItem * parentItem() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:51:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.