Open projects from file system view.

Use ICore::openFiles() instead of
EditorManager::openEditor() in FolderNavigationWidget
since it is also able to open projects.

Add menu entry to open a project in a directory, which
uses a logic similar to the command line processing,
trying to locate a project file in the directory
using file globs.

This allows for conveniently opening several projects by
right-clicking instead of having to open the file dialog
and to look for .pro files in directory listings.

Change-Id: I8404342f7f09b4b994e6eee82af3dc712cd97512
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
This commit is contained in:
Friedemann Kleint
2014-04-17 16:49:43 +02:00
parent 8e455f61b6
commit 32bb96ebd4
4 changed files with 37 additions and 9 deletions

View File

@@ -28,6 +28,7 @@
****************************************************************************/
#include "foldernavigationwidget.h"
#include "projectexplorer.h"
#include <extensionsystem/pluginmanager.h>
@@ -58,6 +59,8 @@
#include <QMenu>
#include <QFileDialog>
#include <QContextMenuEvent>
#include <QDir>
#include <QFileInfo>
enum { debug = 0 };
@@ -252,7 +255,7 @@ void FolderNavigationWidget::slotOpenItem(const QModelIndex &viewIndex)
openItem(m_filterModel->mapToSource(viewIndex));
}
void FolderNavigationWidget::openItem(const QModelIndex &srcIndex)
void FolderNavigationWidget::openItem(const QModelIndex &srcIndex, bool openDirectoryAsProject)
{
const QString fileName = m_fileSystemModel->fileName(srcIndex);
if (fileName == QLatin1String("."))
@@ -263,14 +266,27 @@ void FolderNavigationWidget::openItem(const QModelIndex &srcIndex)
setCurrentDirectory(parentPath);
return;
}
if (m_fileSystemModel->isDir(srcIndex)) { // Change to directory
const QString path = m_fileSystemModel->filePath(srcIndex);
if (m_fileSystemModel->isDir(srcIndex)) {
const QFileInfo fi = m_fileSystemModel->fileInfo(srcIndex);
if (fi.isReadable() && fi.isExecutable())
setCurrentDirectory(m_fileSystemModel->filePath(srcIndex));
if (!fi.isReadable() || !fi.isExecutable())
return;
// Try to find project files in directory and open those.
if (openDirectoryAsProject) {
QDir dir(path);
QStringList proFiles;
foreach (const QFileInfo &i, dir.entryInfoList(ProjectExplorerPlugin::projectFileGlobs(), QDir::Files))
proFiles.append(i.absoluteFilePath());
if (!proFiles.isEmpty())
Core::ICore::instance()->openFiles(proFiles);
return;
}
// Change to directory
setCurrentDirectory(path);
return;
}
// Open file.
Core::EditorManager::openEditor(m_fileSystemModel->filePath(srcIndex));
Core::ICore::instance()->openFiles(QStringList(path));
}
void FolderNavigationWidget::setCurrentTitle(QString dirName, const QString &fullPath)
@@ -310,6 +326,13 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
const bool hasCurrentItem = current.isValid();
QAction *actionOpen = menu.addAction(actionOpenText(m_fileSystemModel, current));
actionOpen->setEnabled(hasCurrentItem);
const bool isDirectory = hasCurrentItem && m_fileSystemModel->isDir(current);
QAction *actionOpenDirectoryAsProject = 0;
if (isDirectory && m_fileSystemModel->fileName(current) != QLatin1String("..")) {
actionOpenDirectoryAsProject =
menu.addAction(tr("Open Project in \"%1\"")
.arg(m_fileSystemModel->fileName(current)));
}
// Explorer & teminal
QAction *actionExplorer = menu.addAction(Core::FileUtils::msgGraphicalShellAction());
actionExplorer->setEnabled(hasCurrentItem);
@@ -319,7 +342,7 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
QAction *actionFind = menu.addAction(Core::FileUtils::msgFindInDirectory());
actionFind->setEnabled(hasCurrentItem);
// open with...
if (!m_fileSystemModel->isDir(current)) {
if (hasCurrentItem && !isDirectory) {
QMenu *openWith = menu.addMenu(tr("Open with"));
Core::DocumentManager::populateOpenWithMenu(openWith,
m_fileSystemModel->filePath(current));
@@ -337,6 +360,10 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
openItem(current);
return;
}
if (action == actionOpenDirectoryAsProject) {
openItem(current, true);
return;
}
if (action == actionChooseFolder) { // Open file dialog
const QString newPath = QFileDialog::getExistingDirectory(this, tr("Choose Folder"), currentDirectory());
if (!newPath.isEmpty())