QmlProjectManager: Cleanup qmlprojectplugin.cpp

... in preparation of moving the plugin class.

Change-Id: I449439f8db0a1c74dde51bf33a6f0e01fbc2a727
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2024-01-16 11:53:49 +01:00
parent 9c8e5d4cdb
commit b63e2d39e8
3 changed files with 74 additions and 79 deletions

View File

@@ -87,7 +87,7 @@ void QdsLandingPage::openQds(bool rememberSelection)
auto editor = Core::EditorManager::currentEditor(); auto editor = Core::EditorManager::currentEditor();
if (editor) if (editor)
QmlProjectPlugin::openInQDSWithProject(editor->document()->filePath()); openInQDSWithProject(editor->document()->filePath());
} }
void QdsLandingPage::installQds() void QdsLandingPage::installQds()

View File

@@ -51,6 +51,7 @@
#include <QPushButton> #include <QPushButton>
#include <QTimer> #include <QTimer>
using namespace Core;
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace Utils; using namespace Utils;
@@ -73,19 +74,19 @@ static bool qmlDesignerEnabled()
static QString alwaysOpenWithMode() static QString alwaysOpenWithMode()
{ {
return Core::ICore::settings() return ICore::settings()
->value(QmlProjectManager::Constants::ALWAYS_OPEN_UI_MODE, "") ->value(QmlProjectManager::Constants::ALWAYS_OPEN_UI_MODE, "")
.toString(); .toString();
} }
static void setAlwaysOpenWithMode(const QString &mode) static void setAlwaysOpenWithMode(const QString &mode)
{ {
Core::ICore::settings()->setValue(QmlProjectManager::Constants::ALWAYS_OPEN_UI_MODE, mode); ICore::settings()->setValue(QmlProjectManager::Constants::ALWAYS_OPEN_UI_MODE, mode);
} }
static void clearAlwaysOpenWithMode() static void clearAlwaysOpenWithMode()
{ {
Core::ICore::settings()->remove(QmlProjectManager::Constants::ALWAYS_OPEN_UI_MODE); ICore::settings()->remove(QmlProjectManager::Constants::ALWAYS_OPEN_UI_MODE);
} }
class QmlProjectPluginPrivate class QmlProjectPluginPrivate
@@ -111,20 +112,20 @@ QmlProjectPlugin::~QmlProjectPlugin()
delete d; delete d;
} }
void QmlProjectPlugin::openQDS(const Utils::FilePath &fileName) void openQDS(const FilePath &fileName)
{ {
const Utils::FilePath &qdsPath = QmlProjectPlugin::qdsInstallationEntry(); const FilePath qdsPath = qdsInstallationEntry();
bool qdsStarted = false; bool qdsStarted = false;
qputenv(Constants::enviromentLaunchedQDS, "true"); qputenv(Constants::enviromentLaunchedQDS, "true");
//-a and -client arguments help to append project to open design studio application //-a and -client arguments help to append project to open design studio application
if (Utils::HostOsInfo::isMacHost()) if (HostOsInfo::isMacHost())
qdsStarted = Utils::Process::startDetached( qdsStarted = Process::startDetached(
{"/usr/bin/open", {"-a", qdsPath.path(), fileName.toString()}}); {"/usr/bin/open", {"-a", qdsPath.path(), fileName.toString()}});
else else
qdsStarted = Utils::Process::startDetached({qdsPath, {"-client", fileName.toString()}}); qdsStarted = Process::startDetached({qdsPath, {"-client", fileName.toString()}});
if (!qdsStarted) { if (!qdsStarted) {
QMessageBox::warning(Core::ICore::dialogParent(), QMessageBox::warning(ICore::dialogParent(),
fileName.fileName(), fileName.fileName(),
Tr::tr("Failed to start Qt Design Studio.")); Tr::tr("Failed to start Qt Design Studio."));
if (alwaysOpenWithMode() == Core::Constants::MODE_DESIGN) if (alwaysOpenWithMode() == Core::Constants::MODE_DESIGN)
@@ -132,20 +133,20 @@ void QmlProjectPlugin::openQDS(const Utils::FilePath &fileName)
} }
} }
Utils::FilePath QmlProjectPlugin::qdsInstallationEntry() FilePath qdsInstallationEntry()
{ {
QtcSettings *settings = Core::ICore::settings(); QtcSettings *settings = ICore::settings();
const Key qdsInstallationEntry = "QML/Designer/DesignStudioInstallation"; //set in installer const Key qdsInstallationEntry = "QML/Designer/DesignStudioInstallation"; //set in installer
return Utils::FilePath::fromUserInput(settings->value(qdsInstallationEntry).toString()); return FilePath::fromUserInput(settings->value(qdsInstallationEntry).toString());
} }
bool QmlProjectPlugin::qdsInstallationExists() bool qdsInstallationExists()
{ {
return qdsInstallationEntry().exists(); return qdsInstallationEntry().exists();
} }
bool QmlProjectPlugin::checkIfEditorIsuiQml(Core::IEditor *editor) bool checkIfEditorIsuiQml(IEditor *editor)
{ {
if (editor if (editor
&& (editor->document()->id() == QmlJSEditor::Constants::C_QMLJSEDITOR_ID && (editor->document()->id() == QmlJSEditor::Constants::C_QMLJSEDITOR_ID
@@ -159,18 +160,18 @@ bool QmlProjectPlugin::checkIfEditorIsuiQml(Core::IEditor *editor)
return false; return false;
} }
const Utils::FilePath findQmlProject(const Utils::FilePath &folder) const FilePath findQmlProject(const FilePath &folder)
{ {
const Utils::FilePaths files = folder.dirEntries({QStringList("*.qmlproject"), QDir::Files}); const FilePaths files = folder.dirEntries({QStringList("*.qmlproject"), QDir::Files});
if (files.isEmpty()) if (files.isEmpty())
return {}; return {};
return files.constFirst(); return files.constFirst();
} }
const Utils::FilePath findQmlProjectUpwards(const Utils::FilePath &folder) const FilePath findQmlProjectUpwards(const FilePath &folder)
{ {
auto ret = findQmlProject(folder); FilePath ret = findQmlProject(folder);
if (ret.exists()) if (ret.exists())
return ret; return ret;
@@ -180,41 +181,37 @@ const Utils::FilePath findQmlProjectUpwards(const Utils::FilePath &folder)
return {}; return {};
} }
static bool findAndOpenProject(const Utils::FilePath &filePath) static bool findAndOpenProject(const FilePath &filePath)
{ {
ProjectExplorer::Project *project if (Project *project = ProjectManager::projectForFile(filePath)) {
= ProjectExplorer::ProjectManager::projectForFile(filePath);
if (project) {
if (project->projectFilePath().suffix() == "qmlproject") { if (project->projectFilePath().suffix() == "qmlproject") {
QmlProjectPlugin::openQDS(project->projectFilePath()); openQDS(project->projectFilePath());
return true;
}
FilePath projectFolder = project->rootProjectDirectory();
FilePath qmlProjectFile = findQmlProject(projectFolder);
if (qmlProjectFile.exists()) {
openQDS(qmlProjectFile);
return true; return true;
} else {
auto projectFolder = project->rootProjectDirectory();
auto qmlProjectFile = findQmlProject(projectFolder);
if (qmlProjectFile.exists()) {
QmlProjectPlugin::openQDS(qmlProjectFile);
return true;
}
} }
} }
auto qmlProjectFile = findQmlProjectUpwards(filePath); FilePath qmlProjectFile = findQmlProjectUpwards(filePath);
if (qmlProjectFile.exists()) { if (qmlProjectFile.exists()) {
QmlProjectPlugin::openQDS(qmlProjectFile); openQDS(qmlProjectFile);
return true; return true;
} }
return false; return false;
} }
void QmlProjectPlugin::openInQDSWithProject(const Utils::FilePath &filePath) void openInQDSWithProject(const FilePath &filePath)
{ {
if (findAndOpenProject(filePath)) { if (findAndOpenProject(filePath)) {
openQDS(filePath); openQDS(filePath);
//The first one might be ignored when QDS is starting up //The first one might be ignored when QDS is starting up
QTimer::singleShot(4000, [filePath] { openQDS(filePath); }); QTimer::singleShot(4000, [filePath] { openQDS(filePath); });
} else { } else {
Core::AsynchronousMessageBox::warning( AsynchronousMessageBox::warning(
Tr::tr("Qt Design Studio"), Tr::tr("Qt Design Studio"),
Tr::tr("No project file (*.qmlproject) found for Qt Design " Tr::tr("No project file (*.qmlproject) found for Qt Design "
"Studio.\nQt Design Studio requires a .qmlproject " "Studio.\nQt Design Studio requires a .qmlproject "
@@ -228,7 +225,7 @@ static QmlBuildSystem *qmlBuildSystemforFileNode(const FileNode *fileNode)
return nullptr; return nullptr;
if (QmlProject *qmlProject = qobject_cast<QmlProject*>(fileNode->getProject())) { if (QmlProject *qmlProject = qobject_cast<QmlProject*>(fileNode->getProject())) {
auto target = qmlProject->activeTarget(); Target *target = qmlProject->activeTarget();
if (!target) if (!target)
return nullptr; return nullptr;
@@ -255,20 +252,20 @@ void QmlProjectPlugin::initialize()
const QStringList mimeTypes = {Utils::Constants::QMLUI_MIMETYPE}; const QStringList mimeTypes = {Utils::Constants::QMLUI_MIMETYPE};
auto context = new Internal::DesignModeContext(d->landingPageWidget); auto context = new Internal::DesignModeContext(d->landingPageWidget);
Core::ICore::addContextObject(context); ICore::addContextObject(context);
Core::DesignMode::registerDesignWidget(d->landingPageWidget, mimeTypes, context->context()); DesignMode::registerDesignWidget(d->landingPageWidget, mimeTypes, context->context());
connect(Core::ModeManager::instance(), &Core::ModeManager::currentModeChanged, connect(ModeManager::instance(), &ModeManager::currentModeChanged,
this, &QmlProjectPlugin::editorModeChanged); this, &QmlProjectPlugin::editorModeChanged);
} }
ProjectManager::registerProjectType<QmlProject>(Utils::Constants::QMLPROJECT_MIMETYPE); ProjectManager::registerProjectType<QmlProject>(Utils::Constants::QMLPROJECT_MIMETYPE);
Utils::FileIconProvider::registerIconOverlayForSuffix(":/qmlproject/images/qmlproject.png", FileIconProvider::registerIconOverlayForSuffix(":/qmlproject/images/qmlproject.png",
"qmlproject"); "qmlproject");
if (Core::ICore::isQtDesignStudio()) { if (ICore::isQtDesignStudio()) {
Core::ActionContainer *menu = Core::ActionManager::actionContainer( ActionContainer *menu = ActionManager::actionContainer(
ProjectExplorer::Constants::M_FILECONTEXT); ProjectExplorer::Constants::M_FILECONTEXT);
QAction *mainfileAction = new QAction(Tr::tr("Set as Main .qml File"), this); QAction *mainfileAction = new QAction(Tr::tr("Set as Main .qml File"), this);
mainfileAction->setEnabled(false); mainfileAction->setEnabled(false);
@@ -279,17 +276,17 @@ void QmlProjectPlugin::initialize()
|| currentNode->asFileNode()->fileType() != FileType::QML) || currentNode->asFileNode()->fileType() != FileType::QML)
return; return;
const Utils::FilePath file = currentNode->filePath(); const FilePath file = currentNode->filePath();
QmlBuildSystem *buildSystem = qmlBuildSystemforFileNode(currentNode->asFileNode()); QmlBuildSystem *buildSystem = qmlBuildSystemforFileNode(currentNode->asFileNode());
if (buildSystem) if (buildSystem)
buildSystem->setMainFileInProjectFile(file); buildSystem->setMainFileInProjectFile(file);
}); });
menu->addAction(Core::ActionManager::registerAction( menu->addAction(ActionManager::registerAction(
mainfileAction, mainfileAction,
"QmlProject.setMainFile", "QmlProject.setMainFile",
Core::Context(ProjectExplorer::Constants::C_PROJECT_TREE)), Context(ProjectExplorer::Constants::C_PROJECT_TREE)),
ProjectExplorer::Constants::G_FILE_OTHER); ProjectExplorer::Constants::G_FILE_OTHER);
mainfileAction->setVisible(false); mainfileAction->setVisible(false);
connect(ProjectTree::instance(), connect(ProjectTree::instance(),
@@ -322,14 +319,14 @@ void QmlProjectPlugin::initialize()
|| currentNode->asFileNode()->fileType() != FileType::QML) || currentNode->asFileNode()->fileType() != FileType::QML)
return; return;
const Utils::FilePath file = currentNode->filePath(); const FilePath file = currentNode->filePath();
QmlBuildSystem *buildSystem = qmlBuildSystemforFileNode(currentNode->asFileNode()); QmlBuildSystem *buildSystem = qmlBuildSystemforFileNode(currentNode->asFileNode());
if (buildSystem) if (buildSystem)
buildSystem->setMainUiFileInProjectFile(file); buildSystem->setMainUiFileInProjectFile(file);
}); });
menu->addAction(Core::ActionManager::registerAction( menu->addAction(ActionManager::registerAction(
mainUifileAction, mainUifileAction,
"QmlProject.setMainUIFile", "QmlProject.setMainUIFile",
Core::Context(ProjectExplorer::Constants::C_PROJECT_TREE)), Core::Context(ProjectExplorer::Constants::C_PROJECT_TREE)),
@@ -356,7 +353,7 @@ void QmlProjectPlugin::initialize()
} }
GenerateCmake::generateMenuEntry(this); GenerateCmake::generateMenuEntry(this);
if (Core::ICore::isQtDesignStudio()) if (ICore::isQtDesignStudio())
GenerateCmake::CmakeProjectConverter::generateMenuEntry(this); GenerateCmake::CmakeProjectConverter::generateMenuEntry(this);
} }
@@ -379,14 +376,14 @@ void QmlProjectPlugin::hideQmlLandingPage()
d->landingPage->hide(); d->landingPage->hide();
} }
static bool isDesignerMode(Utils::Id mode) static bool isDesignerMode(Id mode)
{ {
return mode == Core::Constants::MODE_DESIGN; return mode == Core::Constants::MODE_DESIGN;
} }
void QmlProjectPlugin::editorModeChanged(Utils::Id newMode, Utils::Id oldMode) void QmlProjectPlugin::editorModeChanged(Id newMode, Id oldMode)
{ {
Core::IEditor *currentEditor = Core::EditorManager::currentEditor(); IEditor *currentEditor = EditorManager::currentEditor();
if (checkIfEditorIsuiQml(currentEditor)) { if (checkIfEditorIsuiQml(currentEditor)) {
if (isDesignerMode(newMode)) { if (isDesignerMode(newMode)) {
if (alwaysOpenWithMode() == Core::Constants::MODE_DESIGN) if (alwaysOpenWithMode() == Core::Constants::MODE_DESIGN)
@@ -409,7 +406,7 @@ void QmlProjectPlugin::openQtc(bool permanent)
if (d->landingPage) if (d->landingPage)
hideQmlLandingPage(); hideQmlLandingPage();
Core::ModeManager::activateMode(Core::Constants::MODE_EDIT); ModeManager::activateMode(Core::Constants::MODE_EDIT);
} }
void QmlProjectPlugin::openQds(bool permanent) void QmlProjectPlugin::openQds(bool permanent)
@@ -420,12 +417,11 @@ void QmlProjectPlugin::openQds(bool permanent)
if (d->landingPage) if (d->landingPage)
hideQmlLandingPage(); hideQmlLandingPage();
auto editor = Core::EditorManager::currentEditor(); if (IEditor *editor = EditorManager::currentEditor())
if (editor)
openInQDSWithProject(editor->document()->filePath()); openInQDSWithProject(editor->document()->filePath());
} }
void QmlProjectPlugin::updateQmlLandingPageProjectInfo(const Utils::FilePath &projectFile) void QmlProjectPlugin::updateQmlLandingPageProjectInfo(const FilePath &projectFile)
{ {
if (!d->landingPage) if (!d->landingPage)
return; return;
@@ -437,15 +433,16 @@ void QmlProjectPlugin::updateQmlLandingPageProjectInfo(const Utils::FilePath &pr
d->landingPage->setQdsVersion(qdsVersionString); d->landingPage->setQdsVersion(qdsVersionString);
} }
Utils::FilePath QmlProjectPlugin::projectFilePath() FilePath projectFilePath()
{ {
auto project = ProjectExplorer::ProjectManager::startupProject(); Project *project = ProjectManager::startupProject();
const QmlProjectManager::QmlProject *qmlProject = qobject_cast<const QmlProjectManager::QmlProject*>(project);
if (qmlProject) { if (const QmlProject *qmlProject = qobject_cast<const QmlProject*>(project))
return qmlProject->projectFilePath(); return qmlProject->projectFilePath();
} else if (project) {
auto projectFolder = project->rootProjectDirectory(); if (project) {
auto qmlProjectFile = findQmlProject(projectFolder); FilePath projectFolder = project->rootProjectDirectory();
FilePath qmlProjectFile = findQmlProject(projectFolder);
if (qmlProjectFile.exists()) if (qmlProjectFile.exists())
return qmlProjectFile; return qmlProjectFile;
} }

View File

@@ -7,8 +7,18 @@
#include <extensionsystem/iplugin.h> #include <extensionsystem/iplugin.h>
#include <utils/filepath.h> #include <utils/filepath.h>
namespace QmlProjectManager { namespace QmlProjectManager::Internal {
namespace Internal {
void openQDS(const Utils::FilePath &fileName);
Utils::FilePath qdsInstallationEntry();
bool qdsInstallationExists();
bool checkIfEditorIsuiQml(Core::IEditor *editor);
Utils::FilePath projectFilePath();
Utils::FilePaths rootCmakeFiles();
QString qtVersion(const Utils::FilePath &projectFilePath);
QString qdsVersion(const Utils::FilePath &projectFilePath);
void openInQDSWithProject(const Utils::FilePath &filePath);
const QString readFileContents(const Utils::FilePath &filePath);
class QmlProjectPlugin final : public ExtensionSystem::IPlugin class QmlProjectPlugin final : public ExtensionSystem::IPlugin
{ {
@@ -19,17 +29,6 @@ public:
QmlProjectPlugin() = default; QmlProjectPlugin() = default;
~QmlProjectPlugin() final; ~QmlProjectPlugin() final;
static void openQDS(const Utils::FilePath &fileName);
static Utils::FilePath qdsInstallationEntry();
static bool qdsInstallationExists();
static bool checkIfEditorIsuiQml(Core::IEditor *editor);
static Utils::FilePath projectFilePath();
static Utils::FilePaths rootCmakeFiles();
static QString qtVersion(const Utils::FilePath &projectFilePath);
static QString qdsVersion(const Utils::FilePath &projectFilePath);
static void openInQDSWithProject(const Utils::FilePath &filePath);
static const QString readFileContents(const Utils::FilePath &filePath);
public slots: public slots:
void editorModeChanged(Utils::Id newMode, Utils::Id oldMode); void editorModeChanged(Utils::Id newMode, Utils::Id oldMode);
void openQtc(bool permanent = false); void openQtc(bool permanent = false);
@@ -44,5 +43,4 @@ private:
class QmlProjectPluginPrivate *d = nullptr; class QmlProjectPluginPrivate *d = nullptr;
}; };
} // namespace Internal } // QmlProject::Internal
} // namespace QmlProject