From 909dcaeee05b41532586316d6875a2812023e7b5 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 30 Apr 2019 17:41:21 +0200 Subject: [PATCH] ProjectExplorer: Try harder to give new kits a unique name Fixes: QTCREATORBUG-16203 Change-Id: Id79b5f2e84359b1ad96b95393cc7fb60a965e63e Reviewed-by: hjk --- src/plugins/projectexplorer/kit.cpp | 17 +++++++++++++++-- src/plugins/projectexplorer/kit.h | 3 +++ src/plugins/projectexplorer/kitmodel.cpp | 12 +++++++++++- src/plugins/projectexplorer/kitmodel.h | 2 ++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/plugins/projectexplorer/kit.cpp b/src/plugins/projectexplorer/kit.cpp index 6b9f1d75fab..d29feab66ca 100644 --- a/src/plugins/projectexplorer/kit.cpp +++ b/src/plugins/projectexplorer/kit.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -202,8 +203,7 @@ Kit *Kit::clone(bool keepName) const if (keepName) k->d->m_unexpandedDisplayName = d->m_unexpandedDisplayName; else - k->d->m_unexpandedDisplayName = QCoreApplication::translate("ProjectExplorer::Kit", "Clone of %1") - .arg(d->m_unexpandedDisplayName); + k->d->m_unexpandedDisplayName = newKitName(KitManager::kits()); k->d->m_autodetected = false; k->d->m_data = d->m_data; // Do not clone m_fileSystemFriendlyName, needs to be unique @@ -674,6 +674,19 @@ MacroExpander *Kit::macroExpander() const return &d->m_macroExpander; } +QString Kit::newKitName(const QList &allKits) const +{ + return newKitName(unexpandedDisplayName(), allKits); +} + +QString Kit::newKitName(const QString &name, const QList &allKits) +{ + const QString baseName = name.isEmpty() + ? QCoreApplication::translate("ProjectExplorer::Kit", "Unnamed") + : QCoreApplication::translate("ProjectExplorer::Kit", "Clone of %1").arg(name); + return Utils::makeUniquelyNumbered(baseName, transform(allKits, &Kit::unexpandedDisplayName)); +} + void Kit::kitUpdated() { if (d->m_nestedBlockingLevel > 0) { diff --git a/src/plugins/projectexplorer/kit.h b/src/plugins/projectexplorer/kit.h index 81d734c71b9..b500b130860 100644 --- a/src/plugins/projectexplorer/kit.h +++ b/src/plugins/projectexplorer/kit.h @@ -137,6 +137,9 @@ public: bool hasFeatures(const QSet &features) const; Utils::MacroExpander *macroExpander() const; + QString newKitName(const QList &allKits) const; + static QString newKitName(const QString &name, const QList &allKits); + private: void setSdkProvided(bool sdkProvided); diff --git a/src/plugins/projectexplorer/kitmodel.cpp b/src/plugins/projectexplorer/kitmodel.cpp index 5e9f1ad561c..35285bf0878 100644 --- a/src/plugins/projectexplorer/kitmodel.cpp +++ b/src/plugins/projectexplorer/kitmodel.cpp @@ -247,6 +247,7 @@ void KitModel::markForRemoval(Kit *k) Kit *KitModel::markForAddition(Kit *baseKit) { + const QString newName = newKitName(baseKit ? baseKit->unexpandedDisplayName() : QString()); KitNode *node = createNode(nullptr); m_manualRoot->appendChild(node); Kit *k = node->widget->workingCopy(); @@ -255,10 +256,10 @@ Kit *KitModel::markForAddition(Kit *baseKit) k->copyFrom(baseKit); k->setAutoDetected(false); // Make sure we have a manual kit! k->setSdkProvided(false); - k->setUnexpandedDisplayName(tr("Clone of %1").arg(k->unexpandedDisplayName())); } else { k->setup(); } + k->setUnexpandedDisplayName(newName); if (!m_defaultNode) setDefaultNode(node); @@ -273,6 +274,15 @@ void KitModel::updateVisibility() }); } +QString KitModel::newKitName(const QString &sourceName) const +{ + QList allKits; + forItemsAtLevel<2>([&allKits](const TreeItem *ti) { + allKits << static_cast(ti)->widget->workingCopy(); + }); + return Kit::newKitName(sourceName, allKits); +} + KitNode *KitModel::findWorkingCopy(Kit *k) const { return findItemAtLevel<2>([k](KitNode *n) { return n->widget->workingCopy() == k; }); diff --git a/src/plugins/projectexplorer/kitmodel.h b/src/plugins/projectexplorer/kitmodel.h index c752215d419..f4e3d25f25c 100644 --- a/src/plugins/projectexplorer/kitmodel.h +++ b/src/plugins/projectexplorer/kitmodel.h @@ -71,6 +71,8 @@ public: void updateVisibility(); + QString newKitName(const QString &sourceName) const; + signals: void kitStateChanged();