diff --git a/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/BooleanEditorTemplate.qml b/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/BooleanEditorTemplate.qml new file mode 100644 index 0000000000000000000000000000000000000000..f3432a916169b8820eecc1867eae0b336c5cfacb --- /dev/null +++ b/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/BooleanEditorTemplate.qml @@ -0,0 +1,14 @@ +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 diff --git a/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/ColorEditorTemplate.qml b/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/ColorEditorTemplate.qml new file mode 100644 index 0000000000000000000000000000000000000000..e8e4f17d727676f0c980569781cdb73e29ac37c8 --- /dev/null +++ b/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/ColorEditorTemplate.qml @@ -0,0 +1,5 @@ +ColorGroupBox { + caption: "%1" + finished: finishedNotify + backendColor: backendValues.%2 +} \ No newline at end of file diff --git a/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/IntEditorTemplate.qml b/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/IntEditorTemplate.qml new file mode 100644 index 0000000000000000000000000000000000000000..c3c35be70c6869ad48298f29bf3700ca983d3943 --- /dev/null +++ b/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/IntEditorTemplate.qml @@ -0,0 +1,6 @@ +IntEditor { + backendValue: backendValues.%2 + caption: "%1" + baseStateFlag: isBaseState + slider: false +} \ No newline at end of file diff --git a/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/RealEditorTemplate.qml b/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/RealEditorTemplate.qml new file mode 100644 index 0000000000000000000000000000000000000000..cdae8e7ff81a9cbdd87207748b2677fe8dbf9b5d --- /dev/null +++ b/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/RealEditorTemplate.qml @@ -0,0 +1,5 @@ +DoubleSpinBoxAlternate { + text: "%1" + backendValue: backendValues.%2 + baseStateFlag: isBaseState +} \ No newline at end of file diff --git a/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/StringEditorTemplate.qml b/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/StringEditorTemplate.qml new file mode 100644 index 0000000000000000000000000000000000000000..5152da59d68c813438ba4b2ed8eee3de987a924d --- /dev/null +++ b/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/StringEditorTemplate.qml @@ -0,0 +1,12 @@ +QWidget { + layout: HorizontalLayout { + Label { + text: "%1" + toolTip: "%1" + } + LineEdit { + backendValue: backendValues.%2 + baseStateFlag: isBaseState + } + } +} \ No newline at end of file diff --git a/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/TemplateTypes.qml b/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/TemplateTypes.qml new file mode 100644 index 0000000000000000000000000000000000000000..fe6d831369c63b313687de7a712b26bd1eccc94f --- /dev/null +++ b/share/qtcreator/qmldesigner/propertyeditor/PropertyTemplates/TemplateTypes.qml @@ -0,0 +1,25 @@ + +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" + } +} diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.cpp index 54bb08d266fdb4a0d76d0914e45ef038296f1cb1..3dcb4176a71bf17b1e0732815a82836aefe534d1 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.cpp @@ -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