From 9c70e8771561e56b83db0324b95dfbaec09dee74 Mon Sep 17 00:00:00 2001
From: Thomas Hartmann <Thomas.Hartmann@nokia.com>
Date: Thu, 5 Aug 2010 10:44:50 +0200
Subject: [PATCH] QmlJSEditor: using RefactorMarker for Qt Quick ToolBar

* Proper implementation using a timer

* We also do a revision check now

* The RefactorMarker is only shown for types that we support
---
 src/plugins/qmljseditor/qmljseditor.cpp | 62 ++++++++++++++++++++++---
 src/plugins/qmljseditor/qmljseditor.h   |  3 ++
 src/plugins/texteditor/basetexteditor.h |  2 +-
 3 files changed, 59 insertions(+), 8 deletions(-)

diff --git a/src/plugins/qmljseditor/qmljseditor.cpp b/src/plugins/qmljseditor/qmljseditor.cpp
index 62bdd051573..4af64c3da6c 100644
--- a/src/plugins/qmljseditor/qmljseditor.cpp
+++ b/src/plugins/qmljseditor/qmljseditor.cpp
@@ -60,6 +60,7 @@
 #include <texteditor/texteditorconstants.h>
 #include <texteditor/texteditorsettings.h>
 #include <texteditor/syntaxhighlighter.h>
+#include <texteditor/refactoroverlay.h>
 #include <qmldesigner/qmldesignerconstants.h>
 #include <utils/changeset.h>
 #include <utils/uncommentselection.h>
@@ -702,6 +703,11 @@ QmlJSTextEditor::QmlJSTextEditor(QWidget *parent) :
     m_updateOutlineIndexTimer->setSingleShot(true);
     connect(m_updateOutlineIndexTimer, SIGNAL(timeout()), this, SLOT(updateOutlineIndexNow()));
 
+    m_curserPositionTimer  = new QTimer(this);
+    m_curserPositionTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
+    m_curserPositionTimer->setSingleShot(true);
+    connect(m_curserPositionTimer, SIGNAL(timeout()), this, SLOT(updateCursorPositionNow()));
+
     baseTextDocument()->setSyntaxHighlighter(new Highlighter(document()));
 
     m_modelManager = ExtensionSystem::PluginManager::instance()->getObject<ModelManagerInterface>();
@@ -720,6 +726,9 @@ QmlJSTextEditor::QmlJSTextEditor(QWidget *parent) :
     connect(m_semanticHighlighter, SIGNAL(changed(QmlJSEditor::Internal::SemanticInfo)),
             this, SLOT(updateSemanticInfo(QmlJSEditor::Internal::SemanticInfo)));
 
+    connect(this, SIGNAL(refactorMarkerClicked(TextEditor::Internal::RefactorMarker)),
+            SLOT(onRefactorMarkerClicked(TextEditor::Internal::RefactorMarker)));
+
     setRequestMarkEnabled(true);
 }
 
@@ -931,6 +940,46 @@ void QmlJSTextEditor::updateOutlineIndexNow()
     updateUses();
 }
 
