aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmlprojectmanager/qmlprojectplugin.cpp
diff options
context:
space:
mode:
authorThomas Hartmann <[email protected]>2022-05-10 14:23:51 +0200
committerThomas Hartmann <[email protected]>2022-05-17 15:17:12 +0000
commitde7a7b6ac882bf55fc699563aaadbc7c7b3b2061 (patch)
tree2e4bb4a7d40c41af9a7f5680327d4127927ba684 /src/plugins/qmlprojectmanager/qmlprojectplugin.cpp
parent5e31fae6d9b87f47cd95d21bb2309d44632b1e28 (diff)
QmlProject: Allow setting main qml file and main ui.qml file
The .qmlproject file already has the "mainFile" setting which indicates which qml file is run. This patch adds a main ui.qml file that indicates which ui.qml file is opened in the design mode if the .qmlproject is opened. The patch also adds two context menu actions that allow setting the main qml and main.ui.qml files without having to edit the .qmlproject file by hand. Changing the main ui.qml file also checks if the current ui.qml file is used as in the main qml file and if it is, then we switch the component there. For now the actions are only available in QDS. Task-number: QDS-6882 Change-Id: I1c6e19c039036dc635161fa6e06173356dc509aa Reviewed-by: Henning Gründl <[email protected]> Reviewed-by: Qt CI Bot <[email protected]> Reviewed-by: <[email protected]>
Diffstat (limited to 'src/plugins/qmlprojectmanager/qmlprojectplugin.cpp')
-rw-r--r--src/plugins/qmlprojectmanager/qmlprojectplugin.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/plugins/qmlprojectmanager/qmlprojectplugin.cpp b/src/plugins/qmlprojectmanager/qmlprojectplugin.cpp
index bc7b6f3bde9..ff58349dc00 100644
--- a/src/plugins/qmlprojectmanager/qmlprojectplugin.cpp
+++ b/src/plugins/qmlprojectmanager/qmlprojectplugin.cpp
@@ -28,14 +28,20 @@
#include "qmlprojectconstants.h"
#include "qmlprojectrunconfiguration.h"
+#include <coreplugin/actionmanager/actionmanager.h>
+#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/icore.h>
#include <coreplugin/messagebox.h>
+#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectmanager.h>
+#include <projectexplorer/projectnodes.h>
+#include <projectexplorer/projecttree.h>
#include <projectexplorer/runcontrol.h>
#include <projectexplorer/session.h>
+#include <projectexplorer/target.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
@@ -47,6 +53,7 @@
#include <utils/infobar.h>
#include <utils/qtcprocess.h>
+#include <QAction>
#include <QMessageBox>
#include <QPushButton>
#include <QTimer>
@@ -197,6 +204,23 @@ void QmlProjectPlugin::openInQDSWithProject(const Utils::FilePath &filePath)
}
}
+static QmlBuildSystem *qmlBuildSystemforFileNode(const FileNode *fileNode)
+{
+ if (!fileNode)
+ return nullptr;
+
+ if (QmlProject *qmlProject = qobject_cast<QmlProject*>(fileNode->getProject())) {
+ auto target = qmlProject->activeTarget();
+ if (!target)
+ return nullptr;
+
+ return qobject_cast<QmlProjectManager::QmlBuildSystem *>(target->buildSystem());
+
+ }
+
+ return nullptr;
+}
+
bool QmlProjectPlugin::initialize(const QStringList &, QString *errorMessage)
{
Q_UNUSED(errorMessage)
@@ -206,6 +230,7 @@ bool QmlProjectPlugin::initialize(const QStringList &, QString *errorMessage)
if (!qmlDesignerEnabled()) {
connect(Core::EditorManager::instance(),
&Core::EditorManager::currentEditorChanged,
+ this,
[this](Core::IEditor *editor) {
QmlJS::ModelManagerInterface *modelManager
= QmlJS::ModelManagerInterface::instance();
@@ -258,6 +283,95 @@ bool QmlProjectPlugin::initialize(const QStringList &, QString *errorMessage)
ProjectManager::registerProjectType<QmlProject>(QmlJSTools::Constants::QMLPROJECT_MIMETYPE);
Core::FileIconProvider::registerIconOverlayForSuffix(":/qmlproject/images/qmlproject.png",
"qmlproject");
+
+ if (QmlProject::isQtDesignStudio()) {
+ Core::ActionContainer *menu = Core::ActionManager::actionContainer(
+ ProjectExplorer::Constants::M_FILECONTEXT);
+ QAction *mainfileAction = new QAction(tr("Set as main .qml file"), this);
+ mainfileAction->setEnabled(false);
+
+ connect(mainfileAction, &QAction::triggered, this, []() {
+ const Node *currentNode = ProjectTree::currentNode();
+ if (!currentNode || !currentNode->asFileNode()
+ || currentNode->asFileNode()->fileType() != FileType::QML)
+ return;
+
+ const Utils::FilePath file = currentNode->filePath();
+
+ QmlBuildSystem *buildSystem = qmlBuildSystemforFileNode(currentNode->asFileNode());
+ if (buildSystem)
+ buildSystem->setMainFileInProjectFile(file);
+ });
+
+ menu->addAction(Core::ActionManager::registerAction(
+ mainfileAction,
+ "QmlProject.setMainFile",
+ Core::Context(ProjectExplorer::Constants::C_PROJECT_TREE)),
+ ProjectExplorer::Constants::G_FILE_OTHER);
+ mainfileAction->setVisible(false);
+ connect(ProjectTree::instance(),
+ &ProjectTree::currentNodeChanged,
+ mainfileAction,
+ [mainfileAction](Node *node) {
+ const FileNode *fileNode = node ? node->asFileNode() : nullptr;
+
+ const bool isVisible = fileNode && fileNode->fileType() == FileType::QML
+ && fileNode->filePath().completeSuffix() == "qml";
+
+ mainfileAction->setVisible(isVisible);
+
+ if (!isVisible)
+ return;
+
+ QmlBuildSystem *buildSystem = qmlBuildSystemforFileNode(fileNode);
+
+ if (buildSystem)
+ mainfileAction->setEnabled(buildSystem->mainFilePath()
+ != fileNode->filePath());
+ });
+
+ QAction *mainUifileAction = new QAction(tr("Set as main .ui.qml file"), this);
+ mainUifileAction->setEnabled(false);
+
+ connect(mainUifileAction, &QAction::triggered, this, []() {
+ const Node *currentNode = ProjectTree::currentNode();
+ if (!currentNode || !currentNode->asFileNode()
+ || currentNode->asFileNode()->fileType() != FileType::QML)
+ return;
+
+ const Utils::FilePath file = currentNode->filePath();
+
+ QmlBuildSystem *buildSystem = qmlBuildSystemforFileNode(currentNode->asFileNode());
+ if (buildSystem)
+ buildSystem->setMainUiFileInProjectFile(file);
+ });
+
+ menu->addAction(Core::ActionManager::registerAction(
+ mainUifileAction,
+ "QmlProject.setMainUIFile",
+ Core::Context(ProjectExplorer::Constants::C_PROJECT_TREE)),
+ ProjectExplorer::Constants::G_FILE_OTHER);
+ mainUifileAction->setVisible(false);
+ connect(ProjectTree::instance(),
+ &ProjectTree::currentNodeChanged,
+ mainUifileAction,
+ [mainUifileAction](Node *node) {
+ const FileNode *fileNode = node ? node->asFileNode() : nullptr;
+ const bool isVisible = fileNode && fileNode->fileType() == FileType::QML
+ && fileNode->filePath().completeSuffix() == "ui.qml";
+
+ mainUifileAction->setVisible(isVisible);
+
+ if (!isVisible)
+ return;
+
+ QmlBuildSystem *buildSystem = qmlBuildSystemforFileNode(fileNode);
+ if (buildSystem)
+ mainUifileAction->setEnabled(buildSystem->mainUiFilePath()
+ != fileNode->filePath());
+ });
+ }
+
return true;
} // namespace Internal