From b0da2476044a84d8066c7d765e4680d737c01aee Mon Sep 17 00:00:00 2001
From: Konstantin Tokarev <annulen@yandex.ru>
Date: Tue, 24 Jul 2012 23:30:32 +0400
Subject: [PATCH] Unified contextMenuEvent implementations of editor widgets.

Change-Id: Ie5dbde8d0443a65a8e96853002c8bdafd2c0d729
Reviewed-by: Daniel Teske <daniel.teske@nokia.com>
---
 src/plugins/cmakeprojectmanager/cmakeeditor.cpp | 13 +------------
 src/plugins/qt4projectmanager/profileeditor.cpp | 14 +-------------
 src/plugins/texteditor/basetexteditor.cpp       | 17 +++++++++++++++++
 src/plugins/texteditor/basetexteditor.h         |  4 +++-
 4 files changed, 22 insertions(+), 26 deletions(-)

diff --git a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp
index be382322fad..dd37968d7fc 100644
--- a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp
+++ b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp
@@ -142,18 +142,7 @@ void CMakeEditorWidget::unCommentSelection()
 
 void CMakeEditorWidget::contextMenuEvent(QContextMenuEvent *e)
 {
-    QMenu *menu = new QMenu();
-
-    Core::ActionContainer *mcontext = Core::ActionManager::actionContainer(Constants::M_CONTEXT);
-    QMenu *contextMenu = mcontext->menu();
-
-    foreach (QAction *action, contextMenu->actions())
-        menu->addAction(action);
-
-    appendStandardContextMenuActions(menu);
-
-    menu->exec(e->globalPos());
-    delete menu;
+    showDefaultContextMenu(e, Constants::M_CONTEXT);
 }
 
 void CMakeEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
diff --git a/src/plugins/qt4projectmanager/profileeditor.cpp b/src/plugins/qt4projectmanager/profileeditor.cpp
index 62af3f9af1a..ff93c868feb 100644
--- a/src/plugins/qt4projectmanager/profileeditor.cpp
+++ b/src/plugins/qt4projectmanager/profileeditor.cpp
@@ -196,19 +196,7 @@ TextEditor::BaseTextEditor *ProFileEditorWidget::createEditor()
 
 void ProFileEditorWidget::contextMenuEvent(QContextMenuEvent *e)
 {
-    QMenu *menu = new QMenu();
-
-    Core::ActionContainer *mcontext =
-            Core::ActionManager::actionContainer(Qt4ProjectManager::Constants::M_CONTEXT);
-    QMenu *contextMenu = mcontext->menu();
-
-    foreach (QAction *action, contextMenu->actions())
-        menu->addAction(action);
-
-    appendStandardContextMenuActions(menu);
-
-    menu->exec(e->globalPos());
-    delete menu;
+    showDefaultContextMenu(e, Constants::M_CONTEXT);
 }
 
 void ProFileEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index 508c38804dc..5c17d071609 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -4327,6 +4327,14 @@ void BaseTextEditorWidget::dragEnterEvent(QDragEnterEvent *e)
     QPlainTextEdit::dragEnterEvent(e);
 }
 
+void BaseTextEditorWidget::showDefaultContextMenu(QContextMenuEvent *e, const Core::Id menuContextId)
+{
+    QMenu menu;
+    appendMenuActionsFromContext(&menu, menuContextId);
+    appendStandardContextMenuActions(&menu);
+    menu.exec(e->globalPos());
+}
+
 void BaseTextEditorWidget::extraAreaLeaveEvent(QEvent *)
 {
     // fake missing mouse move event from Qt
@@ -6059,6 +6067,15 @@ QMimeData *BaseTextEditorWidget::duplicateMimeData(const QMimeData *source) cons
     return mimeData;
 }
 
+void BaseTextEditorWidget::appendMenuActionsFromContext(QMenu *menu, const Core::Id menuContextId)
+{
+    Core::ActionContainer *mcontext = Core::ActionManager::actionContainer(menuContextId);
+    QMenu *contextMenu = mcontext->menu();
+
+    foreach (QAction *action, contextMenu->actions())
+        menu->addAction(action);
+}
+
 void BaseTextEditorWidget::appendStandardContextMenuActions(QMenu *menu)
 {
     menu->addSeparator();
diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h
index dd260ebc67e..461d0a8e40f 100644
--- a/src/plugins/texteditor/basetexteditor.h
+++ b/src/plugins/texteditor/basetexteditor.h
@@ -166,7 +166,7 @@ public:
     QString mimeType() const;
     virtual void setMimeType(const QString &mt);
 
-
+    void appendMenuActionsFromContext(QMenu *menu, const Core::Id menuContextId);
     void appendStandardContextMenuActions(QMenu *menu);
 
     // Works only in conjunction with a syntax highlighter that puts
@@ -469,6 +469,8 @@ protected:
 
     void dragEnterEvent(QDragEnterEvent *e);
 
+    void showDefaultContextMenu(QContextMenuEvent *e, const Core::Id menuContextId);
+
 public:
     void indentInsertedText(const QTextCursor &tc);
     void indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar);
-- 
GitLab