forked from qt-creator/qt-creator
Move "Open with" menu logic to FileManager.
Better place than ProjectExplorer since it will then be available elsewhere too. Change-Id: I0508f121918105a562d2499ac6b31d19d733abad Reviewed-by: Daniel Teske <daniel.teske@nokia.com>
This commit is contained in:
@@ -33,8 +33,10 @@
|
||||
#include "filemanager.h"
|
||||
|
||||
#include "editormanager.h"
|
||||
#include "ieditor.h"
|
||||
#include "icore.h"
|
||||
#include "ieditor.h"
|
||||
#include "ieditorfactory.h"
|
||||
#include "iexternaleditor.h"
|
||||
#include "ifile.h"
|
||||
#include "iversioncontrol.h"
|
||||
#include "mimedatabase.h"
|
||||
@@ -53,9 +55,11 @@
|
||||
#include <QtCore/QTimer>
|
||||
#include <QtCore/QFileSystemWatcher>
|
||||
#include <QtCore/QDateTime>
|
||||
#include <QtGui/QAction>
|
||||
#include <QtGui/QFileDialog>
|
||||
#include <QtGui/QMessageBox>
|
||||
#include <QtGui/QMainWindow>
|
||||
#include <QtGui/QMenu>
|
||||
#include <QtGui/QMessageBox>
|
||||
#include <QtGui/QPushButton>
|
||||
|
||||
/*!
|
||||
@@ -95,6 +99,9 @@ static const char directoryGroupC[] = "Directories";
|
||||
static const char projectDirectoryKeyC[] = "Projects";
|
||||
static const char useProjectDirectoryKeyC[] = "UseProjectsDirectory";
|
||||
|
||||
Q_DECLARE_METATYPE(Core::IEditorFactory*)
|
||||
Q_DECLARE_METATYPE(Core::IExternalEditor*)
|
||||
|
||||
namespace Core {
|
||||
namespace Internal {
|
||||
|
||||
@@ -1274,6 +1281,65 @@ void FileManager::notifyFilesChangedInternally(const QStringList &files)
|
||||
emit filesChangedInternally(files);
|
||||
}
|
||||
|
||||
void FileManager::populateOpenWithMenu(QMenu *menu, const QString &fileName)
|
||||
{
|
||||
typedef QList<IEditorFactory*> EditorFactoryList;
|
||||
typedef QList<IExternalEditor*> ExternalEditorList;
|
||||
|
||||
menu->clear();
|
||||
|
||||
bool anyMatches = false;
|
||||
|
||||
ICore *core = ICore::instance();
|
||||
if (const MimeType mt = core->mimeDatabase()->findByFile(QFileInfo(fileName))) {
|
||||
const EditorFactoryList factories = core->editorManager()->editorFactories(mt, false);
|
||||
const ExternalEditorList externalEditors = core->editorManager()->externalEditors(mt, false);
|
||||
anyMatches = !factories.empty() || !externalEditors.empty();
|
||||
if (anyMatches) {
|
||||
// Add all suitable editors
|
||||
foreach (IEditorFactory *editorFactory, factories) {
|
||||
// Add action to open with this very editor factory
|
||||
QString const actionTitle = editorFactory->displayName();
|
||||
QAction * const action = menu->addAction(actionTitle);
|
||||
action->setData(qVariantFromValue(editorFactory));
|
||||
}
|
||||
// Add all suitable external editors
|
||||
foreach (IExternalEditor *externalEditor, externalEditors) {
|
||||
QAction * const action = menu->addAction(externalEditor->displayName());
|
||||
action->setData(qVariantFromValue(externalEditor));
|
||||
}
|
||||
}
|
||||
}
|
||||
menu->setEnabled(anyMatches);
|
||||
}
|
||||
|
||||
void FileManager::executeOpenWithMenuAction(QAction *action, const QString &fileName)
|
||||
{
|
||||
EditorManager *em = EditorManager::instance();
|
||||
const QVariant data = action->data();
|
||||
if (qVariantCanConvert<IEditorFactory *>(data)) {
|
||||
IEditorFactory *factory = qVariantValue<IEditorFactory *>(data);
|
||||
|
||||
// close any open editors that have this file open, but have a different type.
|
||||
QList<IEditor *> editorsOpenForFile = em->editorsForFileName(fileName);
|
||||
if (!editorsOpenForFile.isEmpty()) {
|
||||
foreach (IEditor *openEditor, editorsOpenForFile) {
|
||||
if (factory->id() == openEditor->id())
|
||||
editorsOpenForFile.removeAll(openEditor);
|
||||
}
|
||||
if (!em->closeEditors(editorsOpenForFile)) // don't open if cancel was pressed
|
||||
return;
|
||||
}
|
||||
|
||||
em->openEditor(fileName, factory->id(), EditorManager::ModeSwitch);
|
||||
return;
|
||||
}
|
||||
if (qVariantCanConvert<IExternalEditor *>(data)) {
|
||||
IExternalEditor *externalEditor = qVariantValue<IExternalEditor *>(data);
|
||||
em->openExternalEditor(fileName, externalEditor->id());
|
||||
}
|
||||
}
|
||||
|
||||
// -------------- FileChangeBlocker
|
||||
|
||||
FileChangeBlocker::FileChangeBlocker(const QString &fileName)
|
||||
|
Reference in New Issue
Block a user