Exemple #1
0
QmlItemNode QmlItemNode::createQmlItemNode(AbstractView *view, const ItemLibraryEntry &itemLibraryEntry, const QPointF &position, NodeAbstractProperty parentproperty)
{
    QmlItemNode newQmlItemNode;

    try {
        RewriterTransaction transaction = view->beginRewriterTransaction(QByteArrayLiteral("QmlItemNode::createQmlItemNode"));

        NodeMetaInfo metaInfo = view->model()->metaInfo(itemLibraryEntry.typeName());

        int minorVersion = metaInfo.minorVersion();
        int majorVersion = metaInfo.majorVersion();

        typedef QPair<PropertyName, QString> PropertyBindingEntry;
        QList<PropertyBindingEntry> propertyBindingList;
        if (itemLibraryEntry.qmlSource().isEmpty()) {
            QList<QPair<PropertyName, QVariant> > propertyPairList;
            if (!position.isNull()) {
                propertyPairList.append(qMakePair(PropertyName("x"), QVariant(qRound(position.x()))));
                propertyPairList.append(qMakePair(PropertyName("y"), QVariant(qRound(position.y()))));
            }

            foreach (const PropertyContainer &property, itemLibraryEntry.properties()) {
                if (property.type() == QStringLiteral("binding")) {
                    propertyBindingList.append(PropertyBindingEntry(property.name(), property.value().toString()));
                } else {
                    propertyPairList.append(qMakePair(property.name(), property.value()));
                }
            }

            newQmlItemNode = QmlItemNode(view->createModelNode(itemLibraryEntry.typeName(), majorVersion, minorVersion, propertyPairList));
        } else {
            newQmlItemNode = createQmlItemNodeFromSource(view, itemLibraryEntry.qmlSource(), position);
        }

        if (parentproperty.isValid())
            parentproperty.reparentHere(newQmlItemNode);

        if (!newQmlItemNode.isValid())
            return newQmlItemNode;

        newQmlItemNode.setId(view->generateNewId(itemLibraryEntry.name()));

        if (!view->currentState().isBaseState()) {
            newQmlItemNode.modelNode().variantProperty("opacity").setValue(0);
            newQmlItemNode.setVariantProperty("opacity", 1);
        }

        foreach (const PropertyBindingEntry &propertyBindingEntry, propertyBindingList)
            newQmlItemNode.modelNode().bindingProperty(propertyBindingEntry.first).setExpression(propertyBindingEntry.second);

        Q_ASSERT(newQmlItemNode.isValid());
    }
Exemple #2
0
void ModelNode::setParentProperty(NodeAbstractProperty parent)
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

    if (!parent.parentModelNode().isValid())
        throw InvalidArgumentException(__LINE__, __FUNCTION__, __FILE__, "newParentNode");

    if (*this == parent.parentModelNode()) {
        Q_ASSERT_X(*this != parent.parentModelNode(), Q_FUNC_INFO, "cannot set parent to itself");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

    if (parent == parentProperty())
        return;

    parent.reparentHere(*this);
}