From 3d2ef178f67138ce8aa89449aa65fc56e9b74a01 Mon Sep 17 00:00:00 2001
From: David Kaspar <dkaspar@blackberry.com>
Date: Thu, 17 Oct 2013 09:16:18 +0200
Subject: [PATCH] Qnx: Fixing Bar Descriptor XML file editor to provide editor
 actions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

BarDescriptorEditorWidget is using TextEditorActionHandler class
to provide editor actions e.g. Undo, Redo, Cut, Copy, Paste accessible
from main menu and using shortcuts.

TextEditorActionHandler.resolveTextEditorWidget() virtual function is
added to all resolving TextEditorWidget for an Editor. This allows
to have IEditor->widget() to have non-BaseTextEditorWidget instances too.

Task-number: QTCREATORBUG-10040
Change-Id: I6f433fc307c13ef2b2a20c48e6473826f2619544
Reviewed-by: David Kaspar <dkaspar@blackberry.com>
Reviewed-by: David Schulz <david.schulz@digia.com>
Reviewed-by: Tobias Nätterlund <tobias.naetterlund@kdab.com>
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
Reviewed-by: Nicolas Arnaud-Cormos <nicolas@kdab.com>
---
 src/plugins/qnx/bardescriptoreditor.cpp       |  5 ++++
 .../qnx/bardescriptoreditorfactory.cpp        | 24 ++++++++++++++++++-
 src/plugins/qnx/bardescriptoreditorfactory.h  |  8 +++++++
 src/plugins/qnx/bardescriptoreditorwidget.cpp | 20 +++++++++++++++-
 src/plugins/qnx/bardescriptoreditorwidget.h   |  7 +++++-
 src/plugins/qnx/qnxconstants.h                |  1 +
 .../texteditor/texteditoractionhandler.cpp    |  7 +++++-
 .../texteditor/texteditoractionhandler.h      |  1 +
 8 files changed, 69 insertions(+), 4 deletions(-)

diff --git a/src/plugins/qnx/bardescriptoreditor.cpp b/src/plugins/qnx/bardescriptoreditor.cpp
index 548129ff3b3..159e074a7f9 100644
--- a/src/plugins/qnx/bardescriptoreditor.cpp
+++ b/src/plugins/qnx/bardescriptoreditor.cpp
@@ -39,6 +39,8 @@
 #include <projectexplorer/task.h>
 #include <projectexplorer/taskhub.h>
 #include <utils/qtcassert.h>
+#include <texteditor/texteditorconstants.h>
+#include <texteditor/basetexteditor.h>
 
 #include <QAction>
 #include <QToolBar>
@@ -80,6 +82,9 @@ BarDescriptorEditor::BarDescriptorEditor(BarDescriptorEditorWidget *editorWidget
     m_actionGroup->addAction(sourceAction);
 
     generalAction->setChecked(true);
+
+    setContext(Core::Context(Constants::QNX_BAR_DESCRIPTOR_EDITOR_CONTEXT,
+            TextEditor::Constants::C_TEXTEDITOR));
 }
 
 bool BarDescriptorEditor::open(QString *errorString, const QString &fileName, const QString &realFileName)
diff --git a/src/plugins/qnx/bardescriptoreditorfactory.cpp b/src/plugins/qnx/bardescriptoreditorfactory.cpp
index 27a97a3482f..5d9a6cdbef4 100644
--- a/src/plugins/qnx/bardescriptoreditorfactory.cpp
+++ b/src/plugins/qnx/bardescriptoreditorfactory.cpp
@@ -36,20 +36,42 @@
 
 #include <coreplugin/editormanager/editormanager.h>
 #include <coreplugin/editormanager/ieditor.h>
+#include <texteditor/texteditoractionhandler.h>
 
 using namespace Qnx;
 using namespace Qnx::Internal;
 
+class BarDescriptorActionHandler : public TextEditor::TextEditorActionHandler
+{
+public:
+    BarDescriptorActionHandler()
+        : TextEditor::TextEditorActionHandler(Constants::QNX_BAR_DESCRIPTOR_EDITOR_CONTEXT)
+    {
+    }
+protected:
+    TextEditor::BaseTextEditorWidget *resolveTextEditorWidget(Core::IEditor *editor) const
+    {
+        BarDescriptorEditorWidget *w = qobject_cast<BarDescriptorEditorWidget *>(editor->widget());
+        return w ? w->sourceWidget() : 0;
+    }
+};
+
 BarDescriptorEditorFactory::BarDescriptorEditorFactory(QObject *parent)
     : Core::IEditorFactory(parent)
