Improved editor menus

Access native filemanager and terminal from Open documents view
and Editor menu

Task-number: QTCREATORBUG-389

Change-Id: Id6cd8839cdbd270a4ac53ccad1ae1db94d48e900
Reviewed-on: http://codereview.qt.nokia.com/2974
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Eike Ziller <eike.ziller@nokia.com>
This commit is contained in:
Robert Loehning
2011-08-16 18:55:23 +02:00
committed by Eike Ziller
parent d2911d70f3
commit 738544768b
10 changed files with 279 additions and 132 deletions

View File

@@ -39,16 +39,12 @@
#include <coreplugin/filemanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/fileutils.h>
#include <utils/environment.h>
#include <utils/pathchooser.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <utils/unixutils.h>
#include <utils/consoleprocess.h>
#include <QtCore/QDebug>
#include <QtCore/QProcess>
#include <QtCore/QSize>
#include <QtGui/QFileSystemModel>
#include <QtGui/QVBoxLayout>
@@ -61,7 +57,6 @@
#include <QtGui/QMenu>
#include <QtGui/QFileDialog>
#include <QtGui/QContextMenuEvent>
#include <QtGui/QMessageBox>
enum { debug = 0 };
@@ -312,9 +307,9 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
QAction *actionOpen = menu.addAction(actionOpenText(m_fileSystemModel, current));
actionOpen->setEnabled(hasCurrentItem);
// Explorer & teminal
QAction *actionExplorer = menu.addAction(msgGraphicalShellAction());
QAction *actionExplorer = menu.addAction(Core::Internal::FileUtils::msgGraphicalShellAction());
actionExplorer->setEnabled(hasCurrentItem);
QAction *actionTerminal = menu.addAction(msgTerminalAction());
QAction *actionTerminal = menu.addAction(Core::Internal::FileUtils::msgTerminalAction());
actionTerminal->setEnabled(hasCurrentItem);
// open with...
@@ -343,123 +338,17 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
return;
}
if (action == actionTerminal) {
openTerminal(m_fileSystemModel->filePath(current));
Core::Internal::FileUtils::openTerminal(m_fileSystemModel->filePath(current));
return;
}
if (action == actionExplorer) {
showInGraphicalShell(this, m_fileSystemModel->filePath(current));
Core::Internal::FileUtils::showInGraphicalShell(this, m_fileSystemModel->filePath(current));
return;
}
ProjectExplorerPlugin::openEditorFromAction(action,
m_fileSystemModel->filePath(current));
}
QString FolderNavigationWidget::msgGraphicalShellAction()
{
#if defined(Q_OS_WIN)
return tr("Show in Explorer...");
#elif defined(Q_OS_MAC)
return tr("Show in Finder...");
#else
return tr("Show Containing Folder...");
#endif
}
QString FolderNavigationWidget::msgTerminalAction()
{
#ifdef Q_OS_WIN
return tr("Open Command Prompt Here...");
#else
return tr("Open Terminal Here...");
#endif
}
// Show error with option to open settings.
static inline void showGraphicalShellError(QWidget *parent,
const QString &app,
const QString &error)
{
const QString title = FolderNavigationWidget::tr("Launching a file browser failed");
const QString msg = FolderNavigationWidget::tr("Unable to start the file manager:\n\n%1\n\n").arg(app);
QMessageBox mbox(QMessageBox::Warning, title, msg, QMessageBox::Close, parent);
if (!error.isEmpty())
mbox.setDetailedText(FolderNavigationWidget::tr("'%1' returned the following error:\n\n%2").arg(app, error));
QAbstractButton *settingsButton = mbox.addButton(FolderNavigationWidget::tr("Settings..."), QMessageBox::ActionRole);
mbox.exec();
if (mbox.clickedButton() == settingsButton)
Core::ICore::instance()->showOptionsDialog(QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE),
QLatin1String(Core::Constants::SETTINGS_ID_ENVIRONMENT));
}
void FolderNavigationWidget::showInGraphicalShell(QWidget *parent, const QString &pathIn)
{
// Mac, Windows support folder or file.
#if defined(Q_OS_WIN)
const QString explorer = Utils::Environment::systemEnvironment().searchInPath(QLatin1String("explorer.exe"));
if (explorer.isEmpty()) {
QMessageBox::warning(parent,
tr("Launching Windows Explorer Failed"),
tr("Could not find explorer.exe in path to launch Windows Explorer."));
return;
}
QString param;
if (!QFileInfo(pathIn).isDir())
param = QLatin1String("/select,");
param += QDir::toNativeSeparators(pathIn);
QProcess::startDetached(explorer, QStringList(param));
#elif defined(Q_OS_MAC)
Q_UNUSED(parent)
QStringList scriptArgs;
scriptArgs << QLatin1String("-e")
<< QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
.arg(pathIn);
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
scriptArgs.clear();
scriptArgs << QLatin1String("-e")
<< QLatin1String("tell application \"Finder\" to activate");
QProcess::execute("/usr/bin/osascript", scriptArgs);
#else
// we cannot select a file here, because no file browser really supports it...
const QFileInfo fileInfo(pathIn);
const QString folder = fileInfo.isDir() ? fileInfo.absoluteFilePath() : fileInfo.filePath();
const QString app = Utils::UnixUtils::fileBrowser(Core::ICore::instance()->settings());
QProcess browserProc;
const QString browserArgs = Utils::UnixUtils::substituteFileBrowserParameters(app, folder);
if (debug)
qDebug() << browserArgs;
bool success = browserProc.startDetached(browserArgs);
const QString error = QString::fromLocal8Bit(browserProc.readAllStandardError());
success = success && error.isEmpty();
if (!success)
showGraphicalShellError(parent, app, error);
#endif
}
void FolderNavigationWidget::openTerminal(const QString &path)
{
// Get terminal application
#ifdef Q_OS_WIN
const QString terminalEmulator = QString::fromLocal8Bit(qgetenv("COMSPEC"));
const QStringList args; // none
#elif defined(Q_WS_MAC)
const QString terminalEmulator = Core::ICore::instance()->resourcePath()
+ QLatin1String("/scripts/openTerminal.command");
QStringList args;
#else
QStringList args = Utils::QtcProcess::splitArgs(
Utils::ConsoleProcess::terminalEmulator(Core::ICore::instance()->settings()));
const QString terminalEmulator = args.takeFirst();
const QString shell = QString::fromLocal8Bit(qgetenv("SHELL"));
args.append(shell);
#endif
// Launch terminal with working directory set.
const QFileInfo fileInfo(path);
const QString pwd = QDir::toNativeSeparators(fileInfo.isDir() ?
fileInfo.absoluteFilePath() :
fileInfo.absolutePath());
QProcess::startDetached(terminalEmulator, args, pwd);
}
// --------------------FolderNavigationWidgetFactory
FolderNavigationWidgetFactory::FolderNavigationWidgetFactory()
{