From a01f70c089c27b8f7d1a331db900beed92aecaa0 Mon Sep 17 00:00:00 2001
From: Thomas Hartmann <Thomas.Hartmann@digia.com>
Date: Mon, 12 Aug 2013 18:05:24 +0200
Subject: [PATCH] QmlDesigner.PropertyEditorView: moving template generation

Moving template generation from PropertyEditorView to
PropertyEditorQmlBackend.

Change-Id: I8c7d2c46863544b98b203cc690bd15f4f4653f09
Reviewed-by: Marco Bubke <marco.bubke@digia.com>
---
 .../propertyeditorqmlbackend.cpp              | 89 +++++++++++++++++++
 .../propertyeditor/propertyeditorqmlbackend.h |  2 +
 .../propertyeditor/propertyeditorview.cpp     | 89 +------------------
 3 files changed, 92 insertions(+), 88 deletions(-)

diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp
index 3c2d005ace4..98a7c2bcae9 100644
--- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp
+++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp
@@ -40,6 +40,8 @@
 #include <QApplication>
 #include <QFileInfo>
 
+#include <qmljs/qmljssimplereader.h>
+
 #ifdef Q_OS_WIN
 #include <utils/winutils.h>
 #endif
@@ -48,6 +50,36 @@ enum {
     debug = false
 };
 
+static QmlJS::SimpleReaderNode::Ptr s_templateConfiguration;
+
+static inline QString propertyTemplatesPath()
+{
+    return QmlDesigner::PropertyEditorQmlBackend::propertyEditorResourcesPath() + QLatin1String("/PropertyTemplates/");
+}
+
+QmlJS::SimpleReaderNode::Ptr templateConfiguration()
+{
+    if (!s_templateConfiguration) {
+        QmlJS::SimpleReader reader;
+        const QString fileName = propertyTemplatesPath() + QLatin1String("TemplateTypes.qml");
+        s_templateConfiguration = reader.readFile(fileName);
+
+        if (!s_templateConfiguration)
+            qWarning().nospace() << "template definitions:" << reader.errors();
+    }
+
+    return s_templateConfiguration;
+}
+
+QStringList variantToStringList(const QVariant &variant) {
+    QStringList stringList;
+
+    foreach (const QVariant &singleValue, variant.toList())
+        stringList << singleValue.toString();
+
+    return stringList;
+}
+
 static QObject *variantToQObject(const QVariant &value)
 {
     if (value.userType() == QMetaType::QObjectStar || value.userType() > QMetaType::User)
@@ -310,4 +342,61 @@ QString PropertyEditorQmlBackend::propertyEditorResourcesPath() {
     return sharedDirPath() + QLatin1String("/propertyeditor");
 }
 
+QString PropertyEditorQmlBackend::templateGeneration(NodeMetaInfo type,
+                                                     NodeMetaInfo superType,
+                                                     const QmlObjectNode &objectNode)
+{
+    if (!templateConfiguration() && templateConfiguration()->isValid())
+        return QString();
+
+    QStringList imports = variantToStringList(templateConfiguration()->property(QLatin1String("imports")));
+
+    QString qmlTemplate = imports.join(QLatin1String("\n")) + QLatin1Char('\n');
+    qmlTemplate += QLatin1String("GroupBox {\n");
+    qmlTemplate += QString(QLatin1String("caption: \"%1\"\n")).arg(QString::fromUtf8(objectNode.modelNode().simplifiedTypeName()));
+    qmlTemplate += QLatin1String("layout: VerticalLayout {\n");
+
+    QList<PropertyName> orderedList = type.propertyNames();
+    qSort(orderedList);
+
+    bool emptyTemplate = true;
+
+    foreach (const PropertyName &name, orderedList) {
+
+        if (name.startsWith("__"))
+            continue; //private API
+        PropertyName properName = name;
+
+        properName.replace('.', '_');
+
+        QString typeName = type.propertyTypeName(name);
+        //alias resolution only possible with instance
+        if (typeName == QLatin1String("alias") && objectNode.isValid())
+            typeName = objectNode.instanceType(name);
+
+        if (!superType.hasProperty(name) && type.propertyIsWritable(name) && !name.contains(".")) {
+            foreach (const QmlJS::SimpleReaderNode::Ptr &node, templateConfiguration()->children())
+                if (variantToStringList(node->property(QLatin1String("typeNames"))).contains(typeName)) {
+                    const QString fileName = propertyTemplatesPath() + node->property(QLatin1String("sourceFile")).toString();
+                    QFile file(fileName);
+                    if (file.open(QIODevice::ReadOnly)) {
+                        QString source = file.readAll();
+                        file.close();
+                        qmlTemplate += source.arg(QString::fromUtf8(name)).arg(QString::fromUtf8(properName));
+                        emptyTemplate = false;
+                    } else {
+                        qWarning().nospace() << "template definition source file not found:" << fileName;
+                    }
+                }
+        }
+    }
+    qmlTemplate += QLatin1String("}\n"); //VerticalLayout
+    qmlTemplate += QLatin1String("}\n"); //GroupBox
+
+    if (emptyTemplate)
+        return QString();
+
+    return qmlTemplate;
+}
+
 } //QmlDesigner
diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.h b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.h
index 97a78bf70c3..d9ef7347b01 100644
--- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.h
+++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.h
@@ -64,6 +64,8 @@ public:
 
     static QString propertyEditorResourcesPath();
 
+    static QString templateGeneration(NodeMetaInfo type, NodeMetaInfo superType, const QmlObjectNode &objectNode);
+
 private:
     void createPropertyEditorValue(const QmlObjectNode &qmlObjectNode,
                                    const PropertyName &name, const QVariant &value,
diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp
index 4260ddafdfa..034cf16589a 100644
--- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp
+++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp
@@ -55,7 +55,6 @@
 #include "propertyeditortransaction.h"
 #include "originwidget.h"
 
-#include <qmljs/qmljssimplereader.h>
 #include <utils/fileutils.h>
 
 #include <QCoreApplication>
@@ -77,92 +76,6 @@ const char resourcePropertyEditorPath[] = ":/propertyeditor";
 
 namespace QmlDesigner {
 
-static inline QString propertyTemplatesPath()
-{
-    return PropertyEditorQmlBackend::propertyEditorResourcesPath() + QLatin1String("/PropertyTemplates/");
-}
-
-static QmlJS::SimpleReaderNode::Ptr s_templateConfiguration;
-
-QmlJS::SimpleReaderNode::Ptr templateConfiguration()
-{
-    if (!s_templateConfiguration) {
-        QmlJS::SimpleReader reader;
-        const QString fileName = propertyTemplatesPath() + QLatin1String("TemplateTypes.qml");
-        s_templateConfiguration = reader.readFile(fileName);
-
-        if (!s_templateConfiguration)
-            qWarning().nospace() << "template definitions:" << reader.errors();
-    }
-
-    return s_templateConfiguration;
-}
-
-QStringList variantToStringList(const QVariant &variant) {
-    QStringList stringList;
-
-    foreach (const QVariant &singleValue, variant.toList())
-        stringList << singleValue.toString();
-
-    return stringList;
-}
-
-QString templateGeneration(NodeMetaInfo type, NodeMetaInfo superType, const QmlObjectNode &objectNode)
-{
-    if (!templateConfiguration() && templateConfiguration()->isValid())
-        return QString();
-
-    QStringList imports = variantToStringList(templateConfiguration()->property(QLatin1String("imports")));
-
-    QString qmlTemplate = imports.join(QLatin1String("\n")) + QLatin1Char('\n');
-    qmlTemplate += QLatin1String("GroupBox {\n");
-    qmlTemplate += QString(QLatin1String("caption: \"%1\"\n")).arg(QString::fromUtf8(objectNode.modelNode().simplifiedTypeName()));
-    qmlTemplate += QLatin1String("layout: VerticalLayout {\n");
-
-    QList<PropertyName> orderedList = type.propertyNames();
-    qSort(orderedList);
-
-    bool emptyTemplate = true;
-
-    foreach (const PropertyName &name, orderedList) {
-
-        if (name.startsWith("__"))
-            continue; //private API
-        PropertyName properName = name;
-
-        properName.replace('.', '_');
-
-        QString typeName = type.propertyTypeName(name);
-        //alias resolution only possible with instance
-        if (typeName == QLatin1String("alias") && objectNode.isValid())
-            typeName = objectNode.instanceType(name);
-
-        if (!superType.hasProperty(name) && type.propertyIsWritable(name) && !name.contains(".")) {
-            foreach (const QmlJS::SimpleReaderNode::Ptr &node, templateConfiguration()->children())
-                if (variantToStringList(node->property(QLatin1String("typeNames"))).contains(typeName)) {
-                    const QString fileName = propertyTemplatesPath() + node->property(QLatin1String("sourceFile")).toString();
-                    QFile file(fileName);
-                    if (file.open(QIODevice::ReadOnly)) {
-                        QString source = file.readAll();
-                        file.close();
-                        qmlTemplate += source.arg(QString::fromUtf8(name)).arg(QString::fromUtf8(properName));
-                        emptyTemplate = false;
-                    } else {
-                        qWarning().nospace() << "template definition source file not found:" << fileName;
-                    }
-                }
-        }
-    }
-    qmlTemplate += QLatin1String("}\n"); //VerticalLayout
-    qmlTemplate += QLatin1String("}\n"); //GroupBox
-
-    if (emptyTemplate)
-        return QString();
-
-    return qmlTemplate;
-}
-
-
 PropertyEditorView::PropertyEditorView(QWidget *parent) :
         AbstractView(parent),
         m_parent(parent),
@@ -507,7 +420,7 @@ void PropertyEditorView::resetView()
 
     if (m_selectedNode.isValid() && m_selectedNode.metaInfo().isValid() && diffClassName != m_selectedNode.type()) {
         //do magic !!
-        specificQmlData = templateGeneration(m_selectedNode.metaInfo(), model()->metaInfo(diffClassName), m_selectedNode);
+        specificQmlData = PropertyEditorQmlBackend::templateGeneration(m_selectedNode.metaInfo(), model()->metaInfo(diffClassName), m_selectedNode);
     }
 
     PropertyEditorQmlBackend *type = m_typeHash.value(qmlFile.toString());
-- 
GitLab