From 5b693cdac07d0a4bc14d35d89e5b21fa70ae687c Mon Sep 17 00:00:00 2001
From: Thomas Hartmann <Thomas.Hartmann@digia.com>
Date: Wed, 13 Nov 2013 17:28:16 +0100
Subject: [PATCH] QmlDesigner.ItemLibrary: Allow bindings without extra source
 file

This makes drag and drop faster than merging code.

Task-number: QTCREATORBUG-9361
Change-Id: If9779d7439ea41edfb80c08161b7ef3d0f08cf41
Reviewed-by: Thomas Hartmann <Thomas.Hartmann@digia.com>
---
 .../componentsplugin/components.metainfo      | 10 +++----
 .../designercore/model/qmlitemnode.cpp        | 15 +++++++++--
 .../qmldesigner/qtquickplugin/quick.metainfo  | 27 ++++++++++++++-----
 3 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/src/plugins/qmldesigner/componentsplugin/components.metainfo b/src/plugins/qmldesigner/componentsplugin/components.metainfo
index af484dda6b3..cbd5a5835b2 100644
--- a/src/plugins/qmldesigner/componentsplugin/components.metainfo
+++ b/src/plugins/qmldesigner/componentsplugin/components.metainfo
@@ -11,7 +11,7 @@ MetaInfo {
             version: "1.0"
             requiredImport: "QtQuick.Controls"
 
-            Property { name: "text"; type: "QString"; value: "Button"; }
+            Property { name: "text"; type: "binding"; value: "qsTr(\"Button\")"; }
         }
     }
 
@@ -39,7 +39,7 @@ MetaInfo {
             version: "1.0"
             requiredImport: "QtQuick.Controls"
 
-            Property { name: "text"; type: "QString"; value: "Check Box"; }
+            Property { name: "text"; type: "binding"; value: "qsTr(\"Check Box\")"; }
         }
     }
 
@@ -54,7 +54,7 @@ MetaInfo {
             version: "1.0"
             requiredImport: "QtQuick.Controls"
 
-            Property { name: "text"; type: "QString"; value: "Radio Button"; }
+            Property { name: "text"; type: "binding"; value: "qsTr(\"Radio Button\")"; }
         }
     }
 
@@ -108,7 +108,7 @@ MetaInfo {
             version: "1.0"
             requiredImport: "QtQuick.Controls"
 
-            Property { name: "text"; type: "QString"; value: "Label"; }
+            Property { name: "text"; type: "binding"; value: "qsTr(\"Label\")"; }
         }
     }
 
@@ -123,7 +123,7 @@ MetaInfo {
             version: "1.0"
             requiredImport: "QtQuick.Controls"
 
-            Property { name: "placeholderText"; type: "QString"; value: "Text Field"; }
+            Property { name: "placeholderText"; type: "binding"; value: "qsTr(\"Text Field\")"; }
         }
     }
 
diff --git a/src/plugins/qmldesigner/designercore/model/qmlitemnode.cpp b/src/plugins/qmldesigner/designercore/model/qmlitemnode.cpp
index 2c883dbcb7a..32ae0d81594 100644
--- a/src/plugins/qmldesigner/designercore/model/qmlitemnode.cpp
+++ b/src/plugins/qmldesigner/designercore/model/qmlitemnode.cpp
@@ -32,6 +32,7 @@
 #include "qmlchangeset.h"
 #include "nodelistproperty.h"
 #include "variantproperty.h"
+#include "bindingproperty.h"
 #include "qmlanchors.h"
 #include "invalidmodelnodeexception.h"
 #include "itemlibraryinfo.h"
@@ -134,13 +135,20 @@ QmlItemNode QmlItemNode::createQmlItemNode(AbstractView *view, const ItemLibrary
             }
         }
 
