#include "graphicsItem/graphicsBaseItem.h"
#include <QGraphicsScene>

GraphicsBaseItem::GraphicsBaseItem(QGraphicsItem *parent)
    : AbstractShapeType<QGraphicsItem>(parent)
{
    m_type = T_item;
    //初始化缩放操作用的handle
    m_vecHanle.reserve(H_left);
    for(int i = H_leftTop; i <= H_left; i++)
    {
        ItemControlHandle* pHandle = new ItemControlHandle(this);
        pHandle->setType(T_resize);
        pHandle->setTag(i);
        m_vecHanle.push_back(pHandle);
    }
    for(int i = H_rotate_leftTop; i <= H_rotate_leftBottom; i++)
    {
        ItemControlHandle* pHandle = new ItemControlHandle(this);
        pHandle->setType(T_rotate);
        pHandle->setTag(i);
        m_vecHanle.push_back(pHandle);
    }

    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    setAcceptHoverEvents(true);
}

GraphicsBaseItem::~GraphicsBaseItem()
{

}


void GraphicsBaseItem::createOperationCopy()
{
    m_pOperationCopy = new QGraphicsPathItem(this->shape());
    m_pOperationCopy->setPen(Qt::DashLine);
    m_pOperationCopy->setPos(this->pos());
    m_pOperationCopy->setTransformOriginPoint(this->transformOriginPoint());
    m_pOperationCopy->setTransform(this->transform());
    m_pOperationCopy->setRotation(this->rotation());
    m_pOperationCopy->setScale(this->scale());
    m_pOperationCopy->setZValue(this->zValue());

    QGraphicsScene* scene = this->scene();
    if(scene && m_pOperationCopy)
    {
        scene->addItem(m_pOperationCopy);
        m_movingIniPos = this->pos();
    }
}

void GraphicsBaseItem::removeOperationCopy()
{
    QGraphicsScene* scene = this->scene();
    if(scene && m_pOperationCopy)
    {
        if(this->pos() != m_pOperationCopy->pos())
            this->setPos(m_pOperationCopy->pos()); //本体移动到副本的位置
        if(this->rotation() != m_pOperationCopy->rotation())
            this->setRotation(m_pOperationCopy->rotation()); //本体旋转至副本的角度

        scene->removeItem(m_pOperationCopy);
        delete m_pOperationCopy;
        m_pOperationCopy = nullptr;
    }
}

void GraphicsBaseItem::moveOperationCopy(const QPointF& distance)
{
    if(m_pOperationCopy)
        m_pOperationCopy->setPos(m_movingIniPos + distance);
}

void GraphicsBaseItem::rotateOperationCopy(const double& dAngle)
{
    if(m_pOperationCopy)
        m_pOperationCopy->setRotation(dAngle);
}

QVariant GraphicsBaseItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value)
{
    if (change == QGraphicsItem::ItemSelectedHasChanged)
    {
        QGraphicsItemGroup *group = dynamic_cast<QGraphicsItemGroup *>(parentItem());
        if(!group)
            setHandleVisible(value.toBool());
        else //在某一组群中，由组群展示是否选中，自身不做展示
        {
            setSelected(false);
            return QVariant::fromValue<bool>(false);
        }
    }
    return QGraphicsItem::itemChange(change, value);
}

void GraphicsBaseItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
{
    Q_UNUSED(event);
}


