From 76c865fc61a999393541e260cc7ce7c66aabc8f3 Mon Sep 17 00:00:00 2001
From: Thomas Hartmann <Thomas.Hartmann@nokia.com>
Date: Wed, 18 Aug 2010 16:25:05 +0200
Subject: [PATCH] QuickToolBar: support for PropertyChanges

The ToolBar now detects the target type of the target
of a PropertyChanges object.
---
 src/plugins/qmljseditor/quicktoolbar.cpp | 47 ++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/src/plugins/qmljseditor/quicktoolbar.cpp b/src/plugins/qmljseditor/quicktoolbar.cpp
index 94c9b29477a..0d02edff365 100644
--- a/src/plugins/qmljseditor/quicktoolbar.cpp
+++ b/src/plugins/qmljseditor/quicktoolbar.cpp
@@ -12,6 +12,7 @@
 #include <qmljs/qmljsinterpreter.h>
 #include <qmljs/qmljsbind.h>
 #include <qmljs/qmljsscopebuilder.h>
+#include <qmljs/qmljsevaluate.h>
 #include <texteditor/basetexteditor.h>
 #include <texteditor/tabsettings.h>
 #include <coreplugin/icore.h>
@@ -31,6 +32,34 @@ static inline QString textAt(const Document* doc,
     return doc->source().mid(from.offset, to.end() - from.begin());
 }
 
+static inline const Interpreter::ObjectValue * getPropertyChangesTarget(Node *node, LookupContext::Ptr lookupContext)
+{
+    UiObjectInitializer *initializer = 0;
+    if (UiObjectDefinition *definition = cast<UiObjectDefinition *>(node))
+        initializer = definition->initializer;
+    if (UiObjectBinding *binding = cast<UiObjectBinding *>(node))
+        initializer = binding->initializer;
+    if (initializer) {
+        for (UiObjectMemberList *members = initializer->members; members; members = members->next) {
+            if (UiScriptBinding *scriptBinding = cast<UiScriptBinding *>(members->member)) {
+                if (scriptBinding->qualifiedId
+                    && scriptBinding->qualifiedId->name->asString() == QLatin1String("target")
+                    && ! scriptBinding->qualifiedId->next) {
+                    if (ExpressionStatement *expressionStatement = cast<ExpressionStatement *>(scriptBinding->statement)) {
+                        Evaluate evaluator(lookupContext->context());
+                        const Interpreter::Value *targetValue = evaluator(expressionStatement->expression);
+                        if (const Interpreter::ObjectValue *targetObject = Interpreter::value_cast<const Interpreter::ObjectValue *>(targetValue)) {
+                            return targetObject;
+                        } else {
+                            return 0;
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
+
 QuickToolBar::QuickToolBar(QObject *parent) : ::QmlJS::IContextPane(parent), m_blockWriting(false)
 {
     m_node = 0;
@@ -92,6 +121,15 @@ void QuickToolBar::apply(TextEditor::BaseTextEditorEditable *editor, Document::P
         scopeObject =  scopeObject->prototype(lookupContext->context());
     }
 
+    if (prototypes.contains("PropertyChanges")) {
+        const Interpreter::ObjectValue *targetObject = getPropertyChangesTarget(node, lookupContext);
+        prototypes.clear();
+        while (targetObject) {
+            prototypes.append(targetObject->className());
+            targetObject =  targetObject->prototype(lookupContext->context());
+        }
+    }
+
     setEnabled(doc->isParsedCorrectly());
     m_editor = editor;
     contextWidget()->setParent(editor->widget()->parentWidget());
@@ -190,6 +228,15 @@ bool QuickToolBar::isAvailable(TextEditor::BaseTextEditorEditable *, Document::P
             scopeObject =  scopeObject->prototype(lookupContext->context());
         }
 
+        if (prototypes.contains("PropertyChanges")) {
+            const Interpreter::ObjectValue *targetObject = getPropertyChangesTarget(node, lookupContext);
+            prototypes.clear();
+            while (targetObject) {
+                prototypes.append(targetObject->className());
+                targetObject =  targetObject->prototype(lookupContext->context());
+            }
+        }
+
         if (prototypes.contains("Rectangle") ||
             prototypes.contains("Image") ||
             prototypes.contains("BorderImage") ||
-- 
GitLab