From d636140ea4ef39b7e704cb9d787842c157a7f7df Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 15 Aug 2011 15:21:02 +0200 Subject: [PATCH] Add Close/Close Others/Close All items to editor list's context menu Move the context menu actions from the "Open Documents" pane to the editor manager and use these for both the open documents pane and the editor combo box in the editor tool bar. Change-Id: If2a8cb4cf0498c78bd06053919b3cb74692b7cd8 Reviewed-on: http://codereview.qt.nokia.com/2973 Reviewed-by: Eike Ziller Reviewed-by: Qt Sanity Bot --- .../coreplugin/editormanager/editormanager.cpp | 41 ++++++++++++++++++++++ .../coreplugin/editormanager/editormanager.h | 8 ++++- .../coreplugin/editormanager/openeditorsview.cpp | 28 ++------------- src/plugins/coreplugin/editortoolbar.cpp | 6 ++-- 4 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index c7f293f78f..57fb3fcbf4 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -214,6 +214,11 @@ struct EditorManagerPrivate { QAction *m_removeAllSplitsAction; QAction *m_gotoOtherSplitAction; + QAction *m_closeCurrentEditorContextAction; + QAction *m_closeAllEditorsContextAction; + QAction *m_closeOtherEditorsContextAction; + QModelIndex m_contextMenuEditorIndex; + Internal::OpenEditorsWindow *m_windowPopup; Internal::EditorClosingCoreListener *m_coreListener; @@ -246,6 +251,9 @@ EditorManagerPrivate::EditorManagerPrivate(ICore *core, QWidget *parent) : m_gotoPreviousDocHistoryAction(new QAction(EditorManager::tr("Previous Open Document in History"), parent)), m_goBackAction(new QAction(QIcon(QLatin1String(Constants::ICON_PREV)), EditorManager::tr("Go Back"), parent)), m_goForwardAction(new QAction(QIcon(QLatin1String(Constants::ICON_NEXT)), EditorManager::tr("Go Forward"), parent)), + m_closeCurrentEditorContextAction(new QAction(EditorManager::tr("Close"), parent)), + m_closeAllEditorsContextAction(new QAction(EditorManager::tr("Close All"), parent)), + m_closeOtherEditorsContextAction(new QAction(EditorManager::tr("Close Others"), parent)), m_windowPopup(0), m_coreListener(0), m_reloadSetting(IFile::AlwaysAsk), @@ -348,6 +356,11 @@ EditorManager::EditorManager(ICore *core, QWidget *parent) : cmd->setAttribute(Core::Command::CA_UpdateText); connect(m_d->m_closeOtherEditorsAction, SIGNAL(triggered()), this, SLOT(closeOtherEditors())); + // Close XXX Context Actions + connect(m_d->m_closeAllEditorsContextAction, SIGNAL(triggered()), this, SLOT(closeAllEditors())); + connect(m_d->m_closeCurrentEditorContextAction, SIGNAL(triggered()), this, SLOT(closeEditorFromContextMenu())); + connect(m_d->m_closeOtherEditorsContextAction, SIGNAL(triggered()), this, SLOT(closeOtherEditorsFromContextMenu())); + // Goto Previous In History Action cmd = am->registerAction(m_d->m_gotoPreviousDocHistoryAction, Constants::GOTOPREVINHISTORY, editDesignContext); #ifdef Q_WS_MAC @@ -751,6 +764,34 @@ void EditorManager::closeEditor() closeEditor(m_d->m_currentEditor); } +void EditorManager::addCloseEditorActions(QMenu *contextMenu, const QModelIndex &editorIndex) +{ + QTC_ASSERT(contextMenu, return); + m_d->m_contextMenuEditorIndex = editorIndex; + m_d->m_closeCurrentEditorContextAction->setText(editorIndex.isValid() + ? tr("Close \"%1\"").arg(editorIndex.data().toString()) + : tr("Close Editor")); + m_d->m_closeOtherEditorsContextAction->setText(editorIndex.isValid() + ? tr("Close All Except \"%1\"").arg(editorIndex.data().toString()) + : tr("Close Other Editors")); + m_d->m_closeCurrentEditorContextAction->setEnabled(editorIndex.isValid()); + m_d->m_closeOtherEditorsContextAction->setEnabled(editorIndex.isValid()); + m_d->m_closeAllEditorsContextAction->setEnabled(!openedEditors().isEmpty()); + contextMenu->addAction(m_d->m_closeCurrentEditorContextAction); + contextMenu->addAction(m_d->m_closeAllEditorsContextAction); + contextMenu->addAction(m_d->m_closeOtherEditorsContextAction); +} + +void EditorManager::closeEditorFromContextMenu() +{ + closeEditor(m_d->m_contextMenuEditorIndex); +} + +void EditorManager::closeOtherEditorsFromContextMenu() +{ + closeOtherEditors(m_d->m_contextMenuEditorIndex.data(Qt::UserRole).value()); +} + void EditorManager::closeEditor(Core::IEditor *editor) { if (!editor) diff --git a/src/plugins/coreplugin/editormanager/editormanager.h b/src/plugins/coreplugin/editormanager/editormanager.h index ffaec67a26..ca9f74deb8 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.h +++ b/src/plugins/coreplugin/editormanager/editormanager.h @@ -37,8 +37,9 @@ #include // enumerations -#include #include +#include +#include QT_BEGIN_NAMESPACE class QModelIndex; @@ -190,6 +191,8 @@ public: void setWindowTitleAddition(const QString &addition); QString windowTitleAddition() const; + void addCloseEditorActions(QMenu *contextMenu, const QModelIndex &editorIndex); + signals: void currentEditorChanged(Core::IEditor *editor); void currentEditorStateChanged(Core::IEditor *editor); @@ -219,6 +222,9 @@ private slots: void updateVariable(const QString &variable); void autoSave(); + void closeEditorFromContextMenu(); + void closeOtherEditorsFromContextMenu(); + public slots: void goBackInNavigationHistory(); void goForwardInNavigationHistory(); diff --git a/src/plugins/coreplugin/editormanager/openeditorsview.cpp b/src/plugins/coreplugin/editormanager/openeditorsview.cpp index 9695ec1d65..cebdf5176c 100644 --- a/src/plugins/coreplugin/editormanager/openeditorsview.cpp +++ b/src/plugins/coreplugin/editormanager/openeditorsview.cpp @@ -204,33 +204,9 @@ void OpenEditorsWidget::closeEditor(const QModelIndex &index) void OpenEditorsWidget::contextMenuRequested(QPoint pos) { - const QModelIndex index = m_ui.editorList->indexAt(pos); QMenu contextMenu; - QAction *closeEditor = contextMenu.addAction( - index.isValid() ? tr("Close \"%1\"").arg(index.data().toString()) - : tr("Close Editor")); - QAction *closeOtherEditors = contextMenu.addAction( - index.isValid() ? tr("Close All Except \"%1\"").arg(index.data().toString()) - : tr("Close Other Editors")); - QAction *closeAllEditors = contextMenu.addAction(tr("Close All Editors")); - - if (!index.isValid()) { - closeEditor->setEnabled(false); - closeOtherEditors->setEnabled(false); - } - - if (EditorManager::instance()->openedEditors().isEmpty()) - closeAllEditors->setEnabled(false); - - QAction *action = contextMenu.exec(m_ui.editorList->mapToGlobal(pos)); - if (action == 0) - return; - if (action == closeEditor) - EditorManager::instance()->closeEditor(index); - else if (action == closeAllEditors) - EditorManager::instance()->closeAllEditors(); - else if (action == closeOtherEditors) - EditorManager::instance()->closeOtherEditors(index.data(Qt::UserRole).value()); + EditorManager::instance()->addCloseEditorActions(&contextMenu, m_ui.editorList->indexAt(pos)); + contextMenu.exec(m_ui.editorList->mapToGlobal(pos)); } /// diff --git a/src/plugins/coreplugin/editortoolbar.cpp b/src/plugins/coreplugin/editortoolbar.cpp index 4257fb2998..93d2726f8f 100644 --- a/src/plugins/coreplugin/editortoolbar.cpp +++ b/src/plugins/coreplugin/editortoolbar.cpp @@ -289,8 +289,10 @@ void EditorToolBar::listContextMenu(QPoint pos) if (fileName.isEmpty()) return; QMenu menu; - menu.addAction(tr("Copy Full Path to Clipboard")); - if (menu.exec(d->m_editorList->mapToGlobal(pos))) { + QAction *copyPath = menu.addAction(tr("Copy Full Path to Clipboard")); + EditorManager::instance()->addCloseEditorActions(&menu, index); + QAction *result = menu.exec(d->m_editorList->mapToGlobal(pos)); + if (result == copyPath) { QApplication::clipboard()->setText(QDir::toNativeSeparators(fileName)); } } -- 2.11.0