+    , m_actionHandler(new BarDescriptorActionHandler)
 {
     setId(Constants::QNX_BAR_DESCRIPTOR_EDITOR_ID);
     setDisplayName(tr("Bar descriptor editor"));
     addMimeType(Constants::QNX_BAR_DESCRIPTOR_MIME_TYPE);
 }
 
+BarDescriptorEditorFactory::~BarDescriptorEditorFactory()
+{
+    delete m_actionHandler;
+}
+
 Core::IEditor *BarDescriptorEditorFactory::createEditor(QWidget *parent)
 {
-    BarDescriptorEditorWidget *editorWidget = new BarDescriptorEditorWidget(parent);
+    BarDescriptorEditorWidget *editorWidget = new BarDescriptorEditorWidget(parent, m_actionHandler);
     return editorWidget->editor();
 }
diff --git a/src/plugins/qnx/bardescriptoreditorfactory.h b/src/plugins/qnx/bardescriptoreditorfactory.h
index fac30e79208..ed5a012d6a3 100644
--- a/src/plugins/qnx/bardescriptoreditorfactory.h
+++ b/src/plugins/qnx/bardescriptoreditorfactory.h
@@ -34,6 +34,10 @@
 
 #include <coreplugin/editormanager/ieditorfactory.h>
 
+namespace TextEditor {
+class TextEditorActionHandler;
+}
+
 namespace Qnx {
 namespace Internal {
 
@@ -43,8 +47,12 @@ class BarDescriptorEditorFactory : public Core::IEditorFactory
 
 public:
     explicit BarDescriptorEditorFactory(QObject *parent = 0);
+    ~BarDescriptorEditorFactory();
 
     Core::IEditor *createEditor(QWidget *parent);
+
+private:
+    TextEditor::TextEditorActionHandler *m_actionHandler;
 };
 
 } // namespace Internal
diff --git a/src/plugins/qnx/bardescriptoreditorwidget.cpp b/src/plugins/qnx/bardescriptoreditorwidget.cpp
index 604523437b8..5d069112a85 100644
--- a/src/plugins/qnx/bardescriptoreditorwidget.cpp
+++ b/src/plugins/qnx/bardescriptoreditorwidget.cpp
@@ -41,18 +41,29 @@
 #include "bardescriptoreditorpackageinformationwidget.h"
 #include "bardescriptoreditorpermissionswidget.h"
 
+#include <coreplugin/icore.h>
 #include <projectexplorer/iprojectproperties.h>
 #include <projectexplorer/projectwindow.h>
 #include <texteditor/plaintexteditor.h>
+#include <texteditor/texteditoractionhandler.h>
+#include <texteditor/texteditorsettings.h>
+#include <texteditor/texteditorconstants.h>
 
 using namespace Qnx;
 using namespace Qnx::Internal;
 
-BarDescriptorEditorWidget::BarDescriptorEditorWidget(QWidget *parent)
+BarDescriptorEditorWidget::BarDescriptorEditorWidget(
+        QWidget *parent, TextEditor::TextEditorActionHandler *handler)
     : QStackedWidget(parent)
     , m_editor(0)
+    , m_handler(handler)
     , m_dirty(false)
 {
+    Core::IContext *myContext = new Core::IContext(this);
+    myContext->setWidget(this);
+    myContext->setContext(Core::Context(Constants::QNX_BAR_DESCRIPTOR_EDITOR_CONTEXT, TextEditor::Constants::C_TEXTEDITOR));
+    Core::ICore::addContextObject(myContext);
+
     initGeneralPage();
     initApplicationPage();
     initAssetsPage();
@@ -149,6 +160,8 @@ void BarDescriptorEditorWidget::initSourcePage()
     m_xmlSourceWidget = new TextEditor::PlainTextEditorWidget(this);
     addWidget(m_xmlSourceWidget);
 
+    TextEditor::TextEditorSettings::initializeEditor(m_xmlSourceWidget);
+    m_handler->setupActions(m_xmlSourceWidget);
     m_xmlSourceWidget->configure(QLatin1String(Constants::QNX_BAR_DESCRIPTOR_MIME_TYPE));
     connect(m_xmlSourceWidget, SIGNAL(textChanged()), this, SLOT(setDirty()));
 }
@@ -204,6 +217,11 @@ BarDescriptorEditorAssetsWidget *BarDescriptorEditorWidget::assetsWidget() const
     return m_assetsWidget;
 }
 
