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