PE: Fix adding first project for subdirs project

Change-Id: Iab2d463d7b138e9a6d4c9bf10e94556ea9a10d77
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Christian Stenger
2024-06-10 15:42:40 +02:00
parent eb7332fb8b
commit e5844211ab
3 changed files with 50 additions and 7 deletions

View File

@@ -4,12 +4,14 @@
#include "jsonsummarypage.h" #include "jsonsummarypage.h"
#include "jsonwizard.h" #include "jsonwizard.h"
#include "../buildsystem.h"
#include "../project.h" #include "../project.h"
#include "../projectexplorerconstants.h" #include "../projectexplorerconstants.h"
#include "../projectexplorertr.h" #include "../projectexplorertr.h"
#include "../projectnodes.h" #include "../projectnodes.h"
#include "../projectmanager.h" #include "../projectmanager.h"
#include "../projecttree.h" #include "../projecttree.h"
#include "../target.h"
#include <coreplugin/coreconstants.h> #include <coreplugin/coreconstants.h>
#include <coreplugin/iversioncontrol.h> #include <coreplugin/iversioncontrol.h>
@@ -76,6 +78,24 @@ void JsonSummaryPage::setHideProjectUiValue(const QVariant &hideProjectUiValue)
m_hideProjectUiValue = hideProjectUiValue; m_hideProjectUiValue = hideProjectUiValue;
} }
static Node *extractPreferredNode(const JsonWizard *wizard)
{
// Use static cast from void * to avoid qobject_cast (which needs a valid object) in value()
// in the following code:
Node *preferred = nullptr;
QVariant variant = wizard->value(Constants::PREFERRED_PROJECT_NODE);
if (variant.isValid()) {
preferred = static_cast<Node *>(variant.value<void *>());
} else {
variant = wizard->value(Constants::PREFERRED_PROJECT_NODE_PATH);
if (variant.isValid()) {
const FilePath fp = FilePath::fromVariant(variant);
preferred = ProjectTree::instance()->nodeForFile(fp);
}
}
return preferred;
}
void JsonSummaryPage::initializePage() void JsonSummaryPage::initializePage()
{ {
m_wizard = qobject_cast<JsonWizard *>(wizard()); m_wizard = qobject_cast<JsonWizard *>(wizard());
@@ -109,18 +129,39 @@ void JsonSummaryPage::initializePage()
}); });
} }
// Use static cast from void * to avoid qobject_cast (which needs a valid object) in value() Node *preferredNode = extractPreferredNode(m_wizard);
// in the following code: const FilePath preferredNodePath = preferredNode ? preferredNode->filePath() : FilePath{};
auto contextNode = findWizardContextNode(static_cast<Node *>(m_wizard->value(Constants::PREFERRED_PROJECT_NODE).value<void *>())); auto contextNode = findWizardContextNode(preferredNode);
const ProjectAction currentAction = isProject ? AddSubProject : AddNewFile; const ProjectAction currentAction = isProject ? AddSubProject : AddNewFile;
auto updateProjectTree = [this, files, kind, currentAction, preferredNodePath]() {
Node *node = currentNode();
if (!node) {
if (auto p = ProjectManager::projectWithProjectFilePath(preferredNodePath))
node = p->rootProjectNode();
}
initializeProjectTree(findWizardContextNode(node), files, kind, currentAction);
if (m_bsConnection && sender() != ProjectTree::instance())
disconnect(m_bsConnection);
};
if (contextNode) {
if (auto p = contextNode->getProject()) {
if (auto targets = p->targets(); !targets.isEmpty()) {
if (auto bs = targets.first()->buildSystem()) {
if (bs->isParsing()) {
m_bsConnection = connect(bs, &BuildSystem::parsingFinished,
this, updateProjectTree);
}
}
}
}
}
initializeProjectTree(contextNode, files, kind, currentAction); initializeProjectTree(contextNode, files, kind, currentAction);
// Refresh combobox on project tree changes: // Refresh combobox on project tree changes:
connect(ProjectTree::instance(), &ProjectTree::treeChanged, connect(ProjectTree::instance(), &ProjectTree::treeChanged,
this, [this, files, kind, currentAction]() { this, updateProjectTree);
initializeProjectTree(findWizardContextNode(currentNode()), files, kind, currentAction);
});
bool hideProjectUi = JsonWizard::boolFromVariant(m_hideProjectUiValue, m_wizard->expander()); bool hideProjectUi = JsonWizard::boolFromVariant(m_hideProjectUiValue, m_wizard->expander());

View File

@@ -38,6 +38,7 @@ private:
JsonWizard *m_wizard; JsonWizard *m_wizard;
JsonWizard::GeneratorFiles m_fileList; JsonWizard::GeneratorFiles m_fileList;
QVariant m_hideProjectUiValue; QVariant m_hideProjectUiValue;
QMetaObject::Connection m_bsConnection;
}; };
} // namespace ProjectExplorer } // namespace ProjectExplorer

View File

@@ -69,7 +69,8 @@ bool SubdirsProjectWizard::postGenerateFiles(const QWizard *w, const Core::Gener
const FilePath projectPath = params.projectPath(); const FilePath projectPath = params.projectPath();
const FilePath profileName = Core::BaseFileWizardFactory::buildFileName(projectPath, params.fileName, profileSuffix()); const FilePath profileName = Core::BaseFileWizardFactory::buildFileName(projectPath, params.fileName, profileSuffix());
QVariantMap map; QVariantMap map;
map.insert(QLatin1String(ProjectExplorer::Constants::PREFERRED_PROJECT_NODE), profileName.toVariant()); map.insert(QLatin1String(ProjectExplorer::Constants::PREFERRED_PROJECT_NODE), QVariant());
map.insert(QLatin1String(ProjectExplorer::Constants::PREFERRED_PROJECT_NODE_PATH), profileName.toVariant());
map.insert(QLatin1String(ProjectExplorer::Constants::PROJECT_KIT_IDS), map.insert(QLatin1String(ProjectExplorer::Constants::PROJECT_KIT_IDS),
Utils::transform<QStringList>(wizard->selectedKits(), &Utils::Id::toString)); Utils::transform<QStringList>(wizard->selectedKits(), &Utils::Id::toString));
IWizardFactory::requestNewItemDialog(Tr::tr("New Subproject", "Title of dialog"), IWizardFactory::requestNewItemDialog(Tr::tr("New Subproject", "Title of dialog"),