From 670116f2c3cd5e2c1ddf73c1b1824fdef5ebfe38 Mon Sep 17 00:00:00 2001 From: hjk Date: Thu, 18 Jan 2024 13:14:00 +0100 Subject: [PATCH] QtSupport: Use setup functions for linguist and designer factories Change-Id: I5083544dd7e8e16e8b5ed349d23fdcfc0d022330 Reviewed-by: Jarek Kobus --- src/plugins/qtsupport/externaleditors.cpp | 139 ++++++++++++---------- src/plugins/qtsupport/externaleditors.h | 18 +-- src/plugins/qtsupport/qtsupportplugin.cpp | 6 +- 3 files changed, 84 insertions(+), 79 deletions(-) diff --git a/src/plugins/qtsupport/externaleditors.cpp b/src/plugins/qtsupport/externaleditors.cpp index 4dffd9fa55d..47e8d21e840 100644 --- a/src/plugins/qtsupport/externaleditors.cpp +++ b/src/plugins/qtsupport/externaleditors.cpp @@ -4,6 +4,7 @@ #include "externaleditors.h" #include +#include #include #include @@ -234,7 +235,7 @@ static bool startEditorProcess(const LaunchData &data, QString *errorMessage) return true; } -// DesignerExternalEditor with Designer Tcp remote control. +// ExternalDesignerEditorFactory with Designer Tcp remote control. // A per-binary entry containing the socket using ProcessCache = QMap; @@ -256,67 +257,76 @@ static void processTerminated(const QString &binary) socket->deleteLater(); } -DesignerExternalEditor::DesignerExternalEditor() +class ExternalDesignerFactory final : public Core::IEditorFactory { - setId("Qt.Designer"); - setDisplayName(::Core::Tr::tr("Qt Designer")); - setMimeTypes({Utils::Constants::FORM_MIMETYPE}); +public: + explicit ExternalDesignerFactory(QObject *guard) + { + setId("Qt.Designer"); + setDisplayName(::Core::Tr::tr("Qt Designer")); + setMimeTypes({Utils::Constants::FORM_MIMETYPE}); - setEditorStarter([this](const FilePath &filePath, QString *errorMessage) { - LaunchData data; + setEditorStarter([guard](const FilePath &filePath, QString *errorMessage) { + LaunchData data; - // Find the editor binary - if (!getEditorLaunchData(designerBinary, filePath, &data, errorMessage)) - return false; + // Find the editor binary + if (!getEditorLaunchData(designerBinary, filePath, &data, errorMessage)) + return false; - if (HostOsInfo::isMacHost()) - return startEditorProcess(data, errorMessage); + if (HostOsInfo::isMacHost()) + return startEditorProcess(data, errorMessage); - /* Qt Designer on the remaining platforms: Uses Designer's own + /* Qt Designer on the remaining platforms: Uses Designer's own * Tcp-based communication mechanism to ensure all files are opened * in one instance (per version). */ - // Known one? - const ProcessCache::iterator it = m_processCache.find(data.binary); - if (it != m_processCache.end()) { - // Process is known, write to its socket to cause it to open the file - if (debug) - qDebug() << Q_FUNC_INFO << "\nWriting to socket:" << data.binary << filePath; - QTcpSocket *socket = it.value(); - if (!socket->write(filePath.toString().toUtf8() + '\n')) { - *errorMessage = Tr::tr("Qt Designer is not responding (%1).").arg(socket->errorString()); + // Known one? + const ProcessCache::iterator it = m_processCache.find(data.binary); + if (it != m_processCache.end()) { + // Process is known, write to its socket to cause it to open the file + if (debug) + qDebug() << Q_FUNC_INFO << "\nWriting to socket:" << data.binary << filePath; + QTcpSocket *socket = it.value(); + if (!socket->write(filePath.toString().toUtf8() + '\n')) { + *errorMessage = Tr::tr("Qt Designer is not responding (%1).").arg(socket->errorString()); + return false; + } + return true; + } + // No process yet. Create socket & launch the process + QTcpServer server; + if (!server.listen(QHostAddress::LocalHost)) { + *errorMessage = Tr::tr("Unable to create server socket: %1").arg(server.errorString()); return false; } - return true; - } - // No process yet. Create socket & launch the process - QTcpServer server; - if (!server.listen(QHostAddress::LocalHost)) { - *errorMessage = Tr::tr("Unable to create server socket: %1").arg(server.errorString()); - return false; - } - const quint16 port = server.serverPort(); - if (debug) - qDebug() << Q_FUNC_INFO << "\nLaunching server:" << port << data.binary << filePath; - // Start first one with file and socket as '-client port file' - // Wait for the socket listening - data.arguments.push_front(QString::number(port)); - data.arguments.push_front(QLatin1String("-client")); + const quint16 port = server.serverPort(); + if (debug) + qDebug() << Q_FUNC_INFO << "\nLaunching server:" << port << data.binary << filePath; + // Start first one with file and socket as '-client port file' + // Wait for the socket listening + data.arguments.push_front(QString::number(port)); + data.arguments.push_front(QLatin1String("-client")); - if (!startEditorProcess(data, errorMessage)) - return false; - // Insert into cache if socket is created, else try again next time - if (server.waitForNewConnection(3000)) { - QTcpSocket *socket = server.nextPendingConnection(); - socket->setParent(&m_guard); - const QString binary = data.binary; - m_processCache.insert(binary, socket); - auto mapSlot = [binary] { processTerminated(binary); }; - QObject::connect(socket, &QAbstractSocket::disconnected, &m_guard, mapSlot); - QObject::connect(socket, &QAbstractSocket::errorOccurred, &m_guard, mapSlot); - } - return true; - }); + if (!startEditorProcess(data, errorMessage)) + return false; + // Insert into cache if socket is created, else try again next time + if (server.waitForNewConnection(3000)) { + QTcpSocket *socket = server.nextPendingConnection(); + socket->setParent(guard); + const QString binary = data.binary; + m_processCache.insert(binary, socket); + auto mapSlot = [binary] { processTerminated(binary); }; + QObject::connect(socket, &QAbstractSocket::disconnected, guard, mapSlot); + QObject::connect(socket, &QAbstractSocket::errorOccurred, guard, mapSlot); + } + return true; + }); + } +}; + +void setupExternalDesigner(QObject *guard) +{ + static ExternalDesignerFactory theExternalDesignerFactory(guard); } // Linguist @@ -328,16 +338,25 @@ static QString linguistBinary(const QtSupport::QtVersion *qtVersion) return QLatin1String(HostOsInfo::isMacHost() ? "Linguist" : "linguist"); } -LinguistEditor::LinguistEditor() +class ExternalLinguistFactory : public Core::IEditorFactory { - setId("Qt.Linguist"); - setDisplayName(::Core::Tr::tr("Qt Linguist")); - setMimeTypes({Utils::Constants::LINGUIST_MIMETYPE}); - setEditorStarter([](const FilePath &filePath, QString *errorMessage) { - LaunchData data; - return getEditorLaunchData(linguistBinary, filePath, &data, errorMessage) - && startEditorProcess(data, errorMessage); - }); +public: + ExternalLinguistFactory() + { + setId("Qt.Linguist"); + setDisplayName(::Core::Tr::tr("Qt Linguist")); + setMimeTypes({Utils::Constants::LINGUIST_MIMETYPE}); + setEditorStarter([](const FilePath &filePath, QString *errorMessage) { + LaunchData data; + return getEditorLaunchData(linguistBinary, filePath, &data, errorMessage) + && startEditorProcess(data, errorMessage); + }); + } +}; + +void setupExternalLinguist() +{ + static ExternalLinguistFactory theExternalDesignerFactory; } } // QtSupport::Internal diff --git a/src/plugins/qtsupport/externaleditors.h b/src/plugins/qtsupport/externaleditors.h index 5c34940f572..a22bae9507f 100644 --- a/src/plugins/qtsupport/externaleditors.h +++ b/src/plugins/qtsupport/externaleditors.h @@ -3,25 +3,11 @@ #pragma once -#include - #include namespace QtSupport::Internal { -class DesignerExternalEditor : public Core::IEditorFactory -{ -public: - DesignerExternalEditor(); - -private: - QObject m_guard; -}; - -class LinguistEditor : public Core::IEditorFactory -{ -public: - LinguistEditor(); -}; +void setupExternalDesigner(QObject *guard); +void setupExternalLinguist(); } // QtSupport::Internal diff --git a/src/plugins/qtsupport/qtsupportplugin.cpp b/src/plugins/qtsupport/qtsupportplugin.cpp index c69faf4a5d9..80e36f7f8ad 100644 --- a/src/plugins/qtsupport/qtsupportplugin.cpp +++ b/src/plugins/qtsupport/qtsupportplugin.cpp @@ -48,9 +48,6 @@ namespace QtSupport::Internal { class QtSupportPluginPrivate { public: - DesignerExternalEditor designerEditor; - LinguistEditor linguistEditor; - TranslationWizardPageFactory translationWizardPageFactory; }; @@ -109,6 +106,9 @@ void QtSupportPlugin::initialize() setupUicGenerator(); setupQScxmlcGenerator(); + setupExternalDesigner(this); + setupExternalLinguist(); + theProcessRunner() = processRunnerCallback; thePrompter() = [this](const QString &msg, const QStringList &context) -> std::optional {