1 | // Copyright (C) 2017 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qquickviewcontroller_p.h" |
5 | #include "QtWebView/private/qwebview_p.h" |
6 | |
7 | #include <QtGui/QWindow> |
8 | #include <QtQuick/QQuickWindow> |
9 | |
10 | #include <QtQuick/qquickrendercontrol.h> |
11 | #include <QtQuick/private/qquickitem_p.h> |
12 | #include <QtQuick/private/qquickitemchangelistener_p.h> |
13 | |
14 | QT_BEGIN_NAMESPACE |
15 | |
16 | static const QQuickItemPrivate::ChangeTypes changeMask = |
17 | QQuickItemPrivate::Geometry | QQuickItemPrivate::Children | QQuickItemPrivate::Parent; |
18 | |
19 | class QQuickViewChangeListener : public QQuickItemChangeListener |
20 | { |
21 | public: |
22 | explicit QQuickViewChangeListener(QQuickViewController *item); |
23 | ~QQuickViewChangeListener(); |
24 | |
25 | inline void itemGeometryChanged(QQuickItem *, QQuickGeometryChange, const QRectF &) override; |
26 | void itemChildRemoved(QQuickItem *item, QQuickItem *child) override; |
27 | void itemParentChanged(QQuickItem *item, QQuickItem *parent) override; |
28 | |
29 | private: |
30 | Q_DISABLE_COPY(QQuickViewChangeListener) |
31 | QQuickViewController *m_item; |
32 | void addAncestorListeners(QQuickItem *item, QQuickItemPrivate::ChangeTypes changeType); |
33 | void removeAncestorListeners(QQuickItem *item, QQuickItemPrivate::ChangeTypes changeType); |
34 | bool isAncestor(QQuickItem *item); |
35 | }; |
36 | |
37 | QQuickViewChangeListener::QQuickViewChangeListener(QQuickViewController *item) : m_item(item) |
38 | { |
39 | // Only listen for parent changes on the view controller item. |
40 | QQuickItemPrivate::get(item)->addItemChangeListener(listener: this, types: QQuickItemPrivate::Parent); |
41 | // Listen to all changes, that are relevant, on all ancestors. |
42 | addAncestorListeners(item: item->parentItem(), changeType: changeMask); |
43 | } |
44 | |
45 | QQuickViewChangeListener::~QQuickViewChangeListener() |
46 | { |
47 | if (m_item == 0) |
48 | return; |
49 | |
50 | QQuickItemPrivate::get(item: m_item)->removeItemChangeListener(this, types: QQuickItemPrivate::Parent); |
51 | removeAncestorListeners(item: m_item->parentItem(), changeType: changeMask); |
52 | } |
53 | |
54 | void QQuickViewChangeListener::itemGeometryChanged(QQuickItem *, QQuickGeometryChange, |
55 | const QRectF &) |
56 | { |
57 | m_item->polish(); |
58 | } |
59 | |
60 | void QQuickViewChangeListener::itemChildRemoved(QQuickItem *item, QQuickItem *child) |
61 | { |
62 | Q_UNUSED(item); |
63 | Q_ASSERT(item != m_item); |
64 | |
65 | const bool remove = (child == m_item) || isAncestor(item: child); |
66 | |
67 | // if the child isn't the view item or its ancestor, then we don't care. |
68 | if (!remove) |
69 | return; |
70 | |
71 | // Remove any listener we attached to the child and its ancestors. |
72 | removeAncestorListeners(item, changeType: changeMask); |
73 | } |
74 | |
75 | void QQuickViewChangeListener::itemParentChanged(QQuickItem *item, QQuickItem *newParent) |
76 | { |
77 | removeAncestorListeners(item: item->parentItem(), changeType: changeMask); |
78 | // Adds this as a listener for newParent and its ancestors. |
79 | addAncestorListeners(item: newParent, changeType: changeMask); |
80 | } |
81 | |
82 | void QQuickViewChangeListener::addAncestorListeners(QQuickItem *item, |
83 | QQuickItemPrivate::ChangeTypes changeType) |
84 | { |
85 | QQuickItem *p = item; |
86 | while (p != 0) { |
87 | QQuickItemPrivate::get(item: p)->addItemChangeListener(listener: this, types: changeType); |
88 | p = p->parentItem(); |
89 | } |
90 | } |
91 | |
92 | void QQuickViewChangeListener::removeAncestorListeners(QQuickItem *item, |
93 | QQuickItemPrivate::ChangeTypes changeType) |
94 | { |
95 | QQuickItem *p = item; |
96 | while (p != 0) { |
97 | QQuickItemPrivate::get(item: p)->removeItemChangeListener(this, types: changeType); |
98 | p = p->parentItem(); |
99 | } |
100 | } |
101 | |
102 | bool QQuickViewChangeListener::isAncestor(QQuickItem *item) |
103 | { |
104 | Q_ASSERT(m_item != 0); |
105 | |
106 | if (item == 0) |
107 | return false; |
108 | |
109 | QQuickItem *p = m_item->parentItem(); |
110 | while (p != 0) { |
111 | if (p == item) |
112 | return true; |
113 | p = p->parentItem(); |
114 | } |
115 | |
116 | return false; |
117 | } |
118 | |
119 | /// |
120 | /// \brief QQuickViewController::QQuickViewController |
121 | /// \param parent |
122 | /// |
123 | |
124 | QQuickViewController::QQuickViewController(QQuickItem *parent) |
125 | : QQuickItem(parent), m_view(0), m_changeListener(new QQuickViewChangeListener(this)) |
126 | { |
127 | connect(sender: this, signal: &QQuickViewController::windowChanged, context: this, |
128 | slot: &QQuickViewController::onWindowChanged); |
129 | connect(sender: this, signal: &QQuickViewController::visibleChanged, context: this, |
130 | slot: &QQuickViewController::onVisibleChanged); |
131 | } |
132 | |
133 | QQuickViewController::~QQuickViewController() |
134 | { |
135 | disconnect(receiver: this); |
136 | onWindowChanged(window: nullptr); |
137 | } |
138 | |
139 | void QQuickViewController::componentComplete() |
140 | { |
141 | QQuickItem::componentComplete(); |
142 | m_view->init(); |
143 | m_view->setVisibility(QWindow::Windowed); |
144 | } |
145 | |
146 | void QQuickViewController::updatePolish() |
147 | { |
148 | if (m_view == nullptr) |
149 | return; |
150 | |
151 | QSize itemSize = QSize(width(), height()); |
152 | if (!itemSize.isValid()) |
153 | return; |
154 | |
155 | QQuickWindow *w = window(); |
156 | if (w == nullptr) |
157 | return; |
158 | |
159 | // Find this item's geometry in the scene. |
160 | QRect itemGeometry = mapRectToScene(rect: QRect(QPoint(0, 0), itemSize)).toRect(); |
161 | // Check if we should be clipped to our parent's shape |
162 | // Note: This is crude but it should give an acceptable result on all platforms. |
163 | QQuickItem *p = parentItem(); |
164 | const bool clip = p != 0 ? p->clip() : false; |
165 | if (clip) { |
166 | const QSize &parentSize = QSize(p->width(), p->height()); |
167 | const QRect &parentGeometry = p->mapRectToScene(rect: QRect(QPoint(0, 0), parentSize)).toRect(); |
168 | itemGeometry &= parentGeometry; |
169 | itemSize = itemGeometry.size(); |
170 | } |
171 | |
172 | // Find the top left position of this item, in global coordinates. |
173 | const QPoint &tl = w->mapToGlobal(pos: itemGeometry.topLeft()); |
174 | // Get the actual render window, in case we're rendering into a off-screen window. |
175 | QWindow *rw = QQuickRenderControl::renderWindowFor(win: w); |
176 | |
177 | m_view->setGeometry(rw ? QRect(rw->mapFromGlobal(pos: tl), itemSize) : itemGeometry); |
178 | m_view->setVisible(isVisible()); |
179 | m_view->updatePolish(); |
180 | } |
181 | |
182 | void QQuickViewController::setView(QNativeViewController *view) |
183 | { |
184 | Q_ASSERT(m_view == 0); |
185 | m_view = view; |
186 | } |
187 | |
188 | void QQuickViewController::scheduleUpdatePolish() |
189 | { |
190 | polish(); |
191 | } |
192 | |
193 | void QQuickViewController::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) |
194 | { |
195 | QQuickItem::geometryChange(newGeometry, oldGeometry); |
196 | if (newGeometry.isValid()) |
197 | polish(); |
198 | } |
199 | |
200 | void QQuickViewController::onWindowChanged(QQuickWindow *window) |
201 | { |
202 | QQuickWindow *oldParent = qobject_cast<QQuickWindow *>(object: m_view->parentView()); |
203 | if (oldParent) |
204 | oldParent->disconnect(receiver: this); |
205 | |
206 | if (!window) { |
207 | m_view->setParentView(nullptr); |
208 | return; |
209 | } |
210 | |
211 | // Check if there's an actual native window available. |
212 | QWindow *rw = QQuickRenderControl::renderWindowFor(win: window); |
213 | |
214 | if (rw) { |
215 | connect(sender: rw, signal: &QWindow::widthChanged, context: this, slot: &QQuickViewController::scheduleUpdatePolish); |
216 | connect(sender: rw, signal: &QWindow::heightChanged, context: this, slot: &QQuickViewController::scheduleUpdatePolish); |
217 | connect(sender: rw, signal: &QWindow::xChanged, context: this, slot: &QQuickViewController::scheduleUpdatePolish); |
218 | connect(sender: rw, signal: &QWindow::yChanged, context: this, slot: &QQuickViewController::scheduleUpdatePolish); |
219 | connect(sender: rw, signal: &QWindow::visibleChanged, context: this, |
220 | slot: [this](bool visible) { m_view->setVisible(visible); }); |
221 | connect(sender: window, signal: &QQuickWindow::sceneGraphInitialized, context: this, |
222 | slot: &QQuickViewController::scheduleUpdatePolish); |
223 | connect(sender: window, signal: &QQuickWindow::sceneGraphInvalidated, context: this, |
224 | slot: &QQuickViewController::onSceneGraphInvalidated); |
225 | connect(sender: rw, signal: &QWindow::visibilityChanged, context: this, slot: &QQuickViewController::scheduleUpdatePolish); |
226 | m_view->setParentView(rw); |
227 | } else { |
228 | connect(sender: window, signal: &QWindow::widthChanged, context: this, slot: &QQuickViewController::scheduleUpdatePolish); |
229 | connect(sender: window, signal: &QWindow::heightChanged, context: this, slot: &QQuickViewController::scheduleUpdatePolish); |
230 | connect(sender: window, signal: &QWindow::xChanged, context: this, slot: &QQuickViewController::scheduleUpdatePolish); |
231 | connect(sender: window, signal: &QWindow::yChanged, context: this, slot: &QQuickViewController::scheduleUpdatePolish); |
232 | connect(sender: window, signal: &QQuickWindow::sceneGraphInitialized, context: this, |
233 | slot: &QQuickViewController::scheduleUpdatePolish); |
234 | connect(sender: window, signal: &QQuickWindow::sceneGraphInvalidated, context: this, |
235 | slot: &QQuickViewController::onSceneGraphInvalidated); |
236 | connect(sender: window, signal: &QWindow::visibilityChanged, context: this, slot: [this](QWindow::Visibility visibility) { |
237 | m_view->setVisible(visibility != QWindow::Hidden); |
238 | }); |
239 | connect(sender: window, signal: &QWindow::visibilityChanged, context: this, slot: &QQuickViewController::scheduleUpdatePolish); |
240 | m_view->setVisible(window->visibility() != QWindow::Hidden); |
241 | m_view->setParentView(window); |
242 | } |
243 | } |
244 | |
245 | void QQuickViewController::onVisibleChanged() |
246 | { |
247 | m_view->setVisible(isVisible()); |
248 | } |
249 | |
250 | void QQuickViewController::onSceneGraphInvalidated() |
251 | { |
252 | if (m_view == 0) |
253 | return; |
254 | |
255 | m_view->setVisible(false); |
256 | } |
257 | |
258 | QT_END_NAMESPACE |
259 | |