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 <private/qqmldebugserviceinterfaces_p.h>
5#include <private/qqmldebugservicefactory_p.h>
6
7#include "globalinspector.h"
8#include "qquickwindowinspector.h"
9
10#include <QtGui/QWindow>
11
12QT_BEGIN_NAMESPACE
13
14class QQmlInspectorServiceImpl : public QQmlInspectorService
15{
16 Q_OBJECT
17public:
18 QQmlInspectorServiceImpl(QObject *parent = nullptr);
19
20 void addWindow(QQuickWindow *window) override;
21 void setParentWindow(QQuickWindow *window, QWindow *parent) override;
22 void removeWindow(QQuickWindow *window) override;
23
24signals:
25 void scheduleMessage(const QByteArray &message);
26
27protected:
28 void messageReceived(const QByteArray &) override;
29
30private:
31 friend class QQmlInspectorServiceFactory;
32
33 QmlJSDebugger::GlobalInspector *checkInspector();
34 QmlJSDebugger::GlobalInspector *m_globalInspector;
35 QHash<QQuickWindow *, QWindow *> m_waitingWindows;
36
37 void messageFromClient(const QByteArray &message);
38};
39
40QQmlInspectorServiceImpl::QQmlInspectorServiceImpl(QObject *parent):
41 QQmlInspectorService(1, parent), m_globalInspector(nullptr)
42{
43 connect(sender: this, signal: &QQmlInspectorServiceImpl::scheduleMessage,
44 context: this, slot: &QQmlInspectorServiceImpl::messageFromClient, type: Qt::QueuedConnection);
45}
46
47QmlJSDebugger::GlobalInspector *QQmlInspectorServiceImpl::checkInspector()
48{
49 if (state() == Enabled) {
50 if (!m_globalInspector) {
51 m_globalInspector = new QmlJSDebugger::GlobalInspector(this);
52 connect(sender: m_globalInspector, signal: &QmlJSDebugger::GlobalInspector::messageToClient,
53 context: this, slot: &QQmlDebugService::messageToClient);
54 for (QHash<QQuickWindow *, QWindow *>::ConstIterator i = m_waitingWindows.constBegin();
55 i != m_waitingWindows.constEnd(); ++i) {
56 m_globalInspector->addWindow(window: i.key());
57 if (i.value() != 0)
58 m_globalInspector->setParentWindow(window: i.key(), parentWindow: i.value());
59 }
60 m_waitingWindows.clear();
61 }
62 } else if (m_globalInspector) {
63 delete m_globalInspector;
64 m_globalInspector = nullptr;
65 }
66 return m_globalInspector;
67}
68
69void QQmlInspectorServiceImpl::addWindow(QQuickWindow *window)
70{
71 if (QmlJSDebugger::GlobalInspector *inspector = checkInspector())
72 inspector->addWindow(window);
73 else
74 m_waitingWindows.insert(key: window, value: 0);
75}
76
77void QQmlInspectorServiceImpl::removeWindow(QQuickWindow *window)
78{
79 if (QmlJSDebugger::GlobalInspector *inspector = checkInspector())
80 inspector->removeWindow(window);
81 else
82 m_waitingWindows.remove(key: window);
83}
84
85void QQmlInspectorServiceImpl::setParentWindow(QQuickWindow *window, QWindow *parent)
86{
87 if (QmlJSDebugger::GlobalInspector *inspector = checkInspector())
88 inspector->setParentWindow(window, parentWindow: parent);
89 else
90 m_waitingWindows[window] = parent;
91}
92
93void QQmlInspectorServiceImpl::messageReceived(const QByteArray &message)
94{
95 // Move the message to the right thread via queued signal
96 emit scheduleMessage(message);
97}
98
99void QQmlInspectorServiceImpl::messageFromClient(const QByteArray &message)
100{
101 if (QmlJSDebugger::GlobalInspector *inspector = checkInspector())
102 inspector->processMessage(message);
103}
104
105class QQmlInspectorServiceFactory : public QQmlDebugServiceFactory
106{
107 Q_OBJECT
108 Q_PLUGIN_METADATA(IID QQmlDebugServiceFactory_iid FILE "qqmlinspectorservice.json")
109public:
110 QQmlDebugService *create(const QString &key) override;
111};
112
113QQmlDebugService *QQmlInspectorServiceFactory::create(const QString &key)
114{
115 return key == QQmlInspectorServiceImpl::s_key ? new QQmlInspectorServiceImpl(this) : nullptr;
116}
117
118QT_END_NAMESPACE
119
120#include "qqmlinspectorservice.moc"
121

source code of qtdeclarative/src/plugins/qmltooling/qmldbg_inspector/qqmlinspectorservice.cpp