From ece749c30e978f26ac2fc8e9b9c74c45a68307b4 Mon Sep 17 00:00:00 2001
From: dt <qtc-committer@nokia.com>
Date: Tue, 24 Aug 2010 18:22:57 +0200
Subject: [PATCH] Fix context menu for the QML virtual folder.

That is: a) Figure out a suitable default location for Add New on a
virtual folder.
b) If that default location is deployed, don't show a Add Existing Files
option, since that is confusing.
---
 .../projectexplorer/projectexplorer.cpp       | 19 ++++++++++++++--
 src/plugins/qt4projectmanager/qt4nodes.cpp    | 22 ++++++++++++++++---
 2 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp
index 227d20e0ed4..b69649ab313 100644
--- a/src/plugins/projectexplorer/projectexplorer.cpp
+++ b/src/plugins/projectexplorer/projectexplorer.cpp
@@ -2002,8 +2002,23 @@ void ProjectExplorerPlugin::updateContextMenuActions(Node *node)
 void ProjectExplorerPlugin::addNewFile()
 {
     QTC_ASSERT(d->m_currentNode, return)
-    QFileInfo fi(d->m_currentNode->path());
-    const QString location = (fi.isDir() ? fi.absoluteFilePath() : fi.absolutePath());
+    QString path = d->m_currentNode->path();
+    QString location;
+    FolderNode *folder = qobject_cast<FolderNode *>(d->m_currentNode);
+    if (path.contains("#") && folder) {
+        // Virtual Folder case
+        // We figure out a commonPath from the subfolders
+        QStringList list;
+        foreach (FolderNode *f, folder->subFolderNodes())
+            list << f->path() + "/";
+        if (list.isEmpty())
+            location = path.left(path.indexOf('#'));
+        else
+            location = Utils::commonPath(list);
+    } else {
+        QFileInfo fi(path);
+        location = (fi.isDir() ? fi.absoluteFilePath() : fi.absolutePath());
+    }
     Core::ICore::instance()->showNewItemDialog(tr("New File", "Title of dialog"),
                               Core::IWizard::wizardsOfKind(Core::IWizard::FileWizard)
                               + Core::IWizard::wizardsOfKind(Core::IWizard::ClassWizard),
diff --git a/src/plugins/qt4projectmanager/qt4nodes.cpp b/src/plugins/qt4projectmanager/qt4nodes.cpp
index 9f3087634b2..343a44a7483 100644
--- a/src/plugins/qt4projectmanager/qt4nodes.cpp
+++ b/src/plugins/qt4projectmanager/qt4nodes.cpp
@@ -53,6 +53,7 @@
 #include <projectexplorer/buildmanager.h>
 
 #include <utils/qtcassert.h>
+#include <utils/stringutils.h>
 #include <algorithm>
 
 #include <QtCore/QDebug>
@@ -728,7 +729,7 @@ QList<ProjectNode::ProjectAction> Qt4PriFileNode::supportedActions(Node *node) c
 
     switch (proFileNode->projectType()) {
     case ApplicationTemplate:
-    case LibraryTemplate:
+    case LibraryTemplate: {
         actions << AddNewFile;
         if (m_recursiveEnumerateFiles.contains(node->path())) {
             actions << EraseFile;
@@ -736,11 +737,26 @@ QList<ProjectNode::ProjectAction> Qt4PriFileNode::supportedActions(Node *node) c
             actions << RemoveFile;
         }
 
-        // Only enable 'add existing file' if we don't deploy the folder
-        if (!deploysFolder(node->path()))
+        bool addExistingFiles = true;
+        if (node->path().contains('#')) {
+            // A virtual folder, we do what the projectexplorer does
+            FolderNode *folder = qobject_cast<FolderNode *>(node);
+            if (folder) {
+                QStringList list;
+                foreach (FolderNode *f, folder->subFolderNodes())
+                    list << f->path() + '/';
+                if (deploysFolder(Utils::commonPath(list)))
+                    addExistingFiles = false;
+            }
+        }
+
+        addExistingFiles = addExistingFiles && deploysFolder(node->path());
+
+        if (addExistingFiles)
             actions << AddExistingFile;
 
         break;
+    }
     case SubDirsTemplate:
         actions << AddSubProject << RemoveSubProject;
         break;
-- 
GitLab