1 | // Copyright (C) 2016 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 "globalinspector.h" |
5 | #include "highlight.h" |
6 | #include "inspecttool.h" |
7 | |
8 | #include <private/qqmldebugserviceinterfaces_p.h> |
9 | #include <private/qabstractanimation_p.h> |
10 | #include <private/qqmlcomponent_p.h> |
11 | #include <private/qqmldebugconnector_p.h> |
12 | #include <private/qversionedpacket_p.h> |
13 | |
14 | #include <QtGui/qwindow.h> |
15 | #if QT_CONFIG(regularexpression) |
16 | # include <QtCore/qregularexpression.h> |
17 | #endif |
18 | //INSPECTOR SERVICE PROTOCOL |
19 | // <HEADER><COMMAND><DATA> |
20 | // <HEADER> : <type{request, response, event}><requestId/eventId>[<response_success_bool>] |
21 | // <COMMAND> : {"enable", "disable", "select", "setAnimationSpeed", |
22 | // "showAppOnTop", "createObject", "destroyObject", "moveObject"} |
23 | // <DATA> : select: <debugIds_int_list> |
24 | // setAnimationSpeed: <speed_real> |
25 | // showAppOnTop: <set_bool> |
26 | // createObject: <qml_string><parentId_int><imports_string_list><filename_string> |
27 | // destroyObject: <debugId_int> |
28 | // moveObject: <debugId_int><newParentId_int> |
29 | // Response for "destroyObject" carries the <debugId_int> of the destroyed object. |
30 | |
31 | QT_BEGIN_NAMESPACE |
32 | |
33 | using QQmlDebugPacket = QVersionedPacket<QQmlDebugConnector>; |
34 | |
35 | const char REQUEST[] = "request" ; |
36 | const char RESPONSE[] = "response" ; |
37 | const char EVENT[] = "event" ; |
38 | const char ENABLE[] = "enable" ; |
39 | const char DISABLE[] = "disable" ; |
40 | const char SELECT[] = "select" ; |
41 | const char SET_ANIMATION_SPEED[] = "setAnimationSpeed" ; |
42 | const char SHOW_APP_ON_TOP[] = "showAppOnTop" ; |
43 | const char CREATE_OBJECT[] = "createObject" ; |
44 | const char DESTROY_OBJECT[] = "destroyObject" ; |
45 | const char MOVE_OBJECT[] = "moveObject" ; |
46 | |
47 | namespace QmlJSDebugger { |
48 | |
49 | void GlobalInspector::removeFromSelectedItems(QObject *object) |
50 | { |
51 | if (QQuickItem *item = qobject_cast<QQuickItem*>(o: object)) { |
52 | if (m_selectedItems.removeOne(t: item)) |
53 | delete m_highlightItems.take(key: item); |
54 | } |
55 | } |
56 | |
57 | void GlobalInspector::setSelectedItems(const QList<QQuickItem *> &items) |
58 | { |
59 | if (!syncSelectedItems(items)) |
60 | return; |
61 | |
62 | QList<QObject*> objectList; |
63 | objectList.reserve(asize: items.size()); |
64 | for (QQuickItem *item : items) |
65 | objectList << item; |
66 | |
67 | sendCurrentObjects(objects: objectList); |
68 | } |
69 | |
70 | void GlobalInspector::showSelectedItemName(QQuickItem *item, const QPointF &point) |
71 | { |
72 | SelectionHighlight *highlightItem = m_highlightItems.value(key: item, defaultValue: 0); |
73 | if (highlightItem) |
74 | highlightItem->showName(displayPoint: point); |
75 | } |
76 | |
77 | void GlobalInspector::sendCurrentObjects(const QList<QObject*> &objects) |
78 | { |
79 | QQmlDebugPacket ds; |
80 | |
81 | ds << QByteArray(EVENT) << m_eventId++ << QByteArray(SELECT); |
82 | |
83 | QList<int> debugIds; |
84 | debugIds.reserve(asize: objects.size()); |
85 | for (QObject *object : objects) |
86 | debugIds << QQmlDebugService::idForObject(object); |
87 | ds << debugIds; |
88 | |
89 | emit messageToClient(name: QQmlInspectorService::s_key, data: ds.data()); |
90 | } |
91 | |
92 | static bool reparentQmlObject(QObject *object, QObject *newParent) |
93 | { |
94 | if (!newParent) |
95 | return false; |
96 | |
97 | object->setParent(newParent); |
98 | QQuickItem *newParentItem = qobject_cast<QQuickItem*>(o: newParent); |
99 | QQuickItem *item = qobject_cast<QQuickItem*>(o: object); |
100 | if (newParentItem && item) |
101 | item->setParentItem(newParentItem); |
102 | return true; |
103 | } |
104 | |
105 | class ObjectCreator : public QObject |
106 | { |
107 | Q_OBJECT |
108 | public: |
109 | ObjectCreator(int requestId, QQmlEngine *engine, QObject *parent) : |
110 | QObject(parent), m_component(engine), m_requestId(requestId) |
111 | { |
112 | connect(sender: &m_component, signal: &QQmlComponent::statusChanged, context: this, slot: &ObjectCreator::tryCreateObject); |
113 | } |
114 | |
115 | void run(const QByteArray &qml, const QUrl &filename) |
116 | { |
117 | m_component.setData(qml, baseUrl: filename); |
118 | } |
119 | |
120 | void tryCreateObject(QQmlComponent::Status status) |
121 | { |
122 | switch (status) { |
123 | case QQmlComponent::Error: |
124 | emit result(requestId: m_requestId, success: false); |
125 | delete this; |
126 | return; |
127 | case QQmlComponent::Ready: { |
128 | // Stuff might have changed. We have to lookup the parentContext again. |
129 | QQmlContext *parentContext = QQmlEngine::contextForObject(parent()); |
130 | if (!parentContext) { |
131 | emit result(requestId: m_requestId, success: false); |
132 | } else { |
133 | QObject *newObject = m_component.create(context: parentContext); |
134 | if (newObject && reparentQmlObject(object: newObject, newParent: parent())) |
135 | emit result(requestId: m_requestId, success: true); |
136 | else |
137 | emit result(requestId: m_requestId, success: false); |
138 | } |
139 | deleteLater(); // The component might send more signals |
140 | return; |
141 | } |
142 | default: |
143 | break; |
144 | } |
145 | } |
146 | |
147 | signals: |
148 | void result(int requestId, bool success); |
149 | |
150 | private: |
151 | QQmlComponent m_component; |
152 | int m_requestId; |
153 | }; |
154 | |
155 | bool GlobalInspector::createQmlObject(int requestId, const QString &qml, QObject *parent, |
156 | const QStringList &importList, const QString &filename) |
157 | { |
158 | if (!parent) |
159 | return false; |
160 | |
161 | QQmlContext *parentContext = QQmlEngine::contextForObject(parent); |
162 | if (!parentContext) |
163 | return false; |
164 | |
165 | QString imports; |
166 | for (const QString &s : importList) |
167 | imports += s + QLatin1Char('\n'); |
168 | |
169 | ObjectCreator *objectCreator = new ObjectCreator(requestId, parentContext->engine(), parent); |
170 | connect(sender: objectCreator, signal: &ObjectCreator::result, context: this, slot: &GlobalInspector::sendResult); |
171 | objectCreator->run(qml: (imports + qml).toUtf8(), filename: QUrl::fromLocalFile(localfile: filename)); |
172 | return true; |
173 | } |
174 | |
175 | void GlobalInspector::addWindow(QQuickWindow *window) |
176 | { |
177 | m_windowInspectors.append(t: new QQuickWindowInspector(window, this)); |
178 | } |
179 | |
180 | void GlobalInspector::removeWindow(QQuickWindow *window) |
181 | { |
182 | for (QList<QmlJSDebugger::QQuickWindowInspector *>::Iterator i = m_windowInspectors.begin(); |
183 | i != m_windowInspectors.end();) { |
184 | if ((*i)->quickWindow() == window) { |
185 | delete *i; |
186 | i = m_windowInspectors.erase(pos: i); |
187 | } else { |
188 | ++i; |
189 | } |
190 | } |
191 | } |
192 | |
193 | void GlobalInspector::setParentWindow(QQuickWindow *window, QWindow *parentWindow) |
194 | { |
195 | for (QmlJSDebugger::QQuickWindowInspector *inspector : std::as_const(t&: m_windowInspectors)) { |
196 | if (inspector->quickWindow() == window) |
197 | inspector->setParentWindow(parentWindow); |
198 | } |
199 | } |
200 | |
201 | bool GlobalInspector::syncSelectedItems(const QList<QQuickItem *> &items) |
202 | { |
203 | bool selectionChanged = false; |
204 | |
205 | // Disconnect and remove items that are no longer selected |
206 | const auto selectedItemsCopy = m_selectedItems; |
207 | for (QQuickItem *item : selectedItemsCopy) { |
208 | if (items.contains(t: item)) |
209 | continue; |
210 | |
211 | selectionChanged = true; |
212 | item->disconnect(receiver: this); |
213 | m_selectedItems.removeOne(t: item); |
214 | delete m_highlightItems.take(key: item); |
215 | } |
216 | |
217 | // Connect and add newly selected items |
218 | for (QQuickItem *item : items) { |
219 | if (m_selectedItems.contains(t: item)) |
220 | continue; |
221 | |
222 | selectionChanged = true; |
223 | connect(sender: item, signal: &QObject::destroyed, context: this, slot: &GlobalInspector::removeFromSelectedItems); |
224 | m_selectedItems.append(t: item); |
225 | for (QQuickWindowInspector *inspector : std::as_const(t&: m_windowInspectors)) { |
226 | if (inspector->isEnabled() && inspector->quickWindow() == item->window()) { |
227 | m_highlightItems.insert(key: item, value: new SelectionHighlight(titleForItem(item), item, |
228 | inspector->overlay())); |
229 | break; |
230 | } |
231 | } |
232 | } |
233 | |
234 | return selectionChanged; |
235 | } |
236 | |
237 | QString GlobalInspector::titleForItem(QQuickItem *item) const |
238 | { |
239 | QString className = QLatin1String(item->metaObject()->className()); |
240 | QString objectStringId = idStringForObject(obj: item); |
241 | |
242 | #if QT_CONFIG(regularexpression) |
243 | className.remove(re: QRegularExpression(QLatin1String("_QMLTYPE_\\d+" ))); |
244 | className.remove(re: QRegularExpression(QLatin1String("_QML_\\d+" ))); |
245 | #endif |
246 | if (className.startsWith(s: QLatin1String("QQuick" ))) |
247 | className = className.mid(position: 6); |
248 | |
249 | QString constructedName; |
250 | |
251 | if (!objectStringId.isEmpty()) { |
252 | constructedName = objectStringId + QLatin1String(" (" ) + className + QLatin1Char(')'); |
253 | } else if (!item->objectName().isEmpty()) { |
254 | constructedName = item->objectName() + QLatin1String(" (" ) + className + QLatin1Char(')'); |
255 | } else { |
256 | constructedName = className; |
257 | } |
258 | |
259 | return constructedName; |
260 | } |
261 | |
262 | QString GlobalInspector::idStringForObject(QObject *obj) const |
263 | { |
264 | QQmlContext *context = qmlContext(obj); |
265 | if (context) { |
266 | QQmlRefPointer<QQmlContextData> cdata = QQmlContextData::get(context); |
267 | if (cdata) |
268 | return cdata->findObjectId(obj); |
269 | } |
270 | return QString(); |
271 | } |
272 | |
273 | void GlobalInspector::processMessage(const QByteArray &message) |
274 | { |
275 | bool success = true; |
276 | QQmlDebugPacket ds(message); |
277 | |
278 | QByteArray type; |
279 | ds >> type; |
280 | |
281 | int requestId = -1; |
282 | if (type == REQUEST) { |
283 | QByteArray command; |
284 | ds >> requestId >> command; |
285 | |
286 | if (command == ENABLE) { |
287 | for (QQuickWindowInspector *inspector : std::as_const(t&: m_windowInspectors)) |
288 | inspector->setEnabled(true); |
289 | success = !m_windowInspectors.isEmpty(); |
290 | } else if (command == DISABLE) { |
291 | setSelectedItems(QList<QQuickItem*>()); |
292 | for (QQuickWindowInspector *inspector : std::as_const(t&: m_windowInspectors)) |
293 | inspector->setEnabled(false); |
294 | success = !m_windowInspectors.isEmpty(); |
295 | } else if (command == SELECT) { |
296 | QList<int> debugIds; |
297 | ds >> debugIds; |
298 | |
299 | QList<QQuickItem *> selectedObjects; |
300 | for (int debugId : std::as_const(t&: debugIds)) { |
301 | if (QQuickItem *obj = |
302 | qobject_cast<QQuickItem *>(o: QQmlDebugService::objectForId(id: debugId))) |
303 | selectedObjects << obj; |
304 | } |
305 | syncSelectedItems(items: selectedObjects); |
306 | } else if (command == SET_ANIMATION_SPEED) { |
307 | qreal speed; |
308 | ds >> speed; |
309 | QUnifiedTimer::instance()->setSlowModeEnabled(speed != 1.0); |
310 | QUnifiedTimer::instance()->setSlowdownFactor(speed); |
311 | } else if (command == SHOW_APP_ON_TOP) { |
312 | bool showOnTop; |
313 | ds >> showOnTop; |
314 | for (QmlJSDebugger::QQuickWindowInspector *inspector : std::as_const(t&: m_windowInspectors)) |
315 | inspector->setShowAppOnTop(showOnTop); |
316 | success = !m_windowInspectors.isEmpty(); |
317 | } else if (command == CREATE_OBJECT) { |
318 | QString qml; |
319 | int parentId; |
320 | QString filename; |
321 | QStringList imports; |
322 | ds >> qml >> parentId >> imports >> filename; |
323 | if (QObject *parent = QQmlDebugService::objectForId(id: parentId)) { |
324 | if (createQmlObject(requestId, qml, parent, importList: imports, filename)) |
325 | return; // will callback for result |
326 | else { |
327 | success = false; |
328 | } |
329 | } else { |
330 | success = false; |
331 | } |
332 | |
333 | } else if (command == DESTROY_OBJECT) { |
334 | int debugId; |
335 | ds >> debugId; |
336 | if (QObject *obj = QQmlDebugService::objectForId(id: debugId)) |
337 | delete obj; |
338 | else |
339 | success = false; |
340 | |
341 | } else if (command == MOVE_OBJECT) { |
342 | int debugId, newParent; |
343 | ds >> debugId >> newParent; |
344 | success = reparentQmlObject(object: QQmlDebugService::objectForId(id: debugId), |
345 | newParent: QQmlDebugService::objectForId(id: newParent)); |
346 | } else { |
347 | qWarning() << "Warning: Not handling command:" << command; |
348 | success = false; |
349 | } |
350 | } else { |
351 | qWarning() << "Warning: Not handling type:" << type << REQUEST; |
352 | success = false; |
353 | } |
354 | |
355 | sendResult(requestId, success); |
356 | } |
357 | |
358 | void GlobalInspector::sendResult(int requestId, bool success) |
359 | { |
360 | QQmlDebugPacket rs; |
361 | rs << QByteArray(RESPONSE) << requestId << success; |
362 | emit messageToClient(name: QQmlInspectorService::s_key, data: rs.data()); |
363 | } |
364 | |
365 | GlobalInspector::~GlobalInspector() |
366 | { |
367 | // Everything else is parented |
368 | qDeleteAll(c: m_highlightItems); |
369 | } |
370 | |
371 | } |
372 | |
373 | QT_END_NAMESPACE |
374 | |
375 | #include "moc_globalinspector.cpp" |
376 | |
377 | #include <globalinspector.moc> |
378 | |