From ebb8faae6d0746cdaaa67c0bac4f6fa83beb8112 Mon Sep 17 00:00:00 2001
From: Daniel Molkentin <daniel.molkentin@nokia.com>
Date: Wed, 19 May 2010 18:30:45 +0200
Subject: [PATCH] New dialog: Replace "Preferred wizards" concept by
 remembering the last category

Reviewed-by: trustme
---
 src/plugins/coreplugin/coreimpl.cpp           |  6 ----
 src/plugins/coreplugin/coreimpl.h             |  4 ---
 src/plugins/coreplugin/dialogs/newdialog.cpp  | 36 +++++++++++++------
 src/plugins/coreplugin/dialogs/newdialog.h    |  5 ++-
 src/plugins/coreplugin/icore.cpp              | 11 ------
 src/plugins/coreplugin/icore.h                |  1 -
 src/plugins/coreplugin/mainwindow.cpp         |  7 ++--
 src/plugins/coreplugin/mainwindow.h           |  3 +-
 .../projectexplorer/projectexplorer.cpp       | 15 --------
 src/plugins/projectexplorer/projectexplorer.h |  1 -
 10 files changed, 30 insertions(+), 59 deletions(-)

diff --git a/src/plugins/coreplugin/coreimpl.cpp b/src/plugins/coreplugin/coreimpl.cpp
index 2c7be1e9cf5..c6da6362d44 100644
--- a/src/plugins/coreplugin/coreimpl.cpp
+++ b/src/plugins/coreplugin/coreimpl.cpp
@@ -56,7 +56,6 @@ ICore* ICore::instance()
 }
 
 CoreImpl::CoreImpl(MainWindow *mainwindow)
-    : m_preferredWizardKinds(IWizard::ProjectWizard)
 {
     m_instance = this;
     m_mainwindow = mainwindow;
@@ -69,11 +68,6 @@ QStringList CoreImpl::showNewItemDialog(const QString &title,
     return m_mainwindow->showNewItemDialog(title, wizards, defaultLocation);
 }
 
-void CoreImpl::setNewItemDialogPreferredWizardKinds(IWizard::WizardKinds kinds)
-{
-    m_preferredWizardKinds = kinds;
-}
-
 bool CoreImpl::showOptionsDialog(const QString &group, const QString &page, QWidget *parent)
 {
     return m_mainwindow->showOptionsDialog(group, page, parent);
diff --git a/src/plugins/coreplugin/coreimpl.h b/src/plugins/coreplugin/coreimpl.h
index 749a7962e6f..d04defd3181 100644
--- a/src/plugins/coreplugin/coreimpl.h
+++ b/src/plugins/coreplugin/coreimpl.h
@@ -47,8 +47,6 @@ public:
     QStringList showNewItemDialog(const QString &title,
                                   const QList<IWizard *> &wizards,
                                   const QString &defaultLocation = QString());
-    void setNewItemDialogPreferredWizardKinds(IWizard::WizardKinds kinds);
-    IWizard::WizardKinds newItemDialogPreferredWizardKinds() { return m_preferredWizardKinds; }
     bool showOptionsDialog(const QString &group = QString(),
                            const QString &page = QString(),
                            QWidget *parent = 0);
@@ -93,8 +91,6 @@ public:
 private:
     MainWindow *m_mainwindow;
     friend class MainWindow;
-
-    IWizard::WizardKinds m_preferredWizardKinds;
 };
 
 } // namespace Internal
diff --git a/src/plugins/coreplugin/dialogs/newdialog.cpp b/src/plugins/coreplugin/dialogs/newdialog.cpp
index 75170a4de13..b8bd4a5d852 100644
--- a/src/plugins/coreplugin/dialogs/newdialog.cpp
+++ b/src/plugins/coreplugin/dialogs/newdialog.cpp
@@ -152,8 +152,7 @@ using namespace Core::Internal;
 NewDialog::NewDialog(QWidget *parent) :
     QDialog(parent),
     m_ui(new Core::Internal::Ui::NewDialog),
-    m_okButton(0),
-    m_preferredWizardKinds(0)
+    m_okButton(0)
 {
     typedef QMap<QString, QStandardItem *> CategoryItemMap;
     m_ui->setupUi(this);
@@ -194,11 +193,6 @@ bool wizardLessThan(const IWizard *w1, const IWizard *w2)
     return w1->id().compare(w2->id()) < 0;
 }
 
-void NewDialog::setPreferredWizardKinds(IWizard::WizardKinds kinds)
-{
-    m_preferredWizardKinds = kinds;
-}
-
 void NewDialog::setWizards(QList<IWizard*> wizards)
 {
     typedef QMap<QString, QStandardItem *> CategoryItemMap;
@@ -243,9 +237,10 @@ void NewDialog::setWizards(QList<IWizard*> wizards)
                 break;
             }
             kindItem->appendRow(categoryItem);
