Commit a01f70c0 authored by Thomas Hartmann's avatar Thomas Hartmann

QmlDesigner.PropertyEditorView: moving template generation

Moving template generation from PropertyEditorView to
PropertyEditorQmlBackend.

Change-Id: I8c7d2c46863544b98b203cc690bd15f4f4653f09
Reviewed-by: default avatarMarco Bubke <marco.bubke@digia.com>
parent eeb0dc2d
......@@ -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
......@@ -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,
......
......@@ -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());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment