Provide "Open with" context menu in qrc editor.

Task-number: QTCREATORBUG-4224

Change-Id: If2b13fa8b58779058d483d532c2c9649f7fa8dfd
Reviewed-by: Jarek Kobus <jaroslaw.kobus@nokia.com>
This commit is contained in:
Eike Ziller
2011-12-09 12:23:46 +01:00
parent cfcb7e3101
commit 1899a3838f
11 changed files with 54 additions and 14 deletions

View File

@@ -1333,6 +1333,7 @@ void FileManager::populateOpenWithMenu(QMenu *menu, const QString &fileName)
void FileManager::executeOpenWithMenuAction(QAction *action) void FileManager::executeOpenWithMenuAction(QAction *action)
{ {
QTC_ASSERT(action, return);
EditorManager *em = EditorManager::instance(); EditorManager *em = EditorManager::instance();
const QVariant data = action->data(); const QVariant data = action->data();
Internal::OpenWithEntry entry = qVariantValue<Internal::OpenWithEntry>(data); Internal::OpenWithEntry entry = qVariantValue<Internal::OpenWithEntry>(data);

View File

@@ -138,13 +138,14 @@ public:
void setProjectsDirectory(const QString &); void setProjectsDirectory(const QString &);
static void populateOpenWithMenu(QMenu *menu, const QString &fileName); static void populateOpenWithMenu(QMenu *menu, const QString &fileName);
static void executeOpenWithMenuAction(QAction *action);
public slots: public slots:
/* Used to notify e.g. the code model to update the given files. Does *not* /* Used to notify e.g. the code model to update the given files. Does *not*
lead to any editors to reload or any other editor manager actions. */ lead to any editors to reload or any other editor manager actions. */
void notifyFilesChangedInternally(const QStringList &files); void notifyFilesChangedInternally(const QStringList &files);
void executeOpenWithMenuAction(QAction *action);
signals: signals:
void currentFileChanged(const QString &filePath); void currentFileChanged(const QString &filePath);
/* Used to notify e.g. the code model to update the given files. Does *not* /* Used to notify e.g. the code model to update the given files. Does *not*

View File

@@ -361,7 +361,7 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
findOnFileSystem(info.absolutePath()); findOnFileSystem(info.absolutePath());
return; return;
} }
Core::FileManager::executeOpenWithMenuAction(action); Core::FileManager::instance()->executeOpenWithMenuAction(action);
} }
QString FolderNavigationWidget::msgFindOnFileSystem() QString FolderNavigationWidget::msgFindOnFileSystem()

View File

@@ -515,7 +515,7 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
d->m_openWithMenu->setTitle(tr("Open With")); d->m_openWithMenu->setTitle(tr("Open With"));
connect(d->m_openWithMenu, SIGNAL(triggered(QAction *)), connect(d->m_openWithMenu, SIGNAL(triggered(QAction *)),
this, SLOT(openWithMenuTriggered(QAction *))); Core::FileManager::instance(), SLOT(executeOpenWithMenuAction(QAction*)));
// //
// Separators // Separators
@@ -2785,14 +2785,6 @@ void ProjectExplorerPlugin::populateOpenWithMenu()
Core::FileManager::populateOpenWithMenu(d->m_openWithMenu, currentNode()->path()); Core::FileManager::populateOpenWithMenu(d->m_openWithMenu, currentNode()->path());
} }
void ProjectExplorerPlugin::openWithMenuTriggered(QAction *action)
{
if (!action)
qWarning() << "ProjectExplorerPlugin::openWithMenuTriggered no action, can't happen.";
else
Core::FileManager::executeOpenWithMenuAction(action);
}
void ProjectExplorerPlugin::updateSessionMenu() void ProjectExplorerPlugin::updateSessionMenu()
{ {
d->m_sessionMenu->clear(); d->m_sessionMenu->clear();

View File

@@ -176,7 +176,6 @@ private slots:
void newProject(); void newProject();
void showSessionManager(); void showSessionManager();
void populateOpenWithMenu(); void populateOpenWithMenu();
void openWithMenuTriggered(QAction *action);
void updateSessionMenu(); void updateSessionMenu();
void setSession(QAction *action); void setSession(QAction *action);

View File

@@ -38,6 +38,7 @@
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/filemanager.h>
#include <utils/reloadpromptutils.h> #include <utils/reloadpromptutils.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
@@ -45,8 +46,9 @@
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <QtCore/QDir> #include <QtCore/QDir>
#include <QtCore/qdebug.h> #include <QtCore/qdebug.h>
#include <QtGui/QMainWindow>
#include <QtGui/QHBoxLayout> #include <QtGui/QHBoxLayout>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
namespace ResourceEditor { namespace ResourceEditor {
namespace Internal { namespace Internal {
@@ -77,16 +79,27 @@ ResourceEditorW::ResourceEditorW(const Core::Context &context,
m_resourceFile(new ResourceEditorFile(this)), m_resourceFile(new ResourceEditorFile(this)),
m_plugin(plugin), m_plugin(plugin),
m_shouldAutoSave(false), m_shouldAutoSave(false),
m_diskIo(false) m_diskIo(false),
m_contextMenu(new QMenu)
{ {
setContext(context); setContext(context);
setWidget(m_resourceEditor); setWidget(m_resourceEditor);
m_resourceEditor->setResourceDragEnabled(true); m_resourceEditor->setResourceDragEnabled(true);
m_openWithMenu = m_contextMenu->addMenu(tr("Open With"));
// Below we need QueuedConnection because otherwise, if this qrc file
// is inside of the qrc file, crashes happen when using "Open With" on it.
// (That is because this editor instance is deleted in executeOpenWithMenuAction
// in that case.)
connect(m_openWithMenu, SIGNAL(triggered(QAction*)),
Core::FileManager::instance(), SLOT(executeOpenWithMenuAction(QAction*)),
Qt::QueuedConnection);
connect(m_resourceEditor, SIGNAL(dirtyChanged(bool)), this, SLOT(dirtyChanged(bool))); connect(m_resourceEditor, SIGNAL(dirtyChanged(bool)), this, SLOT(dirtyChanged(bool)));
connect(m_resourceEditor, SIGNAL(undoStackChanged(bool, bool)), connect(m_resourceEditor, SIGNAL(undoStackChanged(bool, bool)),
this, SLOT(onUndoStackChanged(bool, bool))); this, SLOT(onUndoStackChanged(bool, bool)));
connect(m_resourceEditor, SIGNAL(showContextMenu(QPoint,QString)),
this, SLOT(showContextMenu(QPoint,QString)));
connect(m_resourceEditor->commandHistory(), SIGNAL(indexChanged(int)), connect(m_resourceEditor->commandHistory(), SIGNAL(indexChanged(int)),
this, SLOT(setShouldAutoSave())); this, SLOT(setShouldAutoSave()));
connect(m_resourceFile, SIGNAL(changed()), this, SIGNAL(changed())); connect(m_resourceFile, SIGNAL(changed()), this, SIGNAL(changed()));
@@ -98,6 +111,7 @@ ResourceEditorW::~ResourceEditorW()
{ {
if (m_resourceEditor) if (m_resourceEditor)
m_resourceEditor->deleteLater(); m_resourceEditor->deleteLater();
delete m_contextMenu;
} }
bool ResourceEditorW::createNew(const QString &contents) bool ResourceEditorW::createNew(const QString &contents)
@@ -264,6 +278,13 @@ void ResourceEditorW::onUndoStackChanged(bool canUndo, bool canRedo)
m_plugin->onUndoStackChanged(this, canUndo, canRedo); m_plugin->onUndoStackChanged(this, canUndo, canRedo);
} }
void ResourceEditorW::showContextMenu(const QPoint &globalPoint, const QString &fileName)
{
Core::FileManager::populateOpenWithMenu(m_openWithMenu, fileName);
if (!m_openWithMenu->actions().isEmpty())
m_contextMenu->popup(globalPoint);
}
void ResourceEditorW::onUndo() void ResourceEditorW::onUndo()
{ {
if (!m_resourceEditor.isNull()) if (!m_resourceEditor.isNull())

View File

@@ -38,6 +38,10 @@
#include <QtCore/QPointer> #include <QtCore/QPointer>
QT_BEGIN_NAMESPACE
class QMenu;
QT_END_NAMESPACE
namespace SharedTools { namespace SharedTools {
class QrcEditor; class QrcEditor;
} }
@@ -104,6 +108,7 @@ private slots:
void dirtyChanged(bool); void dirtyChanged(bool);
void onUndoStackChanged(bool canUndo, bool canRedo); void onUndoStackChanged(bool canUndo, bool canRedo);
void setShouldAutoSave(bool sad = true) { m_shouldAutoSave = sad; } void setShouldAutoSave(bool sad = true) { m_shouldAutoSave = sad; }
void showContextMenu(const QPoint &globalPoint, const QString &fileName);
private: private:
const QString m_extension; const QString m_extension;
@@ -115,6 +120,8 @@ private:
ResourceEditorPlugin *m_plugin; ResourceEditorPlugin *m_plugin;
bool m_shouldAutoSave; bool m_shouldAutoSave;
bool m_diskIo; bool m_diskIo;
QMenu *m_contextMenu;
QMenu *m_openWithMenu;
public: public:
void onUndo(); void onUndo();

View File

@@ -66,6 +66,8 @@ QrcEditor::QrcEditor(QWidget *parent)
connect(m_treeview->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), connect(m_treeview->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(updateCurrent())); this, SLOT(updateCurrent()));
connect(m_treeview, SIGNAL(dirtyChanged(bool)), this, SIGNAL(dirtyChanged(bool))); connect(m_treeview, SIGNAL(dirtyChanged(bool)), this, SIGNAL(dirtyChanged(bool)));
connect(m_treeview, SIGNAL(showContextMenu(QPoint,QString)),
this, SIGNAL(showContextMenu(QPoint,QString)));
m_treeview->setFocus(); m_treeview->setFocus();
connect(m_ui.aliasText, SIGNAL(textEdited(QString)), connect(m_ui.aliasText, SIGNAL(textEdited(QString)),

View File

@@ -68,6 +68,7 @@ public:
signals: signals:
void dirtyChanged(bool dirty); void dirtyChanged(bool dirty);
void showContextMenu(const QPoint &globalPos, const QString &fileName);
private slots: private slots:
void updateCurrent(); void updateCurrent();

View File

@@ -182,11 +182,14 @@ ResourceView::ResourceView(QUndoStack *history, QWidget *parent) :
{ {
advanceMergeId(); advanceMergeId();
setModel(m_qrcModel); setModel(m_qrcModel);
setContextMenuPolicy(Qt::CustomContextMenu);
header()->hide(); header()->hide();
connect(m_qrcModel, SIGNAL(dirtyChanged(bool)), connect(m_qrcModel, SIGNAL(dirtyChanged(bool)),
this, SIGNAL(dirtyChanged(bool))); this, SIGNAL(dirtyChanged(bool)));
connect(this, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(showContextMenu(QPoint)));
} }
ResourceView::~ResourceView() ResourceView::~ResourceView()
@@ -429,6 +432,15 @@ void ResourceView::changeValue(const QModelIndex &nodeIndex, NodeProperty proper
} }
} }
void ResourceView::showContextMenu(const QPoint &pos)
{
const QModelIndex index = indexAt(pos);
const QString fileName = m_qrcModel->file(index);
if (fileName.isEmpty())
return;
emit showContextMenu(mapToGlobal(pos), fileName);
}
void ResourceView::advanceMergeId() void ResourceView::advanceMergeId()
{ {
m_mergeId++; m_mergeId++;

View File

@@ -130,11 +130,15 @@ protected:
signals: signals:
void removeItem(); void removeItem();
void dirtyChanged(bool b); void dirtyChanged(bool b);
void showContextMenu(const QPoint &globalPos, const QString &fileName);
public: public:
QString getCurrentValue(NodeProperty property) const; QString getCurrentValue(NodeProperty property) const;
void changeValue(const QModelIndex &nodeIndex, NodeProperty property, const QString &value); void changeValue(const QModelIndex &nodeIndex, NodeProperty property, const QString &value);
private slots:
void showContextMenu(const QPoint &pos);
private: private:
void addUndoCommand(const QModelIndex &nodeIndex, NodeProperty property, void addUndoCommand(const QModelIndex &nodeIndex, NodeProperty property,
const QString &before, const QString &after); const QString &before, const QString &after);