QmlDesigner: Add hint to set a property of the parent

This allows to do this in hints:

Hints {
   setParentProperty: "layer.enabled: true"
}

This will always set layer.enabled to true on the parent
if an item of this type is added. This is required for
adding effects from the item library.

Change-Id: Ic9600e0bbcde11df8d060d4a7ad05b4a590c8bea
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Thomas Hartmann
2020-10-01 16:33:44 +02:00
parent 3a0182d28e
commit 6307b094fe
3 changed files with 42 additions and 0 deletions

View File

@@ -70,6 +70,7 @@ public:
bool visibleInNavigator() const;
bool visibleInLibrary() const;
QString forceNonDefaultProperty() const;
QPair<QString, QVariant> setParentProperty() const;
QHash<QString, QString> hints() const;
static NodeHints fromModelNode(const ModelNode &modelNode);

View File

@@ -259,6 +259,37 @@ QString NodeHints::forceNonDefaultProperty() const
return Internal::evaluateExpression(expression, modelNode(), ModelNode()).toString();
}
QVariant parseValue(const QString &string)
{
if (string == "true")
return true;
if (string == "false")
return false;
bool ok = false;
double d = string.toDouble(&ok);
if (ok)
return d;
return string;
}
QPair<QString, QVariant> NodeHints::setParentProperty() const
{
const QString expression = m_hints.value("setParentProperty");
if (expression.isEmpty())
return {};
const QString str = Internal::evaluateExpression(expression, modelNode(), ModelNode()).toString();
QStringList list = str.split(":");
if (list.count() != 2)
return {};
return qMakePair(list.first().trimmed(), parseValue(list.last().trimmed()));
}
QHash<QString, QString> NodeHints::hints() const
{
return m_hints;

View File

@@ -259,6 +259,8 @@ QmlObjectNode QmlVisualNode::createQmlObjectNode(AbstractView *view,
{
QmlObjectNode newQmlObjectNode;
NodeHints hints = NodeHints::fromItemLibraryEntry(itemLibraryEntry);
auto createNodeFunc = [=, &newQmlObjectNode, &parentProperty]() {
NodeMetaInfo metaInfo = view->model()->metaInfo(itemLibraryEntry.typeName());
@@ -310,6 +312,14 @@ QmlObjectNode QmlVisualNode::createQmlObjectNode(AbstractView *view,
Q_ASSERT(newQmlObjectNode.isValid());
if (!hints.setParentProperty().first.isEmpty() && parentProperty.isValid()) {
ModelNode parent = parentProperty.parentModelNode();
const PropertyName property = hints.setParentProperty().first.toUtf8();
const QVariant value = hints.setParentProperty().second;
parent.variantProperty(property).setValue(value);
}
return newQmlObjectNode;
}