Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
globalinspector.cpp
Go to the documentation of this file.
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
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
32
33using QQmlDebugPacket = QVersionedPacket<QQmlDebugConnector>;
34
35const char REQUEST[] = "request";
36const char RESPONSE[] = "response";
37const char EVENT[] = "event";
38const char ENABLE[] = "enable";
39const char DISABLE[] = "disable";
40const char SELECT[] = "select";
41const char SET_ANIMATION_SPEED[] = "setAnimationSpeed";
42const char SHOW_APP_ON_TOP[] = "showAppOnTop";
43const char CREATE_OBJECT[] = "createObject";
44const char DESTROY_OBJECT[] = "destroyObject";
45const char MOVE_OBJECT[] = "moveObject";
46
47namespace QmlJSDebugger {
48
49void GlobalInspector::removeFromSelectedItems(QObject *object)
50{
51 if (QQuickItem *item = qobject_cast<QQuickItem*>(object)) {
52 if (m_selectedItems.removeOne(item))
53 delete m_highlightItems.take(item);
54 }
55}
56
57void GlobalInspector::setSelectedItems(const QList<QQuickItem *> &items)
58{
59 if (!syncSelectedItems(items))
60 return;
61
62 QList<QObject*> objectList;
63 objectList.reserve(items.size());
64 for (QQuickItem *item : items)
65 objectList << item;
66
67 sendCurrentObjects(objectList);
68}
69
70void GlobalInspector::showSelectedItemName(QQuickItem *item, const QPointF &point)
71{
72 SelectionHighlight *highlightItem = m_highlightItems.value(item, 0);
73 if (highlightItem)
74 highlightItem->showName(point);
75}
76
77void 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(objects.size());
85 for (QObject *object : objects)
86 debugIds << QQmlDebugService::idForObject(object);
87 ds << debugIds;
88
89 emit messageToClient(QQmlInspectorService::s_key, ds.data());
90}
91
92static bool reparentQmlObject(QObject *object, QObject *newParent)
93{
94 if (!newParent)
95 return false;
96
97 object->setParent(newParent);
98 QQuickItem *newParentItem = qobject_cast<QQuickItem*>(newParent);
99 QQuickItem *item = qobject_cast<QQuickItem*>(object);
100 if (newParentItem && item)
101 item->setParentItem(newParentItem);
102 return true;
103}
104
105class ObjectCreator : public QObject
106{
108public:
114
115 void run(const QByteArray &qml, const QUrl &filename)
116 {
118 }
119
121 {
122 switch (status) {
123 case QQmlComponent::Error:
124 emit result(m_requestId, false);
125 delete this;
126 return;
127 case QQmlComponent::Ready: {
128 // Stuff might have changed. We have to lookup the parentContext again.
130 if (!parentContext) {
131 emit result(m_requestId, false);
132 } else {
135 emit result(m_requestId, true);
136 else
137 emit result(m_requestId, false);
138 }
139 deleteLater(); // The component might send more signals
140 return;
141 }
142 default:
143 break;
144 }
145 }
146
147signals:
148 void result(int requestId, bool success);
149
150private:
151 QQmlComponent m_component;
152 int m_requestId;
153};
154
155bool 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(objectCreator, &ObjectCreator::result, this, &GlobalInspector::sendResult);
171 objectCreator->run((imports + qml).toUtf8(), QUrl::fromLocalFile(filename));
172 return true;
173}
174
175void GlobalInspector::addWindow(QQuickWindow *window)
176{
177 m_windowInspectors.append(new QQuickWindowInspector(window, this));
178}
179
180void 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(i);
187 } else {
188 ++i;
189 }
190 }
191}
192
193void GlobalInspector::setParentWindow(QQuickWindow *window, QWindow *parentWindow)
194{
195 for (QmlJSDebugger::QQuickWindowInspector *inspector : std::as_const(m_windowInspectors)) {
196 if (inspector->quickWindow() == window)
197 inspector->setParentWindow(parentWindow);
198 }
199}
200
201bool 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(item))
209 continue;
210
211 selectionChanged = true;
212 item->disconnect(this);
213 m_selectedItems.removeOne(item);
214 delete m_highlightItems.take(item);
215 }
216
217 // Connect and add newly selected items
218 for (QQuickItem *item : items) {
219 if (m_selectedItems.contains(item))
220 continue;
221
222 selectionChanged = true;
223 connect(item, &QObject::destroyed, this, &GlobalInspector::removeFromSelectedItems);
224 m_selectedItems.append(item);
225 for (QQuickWindowInspector *inspector : std::as_const(m_windowInspectors)) {
226 if (inspector->isEnabled() && inspector->quickWindow() == item->window()) {
227 m_highlightItems.insert(item, new SelectionHighlight(titleForItem(item), item,
228 inspector->overlay()));
229 break;
230 }
231 }
232 }
233
234 return selectionChanged;
235}
236
237QString GlobalInspector::titleForItem(QQuickItem *item) const
238{
239 QString className = QLatin1String(item->metaObject()->className());
240 QString objectStringId = idStringForObject(item);
241
242#if QT_CONFIG(regularexpression)
243 className.remove(QRegularExpression(QLatin1String("_QMLTYPE_\\d+")));
244 className.remove(QRegularExpression(QLatin1String("_QML_\\d+")));
245#endif
246 if (className.startsWith(QLatin1String("QQuick")))
247 className = className.mid(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
262QString 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
273void 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(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(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(debugIds)) {
301 if (QQuickItem *obj =
302 qobject_cast<QQuickItem *>(QQmlDebugService::objectForId(debugId)))
303 selectedObjects << obj;
304 }
305 syncSelectedItems(selectedObjects);
306 } else if (command == SET_ANIMATION_SPEED) {
307 qreal speed;
308 ds >> speed;
309 QUnifiedTimer::instance()->setSpeedModifier(1 / speed);
310 } else if (command == SHOW_APP_ON_TOP) {
311 bool showOnTop;
312 ds >> showOnTop;
313 for (QmlJSDebugger::QQuickWindowInspector *inspector : std::as_const(m_windowInspectors))
314 inspector->setShowAppOnTop(showOnTop);
315 success = !m_windowInspectors.isEmpty();
316 } else if (command == CREATE_OBJECT) {
317 QString qml;
318 int parentId;
319 QString filename;
320 QStringList imports;
321 ds >> qml >> parentId >> imports >> filename;
322 if (QObject *parent = QQmlDebugService::objectForId(parentId)) {
323 if (createQmlObject(requestId, qml, parent, imports, filename))
324 return; // will callback for result
325 else {
326 success = false;
327 }
328 } else {
329 success = false;
330 }
331
332 } else if (command == DESTROY_OBJECT) {
333 int debugId;
334 ds >> debugId;
335 if (QObject *obj = QQmlDebugService::objectForId(debugId))
336 delete obj;
337 else
338 success = false;
339
340 } else if (command == MOVE_OBJECT) {
341 int debugId, newParent;
342 ds >> debugId >> newParent;
343 success = reparentQmlObject(QQmlDebugService::objectForId(debugId),
344 QQmlDebugService::objectForId(newParent));
345 } else {
346 qWarning() << "Warning: Not handling command:" << command;
347 success = false;
348 }
349 } else {
350 qWarning() << "Warning: Not handling type:" << type << REQUEST;
351 success = false;
352 }
353
354 sendResult(requestId, success);
355}
356
357void GlobalInspector::sendResult(int requestId, bool success)
358{
359 QQmlDebugPacket rs;
360 rs << QByteArray(RESPONSE) << requestId << success;
361 emit messageToClient(QQmlInspectorService::s_key, rs.data());
362}
363
364GlobalInspector::~GlobalInspector()
365{
366 // Everything else is parented
367 qDeleteAll(m_highlightItems);
368}
369
370}
371
372QT_END_NAMESPACE
373
374#include "moc_globalinspector.cpp"
375
376#include <globalinspector.moc>
void processMessage(const QByteArray &message)
void setParentWindow(QQuickWindow *window, QWindow *parentWindow)
void removeWindow(QQuickWindow *window)
void showSelectedItemName(QQuickItem *item, const QPointF &point)
void setSelectedItems(const QList< QQuickItem * > &items)
void addWindow(QQuickWindow *window)
const char RESPONSE[]
const char SHOW_APP_ON_TOP[]
const char MOVE_OBJECT[]
const char SET_ANIMATION_SPEED[]
const char SELECT[]
const char DISABLE[]
const char ENABLE[]
const char REQUEST[]
const char CREATE_OBJECT[]
const char EVENT[]
const char DESTROY_OBJECT[]
static bool reparentQmlObject(QObject *object, QObject *newParent)