+static UiQualifiedId *qualifiedTypeNameId(UiObjectMember *m)
+{
+    if (UiObjectDefinition *def = cast<UiObjectDefinition *>(m))
+        return def->qualifiedTypeNameId;
+    else if (UiObjectBinding *binding = cast<UiObjectBinding *>(m))
+        return binding->qualifiedTypeNameId;
+    return 0;
+}
+
+void QmlJSTextEditor::updateCursorPositionNow()
+{
+    if (m_contextPane && document() && !semanticInfo().document.isNull() &&
+        document()->revision() == semanticInfo().document->editorRevision()) {
+        Node *oldNode = m_semanticInfo.declaringMemberNoProperties(m_oldCursorPosition);
+        Node *newNode = m_semanticInfo.declaringMemberNoProperties(position());
+        if (oldNode != newNode &&
+            m_contextPane->isAvailable(editableInterface(), m_semanticInfo.document, m_semanticInfo.snapshot, newNode)) {
+            QList<TextEditor::Internal::RefactorMarker> markers;
+            if (UiObjectMember *m = newNode->uiObjectMemberCast()) {
+                const int start = m->firstSourceLocation().begin();
+                for (UiQualifiedId *q = qualifiedTypeNameId(m); q; q = q->next) {
+                    if (! q->next) {
+                        const int end = q->identifierToken.end();
+                        TextEditor::Internal::RefactorMarker marker;
+                        QTextCursor tc(document());
+                        tc.setPosition(end);
+                        marker.cursor = tc;
+                        marker.tooltip = tr("Show Qt Quick Helper");
+                        markers.append(marker);
+                    }
+                }
+            }
+            setRefactorMarkers(markers);
+        }
+        if (oldNode != newNode)
+            m_contextPane->apply(editableInterface(), m_semanticInfo.document, m_semanticInfo.snapshot, newNode, false);
+        m_oldCursorPosition = position();
+    }
+}
+
 void QmlJSTextEditor::updateUses()
 {
     m_updateUsesTimer->start();
@@ -1661,15 +1710,14 @@ void QmlJSTextEditor::updateSemanticInfo(const SemanticInfo &semanticInfo)
     setExtraSelections(CodeWarningsSelection, selections);
 }
 
+void QmlJSTextEditor::onRefactorMarkerClicked(const TextEditor::Internal::RefactorMarker &)
+{
+    showContextPane();
+}
+
 void QmlJSTextEditor::onCursorPositionChanged()
 {
-    if (m_contextPane) {
-        Node *newNode = m_semanticInfo.declaringMemberNoProperties(position());
-        Node *oldNode = m_semanticInfo.declaringMemberNoProperties(m_oldCursorPosition);
-        if (oldNode != newNode)
-            m_contextPane->apply(editableInterface(), m_semanticInfo.document, m_semanticInfo.snapshot, newNode, false);
-        m_oldCursorPosition = position();
-    }
+    m_curserPositionTimer->start();
 }
 
 QModelIndex QmlJSTextEditor::indexForPosition(unsigned cursorPosition, const QModelIndex &rootIndex) const
diff --git a/src/plugins/qmljseditor/qmljseditor.h b/src/plugins/qmljseditor/qmljseditor.h
index 79f03fd6137..5f9765a7a11 100644
--- a/src/plugins/qmljseditor/qmljseditor.h
+++ b/src/plugins/qmljseditor/qmljseditor.h
@@ -243,6 +243,7 @@ private slots:
     void jumpToOutlineElement(int index);
     void updateOutlineNow();
     void updateOutlineIndexNow();
+    void updateCursorPositionNow();
     void updateFileName();
 
     void updateUses();
@@ -255,6 +256,7 @@ private slots:
     void forceSemanticRehighlight();
     void updateSemanticInfo(const QmlJSEditor::Internal::SemanticInfo &semanticInfo);
     void onCursorPositionChanged();
+    void onRefactorMarkerClicked(const TextEditor::Internal::RefactorMarker &marker);
 
 protected:
     void contextMenuEvent(QContextMenuEvent *e);
@@ -292,6 +294,7 @@ private:
     QTimer *m_semanticRehighlightTimer;
     QTimer *m_updateOutlineTimer;
     QTimer *m_updateOutlineIndexTimer;
+    QTimer *m_curserPositionTimer;
     QComboBox *m_outlineCombo;
     QmlOutlineModel *m_outlineModel;
     QModelIndex m_outlineModelIndex;
diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h
index c15b78d16eb..ecd0494fce0 100644
--- a/src/plugins/texteditor/basetexteditor.h
+++ b/src/plugins/texteditor/basetexteditor.h
@@ -359,7 +359,7 @@ public:
 
     void setRefactorMarkers(const Internal::RefactorMarkers &markers);
 signals:
-    void refactorMarkerClicked(const Internal::RefactorMarker &marker);
+    void refactorMarkerClicked(const TextEditor::Internal::RefactorMarker &marker);
 
 public:
 
-- 
GitLab