Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
No related merge requests found
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 @@ ...@@ -55,6 +55,7 @@
#include "propertyeditortransaction.h" #include "propertyeditortransaction.h"
#include "originwidget.h" #include "originwidget.h"
#include <qmljs/qmljssimplereader.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <QCoreApplication> #include <QCoreApplication>
...@@ -95,6 +96,11 @@ static inline QString sharedDirPath() ...@@ -95,6 +96,11 @@ static inline QString sharedDirPath()
return QFileInfo(appPath + SHARE_PATH).absoluteFilePath(); return QFileInfo(appPath + SHARE_PATH).absoluteFilePath();
} }
static inline QString propertyTemplatesPath()
{
return sharedDirPath() + QLatin1String("/propertyeditor/PropertyTemplates/");
}
static QObject *variantToQObject(const QVariant &v) static QObject *variantToQObject(const QVariant &v)
{ {
if (v.userType() == QMetaType::QObjectStar || v.userType() > QMetaType::User) if (v.userType() == QMetaType::QObjectStar || v.userType() > QMetaType::User)
...@@ -103,6 +109,32 @@ static QObject *variantToQObject(const QVariant &v) ...@@ -103,6 +109,32 @@ static QObject *variantToQObject(const QVariant &v)
return 0; 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) : PropertyEditor::NodeType::NodeType(PropertyEditor *propertyEditor) :
m_view(new DeclarativeWidgetView), m_propertyEditorTransaction(new PropertyEditorTransaction(propertyEditor)), m_dummyPropertyEditorValue(new PropertyEditorValue()), m_view(new DeclarativeWidgetView), m_propertyEditorTransaction(new PropertyEditorTransaction(propertyEditor)), m_dummyPropertyEditorValue(new PropertyEditorValue()),
m_contextObject(new PropertyEditorContextObject()) m_contextObject(new PropertyEditorContextObject())
...@@ -604,6 +636,7 @@ void PropertyEditor::setQmlDir(const QString &qmlDir) ...@@ -604,6 +636,7 @@ void PropertyEditor::setQmlDir(const QString &qmlDir)
{ {
m_qmlDir = qmlDir; m_qmlDir = qmlDir;
QFileSystemWatcher *watcher = new QFileSystemWatcher(this); QFileSystemWatcher *watcher = new QFileSystemWatcher(this);
watcher->addPath(m_qmlDir); watcher->addPath(m_qmlDir);
connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(reloadQml())); connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(reloadQml()));
...@@ -624,7 +657,12 @@ void PropertyEditor::timerEvent(QTimerEvent *timerEvent) ...@@ -624,7 +657,12 @@ void PropertyEditor::timerEvent(QTimerEvent *timerEvent)
QString templateGeneration(NodeMetaInfo type, NodeMetaInfo superType, const QmlObjectNode &objectNode) 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 += QLatin1String("GroupBox {\n");
qmlTemplate += QString(QLatin1String("caption: \"%1\"\n")).arg(objectNode.modelNode().simplifiedTypeName()); qmlTemplate += QString(QLatin1String("caption: \"%1\"\n")).arg(objectNode.modelNode().simplifiedTypeName());
qmlTemplate += QLatin1String("layout: VerticalLayout {\n"); qmlTemplate += QLatin1String("layout: VerticalLayout {\n");
...@@ -645,40 +683,23 @@ QString templateGeneration(NodeMetaInfo type, NodeMetaInfo superType, const QmlO ...@@ -645,40 +683,23 @@ QString templateGeneration(NodeMetaInfo type, NodeMetaInfo superType, const QmlO
QString typeName = type.propertyTypeName(name); QString typeName = type.propertyTypeName(name);
//alias resolution only possible with instance //alias resolution only possible with instance
if (typeName == QLatin1String("alias") && objectNode.isValid()) if (typeName == QLatin1String("alias") && objectNode.isValid())
typeName = objectNode.instanceType(name); typeName = objectNode.instanceType(name);
if (!superType.hasProperty(name) && type.propertyIsWritable(name)) { if (!superType.hasProperty(name) && type.propertyIsWritable(name)) {
if (typeName == "int") { foreach (const QmlJS::SimpleReaderNode::Ptr &node, templateConfiguration()->children())
qmlTemplate += QString(QLatin1String( if (variantToStringList(node->property(QLatin1String("typeNames"))).contains(typeName)) {
"IntEditor { backendValue: backendValues.%2\n caption: \"%1\"\nbaseStateFlag: isBaseState\nslider: false\n}" const QString fileName = propertyTemplatesPath() + node->property(QLatin1String("sourceFile")).toString();
)).arg(name).arg(properName); QFile file(fileName);
emptyTemplate = false; if (file.open(QIODevice::ReadOnly)) {
} QString source = file.readAll();
if (typeName == "real" || typeName == "double" || typeName == "qreal") { file.close();
qmlTemplate += QString(QLatin1String( qmlTemplate += source.arg(name).arg(properName);
"DoubleSpinBoxAlternate {\ntext: \"%1\"\nbackendValue: backendValues.%2\nbaseStateFlag: isBaseState\n}\n" emptyTemplate = false;
)).arg(name).arg(properName); } else {
emptyTemplate = false; qWarning() << PropertyEditor::tr("template defition source file not found:") << fileName;
} }
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;
}
} }
} }
qmlTemplate += QLatin1String("}\n"); //VerticalLayout qmlTemplate += QLatin1String("}\n"); //VerticalLayout
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment