Commit 32bb96eb authored by Friedemann Kleint's avatar Friedemann Kleint

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: default avatarDaniel Teske <daniel.teske@digia.com>
parent 8e455f61
......@@ -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())
......
......@@ -79,7 +79,7 @@ protected:
private:
void setCurrentTitle(QString dirName, const QString &fullPath);
bool setCurrentDirectory(const QString &directory);
void openItem(const QModelIndex &srcIndex);
void openItem(const QModelIndex &srcIndex, bool openDirectoryAsProject = false);
QModelIndex currentItem() const;
QString currentDirectory() const;
......
......@@ -1533,7 +1533,7 @@ void ProjectExplorerPlugin::determineSessionToRestoreAtStartup()
}
// Return a list of glob patterns for project files ("*.pro", etc), use first, main pattern only.
static inline QStringList projectFileGlobs()
QStringList ProjectExplorerPlugin::projectFileGlobs()
{
QStringList result;
foreach (const IProjectManager *ipm, ExtensionSystem::PluginManager::getObjects<IProjectManager>()) {
......@@ -1572,7 +1572,7 @@ void ProjectExplorerPlugin::restoreSession()
// "filename+45" and "filename:23".
if (!arguments.isEmpty()) {
const QStringList sessions = SessionManager::sessions();
QStringList projectGlobs = projectFileGlobs();
QStringList projectGlobs = ProjectExplorerPlugin::projectFileGlobs();
for (int a = 0; a < arguments.size(); ) {
const QString &arg = arguments.at(a);
const QFileInfo fi(arg);
......
......@@ -128,6 +128,7 @@ public:
static QString directoryFor(Node *node);
static QString pathFor(Node *node);
static QStringList projectFileGlobs();
signals:
void runControlStarted(ProjectExplorer::RunControl *rc);
......
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