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 QFileDevice::Permissions permissions = QQmlPreviewFileLoader::DefaultPermissions;
76 packet >> path >> contents;
77 if (!packet.atEnd())
78 packet >> permissions;
79
80 const QUrl url = path.startsWith(QLatin1Char(':'))
81 ? QUrl(QLatin1String("qrc") + path)
82 : QUrl::fromLocalFile(path);
83
84 // Only a re-serve of a URL we already saw is an actual edit that needs relinking. The
85 // first time any file is served (e.g. while the whole app is still starting up) isn't
86 // dropping anything; treating it as such piles up the entire type registry as "dropped"
87 // and causes a mass relink storm on the first Load.
88 if (m_servedUrls.contains(url))
89 emit drop(url);
90 else
91 m_servedUrls.insert(url);
92 emit file(path, contents, permissions);
93
94 // Remember the first .qml file as the current URL, so that a Load command
95 // without an explicit URL can fall back to it.
96 if (m_currentUrl.isEmpty() && path.endsWith(".qml"))
97 m_currentUrl = url;
98
99 // Never auto-load here. The client must send an explicit Load command
100 // to trigger loading. Auto-loading based on File responses is racy
101 // (the Configuration message may not have arrived yet) and creates
102 // duplicate objects when the host process has already loaded the scene.
103 break;
104 }
105 case Directory: {
106 QString path;
107 QStringList entries;
108 QFileDevice::Permissions permissions = QQmlPreviewFileLoader::DefaultPermissions;
109 packet >> path >> entries;
110 if (!packet.atEnd())
111 packet >> permissions;
112 emit directory(path, entries, permissions);
113 break;
114 }
115 case Load: {
116 QUrl url;
117 packet >> url;
118 if (url.isEmpty())
119 url = m_currentUrl;
120 else
121 m_currentUrl = url;
122 emit load(url);
123 break;
124 }
125 case Error: {
126 QString file;
127 packet >> file;
128 emit error(file);
129 break;
130 }
131 case Rerun:
132 emit rerun();
133 break;
134 case ClearCache:
136 break;
137 case Zoom: {
138 float factor;
139 packet >> factor;
140 emit zoom(static_cast<qreal>(factor));
141 break;
142 }
143 case AnimationSpeed: {
144 float factor;
145 packet >> factor;
146 emit animationSpeed(qreal(factor));
147 break;
148 }
149 case Configuration: {
151 bool enableInPlaceUpdates;
152 packet >> enableInPlaceUpdates;
153 if (enableInPlaceUpdates
154 && !qobject_cast<QQmlInPlacePreviewHandler *>(m_handler.get())) {
155 // Schedule this to the main thread where the handler lives
156 // We're on the debug server thread here.
157 QMetaObject::invokeMethod(this, &QQmlPreviewServiceImpl::switchToInPlaceHandler);
158 } else if (!enableInPlaceUpdates
159 && qobject_cast<QQmlInPlacePreviewHandler *>(m_handler.get())) {
160 forwardError(QLatin1String("Cannot disable in-place updates once enabled"));
161 }
162 break;
163 } else {
164 Q_FALLTHROUGH();
165 }
166 }
167 default:
168 forwardError(QString::fromLatin1("Invalid command: %1").arg(command));
169 break;
170 }
171}
172
174{
175 if (QQmlEngine *qmlEngine = qobject_cast<QQmlEngine *>(engine))
176 m_handler->addEngine(qmlEngine);
177 emit attachedToEngine(engine);
178}
179
181{
182 if (QQmlEngine *qmlEngine = qobject_cast<QQmlEngine *>(engine))
183 m_handler->removeEngine(qmlEngine);
184 emit detachedFromEngine(engine);
185}
186
187void QQmlPreviewServiceImpl::stateChanged(QQmlDebugService::State state)
188{
189 if (state == Enabled) {
190 m_loader.reset(new QQmlPreviewFileLoader(this));
191 connect(this, &QQmlPreviewServiceImpl::load,
192 m_loader.data(), &QQmlPreviewFileLoader::whitelist, Qt::DirectConnection);
193 QV4::ExecutionEngine::setPreviewing(true);
194 m_fileEngine.reset(new QQmlPreviewFileEngineHandler(m_loader.data()));
195 } else {
196 QV4::ExecutionEngine::setPreviewing(false);
197 m_fileEngine.reset();
198 m_loader.reset();
199 }
200}
201
202void QQmlPreviewServiceImpl::forwardRequest(const QString &file)
203{
204 QQmlDebugPacket packet;
205 packet << static_cast<qint8>(Request) << file;
206 emit messageToClient(name(), packet.data());
207}
208
209void QQmlPreviewServiceImpl::forwardError(const QString &error)
210{
211 QQmlDebugPacket packet;
212 packet << static_cast<qint8>(Error) << error;
213 emit messageToClient(name(), packet.data());
214}
215
216void QQmlPreviewServiceImpl::forwardFps(const QQmlPreviewHandler::FpsInfo &frames)
217{
218 QQmlDebugPacket packet;
219 packet << static_cast<qint8>(Fps)
220 << frames.numSyncs << frames.minSync << frames.maxSync << frames.totalSync
221 << frames.numRenders << frames.minRender << frames.maxRender << frames.totalRender;
222 emit messageToClient(name(), packet.data());
223}
224
226{
227 QQmlDebugPacket packet;
228 packet << static_cast<qint8>(HotReloadFailure) << reason;
229 emit messageToClient(name(), packet.data());
230}
231
233{
234 return m_handler->currentRootItem();
235}
236
237QT_END_NAMESPACE
238
239#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()