+            m_categoryItems.append(categoryItem);
             categoryItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
             categoryItem->setText(wizard->displayCategory());
-            categoryItem->setData(QVariant::fromValue(0), Qt::UserRole);
+            categoryItem->setData(wizard->category(), Qt::UserRole);
             cit = categories.insert(categoryName, categoryItem);
         }
         // add item
@@ -277,8 +272,20 @@ void NewDialog::setWizards(QList<IWizard*> wizards)
 
 Core::IWizard *NewDialog::showDialog()
 {
-    // Select first category, first item by default
-    m_ui->templateCategoryView->setCurrentIndex(m_proxyModel->index(0,0, m_proxyModel->index(0,0)));
+    static QString lastCategory;
+    QModelIndex idx;
+
+    if (!lastCategory.isEmpty())
+        foreach(QStandardItem* item, m_categoryItems) {
+            if (item->data(Qt::UserRole) == lastCategory) {
+                idx = m_proxyModel->mapToSource(m_model->indexFromItem(item));
+        }
+    }
+    if (!idx.isValid())
+        idx = m_proxyModel->index(0,0, m_proxyModel->index(0,0));
+
+    m_ui->templateCategoryView->setCurrentIndex(idx);
+
     // We need to set ensure that the category has default focus
     m_ui->templateCategoryView->setFocus(Qt::NoFocusReason);
 
@@ -289,8 +296,15 @@ Core::IWizard *NewDialog::showDialog()
     currentItemChanged(m_ui->templatesView->rootIndex().child(0,0));
 
     updateOkButton();
-    if (exec() != Accepted)
+
+    const int retVal = exec();
+
+    idx = m_ui->templateCategoryView->currentIndex();
+    lastCategory = m_model->itemFromIndex(m_proxyModel->mapToSource(idx))->data(Qt::UserRole).toString();
+
+    if (retVal != Accepted)
         return 0;
+
     return currentWizard();
 }
 
diff --git a/src/plugins/coreplugin/dialogs/newdialog.h b/src/plugins/coreplugin/dialogs/newdialog.h
index bcfe5a7d2fd..02d764bee4b 100644
--- a/src/plugins/coreplugin/dialogs/newdialog.h
+++ b/src/plugins/coreplugin/dialogs/newdialog.h
@@ -34,6 +34,7 @@
 
 #include <QtGui/QDialog>
 #include <QtCore/QList>
+#include <QtCore/QModelIndex>
 
 QT_BEGIN_NAMESPACE
 class QAbstractProxyModel;
@@ -41,7 +42,6 @@ class QPushButton;
 class QStandardItem;
 class QStandardItemModel;
 class QStringList;
-class QModelIndex;
 QT_END_NAMESPACE
 
 namespace Core {
@@ -61,7 +61,6 @@ public:
     virtual ~NewDialog();
 
     void setWizards(QList<IWizard*> wizards);
-    void setPreferredWizardKinds(IWizard::WizardKinds kinds);
 
     Core::IWizard *showDialog();
 
@@ -77,8 +76,8 @@ private:
     QStandardItemModel *m_model;
     QAbstractProxyModel *m_proxyModel;
     QPushButton *m_okButton;
-    IWizard::WizardKinds m_preferredWizardKinds;
     QPixmap m_dummyIcon;
+    QList<QStandardItem*> m_categoryItems;
 };
 
 } // namespace Internal
diff --git a/src/plugins/coreplugin/icore.cpp b/src/plugins/coreplugin/icore.cpp
index c1f85bc44cd..363d886b2ca 100644
--- a/src/plugins/coreplugin/icore.cpp
+++ b/src/plugins/coreplugin/icore.cpp
@@ -67,17 +67,6 @@
     \sa Core::FileManager
 */
 
