ProjectExplorer: Add convenience Project::addTargetFor{Default,}Kit

Less noise on the user side.

Change-Id: I5cdf4af4910a3cc1ee0af1b43fcbc7329a6d59db
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2019-08-01 14:58:00 +02:00
parent 72fe2c706e
commit 09e5c167bd
10 changed files with 41 additions and 44 deletions

View File

@@ -34,7 +34,6 @@
#include <projectexplorer/abi.h>
#include <projectexplorer/toolchain.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/buildsteplist.h>
@@ -105,9 +104,8 @@ Project::RestoreResult AutotoolsProject::fromMap(const QVariantMap &map, QString
// Load the project tree structure.
loadProjectTree();
Kit *defaultKit = KitManager::defaultKit();
if (!activeTarget() && defaultKit)
addTarget(createTarget(defaultKit));
if (!activeTarget())
addTargetForDefaultKit();
return RestoreResult::Ok;
}

View File

@@ -405,7 +405,7 @@ CompilationDatabaseProject::CompilationDatabaseProject(const Utils::FilePath &pr
setPreferredKitPredicate([](const Kit *) { return false; });
m_kit.reset(KitManager::defaultKit()->clone());
addTarget(createTarget(m_kit.get()));
addTargetForKit(m_kit.get());
connect(this, &CompilationDatabaseProject::rootProjectDirectoryChanged,
m_parseDelay, QOverload<>::of(&QTimer::start));

View File

@@ -45,7 +45,6 @@
#include <projectexplorer/deploymentdata.h>
#include <projectexplorer/headerpath.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/target.h>
@@ -562,9 +561,8 @@ Project::RestoreResult GenericProject::fromMap(const QVariantMap &map, QString *
if (result != RestoreResult::Ok)
return result;
Kit *defaultKit = KitManager::defaultKit();
if (!activeTarget() && defaultKit)
addTarget(createTarget(defaultKit));
if (!activeTarget())
addTargetForDefaultKit();
// Sanity check: We need both a buildconfiguration and a runconfiguration!
const QList<Target *> targetList = targets();

View File

@@ -267,6 +267,27 @@ void Project::addTarget(std::unique_ptr<Target> &&t)
SessionManager::setActiveTarget(this, pointer, SetActive::Cascade);
}
Target *Project::addTargetForDefaultKit()
{
return addTargetForKit(KitManager::defaultKit());
}
Target *Project::addTargetForKit(Kit *kit)
{
if (!kit || target(kit))
return nullptr;
auto t = std::make_unique<Target>(this, kit, Target::_constructor_tag{});
Target *pointer = t.get();
if (!setupTarget(pointer))
return {};
addTarget(std::move(t));
return pointer;
}
bool Project::removeTarget(Target *target)
{
QTC_ASSERT(target && Utils::contains(d->m_targets, target), return false);
@@ -336,17 +357,6 @@ Tasks Project::projectIssues(const Kit *k) const
return {};
}
std::unique_ptr<Target> Project::createTarget(Kit *k)
{
if (!k || target(k))
return nullptr;
auto t = std::make_unique<Target>(this, k, Target::_constructor_tag{});
if (!setupTarget(t.get()))
return {};
return t;
}
bool Project::copySteps(Target *sourceTarget, Target *newTarget)
{
QTC_ASSERT(newTarget, return false);

View File

@@ -122,7 +122,8 @@ public:
EditorConfiguration *editorConfiguration() const;
// Target:
void addTarget(std::unique_ptr<Target> &&target);
Target *addTargetForDefaultKit();
Target *addTargetForKit(Kit *kit);
bool removeTarget(Target *target);
QList<Target *> targets() const;
@@ -132,7 +133,6 @@ public:
Target *target(Kit *k) const;
virtual Tasks projectIssues(const Kit *k) const;
std::unique_ptr<Target> createTarget(Kit *k);
static bool copySteps(Target *sourceTarget, Target *newTarget);
void saveSettings();
@@ -268,6 +268,8 @@ protected:
Utils::Environment activeBuildEnvironment() const;
private:
void addTarget(std::unique_ptr<Target> &&target);
void handleSubTreeChanged(FolderNode *node);
void setActiveTarget(Target *target);
ProjectPrivate *d;

View File

@@ -552,12 +552,8 @@ public:
BuildConfiguration *lastBc = nullptr;
for (const BuildInfo &info : projectImporter->import(path, false)) {
Target *target = project->target(info.kitId);
if (!target) {
std::unique_ptr<Target> newTarget = project->createTarget(KitManager::kit(info.kitId));
target = newTarget.get();
if (newTarget)
project->addTarget(std::move(newTarget));
}
if (!target)
target = project->addTargetForKit(KitManager::kit(info.kitId));
if (target) {
projectImporter->makePersistent(target->kit());
BuildConfiguration *bc = info.factory()->create(target, info);

View File

@@ -407,8 +407,7 @@ public:
QTC_ASSERT(!data.isValid(), return false);
if (!isEnabled()) {
m_currentChild = DefaultPage;
Kit *k = KitManager::kit(m_kitId);
m_project->addTarget(m_project->createTarget(k));
m_project->addTargetForKit(KitManager::kit(m_kitId));
} else {
// Go to Run page, when on Run previously etc.
TargetItem *previousItem = parent()->currentTargetItem();
@@ -450,7 +449,7 @@ public:
QAction *enableAction = menu->addAction(tr("Enable Kit \"%1\" for Project \"%2\"").arg(kitName, projectName));
enableAction->setEnabled(isSelectable && m_kitId.isValid() && !isEnabled());
QObject::connect(enableAction, &QAction::triggered, [this, kit] {
m_project->addTarget(m_project->createTarget(kit));
m_project->addTargetForKit(kit);
});
QAction *disableAction = menu->addAction(tr("Disable Kit \"%1\" for Project \"%2\"").arg(kitName, projectName));

View File

@@ -38,7 +38,6 @@
#include <coreplugin/editormanager/editormanager.h>
#include <projectexplorer/buildtargetinfo.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/localenvironmentaspect.h>
#include <projectexplorer/runcontrol.h>
#include <projectexplorer/runconfiguration.h>
@@ -726,9 +725,8 @@ Project::RestoreResult PythonProject::fromMap(const QVariantMap &map, QString *e
if (res == RestoreResult::Ok) {
refresh();
Kit *defaultKit = KitManager::defaultKit();
if (!activeTarget() && defaultKit)
addTarget(createTarget(defaultKit));
if (!activeTarget())
addTargetForDefaultKit();
}
return res;

View File

@@ -223,13 +223,7 @@ void QmlProfilerDetailsRewriterTest::seedRewriter()
DummyProject *project = new DummyProject(Utils::FilePath::fromString(filename));
ProjectExplorer::SessionManager::addProject(project);
{
// Make sure the uniqe_ptr gets deleted before the project.
// Otherwise we'll get a double free because the target is also parented to the project
// and unique_ptr doesn't know anything about QObject parent/child relationships.
std::unique_ptr<ProjectExplorer::Target> target = project->createTarget(kit.get());
m_rewriter.populateFileFinder(target.get());
}
m_rewriter.populateFileFinder(project->addTargetForKit(kit.get()));
ProjectExplorer::SessionManager::removeProject(project);
}

View File

@@ -341,8 +341,10 @@ Project::RestoreResult QmlProject::fromMap(const QVariantMap &map, QString *erro
});
if (!kits.isEmpty()) {
Kit *kit = kits.contains(KitManager::defaultKit()) ? KitManager::defaultKit() : kits.first();
addTarget(createTarget(kit));
if (kits.contains(KitManager::defaultKit()))
addTargetForDefaultKit();
else
addTargetForKit(kits.first());
}
}