Commit 41dfa8bc authored by Tobias Hunger's avatar Tobias Hunger

Kits: Fix making names unique in optionspage for kits

Task-number: QTCREATORBUG-8861

Change-Id: Iea9948f1f62a79df36b239ad3981cb137b44e5bd
Reviewed-by: default avatarDaniel Teske <daniel.teske@digia.com>
parent da8d2235
......@@ -91,7 +91,8 @@ public:
m_isValid(true),
m_hasWarning(false),
m_nestedBlockingLevel(0),
m_mustNotify(false)
m_mustNotify(false),
m_mustNotifyAboutDisplayName(false)
{
if (!id.isValid())
m_id = Id::fromString(QUuid::createUuid().toString());
......@@ -107,6 +108,7 @@ public:
QString m_iconPath;
int m_nestedBlockingLevel;
bool m_mustNotify;
bool m_mustNotifyAboutDisplayName;
QHash<Core::Id, QVariant> m_data;
};
......@@ -144,9 +146,12 @@ void Kit::unblockNotification()
--d->m_nestedBlockingLevel;
if (d->m_nestedBlockingLevel > 0)
return;
if (d->m_mustNotify)
if (d->m_mustNotifyAboutDisplayName)
kitDisplayNameChanged();
else if (d->m_mustNotify)
kitUpdated();
d->m_mustNotify = false;
d->m_mustNotifyAboutDisplayName = false;
}
Kit *Kit::clone(bool keepName) const
......@@ -174,6 +179,7 @@ void Kit::copyFrom(const Kit *k)
d->m_autodetected = k->d->m_autodetected;
d->m_displayName = k->d->m_displayName;
d->m_mustNotify = true;
d->m_mustNotifyAboutDisplayName = true;
}
bool Kit::isValid() const
......@@ -241,46 +247,22 @@ static QString candidateName(const QString &name, const QString &postfix)
void Kit::setDisplayName(const QString &name)
{
KitManager *km = KitManager::instance();
QList<KitInformation *> kitInfo = km->kitInformation();
QStringList nameList;
nameList << QString(); // Disallow empty kit names!
foreach (Kit *k, km->kits()) {
if (k == this)
continue;
nameList << k->displayName();
foreach (KitInformation *ki, kitInfo) {
const QString postfix = ki->displayNamePostfix(k);
if (!postfix.isEmpty())
nameList << candidateName(k->displayName(), postfix);
}
}
QStringList candidateNames;
candidateNames << name;
if (d->m_displayName == name)
return;
d->m_displayName = name;
kitDisplayNameChanged();
}
foreach (KitInformation *ki, kitInfo) {
QStringList Kit::candidateNameList(const QString &base) const
{
QStringList result;
result << base;
foreach (KitInformation *ki, KitManager::instance()->kitInformation()) {
const QString postfix = ki->displayNamePostfix(this);
if (!postfix.isEmpty())
candidateNames << candidateName(name, postfix);
}
QString uniqueName = Project::makeUnique(name, nameList);
if (uniqueName != name) {
foreach (const QString &candidate, candidateNames) {
const QString tmp = Project::makeUnique(candidate, nameList);
if (tmp == candidate) {
uniqueName = tmp;
break;
}
}
result << candidateName(base, postfix);
}
if (d->m_displayName == uniqueName)
return;
d->m_displayName = uniqueName;
kitUpdated();
return result;
}
QString Kit::fileSystemFriendlyName() const
......@@ -494,7 +476,7 @@ void Kit::setSdkProvided(bool sdkProvided)
void Kit::kitUpdated()
{
if (d->m_nestedBlockingLevel > 0) {
if (d->m_nestedBlockingLevel > 0 && !d->m_mustNotifyAboutDisplayName) {
d->m_mustNotify = true;
return;
}
......@@ -502,4 +484,15 @@ void Kit::kitUpdated()
KitManager::instance()->notifyAboutUpdate(this);
}
void Kit::kitDisplayNameChanged()
{
if (d->m_nestedBlockingLevel > 0) {
d->m_mustNotifyAboutDisplayName = true;
d->m_mustNotify = false;
return;
}
validate();
KitManager::instance()->notifyAboutDisplayNameChange(this);
}
} // namespace ProjectExplorer
......@@ -73,6 +73,8 @@ public:
QString displayName() const;
void setDisplayName(const QString &name);
QStringList candidateNameList(const QString &base) const;
QString fileSystemFriendlyName() const;
bool isAutoDetected() const;
......@@ -107,7 +109,7 @@ private:
Kit(const Kit &other);
void operator=(const Kit &other);
void kitDisplayNameChanged();
void kitUpdated();
QVariantMap toMap() const;
......
......@@ -114,13 +114,14 @@ public:
Kit *m_defaultKit;
bool m_initialized;
bool m_keepDisplayNameUnique;
QList<KitInformation *> m_informationList;
QList<Kit *> m_kitList;
Utils::PersistentSettingsWriter *m_writer;
};
KitManagerPrivate::KitManagerPrivate()
: m_defaultKit(0), m_initialized(false), m_writer(0)
KitManagerPrivate::KitManagerPrivate() :
m_defaultKit(0), m_initialized(false), m_keepDisplayNameUnique(true), m_writer(0)
{ }
KitManagerPrivate::~KitManagerPrivate()
......@@ -158,6 +159,13 @@ KitManager::KitManager(QObject *parent) :
this, SIGNAL(kitsChanged()));
}
bool KitManager::setKeepDisplayNameUnique(bool unique)
{
bool current = d->m_keepDisplayNameUnique;
d->m_keepDisplayNameUnique = unique;
return current;
}
void KitManager::restoreKits()
{
QTC_ASSERT(!d->m_initialized, return);
......@@ -428,17 +436,53 @@ bool KitManager::isLoaded() const
return d->m_initialized;
}
void KitManager::notifyAboutUpdate(ProjectExplorer::Kit *k)
QString KitManager::uniqueKitName(const Kit *k, const QString name, const QList<Kit *> &allKits)
{
QStringList nameList;
nameList << QString(); // Disallow empty kit names!
foreach (Kit *tmp, allKits) {
if (tmp == k)
continue;
nameList.append(tmp->candidateNameList(tmp->displayName()));
}
QStringList candidateNames = k->candidateNameList(name);
QString uniqueName = Project::makeUnique(name, nameList);
if (uniqueName != name) {
foreach (const QString &candidate, candidateNames) {
const QString tmp = Project::makeUnique(candidate, nameList);
if (tmp == candidate) {
uniqueName = tmp;
break;
}
}
}
return uniqueName;
}
void KitManager::notifyAboutDisplayNameChange(Kit *k)
{
if (!k)
return;
if (d->m_kitList.contains(k) && d->m_keepDisplayNameUnique)
k->setDisplayName(uniqueKitName(k, k->displayName(), kits()));
int pos = d->m_kitList.indexOf(k);
if (pos >= 0 && d->m_initialized) {
if (pos >= 0 && d->m_initialized)
d->moveKit(pos);
notifyAboutUpdate(k);
}
void KitManager::notifyAboutUpdate(ProjectExplorer::Kit *k)
{
if (!k || !d->m_initialized)
return;
if (d->m_kitList.contains(k))
emit kitUpdated(k);
} else {
else
emit unmanagedKitUpdated(k);
}
}
bool KitManager::registerKit(ProjectExplorer::Kit *k)
......@@ -451,6 +495,8 @@ bool KitManager::registerKit(ProjectExplorer::Kit *k)
return false;
}
k->setDisplayName(uniqueKitName(k, k->displayName(), kits()));
// make sure we have all the information in our kits:
addKit(k);
if (d->m_initialized)
......
......@@ -122,6 +122,8 @@ public:
bool isLoaded() const;
static QString uniqueKitName(const Kit *k, const QString name, const QList<Kit *> &allKits);
public slots:
bool registerKit(ProjectExplorer::Kit *k);
void deregisterKit(ProjectExplorer::Kit *k);
......@@ -149,6 +151,8 @@ signals:
private:
explicit KitManager(QObject *parent = 0);
bool setKeepDisplayNameUnique(bool unique);
// Make sure the this is only called after all
// KitInformation are registered!
void restoreKits();
......@@ -162,6 +166,7 @@ private:
};
KitList restoreKits(const Utils::FileName &fileName);
void notifyAboutDisplayNameChange(ProjectExplorer::Kit *k);
void notifyAboutUpdate(ProjectExplorer::Kit *k);
void addKit(Kit *k);
......
......@@ -53,7 +53,7 @@ public:
}
KitNode(KitNode *kn, Kit *k) :
parent(kn)
parent(kn), widget(0)
{
if (kn)
kn->childNodes.append(this);
......@@ -96,6 +96,8 @@ KitModel::KitModel(QBoxLayout *parentLayout, QObject *parent) :
this, SLOT(addKit(ProjectExplorer::Kit*)));
connect(KitManager::instance(), SIGNAL(kitRemoved(ProjectExplorer::Kit*)),
this, SLOT(removeKit(ProjectExplorer::Kit*)));
connect(KitManager::instance(), SIGNAL(unmanagedKitUpdated(ProjectExplorer::Kit*)),
this, SLOT(updateKit(ProjectExplorer::Kit*)));
connect(KitManager::instance(), SIGNAL(defaultkitChanged()),
this, SLOT(changeDefaultKit()));
......@@ -284,6 +286,8 @@ void KitModel::apply()
}
// Update kits:
KitManager *km = KitManager::instance();
bool unique = km->setKeepDisplayNameUnique(false);
nodes = m_autoRoot->childNodes; // These can be dirty due to being made default!
nodes.append(m_manualRoot->childNodes);
foreach (KitNode *n, nodes) {
......@@ -294,6 +298,7 @@ void KitModel::apply()
emit dataChanged(index(n, 0), index(n, columnCount(QModelIndex())));
}
}
km->setKeepDisplayNameUnique(unique);
}
void KitModel::markForRemoval(Kit *k)
......@@ -346,6 +351,12 @@ Kit *KitModel::markForAddition(Kit *baseKit)
return k;
}
QString KitModel::findNameFor(Kit *k, const QString baseName)
{
QList<Kit *> kits = kitList(m_root);
return KitManager::uniqueKitName(k, baseName, kits);
}
QModelIndex KitModel::index(KitNode *node, int column) const
{
if (node->parent == 0) // is root (or was marked for deletion)
......@@ -390,6 +401,18 @@ void KitModel::setDefaultNode(KitNode *node)
}
}
QList<Kit *> KitModel::kitList(KitNode *node) const
{
QList<Kit *> result;
if (!node)
return result;
foreach (KitNode *n, node->childNodes)
result.append(kitList(n));
if (node->widget)
result.append(node->widget->workingCopy());
return result;
}
void KitModel::addKit(Kit *k)
{
foreach (KitNode *n, m_manualRoot->childNodes) {
......@@ -446,6 +469,11 @@ void KitModel::removeKit(Kit *k)
emit kitStateChanged();
}
void KitModel::updateKit(Kit *k)
{
k->setDisplayName(findNameFor(k, k->displayName()));
}
void KitModel::changeDefaultKit()
{
Kit *defaultKit = KitManager::instance()->defaultKit();
......
......@@ -87,12 +87,15 @@ public:
void markForRemoval(Kit *k);
Kit *markForAddition(Kit *baseKit);
QString findNameFor(Kit *k, const QString baseName);
signals:
void kitStateChanged();
private slots:
void addKit(ProjectExplorer::Kit *k);
void removeKit(ProjectExplorer::Kit *k);
void updateKit(ProjectExplorer::Kit*);
void changeDefaultKit();
void setDirty();
......@@ -101,6 +104,7 @@ private:
KitNode *findWorkingCopy(Kit *k) const;
KitNode *createNode(KitNode *parent, Kit *k);
void setDefaultNode(KitNode *node);
QList<Kit *> kitList(KitNode *node) const;
KitNode *m_root;
KitNode *m_autoRoot;
......
......@@ -168,11 +168,15 @@ void KitOptionsPage::showKit(Kit *k)
void KitOptionsPage::kitSelectionChanged()
{
QModelIndex current = currentIndex();
QWidget *newWidget = current.isValid() ? m_model->widget(current) : 0;
if (newWidget == m_currentWidget)
return;
if (m_currentWidget)
m_currentWidget->setVisible(false);
QModelIndex current = currentIndex();
m_currentWidget = current.isValid() ? m_model->widget(current) : 0;
m_currentWidget = newWidget;
if (m_currentWidget) {
m_currentWidget->setVisible(true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment