HierarchyTreeControlNode::HierarchyTreeControlNode(HierarchyTreeNode* parent,
												   const HierarchyTreeControlNode* node):
	HierarchyTreeNode(node)
{
	this->parent = parent;
	this->uiObject = node->GetUIObject()->Clone();
	this->needReleaseUIObjects = false;
	
	// Remove real children & subcontrols - each control is responsible for its
	// subcontrols by itself.
	const List<UIControl* > &realChildren = GetUIObject()->GetRealChildrenAndSubcontrols();
	for (List<UIControl* >::const_iterator iter = realChildren.begin();
		 iter != realChildren.end();
		 ++iter)
	{
        GetUIObject()->RemoveControl(*iter);
	}
	
	AddControlToParent();
	
	const HierarchyTreeNode::HIERARCHYTREENODESLIST& child = node->GetChildNodes();
	for (HierarchyTreeNode::HIERARCHYTREENODESLIST::const_iterator iter = child.begin();
		 iter != child.end();
		 ++iter)
	{
		const HierarchyTreeControlNode* controlNode = dynamic_cast<const HierarchyTreeControlNode*>((*iter));
		if (!controlNode)
			continue;
				
		AddTreeNode(new HierarchyTreeControlNode(this, controlNode));
	}
}
HierarchyTreeControlNode::HierarchyTreeControlNode(HierarchyTreeNode* parent,
												   UIControl* uiObject,
												   const QString& name) :
	HierarchyTreeNode(name)
{
	this->parent = parent;
	
	this->uiObject = uiObject;
	this->parentUIObject = NULL;
	this->needReleaseUIObjects = false;

	AddControlToParent();
}
HierarchyTreeControlNode::HierarchyTreeControlNode(HierarchyTreeNode* parent,
        const HierarchyTreeControlNode* node):
    HierarchyTreeNode(node),
    listDelegate(NULL)
{
    this->parent = parent;
    this->uiObject = node->GetUIObject()->Clone();
    this->needReleaseUIObjects = false;

    // All UIList controls should always have a delegate
    // We set a delegate here to avoid inappropriate loading of saved list
    UIList *list = dynamic_cast<UIList*>(this->uiObject);
    UIList *srcList = dynamic_cast<UIList*>(node->GetUIObject());
    if (list)
    {
        listDelegate = new EditorListDelegate(list->GetRect(), list->GetOrientation());
        EditorListDelegate *srcListDelegate = dynamic_cast<EditorListDelegate*>(srcList->GetDelegate());
        if (srcListDelegate)
        {
            listDelegate->SetAggregatorID(srcListDelegate->GetAggregatorID());
        }
        list->SetDelegate(listDelegate);
    }

    // Remove real children & subcontrols - each control is responsible for its
    // subcontrols by itself.
    const List<UIControl* > &realChildren = GetUIObject()->GetRealChildren();
    for (List<UIControl* >::const_iterator iter = realChildren.begin();
            iter != realChildren.end();
            ++iter)
    {
        GetUIObject()->RemoveControl(*iter);
    }

    AddControlToParent();

    const HierarchyTreeNode::HIERARCHYTREENODESLIST& child = node->GetChildNodes();
    for (HierarchyTreeNode::HIERARCHYTREENODESLIST::const_iterator iter = child.begin();
            iter != child.end();
            ++iter)
    {
        const HierarchyTreeControlNode* controlNode = dynamic_cast<const HierarchyTreeControlNode*>((*iter));
        if (!controlNode)
            continue;

        AddTreeNode(controlNode->CreateControlCopy(parent ? this : NULL));
    }
}
HierarchyTreeControlNode::HierarchyTreeControlNode(HierarchyTreeNode* parent,
        UIControl* uiObject,
        const QString& name) :
    HierarchyTreeNode(name),
    listDelegate(NULL)
{
    this->parent = parent;

    this->uiObject = uiObject;
    this->parentUIObject = NULL;
    this->needReleaseUIObjects = false;

    // All UIList controls should always have a delegate
    // We set a delegate here to avoid inappropriate loading of saved list
    UIList *list = dynamic_cast<UIList*>(uiObject);
    if (list)
    {
        listDelegate = new EditorListDelegate(list->GetRect(), list->GetOrientation());
        list->SetDelegate(listDelegate);
    }

    AddControlToParent();
}