+        typedef QPair<PropertyName, QString> PropertyBindingEntry;
+        QList<PropertyBindingEntry> propertyBindingList;
         if (itemLibraryEntry.qmlSource().isEmpty()) {
             QList<QPair<PropertyName, QVariant> > propertyPairList;
             propertyPairList.append(qMakePair(PropertyName("x"), QVariant(qRound(position.x()))));
             propertyPairList.append(qMakePair(PropertyName("y"), QVariant(qRound(position.y()))));
 
-            foreach (const PropertyContainer &property, itemLibraryEntry.properties())
-                propertyPairList.append(qMakePair(property.name(), property.value()));
+            foreach (const PropertyContainer &property, itemLibraryEntry.properties()) {
+                if (property.type() == QLatin1String("binding")) {
+                    propertyBindingList.append(PropertyBindingEntry(property.name(), property.value().toString()));
+                } else {
+                    propertyPairList.append(qMakePair(property.name(), property.value()));
+                }
+            }
 
             newQmlItemNode = QmlItemNode(view->createModelNode(itemLibraryEntry.typeName(), majorVersion, minorVersion, propertyPairList));
         } else {
@@ -160,6 +168,9 @@ QmlItemNode QmlItemNode::createQmlItemNode(AbstractView *view, const ItemLibrary
             newQmlItemNode.setVariantProperty("opacity", 1);
         }
 
+        foreach (const PropertyBindingEntry &propertyBindingEntry, propertyBindingList)
+            newQmlItemNode.modelNode().bindingProperty(propertyBindingEntry.first).setExpression(propertyBindingEntry.second);
+
         Q_ASSERT(newQmlItemNode.isValid());
     }
     catch (RewritingException &e) {
diff --git a/src/plugins/qmldesigner/qtquickplugin/quick.metainfo b/src/plugins/qmldesigner/qtquickplugin/quick.metainfo
index 31a1b97a3d3..bd8a6b2dc18 100644
--- a/src/plugins/qmldesigner/qtquickplugin/quick.metainfo
+++ b/src/plugins/qmldesigner/qtquickplugin/quick.metainfo
@@ -63,7 +63,8 @@ MetaInfo {
             libraryIcon: ":/qtquickplugin/images/text-icon.png"
             version: "1.0"
 
-            QmlSource { source: ":/qtquickplugin/source/text.qml" }
+            Property { name: "font.pixelSize"; type: "int"; value: 12; }
+            Property { name: "text"; type: "binding"; value: "qsTr(\"Text\")"; }
         }
 
         ItemLibraryEntry {
@@ -72,7 +73,8 @@ MetaInfo {
             libraryIcon: ":/qtquickplugin/images/text-icon.png"
             version: "2.0"
 
-            QmlSource { source: ":/qtquickplugin/source/textv2.qml" }
+            Property { name: "font.pixelSize"; type: "int"; value: 12; }
+            Property { name: "text"; type: "binding"; value: "qsTr(\"Text\")"; }
         }
     }
 
@@ -86,7 +88,11 @@ MetaInfo {
             libraryIcon: ":/qtquickplugin/images/text-edit-icon.png"
             version: "1.0"
 
-            QmlSource { source: ":/qtquickplugin/source/textedit.qml" }
+            Property { name: "width"; type: "int"; value: 80; }
+            Property { name: "height"; type: "int"; value: 20; }
+            Property { name: "font.pixelSize"; type: "int"; value: 12; }
+            Property { name: "text"; type: "binding"; value: "qsTr(\"Text Edit\")"; }
+
         }
 
         ItemLibraryEntry {
@@ -95,7 +101,10 @@ MetaInfo {
             libraryIcon: ":/qtquickplugin/images/text-edit-icon.png"
             version: "2.0"
 
-            QmlSource { source: ":/qtquickplugin/source/texteditv2.qml" }
+            Property { name: "width"; type: "int"; value: 80; }
+            Property { name: "height"; type: "int"; value: 20; }
+            Property { name: "font.pixelSize"; type: "int"; value: 12; }
+            Property { name: "text"; type: "binding"; value: "qsTr(\"Text Edit\")"; }
         }
     }
 
@@ -109,7 +118,10 @@ MetaInfo {
             libraryIcon: ":/qtquickplugin/images/text-edit-icon.png"
             version: "1.0"
 
-            QmlSource { source: ":/qtquickplugin/source/textinput.qml" }
+            Property { name: "width"; type: "int"; value: 80; }
+            Property { name: "height"; type: "int"; value: 20; }
+            Property { name: "font.pixelSize"; type: "int"; value: 12; }
+            Property { name: "text"; type: "binding"; value: "qsTr(\"Text Input\")"; }
         }
 
         ItemLibraryEntry {
@@ -118,7 +130,10 @@ MetaInfo {
             libraryIcon: ":/qtquickplugin/images/text-input-icon.png"
             version: "2.0"
 
-            QmlSource { source: ":/qtquickplugin/source/textinput.qml" }
+            Property { name: "width"; type: "int"; value: 80; }
+            Property { name: "height"; type: "int"; value: 20; }
+            Property { name: "font.pixelSize"; type: "int"; value: 12; }
+            Property { name: "text"; type: "binding"; value: "qsTr(\"Text Input\")"; }
         }
     }
 
-- 
GitLab