Commit ccd8b92c authored by Thomas Hartmann's avatar Thomas Hartmann
Browse files

QmlDesigner.PropertyEditor: move template definitions from C++ to QML



Moving the template defintions for property types from C++ to QML.
This allows supporting more types by just editing TemplateTypes.qml.

Change-Id: Ic93a36067ac4a5e2736005b762b4ca4b7beb7cf7
Reviewed-by: default avatarMarco Bubke <marco.bubke@digia.com>
parent abb7bb78
QWidget {
layout: HorizontalLayout {
Label {
text: "%1"
toolTip: "%1"
}
CheckBox {
text: backendValues.%2.value
backendValue: backendValues.%2
baseStateFlag: isBaseState
checkable: true
}
}
}
\ No newline at end of file
ColorGroupBox {
caption: "%1"
finished: finishedNotify
backendColor: backendValues.%2
}
\ No newline at end of file
IntEditor {
backendValue: backendValues.%2
caption: "%1"
baseStateFlag: isBaseState
slider: false
}
\ No newline at end of file
DoubleSpinBoxAlternate {
text: "%1"
backendValue: backendValues.%2
baseStateFlag: isBaseState
}
\ No newline at end of file
QWidget {
layout: HorizontalLayout {
Label {
text: "%1"
toolTip: "%1"
}
LineEdit {
backendValue: backendValues.%2
baseStateFlag: isBaseState
}
}
}
\ No newline at end of file
AutoTypes {
imports: [ "import HelperWidgets 1.0", "import QtQuick 1.0", "import Bauhaus 1.0" ]
Type {
typeNames: ["int"]
sourceFile: "IntEditorTemplate.qml"
}
Type {
typeNames: ["real", "double", "qreal"]
sourceFile: "RealEditorTemplate.qml"
}
Type {
typeNames: ["string", "QString", "QUrl", "url"]
sourceFile: "StringEditorTemplate.qml"
}
Type {
typeNames: ["bool", "boolean"]
sourceFile: "BooleanEditorTemplate.qml"
}
Type {
typeNames: ["color", "QColor"]
sourceFile: "ColorEditorTemplate.qml"
}
}
......@@ -55,6 +55,7 @@
#include "propertyeditortransaction.h"
#include "originwidget.h"
#include <qmljs/qmljssimplereader.h>
#include <utils/fileutils.h>
#include <QCoreApplication>
......@@ -95,6 +96,11 @@ static inline QString sharedDirPath()
return QFileInfo(appPath + SHARE_PATH).absoluteFilePath();
}
static inline QString propertyTemplatesPath()
{
return sharedDirPath() + QLatin1String("/propertyeditor/PropertyTemplates/");
}
static QObject *variantToQObject(const QVariant &v)
{
if (v.userType() == QMetaType::QObjectStar || v.userType() > QMetaType::User)
......@@ -103,6 +109,32 @@ static QObject *variantToQObject(const QVariant &v)
return 0;
}
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() << PropertyEditor::tr("template defitions:") << reader.errors();
}
}
return s_templateConfiguration;
}
QStringList variantToStringList(const QVariant &variant) {
QStringList stringList;
foreach (const QVariant &singleValue, variant.toList())
stringList << singleValue.toString();
return stringList;
}
PropertyEditor::NodeType::NodeType(PropertyEditor *propertyEditor) :
m_view(new DeclarativeWidgetView), m_propertyEditorTransaction(new PropertyEditorTransaction(propertyEditor)), m_dummyPropertyEditorValue(new PropertyEditorValue()),
m_contextObject(new PropertyEditorContextObject())
......@@ -604,6 +636,7 @@ void PropertyEditor::setQmlDir(const QString &qmlDir)
{
m_qmlDir = qmlDir;
QFileSystemWatcher *watcher = new QFileSystemWatcher(this);
watcher->addPath(m_qmlDir);
connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(reloadQml()));
......@@ -624,7 +657,12 @@ void PropertyEditor::timerEvent(QTimerEvent *timerEvent)
QString templateGeneration(NodeMetaInfo type, NodeMetaInfo superType, const QmlObjectNode &objectNode)
{
QString qmlTemplate = QLatin1String("import QtQuick 1.0\nimport Bauhaus 1.0\n");
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(objectNode.modelNode().simplifiedTypeName());
qmlTemplate += QLatin1String("layout: VerticalLayout {\n");
......@@ -645,40 +683,23 @@ QString templateGeneration(NodeMetaInfo type, NodeMetaInfo superType, const QmlO
QString typeName = type.propertyTypeName(name);
//alias resolution only possible with instance
if (typeName == QLatin1String("alias") && objectNode.isValid())
typeName = objectNode.instanceType(name);
if (typeName == QLatin1String("alias") && objectNode.isValid())
typeName = objectNode.instanceType(name);
if (!superType.hasProperty(name) && type.propertyIsWritable(name)) {
if (typeName == "int") {
qmlTemplate += QString(QLatin1String(
"IntEditor { backendValue: backendValues.%2\n caption: \"%1\"\nbaseStateFlag: isBaseState\nslider: false\n}"
)).arg(name).arg(properName);
emptyTemplate = false;
}
if (typeName == "real" || typeName == "double" || typeName == "qreal") {
qmlTemplate += QString(QLatin1String(
"DoubleSpinBoxAlternate {\ntext: \"%1\"\nbackendValue: backendValues.%2\nbaseStateFlag: isBaseState\n}\n"
)).arg(name).arg(properName);
emptyTemplate = false;
}
if (typeName == "string" || typeName == "QString" || typeName == "QUrl" || typeName == "url") {
qmlTemplate += QString(QLatin1String(
"QWidget {\nlayout: HorizontalLayout {\nLabel {\ntext: \"%1\"\ntoolTip: \"%1\"\n}\nLineEdit {\nbackendValue: backendValues.%2\nbaseStateFlag: isBaseState\n}\n}\n}\n"
)).arg(name).arg(properName);
emptyTemplate = false;
}
if (typeName == "bool" || typeName == "boolean") {
qmlTemplate += QString(QLatin1String(
"QWidget {\nlayout: HorizontalLayout {\nLabel {\ntext: \"%1\"\ntoolTip: \"%1\"\n}\nCheckBox {text: backendValues.%2.value\nbackendValue: backendValues.%2\nbaseStateFlag: isBaseState\ncheckable: true\n}\n}\n}\n"
)).arg(name).arg(properName);
emptyTemplate = false;
}
if (typeName == "color" || typeName == "QColor") {
qmlTemplate += QString(QLatin1String(
"ColorGroupBox {\ncaption: \"%1\"\nfinished: finishedNotify\nbackendColor: backendValues.%2\n}\n\n"
)).arg(name).arg(properName);
emptyTemplate = false;
}
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(name).arg(properName);
emptyTemplate = false;
} else {
qWarning() << PropertyEditor::tr("template defition source file not found:") << fileName;
}
}
}
}
qmlTemplate += QLatin1String("}\n"); //VerticalLayout
......
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