2
* Copyright 2013 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#include <scenerectwatcher.h>
19
SceneRectWatcher::SceneRectWatcher(QObject *parent)
22
m_sceneRectChangedTimer.setInterval(200);
23
m_sceneRectChangedTimer.setSingleShot(true);
24
connect(&m_sceneRectChangedTimer, &QTimer::timeout,
25
this, &SceneRectWatcher::sceneRectChanged);
28
void SceneRectWatcher::setItem(QQuickItem *target)
30
if (m_target == target)
33
m_sceneRectChangedTimer.stop();
35
listenToAscendantsChanges();
38
void SceneRectWatcher::listenToAscendantsChanges()
40
disconnectFromAscendantsChanges();
45
QQuickItem* item = m_target.data();
47
m_connections.append(connect(item, &QQuickItem::widthChanged,
48
this, &SceneRectWatcher::notifySceneRectChange));
49
m_connections.append(connect(item, &QQuickItem::heightChanged,
50
this, &SceneRectWatcher::notifySceneRectChange));
52
while (item != NULL) {
53
m_connections.append(connect(item, &QQuickItem::parentChanged,
54
this, &SceneRectWatcher::onAscendantChanged));
55
m_connections.append(connect(item, &QQuickItem::xChanged,
56
this, &SceneRectWatcher::notifySceneRectChange));
57
m_connections.append(connect(item, &QQuickItem::yChanged,
58
this, &SceneRectWatcher::notifySceneRectChange));
59
m_connections.append(connect(item, &QQuickItem::transformOriginChanged,
60
this, &SceneRectWatcher::notifySceneRectChange));
61
m_connections.append(connect(item, &QQuickItem::rotationChanged,
62
this, &SceneRectWatcher::notifySceneRectChange));
63
m_connections.append(connect(item, &QQuickItem::scaleChanged,
64
this, &SceneRectWatcher::notifySceneRectChange));
65
item = item->parentItem();
69
void SceneRectWatcher::disconnectFromAscendantsChanges()
71
// disconnect all previously connected signals
72
Q_FOREACH (QMetaObject::Connection connection, m_connections) {
73
disconnect(connection);
75
m_connections.clear();
78
void SceneRectWatcher::onAscendantChanged()
80
// Simple approach: Just rebuild connections from scratch.
81
listenToAscendantsChanges();
83
notifySceneRectChange();
86
void SceneRectWatcher::notifySceneRectChange()
88
if (!m_sceneRectChangedTimer.isActive()) {
89
m_sceneRectChangedTimer.start();