From 063f0476da4dea527e6739f5ef45a5ef07298a09 Mon Sep 17 00:00:00 2001
From: Kai Koehne <kai.koehne@nokia.com>
Date: Fri, 17 Jul 2009 18:06:08 +1000
Subject: [PATCH] Fix selection of project files to add new files/classes to

Commit ac8e371486cb69fb did reorder the path list in
ProjectWizardPage, while ProjceFileWizardExtension still assumed
that the list displayed to the user is exactly the same as passed
to ProjectWizardPage.

The patch removes the index based communication between the
classes altogether and directly stores the ProjectNode in the
UserRole of the ComboBox entry.
---
 .../projectfilewizardextension.cpp            | 18 ++--------
 .../projectexplorer/projectwizardpage.cpp     | 36 ++++++++++++++-----
 .../projectexplorer/projectwizardpage.h       |  7 ++--
 3 files changed, 34 insertions(+), 27 deletions(-)

diff --git a/src/plugins/projectexplorer/projectfilewizardextension.cpp b/src/plugins/projectexplorer/projectfilewizardextension.cpp
index 33b7d370760..277b98e564d 100644
--- a/src/plugins/projectexplorer/projectfilewizardextension.cpp
+++ b/src/plugins/projectexplorer/projectfilewizardextension.cpp
@@ -141,20 +141,8 @@ QList<QWizardPage *> ProjectFileWizardExtension::extensionPages(const Core::IWiz
     // Disable "add project to project"
     const bool hasProjects = !m_context->projects.empty();
     if (hasProjects) {
-        // Compile list of names and find current project if there is one
-        QStringList projectNames;
-        ProjectNode *current = currentProject();
-        int currentIndex = -1;
-        const int count = m_context->projects.size();
-        for (int i = 0; i < count; i++) {
-            ProjectNode *pn = m_context->projects.at(i);
-            projectNames.push_back(QFileInfo(pn->path()).fileName());
-            if (current == pn)
-                currentIndex = i;
-        }
-        m_context->page->setProjects(projectNames);
-        if (currentIndex != -1)
-            m_context->page->setCurrentProjectIndex(currentIndex);
+        m_context->page->setProjects(m_context->projects);
+        m_context->page->setCurrentProject(currentProject());
     }
     m_context->page->setAddToProjectEnabled(hasProjects && wizard->kind() != Core::IWizard::ProjectWizard);
 
@@ -166,7 +154,7 @@ bool ProjectFileWizardExtension::process(const QList<Core::GeneratedFile> &files
     typedef QMultiMap<FileType, QString> TypeFileMap;
     // Add files to project && version control
     if (m_context->page->addToProject()) {
-        ProjectNode *project = m_context->projects.at(m_context->page->currentProjectIndex());
+        ProjectNode *project = m_context->page->currentProject();
         // Split into lists by file type and add
         TypeFileMap typeFileMap;
         foreach (const Core::GeneratedFile &generatedFile, files) {
diff --git a/src/plugins/projectexplorer/projectwizardpage.cpp b/src/plugins/projectexplorer/projectwizardpage.cpp
index ff4d2b6060a..57c1d512c7f 100644
--- a/src/plugins/projectexplorer/projectwizardpage.cpp
+++ b/src/plugins/projectexplorer/projectwizardpage.cpp
@@ -29,13 +29,17 @@
 
 #include "projectwizardpage.h"
 #include "ui_projectwizardpage.h"
+#include "projectnodes.h"
 
 #include <QtCore/QDebug>
+#include <QtCore/QFileInfo>
 #include <QtCore/QTextStream>
 
 using namespace ProjectExplorer;
 using namespace Internal;
 
+Q_DECLARE_METATYPE(ProjectExplorer::ProjectNode*)
+
 ProjectWizardPage::ProjectWizardPage(QWidget *parent) :
     QWizardPage(parent),
     m_ui(new Ui::WizardPage)
@@ -51,13 +55,19 @@ ProjectWizardPage::~ProjectWizardPage()
     delete m_ui;
 }
 
-void ProjectWizardPage::setProjects(const QStringList &l)
+void ProjectWizardPage::setProjects(const QList<ProjectNode*> &projectNodes)
 {
-    QStringList list = l;
-    list.removeDuplicates();
-    list.sort();
+    QMap<QString,ProjectNode*> projectMap;
+    foreach (ProjectNode *node, projectNodes) {
+        QString name = QFileInfo(node->path()).fileName();
+        if (!projectMap.contains(name))
+            projectMap.insert(name, node);
+    }
+
     m_ui->projectComboBox->clear();
-    m_ui->projectComboBox->addItems(list);
+    foreach (const QString &name, projectMap.keys()) {
+        m_ui->projectComboBox->addItem(name, qVariantFromValue(projectMap.value(name)));
+    }
 }
 
 void ProjectWizardPage::setAddToProjectEnabled(bool b)
@@ -69,14 +79,22 @@ void ProjectWizardPage::setAddToProjectEnabled(bool b)
     m_ui->projectComboBox->setEnabled(b);
 }
 
-int ProjectWizardPage::currentProjectIndex() const
+ProjectNode *ProjectWizardPage::currentProject() const
 {
-    return m_ui->projectComboBox->currentIndex();
+    QVariant variant = m_ui->projectComboBox->itemData(m_ui->projectComboBox->currentIndex());
+    return qVariantValue<ProjectNode*>(variant);
 }
 
-void ProjectWizardPage::setCurrentProjectIndex(int i)
+void ProjectWizardPage::setCurrentProject(ProjectNode *projectNode)
 {
-    m_ui->projectComboBox->setCurrentIndex(i);
+    if (!projectNode)
+        return;
+    for (int i = 0; i < m_ui->projectComboBox->count(); ++i) {
+        if (qVariantValue<ProjectNode*>(m_ui->projectComboBox->itemData(i)) == projectNode) {
+            m_ui->projectComboBox->setCurrentIndex(i);
+            return;
+        }
+    }
 }
 
 bool ProjectWizardPage::addToProject() const
diff --git a/src/plugins/projectexplorer/projectwizardpage.h b/src/plugins/projectexplorer/projectwizardpage.h
index cc018b5a932..7244a499ff8 100644
--- a/src/plugins/projectexplorer/projectwizardpage.h
+++ b/src/plugins/projectexplorer/projectwizardpage.h
@@ -55,9 +55,10 @@ public:
     explicit ProjectWizardPage(QWidget *parent = 0);
     virtual ~ProjectWizardPage();
 
-    void setProjects(const QStringList &);
-    void setCurrentProjectIndex(int);
-    int currentProjectIndex() const;
+    void setProjects(const QList<ProjectNode *> &);
+    void setCurrentProject(ProjectNode *);
+
+    ProjectNode *currentProject() const;
 
     void setAddToProjectEnabled(bool b);
     bool addToProject() const;
-- 
GitLab