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

GraphicsBaseItem::GraphicsBaseItem(QGraphicsItem *parent)
    : AbstractShapeType<QGraphicsItem>(parent)
{
    //初始化缩放操作用的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);
    }

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

GraphicsBaseItem::~GraphicsBaseItem()
{

}


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

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

void GraphicsBaseItem::removeMovingCopy()
{
    QGraphicsScene* scene = this->scene();
    if(scene && m_pMovingCopy)
    {
        this->setPos(m_pMovingCopy->pos()); //本体先移动到副本的位置
        scene->removeItem(m_pMovingCopy);
        delete m_pMovingCopy;
        m_pMovingCopy = nullptr;
    }
}

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


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);
}


