#include "util/baseSelector.h"
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include <graphicsItem/graphicsBaseItem.h>
#include <QDebug>

QPointF BaseSelector::ms_ptMouseDown(0.0,0.0);
QPointF BaseSelector::ms_ptMouseLast(0.0,0.0);
int BaseSelector::ms_nDragHandle = 0;

BaseSelector::BaseSelector(QObject *parent)
    : QObject(parent)
{
    m_type = ST_base;
    m_opMode = OM_none;
    m_bHoverOnHandel = false;
}
BaseSelector::~BaseSelector()
{

}

void BaseSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
    if (event->button() != Qt::LeftButton)
        return;

    ms_ptMouseDown = event->scenePos();
    ms_ptMouseLast = event->scenePos();

    if(!m_bHoverOnHandel)
        scene->callParentEvent(event); //此处是通过触发QGraphicsScene的事件函数来取消所有被选中item的选中状态

    m_opMode = OM_none;

    QList<QGraphicsItem *> items = scene->selectedItems();
    if (items.count() == 1) //只有一个选中
    {
        GraphicsBaseItem* item = qgraphicsitem_cast<GraphicsBaseItem*>(items.first());
        if(item)
        {
            //需要增加当前是否点击在控制点的判断函数
            ms_nDragHandle = item->collidesWithHandle(event->scenePos());
            if(ms_nDragHandle != H_none && ms_nDragHandle <= H_left) //在缩放控制点上
            {
                m_opMode = OM_scale;
                emit setWorkingSelector(ST_scaling);
            }
            else if(ms_nDragHandle > H_left) //编辑控制点上
            {
                m_opMode = OM_edit;
                //setCursor(scene, Qt::ClosedHandCursor);
            }
            else
            {
                m_opMode = OM_move;
                emit setWorkingSelector(ST_moving);
                setCursor(scene, Qt::ClosedHandCursor);
            }
        }
    }
    else if (items.count() > 1) //选中多个
    {
        m_opMode = OM_move;
        emit setWorkingSelector(ST_moving);
        setCursor(scene, Qt::ClosedHandCursor);
    }

    if(m_opMode == OM_move)
    {
        for(int n = 0; n < items.size(); n++)
        {
            GraphicsBaseItem* item = qgraphicsitem_cast<GraphicsBaseItem*>(items.at(n));
            item->createMovingCopy();
        }
    }
    else if(m_opMode == OM_none)
    {
        QGraphicsView *view = scene->getView();
        if(view)
            view->setDragMode(QGraphicsView::RubberBandDrag);
    }

}

void BaseSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
    ms_ptMouseLast = event->scenePos();

    QList<QGraphicsItem *> items = scene->selectedItems();

    if (items.count() == 1)
    {
        GraphicsBaseItem* item = qgraphicsitem_cast<GraphicsBaseItem*>(items.first());
        if(item)
        {
            if(ms_nDragHandle == H_none)
            {
                //设置光标样式
                int nHandle = item->collidesWithHandle(event->scenePos());
                if(nHandle == H_none)
                {
                    setCursor(scene, Qt::ArrowCursor);
                    m_bHoverOnHandel = false;
                }
                else
                {
                    switch (nHandle)
                    {
                    case H_leftTop:
                        setCursor(scene, Qt::SizeFDiagCursor);
                        break;
                    case H_top:
                        setCursor(scene, Qt::SizeVerCursor);
                        break;
                    case H_rightTop:
                        setCursor(scene, Qt::SizeBDiagCursor);
                        break;
                    case H_right:
                        setCursor(scene, Qt::SizeHorCursor);
                        break;
                    case H_rightBottom:
                        setCursor(scene, Qt::SizeFDiagCursor);
                        break;
                    case H_bottom:
                        setCursor(scene, Qt::SizeVerCursor);
                        break;
                    case H_leftBottom:
                        setCursor(scene, Qt::SizeBDiagCursor);
                        break;
                    case H_left:
                        setCursor(scene, Qt::SizeHorCursor);
                        break;
                    default:
                        break;
                    }

                    m_bHoverOnHandel = true;
                }
            }
        }
    }
}

void BaseSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
    setCursor(scene, Qt::ArrowCursor);

    if(m_opMode == OM_none)
    {
        QGraphicsView *view = scene->getView();
        if(view)
            view->setDragMode(QGraphicsView::NoDrag);
    }

    m_opMode = OM_none;
    m_bHoverOnHandel = false;
    ms_nDragHandle = H_none;
    scene->callParentEvent(event);
}

void BaseSelector::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
    Q_UNUSED(event)
    Q_UNUSED(scene)
}

void BaseSelector::setCursor(DesignerScene *scene, const QCursor &cursor)
{
    QGraphicsView *view = scene->getView();
    if (view)
        view->setCursor(cursor);
}
