summaryrefslogtreecommitdiff
path: root/src/controls/Private/qquicksceneposlistener.cpp
blob: cb0cadfacee7ec2b3cad6ddbf0bb2ce4eed60801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Quick Controls module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qquicksceneposlistener_p.h"
#include <QtQuick/private/qquickitem_p.h>

QT_BEGIN_NAMESPACE

static const QQuickItemPrivate::ChangeTypes AncestorChangeTypes = QQuickItemPrivate::Geometry
                                                                  | QQuickItemPrivate::Parent
                                                                  | QQuickItemPrivate::Children;

static const QQuickItemPrivate::ChangeTypes ItemChangeTypes = QQuickItemPrivate::Geometry
                                                             | QQuickItemPrivate::Parent
                                                             | QQuickItemPrivate::Destroyed;

QQuickScenePosListener1::QQuickScenePosListener1(QObject *parent)
    : QObject(parent)
    , m_enabled(false)
    , m_item(0)
{
}

QQuickScenePosListener1::~QQuickScenePosListener1()
{
    if (m_item == 0)
        return;

    QQuickItemPrivate::get(m_item)->removeItemChangeListener(this, ItemChangeTypes);
    removeAncestorListeners(m_item->parentItem());
}

QQuickItem *QQuickScenePosListener1::item() const
{
    return m_item;
}

void QQuickScenePosListener1::setItem(QQuickItem *item)
{
    if (m_item == item)
        return;

    if (m_item != 0) {
        QQuickItemPrivate::get(m_item)->removeItemChangeListener(this, ItemChangeTypes);
        removeAncestorListeners(m_item->parentItem());
    }

    m_item = item;

    if (m_item == 0)
        return;

    if (m_enabled) {
        QQuickItemPrivate::get(m_item)->addItemChangeListener(this, ItemChangeTypes);
        addAncestorListeners(m_item->parentItem());
    }

    updateScenePos();
}

QPointF QQuickScenePosListener1::scenePos() const
{
    return m_scenePos;
}

bool QQuickScenePosListener1::isEnabled() const
{
    return m_enabled;
}

void QQuickScenePosListener1::setEnabled(bool enabled)
{
    if (m_enabled == enabled)
        return;

    m_enabled = enabled;

    if (m_item != 0) {
        if (enabled) {
            QQuickItemPrivate::get(m_item)->addItemChangeListener(this, ItemChangeTypes);
            addAncestorListeners(m_item->parentItem());
        } else {
            QQuickItemPrivate::get(m_item)->removeItemChangeListener(this, ItemChangeTypes);
            removeAncestorListeners(m_item->parentItem());
        }
    }

    emit enabledChanged();
}

void QQuickScenePosListener1::itemGeometryChanged(QQuickItem *, QQuickGeometryChange, const QRectF &)
{
    updateScenePos();
}

void QQuickScenePosListener1::itemParentChanged(QQuickItem *, QQuickItem *parent)
{
    addAncestorListeners(parent);
}

void QQuickScenePosListener1::itemChildRemoved(QQuickItem *, QQuickItem *child)
{
    if (isAncestor(child))
        removeAncestorListeners(child);
}

void QQuickScenePosListener1::itemDestroyed(QQuickItem *item)
{
    Q_ASSERT(m_item == item);

    // Remove all injected listeners.
    m_item = 0;
    QQuickItemPrivate::get(item)->removeItemChangeListener(this, ItemChangeTypes);
    removeAncestorListeners(item->parentItem());
}

void QQuickScenePosListener1::updateScenePos()
{
    const QPointF &scenePos = m_item->mapToScene(QPointF(0, 0));
    if (m_scenePos != scenePos) {
        m_scenePos = scenePos;
        emit scenePosChanged();
    }
}

/*!
    \internal

    Remove this listener from \a item and all its ancestors.
 */
void QQuickScenePosListener1::removeAncestorListeners(QQuickItem *item)
{
    if (item == m_item)
        return;

    QQuickItem *p = item;
    while (p != 0) {
        QQuickItemPrivate::get(p)->removeItemChangeListener(this, AncestorChangeTypes);
        p = p->parentItem();
    }
}

/*!
    \internal

    Injects this as a listener to \a item and all its ancestors.
 */
void QQuickScenePosListener1::addAncestorListeners(QQuickItem *item)
{
    if (item == m_item)
        return;

    QQuickItem *p = item;
    while (p != 0) {
        QQuickItemPrivate::get(p)->addItemChangeListener(this, AncestorChangeTypes);
        p = p->parentItem();
    }
}

bool QQuickScenePosListener1::isAncestor(QQuickItem *item) const
{
    if (!m_item)
        return false;

    QQuickItem *parent = m_item->parentItem();
    while (parent) {
        if (parent == item)
            return true;
        parent = parent->parentItem();
    }
    return false;
}

QT_END_NAMESPACE