From 84cbbb41007af45e9c346d22f08d6b335ac52076 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Tue, 26 May 2020 17:02:11 +0300 Subject: [PATCH] QmlDesigner: Add support for colors defined with vector3d property Some QtQuick3D effects specify colors as vector3d. Added support for vector3d colors to ColorEditor. Support can be enabled by setting isVector3D property to true. The colors are still displayed as regular "#ffffff" style color strings in the ColorEditor. Task-number: QDS-2114 Change-Id: If62152b0351e452d753eb6da9d90cd59fe2c4c59 Reviewed-by: Thomas Hartmann --- .../imports/HelperWidgets/ColorEditor.qml | 32 ++++++++++++++++--- .../propertyeditorcontextobject.cpp | 19 ++++++++--- .../propertyeditorcontextobject.h | 2 +- .../propertyeditor/propertyeditorview.cpp | 4 ++- .../designercore/metainfo/nodemetainfo.cpp | 3 ++ .../designercore/model/qmltextgenerator.cpp | 5 +++ .../designercore/model/variantproperty.cpp | 3 +- 7 files changed, 55 insertions(+), 13 deletions(-) diff --git a/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/ColorEditor.qml b/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/ColorEditor.qml index 536877b58ed..8c12d37bb57 100644 --- a/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/ColorEditor.qml +++ b/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/ColorEditor.qml @@ -42,7 +42,13 @@ Column { property variant backendValue - property variant value: backendValue.value + property variant value: { + if (isVector3D) + return Qt.rgba(backendValue.value.x, backendValue.value.y, backendValue.value.z, 1); + else + return backendValue.value; + + } property alias gradientPropertyName: gradientLine.gradientPropertyName @@ -52,6 +58,8 @@ Column { property color originalColor + property bool isVector3D: false + function isNotInGradientMode() { return (buttonRow.checkedIndex === 0 || transparent) } @@ -70,8 +78,15 @@ Column { interval: 100 running: false onTriggered: { - if (backendValue !== undefined) - backendValue.value = colorEditor.color + if (colorEditor.backendValue !== undefined) { + if (isVector3D) { + colorEditor.backendValue.value = Qt.vector3d(colorEditor.color.r, + colorEditor.color.g, + colorEditor.color.b); + } else { + colorEditor.backendValue.value = colorEditor.color; + } + } } } @@ -231,8 +246,15 @@ Column { onCommitData: { colorEditor.color = colorFromString(textField.text) - if (isNotInGradientMode()) - backendValue.value = colorEditor.color + if (isNotInGradientMode()) { + if (colorEditor.isVector3D) { + backendValue.value = Qt.vector3d(colorEditor.color.r, + colorEditor.color.g, + colorEditor.color.b); + } else { + backendValue.value = colorEditor.color; + } + } } Layout.fillWidth: true diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorcontextobject.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorcontextobject.cpp index 1a52a639a7e..a479dfeba0b 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorcontextobject.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorcontextobject.cpp @@ -98,13 +98,22 @@ PropertyEditorContextObject::PropertyEditorContextObject(QObject *parent) : } -QString PropertyEditorContextObject::convertColorToString(const QColor &color) +QString PropertyEditorContextObject::convertColorToString(const QVariant &color) { - QString colorString = color.name(); + QString colorString; + QColor theColor; + if (color.canConvert(QVariant::Color)) { + theColor = color.value(); + } else if (color.canConvert(QVariant::Vector3D)) { + auto vec = color.value(); + theColor = QColor::fromRgbF(vec.x(), vec.y(), vec.z()); + } - if (color.alpha() != 255) { - QString hexAlpha = QString("%1").arg(color.alpha(), 2, 16, QLatin1Char('0')); - colorString.remove(0,1); + colorString = theColor.name(); + + if (theColor.alpha() != 255) { + QString hexAlpha = QString("%1").arg(theColor.alpha(), 2, 16, QLatin1Char('0')); + colorString.remove(0, 1); colorString.prepend(hexAlpha); colorString.prepend(QStringLiteral("#")); } diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorcontextobject.h b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorcontextobject.h index 03d82dbc340..35c909192b0 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorcontextobject.h +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorcontextobject.h @@ -78,7 +78,7 @@ public: QQmlPropertyMap* backendValues() const { return m_backendValues; } - Q_INVOKABLE QString convertColorToString(const QColor &color); + Q_INVOKABLE QString convertColorToString(const QVariant &color); Q_INVOKABLE QColor colorFromString(const QString &colorString); Q_INVOKABLE QString translateFunction(); diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp index f097f266e5c..719ad8d8ec9 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp @@ -225,7 +225,9 @@ void PropertyEditorView::changeValue(const QString &name) if (!value->value().isValid()) { //reset removePropertyFromModel(propertyName); } else { - if (castedValue.isValid() && !castedValue.isNull()) { + // QVector3D(0, 0, 0) detects as null variant though it is valid value + if (castedValue.isValid() && (!castedValue.isNull() + || castedValue.type() == QVariant::Vector3D)) { commitVariantValueToModel(propertyName, castedValue); } } diff --git a/src/plugins/qmldesigner/designercore/metainfo/nodemetainfo.cpp b/src/plugins/qmldesigner/designercore/metainfo/nodemetainfo.cpp index 4abf7d28023..73fca899526 100644 --- a/src/plugins/qmldesigner/designercore/metainfo/nodemetainfo.cpp +++ b/src/plugins/qmldesigner/designercore/metainfo/nodemetainfo.cpp @@ -1067,6 +1067,9 @@ QVariant::Type NodeMetaInfoPrivate::variantTypeId(const PropertyName &propertyNa if (typeName == "var") return QVariant::UserType; + if (typeName == "vector3d") + return QVariant::Vector3D; + return QVariant::nameToType(typeName.data()); } diff --git a/src/plugins/qmldesigner/designercore/model/qmltextgenerator.cpp b/src/plugins/qmldesigner/designercore/model/qmltextgenerator.cpp index 8ab244fe2ba..6e06970c76b 100644 --- a/src/plugins/qmldesigner/designercore/model/qmltextgenerator.cpp +++ b/src/plugins/qmldesigner/designercore/model/qmltextgenerator.cpp @@ -27,6 +27,7 @@ #include #include +#include #include "bindingproperty.h" #include "signalhandlerproperty.h" @@ -149,6 +150,10 @@ QString QmlTextGenerator::toQml(const AbstractProperty &property, int indentDept case QMetaType::QString: case QMetaType::QChar: return QStringLiteral("\"%1\"").arg(escape(unicodeEscape(stringValue))); + case QMetaType::QVector3D: { + auto vec = value.value(); + return QStringLiteral("Qt.vector3d(%1, %2, %3)").arg(vec.x()).arg(vec.y()).arg(vec.z()); + } default: return QStringLiteral("\"%1\"").arg(escape(stringValue)); } diff --git a/src/plugins/qmldesigner/designercore/model/variantproperty.cpp b/src/plugins/qmldesigner/designercore/model/variantproperty.cpp index 03c4f5acbf4..c067b111d93 100644 --- a/src/plugins/qmldesigner/designercore/model/variantproperty.cpp +++ b/src/plugins/qmldesigner/designercore/model/variantproperty.cpp @@ -57,7 +57,8 @@ void VariantProperty::setValue(const QVariant &value) if (isDynamic()) qWarning() << "Calling VariantProperty::setValue on dynamic property."; - if (value.isNull()) + // QVector3D(0, 0, 0) detects as null variant though it is valid value + if (value.isNull() && value.type() != QVariant::Vector3D) throw InvalidArgumentException(__LINE__, __FUNCTION__, __FILE__, name()); if (internalNode()->hasProperty(name())) { //check if oldValue != value