<commit_before>/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (<EMAIL>)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "objectnodeinstance.h"

#include <invalidreparentingexception.h>
#include <invalidnodeinstanceexception.h>
#include <notimplementedexception.h>
#include <noanchoringpossibleexception.h>

#include <QmlContext>
#include <QtDeclarative/private/qmlcontext_p.h>
#include <QmlList>
#include <QmlBinding>
#include <private/qmllistaccessor_p.h>

#include <metainfo.h>
#include <propertymetainfo.h>
#include <qmlmetaproperty.h>
#include <QmlMetaType>
#include <QmlEngine>
#include <private/qmlgraphicsanchors_p.h>
#include <private/qmlgraphicsrectangle_p.h>

#include "qmlgraphicsitemnodeinstance.h"
#include "graphicsobjectnodeinstance.h"
#include "graphicsviewnodeinstance.h"
#include "graphicsscenenodeinstance.h"
#include "graphicswidgetnodeinstance.h"
#include "qmlviewnodeinstance.h"
#include "widgetnodeinstance.h"
#include "proxywidgetnodeinstance.h"
#include "variantproperty.h"
#include <nodelistproperty.h>

namespace QmlDesigner {
namespace Internal {

ChildrenChangeEventFilter::ChildrenChangeEventFilter(QObject *parent)
    : QObject(parent)
{
}


bool ChildrenChangeEventFilter::eventFilter(QObject *object, QEvent *event)
{
    switch (event->type()) {
        case QEvent::ChildAdded:
        case QEvent::ChildRemoved: emit childrenChanged(object); break;
        default: break;
    }

    return false;
}

ObjectNodeInstance::ObjectNodeInstance(QObject *object)
    : m_deleteHeldInstance(true),
    m_object(object)
{
}

ObjectNodeInstance::~ObjectNodeInstance()
{
    destroy();
}

static bool isChildrenProperty(const QString &name)
{
    return name == "data" || name == "children" || name == "resources";
}

static void specialRemoveParentForQmlGraphicsItemChildren(QObject *object)
{
    QmlGraphicsItem *item = qobject_cast<QmlGraphicsItem*>(object);
    if (item && item->scene())
        item->scene()->removeItem(item);

    object->setParent(0);
}

void ObjectNodeInstance::destroy()
{
    // Remove from old property
    if (object() && modelNode().isValid() && modelNode().parentProperty().isValid()) {
        NodeAbstractProperty parentProperty = modelNode().parentProperty();
        ModelNode parentNode = parentProperty.parentModelNode();
        if (parentNode.isValid() && nodeInstanceView()->hasInstanceForNode(parentNode)) {
            NodeInstance parentInstance = nodeInstanceView()->instanceForNode(parentNode);
            if (parentInstance.isQmlGraphicsItem() && isChildrenProperty(parentProperty.name())) {
                specialRemoveParentForQmlGraphicsItemChildren(object());
            } else {
                removeFromOldProperty(object(), parentInstance.internalObject(), parentProperty.name());
            }
        }
    }

    if (deleteHeldInstance() && object()) {
        if (!object()->objectName().isEmpty()) {
            context()->engine()->rootContext()->setContextProperty(object()->objectName(), 0);
        }
        QObject *obj = object();
        m_object.clear();
        delete obj;
    }
}

ModelNode ObjectNodeInstance::modelNode() const
{
    return m_modelNode;
}

void ObjectNodeInstance::setModelNode(const ModelNode &node)
{
    m_modelNode = node;
}

NodeInstanceView *ObjectNodeInstance::nodeInstanceView() const
{
    return m_nodeInstanceView.data();
}

void ObjectNodeInstance::setNodeInstance(NodeInstanceView *view)
{
    m_nodeInstanceView = view;
}

void ObjectNodeInstance::setId(const QString &id)
{
    if (!id.isEmpty()) {
        context()->engine()->rootContext()->setContextProperty(id, object()); // will also force refresh of all bindings
    } else if (!object()->objectName().isEmpty()) {
        context()->engine()->rootContext()->setContextProperty(object()->objectName(), 0);
    }
<commit_msg>Fix id setting in the ObjectNodeInstance<commit_after>/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (<EMAIL>)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "objectnodeinstance.h"

#include <invalidreparentingexception.h>
#include <invalidnodeinstanceexception.h>
#include <notimplementedexception.h>
#include <noanchoringpossibleexception.h>

#include <QmlContext>
#include <QtDeclarative/private/qmlcontext_p.h>
#include <QmlList>
#include <QmlBinding>
#include <private/qmllistaccessor_p.h>

#include <metainfo.h>
#include <propertymetainfo.h>
#include <qmlmetaproperty.h>
#include <QmlMetaType>
#include <QmlEngine>
#include <private/qmlgraphicsanchors_p.h>
#include <private/qmlgraphicsrectangle_p.h>

#include "qmlgraphicsitemnodeinstance.h"
#include "graphicsobjectnodeinstance.h"
#include "graphicsviewnodeinstance.h"
#include "graphicsscenenodeinstance.h"
#include "graphicswidgetnodeinstance.h"
#include "qmlviewnodeinstance.h"
#include "widgetnodeinstance.h"
#include "proxywidgetnodeinstance.h"
#include "variantproperty.h"
#include <nodelistproperty.h>

namespace QmlDesigner {
namespace Internal {

ChildrenChangeEventFilter::ChildrenChangeEventFilter(QObject *parent)
    : QObject(parent)
{
}


bool ChildrenChangeEventFilter::eventFilter(QObject *object, QEvent *event)
{
    switch (event->type()) {
        case QEvent::ChildAdded:
        case QEvent::ChildRemoved: emit childrenChanged(object); break;
        default: break;
    }

    return false;
}

ObjectNodeInstance::ObjectNodeInstance(QObject *object)
    : m_deleteHeldInstance(true),
    m_object(object)
{
}

ObjectNodeInstance::~ObjectNodeInstance()
{
    destroy();
}

static bool isChildrenProperty(const QString &name)
{
    return name == "data" || name == "children" || name == "resources";
}

static void specialRemoveParentForQmlGraphicsItemChildren(QObject *object)
{
    QmlGraphicsItem *item = qobject_cast<QmlGraphicsItem*>(object);
    if (item && item->scene())
        item->scene()->removeItem(item);

    object->setParent(0);
}

void ObjectNodeInstance::destroy()
{
    // Remove from old property
    if (object() && modelNode().isValid() && modelNode().parentProperty().isValid()) {
        NodeAbstractProperty parentProperty = modelNode().parentProperty();
        ModelNode parentNode = parentProperty.parentModelNode();
        if (parentNode.isValid() && nodeInstanceView()->hasInstanceForNode(parentNode)) {
            NodeInstance parentInstance = nodeInstanceView()->instanceForNode(parentNode);
            if (parentInstance.isQmlGraphicsItem() && isChildrenProperty(parentProperty.name())) {
                specialRemoveParentForQmlGraphicsItemChildren(object());
            } else {
                removeFromOldProperty(object(), parentInstance.internalObject(), parentProperty.name());
            }
        }
    }

    if (deleteHeldInstance() && object()) {
        if (!object()->objectName().isEmpty()) {
            context()->engine()->rootContext()->setContextProperty(object()->objectName(), 0);
        }
        QObject *obj = object();
        m_object.clear();
        delete obj;
    }
}

ModelNode ObjectNodeInstance::modelNode() const
{
    return m_modelNode;
}

void ObjectNodeInstance::setModelNode(const ModelNode &node)
{
    m_modelNode = node;
}

NodeInstanceView *ObjectNodeInstance::nodeInstanceView() const
{
    return m_nodeInstanceView.data();
}

void ObjectNodeInstance::setNodeInstance(NodeInstanceView *view)
{
    m_nodeInstanceView = view;
}

void ObjectNodeInstance::setId(const QString &id)
{
    if (!object()->objectName().isEmpty()) {
            context()->engine()->rootContext()->setContextProperty(object()->objectName(), 0);
    }

    if (!id.isEmpty()) {
        context()->engine()->rootContext()->setContextProperty(id, object()); // will also force refresh of all bindings
    }

{'fraction_non_alphanumeric': 0.14918744322196426, 'fraction_numerical': 0.0026244069849601292, 'mean_word_length': 4.794152046783625, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4739101', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2672, 'n_words': 674}