1// Copyright (C) 2018 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 "qqmlinspectorclient_p_p.h"
5
6#include <private/qpacket_p.h>
7#include <private/qqmldebugconnection_p.h>
8#include <QtCore/qdebug.h>
9
10QT_BEGIN_NAMESPACE
11
12QQmlInspectorClient::QQmlInspectorClient(QQmlDebugConnection *connection) :
13 QQmlDebugClient(*new QQmlInspectorClientPrivate(connection))
14{
15}
16
17QQmlInspectorClientPrivate::QQmlInspectorClientPrivate(QQmlDebugConnection *connection) :
18 QQmlDebugClientPrivate(QLatin1String("QmlInspector"), connection)
19{
20}
21
22int QQmlInspectorClient::setInspectToolEnabled(bool enabled)
23{
24 Q_D(QQmlInspectorClient);
25 QPacket ds(connection()->currentDataStreamVersion());
26 ds << QByteArray("request") << ++(d->m_lastRequestId)
27 << QByteArray(enabled ? "enable" : "disable");
28
29 sendMessage(message: ds.data());
30 return d->m_lastRequestId;
31}
32
33int QQmlInspectorClient::setShowAppOnTop(bool showOnTop)
34{
35 Q_D(QQmlInspectorClient);
36 QPacket ds(connection()->currentDataStreamVersion());
37 ds << QByteArray("request") << ++(d->m_lastRequestId)
38 << QByteArray("showAppOnTop") << showOnTop;
39
40 sendMessage(message: ds.data());
41 return d->m_lastRequestId;
42}
43
44int QQmlInspectorClient::setAnimationSpeed(qreal speed)
45{
46 Q_D(QQmlInspectorClient);
47 QPacket ds(connection()->currentDataStreamVersion());
48 ds << QByteArray("request") << ++(d->m_lastRequestId)
49 << QByteArray("setAnimationSpeed") << speed;
50
51 sendMessage(message: ds.data());
52 return d->m_lastRequestId;
53}
54
55int QQmlInspectorClient::select(const QList<int> &objectIds)
56{
57 Q_D(QQmlInspectorClient);
58 QPacket ds(connection()->currentDataStreamVersion());
59 ds << QByteArray("request") << ++(d->m_lastRequestId)
60 << QByteArray("select") << objectIds;
61
62 sendMessage(message: ds.data());
63 return d->m_lastRequestId;
64}
65
66int QQmlInspectorClient::createObject(const QString &qml, int parentId, const QStringList &imports,
67 const QString &filename)
68{
69 Q_D(QQmlInspectorClient);
70 QPacket ds(connection()->currentDataStreamVersion());
71 ds << QByteArray("request") << ++(d->m_lastRequestId)
72 << QByteArray("createObject") << qml << parentId << imports << filename;
73 sendMessage(message: ds.data());
74 return d->m_lastRequestId;
75}
76
77int QQmlInspectorClient::moveObject(int childId, int newParentId)
78{
79 Q_D(QQmlInspectorClient);
80 QPacket ds(connection()->currentDataStreamVersion());
81 ds << QByteArray("request") << ++(d->m_lastRequestId)
82 << QByteArray("moveObject") << childId << newParentId;
83 sendMessage(message: ds.data());
84 return d->m_lastRequestId;
85}
86
87int QQmlInspectorClient::destroyObject(int objectId)
88{
89 Q_D(QQmlInspectorClient);
90 QPacket ds(connection()->currentDataStreamVersion());
91 ds << QByteArray("request") << ++(d->m_lastRequestId)
92 << QByteArray("destroyObject") << objectId;
93 sendMessage(message: ds.data());
94 return d->m_lastRequestId;
95}
96
97void QQmlInspectorClient::messageReceived(const QByteArray &message)
98{
99 QPacket ds(connection()->currentDataStreamVersion(), message);
100 QByteArray type;
101 ds >> type;
102
103 if (type != QByteArray("response")) {
104 qDebug() << "Unhandled message of type" << type;
105 return;
106 }
107
108 int responseId;
109 bool result;
110 ds >> responseId >> result;
111 emit responseReceived(requestId: responseId, result);
112}
113
114QT_END_NAMESPACE
115
116#include "moc_qqmlinspectorclient_p.cpp"
117

source code of qtdeclarative/src/qmldebug/qqmlinspectorclient.cpp