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
qqmlpreviewservice.cpp
Go to the documentation of this file.
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// Qt-Security score:significant
4
8
9#include <QtCore/qpointer.h>
10#include <QtQml/qqmlengine.h>
11#include <QtQml/qqmlcomponent.h>
12#include <QtQuick/qquickwindow.h>
13#include <QtQuick/qquickitem.h>
14#include <QtGui/qguiapplication.h>
15
16#include <private/qquickpixmap_p.h>
17#include <private/qqmldebugconnector_p.h>
18#include <private/qversionedpacket_p.h>
19
21
22inline bool convertQmlPreviewHotReload(const char *v)
23{
24 // Either not set or explicitly "1" or "true";
25 return v == nullptr || qstrcmp(v, "1") == 0 || qstrcmp(v, "true") == 0;
26}
27
29{
30 static const bool enabled =
31 qmlGetConfigOption<bool, convertQmlPreviewHotReload>("QMLPREVIEW_HOTRELOAD");
32 return enabled;
33}
34
35const QString QQmlPreviewServiceImpl::s_key = QStringLiteral("QmlPreview");
36using QQmlDebugPacket = QVersionedPacket<QQmlDebugConnector>;
37
41{
42 m_handler->connectToService(this);
43}
44
48
49void QQmlPreviewServiceImpl::switchToInPlaceHandler()
50{
51 // Transfer engines from the old handler to the new one
52 const auto engineList = m_handler->engines();
53 m_handler = std::make_unique<QQmlInPlacePreviewHandler>();
54 m_handler->connectToService(this);
55 for (QQmlEngine *engine : engineList)
56 m_handler->addEngine(engine);
57
58 // Send confirmation back to the client
59 QQmlDebugPacket response;
60 const bool enableInPlaceUpdates = true;
61 response << static_cast<qint8>(Confirmation) << enableInPlaceUpdates;
62 emit messageToClient(name(), response.data());
63}
64
65void QQmlPreviewServiceImpl::messageReceived(const QByteArray &data)
66{
67 QQmlDebugPacket packet(data);
68 qint8 command;
69
70 packet >> command;
71 switch (command) {
72 case File: {
73 QString path;
74 QByteArray contents;
75 packet >> path >> contents;
76
77 const QUrl url = path.startsWith(QLatin1Char(':'))
78 ? QUrl(QLatin1String("qrc") + path)
79 : QUrl::fromLocalFile(path);
80
81 emit drop(url);
82 emit file(path, contents);
83
84 // Remember the first .qml file as the current URL, so that a Load command
85 // without an explicit URL can fall back to it.
86 if (m_currentUrl.isEmpty() && path.endsWith(".qml"))
87 m_currentUrl = url;
88
89 // Never auto-load here. The client must send an explicit Load command
90 // to trigger loading. Auto-loading based on File responses is racy
91 // (the Configuration message may not have arrived yet) and creates
92 // duplicate objects when the host process has already loaded the scene.
93 break;
94 }
95 case Directory: {
96 QString path;
97 QStringList entries;
98 packet >> path >> entries;
99 emit directory(path, entries);
100 break;
101 }
102 case Load: {
103 QUrl url;
104 packet >> url;
105 if (url.isEmpty())
106 url = m_currentUrl;
107 else
108 m_currentUrl = url;
109 emit load(url);
110 break;
111 }
112 case Error: {
113 QString file;
114 packet >> file;
115 emit error(file);
116 break;
117 }
118 case Rerun:
119 emit rerun();
120 break;
121 case ClearCache:
123 break;
124 case Zoom: {
125 float factor;
126 packet >> factor;
127 emit zoom(static_cast<qreal>(factor));
128 break;
129 }
130 case AnimationSpeed: {
131 float factor;
132 packet >> factor;
133 emit animationSpeed(qreal(factor));
134 break;
135 }
136 case Configuration: {
138 bool enableInPlaceUpdates;
139 packet >> enableInPlaceUpdates;
140 if (enableInPlaceUpdates
141 && !qobject_cast<QQmlInPlacePreviewHandler *>(m_handler.get())) {
142 // Schedule this to the main thread where the handler lives
143 // We're on the debug server thread here.
144 QMetaObject::invokeMethod(this, &QQmlPreviewServiceImpl::switchToInPlaceHandler);
145 } else if (!enableInPlaceUpdates
146 && qobject_cast<QQmlInPlacePreviewHandler *>(m_handler.get())) {
147 forwardError(QLatin1String("Cannot disable in-place updates once enabled"));
148 }
149 break;
150 } else {
151 Q_FALLTHROUGH();
152 }
153 }
154 default:
155 forwardError(QString::fromLatin1("Invalid command: %1").arg(command));
156 break;
157 }
158}
159
161{
162 if (QQmlEngine *qmlEngine = qobject_cast<QQmlEngine *>(engine))
163 m_handler->addEngine(qmlEngine);
164 emit attachedToEngine(engine);
165}
166
168{
169 if (QQmlEngine *qmlEngine = qobject_cast<QQmlEngine *>(engine))
170 m_handler->removeEngine(qmlEngine);
171 emit detachedFromEngine(engine);
172}
173
174void QQmlPreviewServiceImpl::stateChanged(QQmlDebugService::State state)
175{
176 if (state == Enabled) {
177 m_loader.reset(new QQmlPreviewFileLoader(this));
178 connect(this, &QQmlPreviewServiceImpl::load,
179 m_loader.data(), &QQmlPreviewFileLoader::whitelist, Qt::DirectConnection);
180 QV4::ExecutionEngine::setPreviewing(true);
181 m_fileEngine.reset(new QQmlPreviewFileEngineHandler(m_loader.data()));
182 } else {
183 QV4::ExecutionEngine::setPreviewing(false);
184 m_fileEngine.reset();
185 m_loader.reset();
186 }
187}
188
189void QQmlPreviewServiceImpl::forwardRequest(const QString &file)
190{
191 QQmlDebugPacket packet;
192 packet << static_cast<qint8>(Request) << file;
193 emit messageToClient(name(), packet.data());
194}
195
196void QQmlPreviewServiceImpl::forwardError(const QString &error)
197{
198 QQmlDebugPacket packet;
199 packet << static_cast<qint8>(Error) << error;
200 emit messageToClient(name(), packet.data());
201}
202
203void QQmlPreviewServiceImpl::forwardFps(const QQmlPreviewHandler::FpsInfo &frames)
204{
205 QQmlDebugPacket packet;
206 packet << static_cast<qint8>(Fps)
207 << frames.numSyncs << frames.minSync << frames.maxSync << frames.totalSync
208 << frames.numRenders << frames.minRender << frames.maxRender << frames.totalRender;
209 emit messageToClient(name(), packet.data());
210}
211
213{
214 QQmlDebugPacket packet;
215 packet << static_cast<qint8>(HotReloadFailure) << reason;
216 emit messageToClient(name(), packet.data());
217}
218
220{
221 return m_handler->currentRootItem();
222}
223
224QT_END_NAMESPACE
225
226#include "moc_qqmlpreviewservice.cpp"
void forwardRequest(const QString &file)
static const QString s_key
void engineAboutToBeRemoved(QJSEngine *engine) override
QQmlPreviewServiceImpl(QObject *parent=nullptr)
void forwardHotReloadFailure(const QString &reason)
void engineAboutToBeAdded(QJSEngine *engine) override
void forwardFps(const QQmlPreviewHandler::FpsInfo &frames)
void stateChanged(State state) override
void forwardError(const QString &error)
void messageReceived(const QByteArray &message) override
void clearCache()
Clears cached information about loaded files, including any type data, scripts and qmldir information...
Combined button and popup list for selecting options.
QT_BEGIN_NAMESPACE bool convertQmlPreviewHotReload(const char *v)
static bool qmlPreviewHotReloadEnabled()