+TextEditor::BaseTextEditorWidget *BarDescriptorEditorWidget::sourceWidget() const
+{
+    return m_xmlSourceWidget;
+}
+
 void BarDescriptorEditorWidget::setFilePath(const QString &filePath)
 {
     Core::IDocument *doc = m_xmlSourceWidget->editorDocument();
diff --git a/src/plugins/qnx/bardescriptoreditorwidget.h b/src/plugins/qnx/bardescriptoreditorwidget.h
index bb2480cabb9..8e5edc69e60 100644
--- a/src/plugins/qnx/bardescriptoreditorwidget.h
+++ b/src/plugins/qnx/bardescriptoreditorwidget.h
@@ -46,6 +46,8 @@ class PanelsWidget;
 
 namespace TextEditor {
 class PlainTextEditorWidget;
+class TextEditorActionHandler;
+class BaseTextEditorWidget;
 }
 
 namespace Qnx {
@@ -65,7 +67,7 @@ class BarDescriptorEditorWidget : public QStackedWidget
     Q_OBJECT
 
 public:
-    explicit BarDescriptorEditorWidget(QWidget *parent = 0);
+    explicit BarDescriptorEditorWidget(QWidget *parent, TextEditor::TextEditorActionHandler *handler);
 
     Core::IEditor *editor() const;
 
@@ -79,6 +81,8 @@ public:
 
     BarDescriptorEditorAssetsWidget *assetsWidget() const;
 
+    TextEditor::BaseTextEditorWidget *sourceWidget() const;
+
     void setFilePath(const QString &filePath);
     QString xmlSource() const;
     void setXmlSource(const QString &xmlSource);
@@ -103,6 +107,7 @@ private:
 
     mutable Core::IEditor *m_editor;
 
+    TextEditor::TextEditorActionHandler *m_handler;
     bool m_dirty;
 
     // New UI
diff --git a/src/plugins/qnx/qnxconstants.h b/src/plugins/qnx/qnxconstants.h
index 728403dc61f..c0d5d1762d6 100644
--- a/src/plugins/qnx/qnxconstants.h
+++ b/src/plugins/qnx/qnxconstants.h
@@ -99,6 +99,7 @@ const char QNX_BB_SIGNING_ID[] = "ZZ.BlackBerry Signing Infrastructure Configura
 
 const char QNX_BAR_DESCRIPTOR_MIME_TYPE[] = "application/vnd.rim.qnx.bar_descriptor";
 const char QNX_BAR_DESCRIPTOR_EDITOR_ID[] = "Qnx.BarDescriptorEditor";
+const char QNX_BAR_DESCRIPTOR_EDITOR_CONTEXT[] = "Qnx.BarDescriptorEditor";
 
 const char QNX_TASK_CATEGORY_BARDESCRIPTOR[] = "Task.Category.BarDescriptor";
 
diff --git a/src/plugins/texteditor/texteditoractionhandler.cpp b/src/plugins/texteditor/texteditoractionhandler.cpp
index 7c492a44b05..15cd1c82c4d 100644
--- a/src/plugins/texteditor/texteditoractionhandler.cpp
+++ b/src/plugins/texteditor/texteditoractionhandler.cpp
@@ -560,6 +560,11 @@ FUNCTION(gotoNextWordCamelCase)
 FUNCTION(gotoNextWordCamelCaseWithSelection)
 
 
+BaseTextEditorWidget *TextEditorActionHandler::resolveTextEditorWidget(Core::IEditor *editor) const
+{
+    return qobject_cast<BaseTextEditorWidget *>(editor->widget());
+}
+
 void TextEditorActionHandler::updateCurrentEditor(Core::IEditor *editor)
 {
     m_currentEditor = 0;
@@ -567,7 +572,7 @@ void TextEditorActionHandler::updateCurrentEditor(Core::IEditor *editor)
     if (!editor)
         return;
 
-    BaseTextEditorWidget *baseEditor = qobject_cast<BaseTextEditorWidget *>(editor->widget());
+    BaseTextEditorWidget *baseEditor = resolveTextEditorWidget(editor);
 
     if (baseEditor && baseEditor->actionHack() == this) {
         m_currentEditor = baseEditor;
diff --git a/src/plugins/texteditor/texteditoractionhandler.h b/src/plugins/texteditor/texteditoractionhandler.h
index 2e768b4b4e4..a09e6a3a84e 100644
--- a/src/plugins/texteditor/texteditoractionhandler.h
+++ b/src/plugins/texteditor/texteditoractionhandler.h
@@ -79,6 +79,7 @@ public slots:
     void updateCopyAction();
 
 protected:
+    virtual BaseTextEditorWidget *resolveTextEditorWidget(Core::IEditor *editor) const;
     const QPointer<BaseTextEditorWidget> &currentEditor() const;
 
     QAction *registerAction(const Core::Id &id,
-- 
GitLab