#include "operationCommand.h"
#include "graphicsItem/graphicsItemGroup.h"
#include <QGraphicsItem>

AddItemCommand::AddItemCommand(QGraphicsItem* item, QGraphicsScene* scene, QUndoCommand* parent)
    : QUndoCommand(parent)
{
    m_pItem = item;
    m_itemPos = item->pos();
    m_pGraphicsScene = scene;
}
AddItemCommand::~AddItemCommand()
{
}
void AddItemCommand::undo()
{
    m_pGraphicsScene->removeItem(m_pItem);
    m_pGraphicsScene->update();
}
void AddItemCommand::redo()
{
    if(m_pItem->scene()) //因为添加图元后同步创建一条该指令，平且在push进入stack的时候redo会被触发一次，因此这里加判断，防止重复操作
        return;

    m_pGraphicsScene->addItem(m_pItem);
    m_pItem->setPos(m_itemPos);
    m_pGraphicsScene->update();
}

DeleteItemCommand::DeleteItemCommand(QGraphicsScene* scene, QUndoCommand* parent)
    : QUndoCommand(parent)
{
    m_pGraphicsScene = scene;
    m_listItem = scene->selectedItems();
}
DeleteItemCommand::~DeleteItemCommand()
{
}
void DeleteItemCommand::undo()
{
    foreach(QGraphicsItem* item, m_listItem)
    {
        // QGraphicsItemGroup* group = dynamic_cast<QGraphicsItemGroup*>(item->parentItem());
        // if(!group)
        {
            m_pGraphicsScene->addItem(item);
        }
    }

    m_pGraphicsScene->update();
}
void DeleteItemCommand::redo()
{
    foreach(QGraphicsItem* item, m_listItem)
    {
        // QGraphicsItemGroup* group = dynamic_cast<QGraphicsItemGroup*>(item->parentItem());
        // if(!group)
        {
            m_pGraphicsScene->removeItem(item); //remove即可，不要delete，因为会影响撤回（undo）操作
        }
    }

    m_pGraphicsScene->update();
}

CreateItemGoupCommand::CreateItemGoupCommand(GraphicsItemGroup* group, QGraphicsScene* scene, QUndoCommand* parent)
    : QUndoCommand(parent)
{
    m_pGroup = group;
    m_pGraphicsScene = scene;
    foreach (QGraphicsItem *item, group->childItems())
    {
        if(!qgraphicsitem_cast<ItemControlHandle*>(item))
            m_listItem.push_back(item);
    }
    //m_listItem = group->getItems();
}
CreateItemGoupCommand::~CreateItemGoupCommand()
{
}
void CreateItemGoupCommand::undo()
{
    m_pGroup->setSelected(false);
    foreach (QGraphicsItem *item, m_listItem)
    {
        //item->setSelected(true);
        m_pGroup->removeFromGroup(item);
        AbstractShape *ab = qgraphicsitem_cast<AbstractShape*>(item);
        ab->updateCoordinate();
        ab->setSelected(true);
    }
    m_pGraphicsScene->removeItem(m_pGroup);
    m_pGraphicsScene->update();
}
void CreateItemGoupCommand::redo()
{
    if(!m_pGroup->scene()) //因为添加图元后同步创建一条该指令，平且在push进入stack的时候redo会被触发一次，因此这里加判断，防止重复操作
    {
        foreach (QGraphicsItem *item, m_listItem)
        {
            item->setSelected(false);
            m_pGroup->addToGroup(item);
        }
        m_pGraphicsScene->addItem(m_pGroup);
    }

    m_pGroup->setSelected(true);
    m_pGraphicsScene->update();
}

DestroyItemGoupCommand::DestroyItemGoupCommand(GraphicsItemGroup* group, QGraphicsScene* scene, QUndoCommand* parent)
    : QUndoCommand(parent)
{
    m_pGroup = group;
    m_pGraphicsScene = scene;
    foreach (QGraphicsItem *item, group->childItems())
    {
        if(!qgraphicsitem_cast<ItemControlHandle*>(item))
            m_listItem.push_back(item);
    }
    //m_listItem = group->getItems();
}
DestroyItemGoupCommand::~DestroyItemGoupCommand()
{
}
void DestroyItemGoupCommand::undo()
{
    foreach (QGraphicsItem *item, m_listItem)
    {
        item->setSelected(false);
        m_pGroup->addToGroup(item);
    }

    m_pGraphicsScene->addItem(m_pGroup);
    m_pGroup->setSelected(true);
    m_pGraphicsScene->update();
}
void DestroyItemGoupCommand::redo()
{
    m_pGroup->setSelected(false);
    foreach (QGraphicsItem *item, m_listItem)
    {
        //item->setSelected(true);
        m_pGroup->removeFromGroup(item);
        AbstractShape *ab = qgraphicsitem_cast<AbstractShape*>(item);
        ab->updateCoordinate();
        ab->setSelected(true);
    }
    m_pGraphicsScene->removeItem(m_pGroup);
    m_pGraphicsScene->update();
}

