From 8cc58c90d370bfe0a0491bf36369c8133780ec25 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Mon, 17 Jun 2024 12:46:12 +0200 Subject: [PATCH] PE: Fix project pointer when adding files or subprojects It is possible to trigger the respective actions directly from the project tree without having the respective project as current active project. Instead of always passing around the project which is currently active use the one we get from the selected node if there is any and fall back to the current active project if there is no selected node. Change-Id: Iccee63f1d75e88188b55f7307fcc46ba52e82a8a Reviewed-by: Christian Kandeler --- src/plugins/projectexplorer/projectexplorer.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 36ad23cfd7e..ecb636ba0be 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -3473,7 +3473,9 @@ void ProjectExplorerPluginPrivate::addNewFile() // to access meta data on an object that get deleted in the meantime: map.insert(QLatin1String(Constants::PREFERRED_PROJECT_NODE), QVariant::fromValue(static_cast(currentNode))); map.insert(Constants::PREFERRED_PROJECT_NODE_PATH, currentNode->filePath().toString()); - if (Project *p = ProjectTree::currentProject()) { + Project *p = ProjectTree::projectForNode(currentNode); + QTC_ASSERT(p, p = ProjectTree::currentProject()); + if (p) { const QStringList profileIds = Utils::transform(p->targets(), [](const Target *t) { return t->id().toString(); }); @@ -3504,7 +3506,9 @@ void ProjectExplorerPluginPrivate::addNewHeaderOrSource() QVariant::fromValue(static_cast(folderNode))); map.insert(Constants::PREFERRED_PROJECT_NODE_PATH, folderNode->filePath().toString()); map.insert("InitialFileName", fileNode->filePath().completeBaseName()); - if (Project *p = ProjectTree::currentProject()) { + Project *p = ProjectTree::projectForNode(folderNode); + QTC_ASSERT(p, p = ProjectTree::currentProject()); + if (p) { const QStringList profileIds = Utils::transform(p->targets(), [](const Target *t) { return t->id().toString(); }); @@ -3528,11 +3532,13 @@ void ProjectExplorerPluginPrivate::addNewSubproject() if (currentNode->isProjectNodeType() && currentNode->supportsAction(AddSubProject, currentNode)) { QVariantMap map; - map.insert(QLatin1String(Constants::PREFERRED_PROJECT_NODE), QVariant::fromValue(currentNode)); - Project *project = ProjectTree::currentProject(); + map.insert(QLatin1String(Constants::PREFERRED_PROJECT_NODE), + QVariant::fromValue(static_cast(currentNode))); + Project *project = ProjectTree::projectForNode(currentNode); + QTC_ASSERT(project, project = ProjectTree::currentProject()); Id projectType; if (project) { - const QStringList profileIds = Utils::transform(ProjectTree::currentProject()->targets(), + const QStringList profileIds = Utils::transform(project->targets(), [](const Target *t) { return t->id().toString(); });