#include "drawingPanel.h"
#include "ui_drawingPanel.h"
#include "designerView.h"
#include "designerScene.h"
#include "util/selectorManager.h"

DrawingPanel::DrawingPanel(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::drawingPanel)
{
    ui->setupUi(this);

    m_pGraphicsScene = new DesignerScene(this);
    //设置场景大小.前两个参数为scene的坐标远点，设置到view的中心点后，无论view如何缩放，secne的坐标原点都不会动，方便后续的位置计算
    m_pGraphicsScene->setSceneRect(-g_dGriaphicsScene_Width / 2, -g_dGriaphicsScene_Height / 2, g_dGriaphicsScene_Width, g_dGriaphicsScene_Height);
    m_pGraphicsScene->setGridVisible(true);

    m_pGraphicsView = new DesignerView(this);
    m_pGraphicsView->setScene(m_pGraphicsScene);
    m_pGraphicsScene->setView(m_pGraphicsView);
    ui->mainLayout->addWidget(m_pGraphicsView);
}

DrawingPanel::~DrawingPanel()
{
    delete ui;
}

QGraphicsScene* DrawingPanel::getQGraphicsScene()
{
    return m_pGraphicsView->scene();
}

DesignerScene* DrawingPanel::getDesignerScene()
{
    return m_pGraphicsScene;
}

void DrawingPanel::grahpicsViewZoomIn()
{
    m_pGraphicsView->zoomIn();
}

void DrawingPanel::grahpicsViewZoomOut()
{
    m_pGraphicsView->zoomOut();
}

void DrawingPanel::grahpicsViewZoomFit()
{
    m_pGraphicsView->zoomFit();
}

GraphicsItemGroup* DrawingPanel::createItemGroup()
{
    return m_pGraphicsScene->createGroup();
}

void DrawingPanel::destroyItemGroup()
{
    m_pGraphicsScene->destroyGroup();
}

void DrawingPanel::onSignal_addGraphicsItem(GraphicsItemType& itemType)
{
    if(SelectorManager::getInstance())
    {
        SelectorManager::getInstance()->setWorkingSelector(ST_cerating);
        SelectorManager::getInstance()->setDrawGraphicsItem(itemType);
    }
}