-/*!
-    \fn void setNewItemDialogPreferredWizardKinds(IWizard::WizardKinds kinds)
-    \internal
-
-    When set to true, the general "New File or Project" dialog will
-    collapse the project categories.
-    This is set by the project explorer: When projects are open, the preferred
-    thing is to create files/classes, if no projects are open, the preferred thing
-    to create are projects.
-*/
-
 /*!
     \fn bool ICore::showOptionsDialog(const QString &group = QString(),
                                const QString &page = QString())
diff --git a/src/plugins/coreplugin/icore.h b/src/plugins/coreplugin/icore.h
index ccc3b3b707c..cae3da1b4d9 100644
--- a/src/plugins/coreplugin/icore.h
+++ b/src/plugins/coreplugin/icore.h
@@ -74,7 +74,6 @@ public:
     virtual QStringList showNewItemDialog(const QString &title,
                                           const QList<IWizard *> &wizards,
                                           const QString &defaultLocation = QString()) = 0;
-    virtual void setNewItemDialogPreferredWizardKinds(IWizard::WizardKinds kinds) = 0;
 
     virtual bool showOptionsDialog(const QString &group = QString(),
                                    const QString &page = QString(),
diff --git a/src/plugins/coreplugin/mainwindow.cpp b/src/plugins/coreplugin/mainwindow.cpp
index c10f0da694e..201105098f5 100644
--- a/src/plugins/coreplugin/mainwindow.cpp
+++ b/src/plugins/coreplugin/mainwindow.cpp
@@ -775,8 +775,7 @@ void MainWindow::registerDefaultActions()
 
 void MainWindow::newFile()
 {
-    showNewItemDialog(tr("New", "Title of dialog"), IWizard::allWizards(),
-                      QString(), m_coreImpl->newItemDialogPreferredWizardKinds());
+    showNewItemDialog(tr("New", "Title of dialog"), IWizard::allWizards(), QString());
 }
 
 void MainWindow::openFile()
@@ -863,8 +862,7 @@ void MainWindow::setFocusToEditor()
 
 QStringList MainWindow::showNewItemDialog(const QString &title,
                                           const QList<IWizard *> &wizards,
-                                          const QString &defaultLocation,
-                                          IWizard::WizardKinds preferredWizardKinds)
+                                          const QString &defaultLocation)
 {
     // Scan for wizards matching the filter and pick one. Don't show
     // dialog if there is only one.
@@ -879,7 +877,6 @@ QStringList MainWindow::showNewItemDialog(const QString &title,
         NewDialog dlg(this);
         dlg.setWizards(wizards);
         dlg.setWindowTitle(title);
-        dlg.setPreferredWizardKinds(preferredWizardKinds);
         wizard = dlg.showDialog();
     }
         break;
diff --git a/src/plugins/coreplugin/mainwindow.h b/src/plugins/coreplugin/mainwindow.h
index 2aecdefc32f..60b911ba1d1 100644
--- a/src/plugins/coreplugin/mainwindow.h
+++ b/src/plugins/coreplugin/mainwindow.h
@@ -134,8 +134,7 @@ public slots:
 
     QStringList showNewItemDialog(const QString &title,
                                   const QList<IWizard *> &wizards,
-                                  const QString &defaultLocation = QString(),
-                                  IWizard::WizardKinds preferredWizardKinds = 0);
+                                  const QString &defaultLocation = QString());
 
     bool showOptionsDialog(const QString &category = QString(),
                            const QString &page = QString(),
diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp
index f3561283b98..9e6f01b1d22 100644
--- a/src/plugins/projectexplorer/projectexplorer.cpp
+++ b/src/plugins/projectexplorer/projectexplorer.cpp
@@ -256,10 +256,6 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
             this, SIGNAL(fileListChanged()));
     connect(d->m_session, SIGNAL(startupProjectChanged(ProjectExplorer::Project *)),
             this, SLOT(startupProjectChanged()));
-    connect(d->m_session, SIGNAL(projectAdded(ProjectExplorer::Project*)),
-            this, SLOT(updatePreferredWizardKinds()));
-    connect(d->m_session, SIGNAL(projectRemoved(ProjectExplorer::Project*)),
-            this, SLOT(updatePreferredWizardKinds()));
     connect(d->m_session, SIGNAL(dependencyChanged(ProjectExplorer::Project*,ProjectExplorer::Project*)),
             this, SLOT(updateActions()));
 
@@ -1882,17 +1878,6 @@ void ProjectExplorerPlugin::openRecentProject()
         openProject(fileName);
 }
 
-void ProjectExplorerPlugin::updatePreferredWizardKinds()
-{
-    if (d->m_session->projects().count()) {
-        Core::ICore::instance()->setNewItemDialogPreferredWizardKinds(
-                Core::IWizard::FileWizard | Core::IWizard::ClassWizard);
-    } else {
-        Core::ICore::instance()->setNewItemDialogPreferredWizardKinds(
-                Core::IWizard::ProjectWizard);
-    }
-}
-
 void ProjectExplorerPlugin::invalidateProject(Project *project)
 {
     if (debug)
diff --git a/src/plugins/projectexplorer/projectexplorer.h b/src/plugins/projectexplorer/projectexplorer.h
index 8ed599bf966..b25d6402a94 100644
--- a/src/plugins/projectexplorer/projectexplorer.h
+++ b/src/plugins/projectexplorer/projectexplorer.h
@@ -170,7 +170,6 @@ private slots:
     void updateRecentProjectMenu();
     void openRecentProject();
     void openTerminalHere();
-    void updatePreferredWizardKinds();
 
     void invalidateProject(ProjectExplorer::Project *project);
 
-- 
GitLab