diff options
Diffstat (limited to 'src/plugins/qmlpreview/qmlpreviewconnectionmanager.cpp')
-rw-r--r-- | src/plugins/qmlpreview/qmlpreviewconnectionmanager.cpp | 148 |
1 files changed, 91 insertions, 57 deletions
diff --git a/src/plugins/qmlpreview/qmlpreviewconnectionmanager.cpp b/src/plugins/qmlpreview/qmlpreviewconnectionmanager.cpp index 8a4f3bc205..0fcf6a36b8 100644 --- a/src/plugins/qmlpreview/qmlpreviewconnectionmanager.cpp +++ b/src/plugins/qmlpreview/qmlpreviewconnectionmanager.cpp @@ -70,10 +70,64 @@ void QmlPreviewConnectionManager::setFpsHandler(QmlPreviewFpsHandler fpsHandler) void QmlPreviewConnectionManager::createClients() { - m_clientPlugin = new QmlPreviewClient(connection()); + createPreviewClient(); + createDebugTranslationClient(); +} + +QUrl QmlPreviewConnectionManager::findValidI18nDirectoryAsUrl(const QString &locale) +{ + const QString shortLocale = locale.left(locale.indexOf("_")); + QString path = m_lastLoadedUrl.path(); + + while (!path.isEmpty()) { + path = path.left(qMax(0, path.lastIndexOf("/"))); + QUrl url = m_lastLoadedUrl; + + auto tryPath = [&](const QString &postfix) { + url.setPath(path + "/i18n/qml_" + postfix); + bool success = false; + m_projectFileFinder.findFile(url, &success); + return success; + }; + + if (tryPath(locale + ".qm")) + break; + if (tryPath(locale)) + break; + if (tryPath(shortLocale + ".qm")) + break; + if (tryPath(shortLocale)) + break; + } + + QUrl url = m_lastLoadedUrl; + url.setPath(path); + return url; +} + +void QmlPreviewConnectionManager::createDebugTranslationClient() +{ + m_qmlDebugTranslationClient = new QmlDebugTranslationClient(connection()); + QObject::connect(this, &QmlPreviewConnectionManager::language, + m_qmlDebugTranslationClient.data(), [this](const QString &locale) { + + // service expects a context URL. + // Search the parent directories of the last loaded URL for i18n files. + m_qmlDebugTranslationClient->changeLanguage(findValidI18nDirectoryAsUrl(locale), locale); + }); + QObject::connect(m_qmlDebugTranslationClient.data(), &QmlDebugTranslationClient::debugServiceUnavailable, + this, []() { + QMessageBox::warning(Core::ICore::dialogParent(), "Error connect to QML DebugTranslation service", + "QML DebugTranslation feature is not available for this version of Qt."); + }, Qt::QueuedConnection); // Queue it, so that it interfere with the connection timer +} + +void QmlPreviewConnectionManager::createPreviewClient() +{ + m_qmlPreviewClient = new QmlPreviewClient(connection()); QObject::connect( - this, &QmlPreviewConnectionManager::loadFile, m_clientPlugin.data(), + this, &QmlPreviewConnectionManager::loadFile, m_qmlPreviewClient.data(), [this](const QString &filename, const QString &changedFile, const QByteArray &contents) { if (!m_fileClassifier(changedFile)) { @@ -84,57 +138,29 @@ void QmlPreviewConnectionManager::createClients() bool success = false; const QString remoteChangedFile = m_targetFileFinder.findPath(changedFile, &success); if (success) - m_clientPlugin->announceFile(remoteChangedFile, contents); + m_qmlPreviewClient->announceFile(remoteChangedFile, contents); else - m_clientPlugin->clearCache(); + m_qmlPreviewClient->clearCache(); m_lastLoadedUrl = m_targetFileFinder.findUrl(filename); - m_clientPlugin->loadUrl(m_lastLoadedUrl); + m_qmlPreviewClient->loadUrl(m_lastLoadedUrl); }); QObject::connect(this, &QmlPreviewConnectionManager::rerun, - m_clientPlugin.data(), &QmlPreviewClient::rerun); + m_qmlPreviewClient.data(), &QmlPreviewClient::rerun); QObject::connect(this, &QmlPreviewConnectionManager::zoom, - m_clientPlugin.data(), &QmlPreviewClient::zoom); + m_qmlPreviewClient.data(), &QmlPreviewClient::zoom); QObject::connect(this, &QmlPreviewConnectionManager::language, - m_clientPlugin.data(), [this](const QString &locale) { + m_qmlPreviewClient.data(), [this](const QString &locale) { - // The preview service expects a context URL. + // service expects a context URL. // Search the parent directories of the last loaded URL for i18n files. - - const QString shortLocale = locale.left(locale.indexOf("_")); - QString path = m_lastLoadedUrl.path(); - - while (!path.isEmpty()) { - path = path.left(qMax(0, path.lastIndexOf("/"))); - QUrl url = m_lastLoadedUrl; - - auto tryPath = [&](const QString &postfix) { - url.setPath(path + "/i18n/qml_" + postfix); - bool success = false; - m_projectFileFinder.findFile(url, &success); - return success; - }; - - if (tryPath(locale + ".qm")) - break; - if (tryPath(locale)) - break; - if (tryPath(shortLocale + ".qm")) - break; - if (tryPath(shortLocale)) - break; - } - - QUrl url = m_lastLoadedUrl; - url.setPath(path); - - m_clientPlugin->language(url, locale); + m_qmlPreviewClient->language(findValidI18nDirectoryAsUrl(locale), locale); }); - QObject::connect(m_clientPlugin.data(), &QmlPreviewClient::pathRequested, + QObject::connect(m_qmlPreviewClient.data(), &QmlPreviewClient::pathRequested, this, [this](const QString &path) { const bool found = m_projectFileFinder.findFileOrDirectory( path, [&](const QString &filename, int confidence) { @@ -146,31 +172,31 @@ void QmlPreviewConnectionManager::createClients() m_fileSystemWatcher.addFile(filename, Utils::FileSystemWatcher::WatchModifiedDate); } - m_clientPlugin->announceFile(path, contents); + m_qmlPreviewClient->announceFile(path, contents); } else { - m_clientPlugin->announceError(path); + m_qmlPreviewClient->announceError(path); } } else { - m_clientPlugin->announceError(path); + m_qmlPreviewClient->announceError(path); } }, [&](const QStringList &entries, int confidence) { if (confidence == path.length()) - m_clientPlugin->announceDirectory(path, entries); + m_qmlPreviewClient->announceDirectory(path, entries); else - m_clientPlugin->announceError(path); + m_qmlPreviewClient->announceError(path); }); if (!found) - m_clientPlugin->announceError(path); + m_qmlPreviewClient->announceError(path); }); - QObject::connect(m_clientPlugin.data(), &QmlPreviewClient::errorReported, + QObject::connect(m_qmlPreviewClient.data(), &QmlPreviewClient::errorReported, this, [](const QString &error) { Core::MessageManager::write("Error loading QML Live Preview:"); Core::MessageManager::write(error); }); - QObject::connect(m_clientPlugin.data(), &QmlPreviewClient::fpsReported, + QObject::connect(m_qmlPreviewClient.data(), &QmlPreviewClient::fpsReported, this, [this](const QmlPreviewClient::FpsInfo &frames) { if (m_fpsHandler) { quint16 stats[] = { @@ -181,14 +207,14 @@ void QmlPreviewConnectionManager::createClients() } }); - QObject::connect(m_clientPlugin.data(), &QmlPreviewClient::debugServiceUnavailable, + QObject::connect(m_qmlPreviewClient.data(), &QmlPreviewClient::debugServiceUnavailable, this, []() { - QMessageBox::warning(Core::ICore::mainWindow(), "Error loading QML Live Preview", + QMessageBox::warning(Core::ICore::dialogParent(), "Error loading QML Live Preview", "QML Live Preview is not available for this version of Qt."); }, Qt::QueuedConnection); // Queue it, so that it interfere with the connection timer QObject::connect(&m_fileSystemWatcher, &Utils::FileSystemWatcher::fileChanged, - m_clientPlugin.data(), [this](const QString &changedFile) { + m_qmlPreviewClient.data(), [this](const QString &changedFile) { if (!m_fileLoader || !m_lastLoadedUrl.isValid()) return; @@ -205,20 +231,28 @@ void QmlPreviewConnectionManager::createClients() const QString remoteChangedFile = m_targetFileFinder.findPath(changedFile, &success); if (success) - m_clientPlugin->announceFile(remoteChangedFile, contents); + m_qmlPreviewClient->announceFile(remoteChangedFile, contents); else - m_clientPlugin->clearCache(); + m_qmlPreviewClient->clearCache(); - m_clientPlugin->loadUrl(m_lastLoadedUrl); + m_qmlPreviewClient->loadUrl(m_lastLoadedUrl); }); } +void QmlPreviewConnectionManager::clearClient(QObject *client) +{ + if (client) { + disconnect(client, nullptr, this, nullptr); + disconnect(this, nullptr, client, nullptr); + client->deleteLater(); + } +}; + + void QmlPreviewConnectionManager::destroyClients() { - disconnect(m_clientPlugin.data(), nullptr, this, nullptr); - disconnect(this, nullptr, m_clientPlugin.data(), nullptr); - m_clientPlugin->deleteLater(); - m_clientPlugin.clear(); + clearClient(m_qmlPreviewClient); + clearClient(m_qmlDebugTranslationClient); m_fileSystemWatcher.removeFiles(m_fileSystemWatcher.files()); QTC_ASSERT(m_fileSystemWatcher.directories().isEmpty(), m_fileSystemWatcher.removeDirectories(m_fileSystemWatcher.directories())); |