| 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 | |
| 4 | #include "qqmlpreviewservice.h" |
| 5 | |
| 6 | #include <QtCore/qpointer.h> |
| 7 | #include <QtQml/qqmlengine.h> |
| 8 | #include <QtQml/qqmlcomponent.h> |
| 9 | #include <QtQuick/qquickwindow.h> |
| 10 | #include <QtQuick/qquickitem.h> |
| 11 | #include <QtGui/qguiapplication.h> |
| 12 | |
| 13 | #include <private/qquickpixmap_p.h> |
| 14 | #include <private/qqmldebugconnector_p.h> |
| 15 | #include <private/qversionedpacket_p.h> |
| 16 | |
| 17 | QT_BEGIN_NAMESPACE |
| 18 | |
| 19 | const QString QQmlPreviewServiceImpl::s_key = QStringLiteral("QmlPreview" ); |
| 20 | using QQmlDebugPacket = QVersionedPacket<QQmlDebugConnector>; |
| 21 | |
| 22 | QQmlPreviewServiceImpl::QQmlPreviewServiceImpl(QObject *parent) : |
| 23 | QQmlDebugService(s_key, 1.0f, parent) |
| 24 | { |
| 25 | connect(sender: this, signal: &QQmlPreviewServiceImpl::load, context: &m_handler, slot: &QQmlPreviewHandler::loadUrl); |
| 26 | connect(sender: this, signal: &QQmlPreviewServiceImpl::drop, context: &m_handler, slot: &QQmlPreviewHandler::dropCU); |
| 27 | connect(sender: this, signal: &QQmlPreviewServiceImpl::rerun, context: &m_handler, slot: &QQmlPreviewHandler::rerun); |
| 28 | connect(sender: this, signal: &QQmlPreviewServiceImpl::zoom, context: &m_handler, slot: &QQmlPreviewHandler::zoom); |
| 29 | connect(sender: &m_handler, signal: &QQmlPreviewHandler::error, context: this, slot: &QQmlPreviewServiceImpl::forwardError, |
| 30 | type: Qt::DirectConnection); |
| 31 | connect(sender: &m_handler, signal: &QQmlPreviewHandler::fps, context: this, slot: &QQmlPreviewServiceImpl::forwardFps, |
| 32 | type: Qt::DirectConnection); |
| 33 | } |
| 34 | |
| 35 | QQmlPreviewServiceImpl::~QQmlPreviewServiceImpl() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | void QQmlPreviewServiceImpl::messageReceived(const QByteArray &data) |
| 40 | { |
| 41 | QQmlDebugPacket packet(data); |
| 42 | qint8 command; |
| 43 | |
| 44 | packet >> command; |
| 45 | switch (command) { |
| 46 | case File: { |
| 47 | QString path; |
| 48 | QByteArray contents; |
| 49 | packet >> path >> contents; |
| 50 | |
| 51 | const QUrl url = path.startsWith(c: QLatin1Char(':')) |
| 52 | ? QUrl(QLatin1String("qrc" ) + path) |
| 53 | : QUrl::fromLocalFile(localfile: path); |
| 54 | |
| 55 | emit drop(url); |
| 56 | emit file(file: path, contents); |
| 57 | |
| 58 | // Replace the whole scene with the first file successfully loaded over the debug |
| 59 | // connection. This is an OK approximation of the root component, and if the client wants |
| 60 | // something specific, it will send an explicit Load anyway. |
| 61 | if (m_currentUrl.isEmpty() && path.endsWith(s: ".qml" )) { |
| 62 | m_currentUrl = url; |
| 63 | emit load(url: m_currentUrl); |
| 64 | } |
| 65 | break; |
| 66 | } |
| 67 | case Directory: { |
| 68 | QString path; |
| 69 | QStringList entries; |
| 70 | packet >> path >> entries; |
| 71 | emit directory(file: path, entries); |
| 72 | break; |
| 73 | } |
| 74 | case Load: { |
| 75 | QUrl url; |
| 76 | packet >> url; |
| 77 | if (url.isEmpty()) |
| 78 | url = m_currentUrl; |
| 79 | else |
| 80 | m_currentUrl = url; |
| 81 | emit load(url); |
| 82 | break; |
| 83 | } |
| 84 | case Error: { |
| 85 | QString file; |
| 86 | packet >> file; |
| 87 | emit error(file); |
| 88 | break; |
| 89 | } |
| 90 | case Rerun: |
| 91 | emit rerun(); |
| 92 | break; |
| 93 | case ClearCache: |
| 94 | emit clearCache(); |
| 95 | break; |
| 96 | case Zoom: { |
| 97 | float factor; |
| 98 | packet >> factor; |
| 99 | emit zoom(factor: static_cast<qreal>(factor)); |
| 100 | break; |
| 101 | } |
| 102 | default: |
| 103 | forwardError(error: QString::fromLatin1(ba: "Invalid command: %1" ).arg(a: command)); |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void QQmlPreviewServiceImpl::engineAboutToBeAdded(QJSEngine *engine) |
| 109 | { |
| 110 | if (QQmlEngine *qmlEngine = qobject_cast<QQmlEngine *>(object: engine)) |
| 111 | m_handler.addEngine(engine: qmlEngine); |
| 112 | emit attachedToEngine(engine); |
| 113 | } |
| 114 | |
| 115 | void QQmlPreviewServiceImpl::engineAboutToBeRemoved(QJSEngine *engine) |
| 116 | { |
| 117 | if (QQmlEngine *qmlEngine = qobject_cast<QQmlEngine *>(object: engine)) |
| 118 | m_handler.removeEngine(engine: qmlEngine); |
| 119 | emit detachedFromEngine(engine); |
| 120 | } |
| 121 | |
| 122 | void QQmlPreviewServiceImpl::stateChanged(QQmlDebugService::State state) |
| 123 | { |
| 124 | if (state == Enabled) { |
| 125 | m_loader.reset(other: new QQmlPreviewFileLoader(this)); |
| 126 | connect(sender: this, signal: &QQmlPreviewServiceImpl::load, |
| 127 | context: m_loader.data(), slot: &QQmlPreviewFileLoader::whitelist, type: Qt::DirectConnection); |
| 128 | QV4::ExecutionEngine::setPreviewing(true); |
| 129 | m_fileEngine.reset(other: new QQmlPreviewFileEngineHandler(m_loader.data())); |
| 130 | } else { |
| 131 | QV4::ExecutionEngine::setPreviewing(false); |
| 132 | m_fileEngine.reset(); |
| 133 | m_loader.reset(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void QQmlPreviewServiceImpl::forwardRequest(const QString &file) |
| 138 | { |
| 139 | QQmlDebugPacket packet; |
| 140 | packet << static_cast<qint8>(Request) << file; |
| 141 | emit messageToClient(name: name(), message: packet.data()); |
| 142 | } |
| 143 | |
| 144 | void QQmlPreviewServiceImpl::forwardError(const QString &error) |
| 145 | { |
| 146 | QQmlDebugPacket packet; |
| 147 | packet << static_cast<qint8>(Error) << error; |
| 148 | emit messageToClient(name: name(), message: packet.data()); |
| 149 | } |
| 150 | |
| 151 | void QQmlPreviewServiceImpl::forwardFps(const QQmlPreviewHandler::FpsInfo &frames) |
| 152 | { |
| 153 | QQmlDebugPacket packet; |
| 154 | packet << static_cast<qint8>(Fps) |
| 155 | << frames.numSyncs << frames.minSync << frames.maxSync << frames.totalSync |
| 156 | << frames.numRenders << frames.minRender << frames.maxRender << frames.totalRender; |
| 157 | emit messageToClient(name: name(), message: packet.data()); |
| 158 | } |
| 159 | |
| 160 | QQuickItem *QQmlPreviewServiceImpl::currentRootItem() |
| 161 | { |
| 162 | return m_handler.currentRootItem(); |
| 163 | } |
| 164 | |
| 165 | QT_END_NAMESPACE |
| 166 | |
| 167 | #include "moc_qqmlpreviewservice.cpp" |
| 168 | |