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
qqmldebugservice.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
6#include <private/qqmldata_p.h>
7#include <private/qqmlcontext_p.h>
8
9#include <QtCore/qdebug.h>
10#include <QtCore/qfileinfo.h>
11#include <QtCore/qstringlist.h>
12#include <QtCore/qthread.h>
13
15
16/*!
17 \class QQmlDebugService
18 \inmodule QtQml
19 \internal
20*/
21
22class QQmlDebugServer;
23
25{
26 Q_DECLARE_PUBLIC(QQmlDebugService)
27public:
29
31 const float version;
33};
34
35QQmlDebugServicePrivate::QQmlDebugServicePrivate(const QString &name, float version) :
36 name(name), version(version), state(QQmlDebugService::NotConnected)
37{
38}
39
40QQmlDebugService::QQmlDebugService(const QString &name, float version, QObject *parent)
41 : QObject(*(new QQmlDebugServicePrivate(name, version)), parent)
42{
43 Q_D(QQmlDebugService);
44 QQmlDebugConnector *server = QQmlDebugConnector::instance();
45
46 if (!server)
47 return;
48
49 if (server->service(d->name)) {
50 qWarning() << "QQmlDebugService: Conflicting plugin name" << d->name;
51 } else {
52 server->addService(d->name, this);
53 }
54}
55
56QQmlDebugService::~QQmlDebugService()
57{
58 QThread *currentThread = QThread::currentThread();
59 QThread *mainThread = thread();
60 if (currentThread != mainThread) {
61 qFatal("Qml debugging framework was cleaned up from wrong thread. Did you leak your "
62 "QCoreApplication?");
63 }
64 Q_D(QQmlDebugService);
65 QQmlDebugConnector *server = QQmlDebugConnector::instance();
66
67 if (!server)
68 return;
69
70 if (server->service(d->name) != this)
71 qWarning() << "QQmlDebugService: Plugin" << d->name << "is not registered.";
72 else
73 server->removeService(d->name);
74}
75
76const QString &QQmlDebugService::name() const
77{
78 Q_D(const QQmlDebugService);
79 return d->name;
80}
81
82float QQmlDebugService::version() const
83{
84 Q_D(const QQmlDebugService);
85 return d->version;
86}
87
88QQmlDebugService::State QQmlDebugService::state() const
89{
90 Q_D(const QQmlDebugService);
91 return d->state;
92}
93
94void QQmlDebugService::setState(QQmlDebugService::State newState)
95{
96 Q_D(QQmlDebugService);
97 d->state = newState;
98}
99
100namespace {
101class ObjectReferenceHash : public QObject
102{
103 Q_OBJECT
104public:
105 ObjectReferenceHash() : nextId(0) {}
106
107 QHash<QObject *, int> objects;
108 QHash<int, QObject *> ids;
109
110 int nextId;
111
112 void remove(QObject *obj);
113};
114}
115Q_GLOBAL_STATIC(ObjectReferenceHash, objectReferenceHash)
116
117void ObjectReferenceHash::remove(QObject *obj)
118{
119 const auto iter = objects.constFind(obj);
120 if (iter != objects.cend()) {
121 ids.remove(iter.value());
122 objects.erase(iter);
123 }
124}
125
126/*!
127 \internal
128 Returns a unique id for \a object. Calling this method multiple times
129 for the same object will return the same id.
130*/
131int QQmlDebugService::idForObject(QObject *object)
132{
133 if (!object)
134 return -1;
135
136 ObjectReferenceHash *hash = objectReferenceHash();
137 auto iter = hash->objects.constFind(object);
138
139 if (iter == hash->objects.cend()) {
140 int id = hash->nextId++;
141 hash->ids.insert(id, object);
142 iter = hash->objects.insert(object, id);
143 connect(object, &QObject::destroyed, hash, &ObjectReferenceHash::remove);
144 }
145 return iter.value();
146}
147
148/*!
149 \internal
150 Returns the mapping of objects to unique \a ids, created through
151 calls to idForObject().
152*/
153const QHash<int, QObject *> &QQmlDebugService::objectsForIds()
154{
155 return objectReferenceHash()->ids;
156}
157
158QT_END_NAMESPACE
159
160#include "qqmldebugservice.moc"
161#include "moc_qqmldebugservice_p.cpp"
QQmlDebugService::State state
Combined button and popup list for selecting options.