#include "wdrawingboard.h"
#include "wProgramManager/wEditProgram/wpageitem.h"
#include "wProgramManager/wEditProgram/wElement/eobject.h"
#include "wProgramManager/wEditProgram/wElement/etext.h"
#include "wProgramManager/wEditProgram/wElement/ephoto.h"
#include "wProgramManager/wEditProgram/wElement/emovie.h"
#include "wProgramManager/wEditProgram/wElement/egif.h"
#include "wProgramManager/wEditProgram/wElement/edclock.h"
#include "wProgramManager/wEditProgram/wElement/eaclock.h"
#include "wProgramManager/wEditProgram/wElement/etemp.h"
#include "wProgramManager/wEditProgram/wElement/eweather.h"
#include "wProgramManager/wEditProgram/wElement/etimer.h"
#include "wProgramManager/wEditProgram/wElement/ewindow.h"
//#include "wProgramManager/wEditProgram/wElement/eaudio.h"
#include <LoUIClass/x_uimsgboxokcancel.h>
#include "wProgramManager/wEditProgram/wElement/imagecutwidget.h"
wDrawingBoard::wDrawingBoard(const QSizeF &res, QWidget *parent) :
    QWidget(parent),
    m_scale(100),
    m_resolution(res),
    m_iScreen(res)
{
    setAttribute(Qt::WA_DeleteOnClose);
    //QAction *spacer          = new QAction();
    QAction *actScaleUp      = new QAction(QIcon(":/res/ProgramManager/EditProgram/ScaleUp.png"),      tr("Zoom In"));
    QAction *actScaleDown    = new QAction(QIcon(":/res/ProgramManager/EditProgram/ScaleDown.png"),    tr("Zoom Out"));
    QAction *actScaleOrg     = new QAction(QIcon(":/res/ProgramManager/EditProgram/ScaleOrg.png"),     tr("Original size"));
    QAction *actDelete       = new QAction(QIcon(":/res/ProgramManager/EditProgram/Delete.png"),       tr("Delete the secect media"));
    QAction *actClean        = new QAction(QIcon(":/res/ProgramManager/EditProgram/Clean.png"),        tr("Clear all media"));
    QAction *actCut          = new QAction(QIcon(":/res/ProgramManager/EditProgram/Cut.png"),          tr("Cut media"));
    QAction *actLayerUp      = new QAction(QIcon(":/res/ProgramManager/EditProgram/LayerUp.png"),      tr("Move layer up"));
    QAction *actLayerDown    = new QAction(QIcon(":/res/ProgramManager/EditProgram/LayerDown.png"),    tr("Send backward"));
    QAction *actLayerTop     = new QAction(QIcon(":/res/ProgramManager/EditProgram/LayerTop.png"),     tr("Bring to front"));
    QAction *actLayerBottom  = new QAction(QIcon(":/res/ProgramManager/EditProgram/LayerBottom.png"),  tr("Move to the bottom layer"));
    QAction *actTileFull     = new QAction(QIcon(":/res/ProgramManager/EditProgram/TileFull.png"),     tr("Fill the entire screen"));
    QAction *actTileH        = new QAction(QIcon(":/res/ProgramManager/EditProgram/TileH.png"),        tr("Fill the screen horizontally"));
    QAction *actTileV        = new QAction(QIcon(":/res/ProgramManager/EditProgram/TileV.png"),        tr("Fill the screen vertically"));
    QAction *actArrayTop     = new QAction(QIcon(":/res/ProgramManager/EditProgram/ArrayTop.png"),     tr("Align top"));
    QAction *actArrayHCenter = new QAction(QIcon(":/res/ProgramManager/EditProgram/ArrayHCenter.png"), tr("Center vertically"));
    QAction *actArrayBottom  = new QAction(QIcon(":/res/ProgramManager/EditProgram/ArrayBottom.png"),  tr("Bottom align"));
    QAction *actArrayLeft    = new QAction(QIcon(":/res/ProgramManager/EditProgram/ArrayLeft.png"),    tr("Align left"));
    QAction *actArrayVCenter = new QAction(QIcon(":/res/ProgramManager/EditProgram/ArrayVCenter.png"), tr("Center horizontally"));
    QAction *actArrayRight   = new QAction(QIcon(":/res/ProgramManager/EditProgram/ArrayRight.png"),   tr("Align right"));

    QSpinBox *wScale = new QSpinBox();
    wScale->setObjectName("wScale");
    wScale->setMinimum(0);
    wScale->setMaximum(768);
    wScale->setFixedSize(30, 22);
    wScale->setReadOnly(true);
    wScale->setAlignment(Qt::AlignCenter);
    wScale->setButtonSymbols(QAbstractSpinBox::NoButtons);
    //主区域编辑工具栏
    QToolBar *tb = new QToolBar();
    tb->setIconSize(QSize(16, 16));
    tb->setProperty("ssType", "progEditor");
    tb->setProperty("ssName", "elementLayout");
    // tb->addAction(spacer);
    tb->addSeparator();
    tb->addAction(actScaleUp);//放大
    tb->addWidget(wScale);//当前视图比例
    tb->addAction(actScaleDown);//缩小
    tb->addAction(actScaleOrg);//1:1恢复视图
    tb->addSeparator();
    tb->addAction(actDelete);
    tb->addAction(actClean);
    tb->addAction(actCut);
    tb->addSeparator();
    tb->addAction(actLayerUp);
    tb->addAction(actLayerDown);
    tb->addAction(actLayerTop);
    tb->addAction(actLayerBottom);
    tb->addSeparator();
    tb->addAction(actTileFull);
    tb->addAction(actTileH);
    tb->addAction(actTileV);
    tb->addSeparator();
    tb->addAction(actArrayTop);
    tb->addAction(actArrayHCenter);
    tb->addAction(actArrayBottom);
    tb->addAction(actArrayLeft);
    tb->addAction(actArrayVCenter);
    tb->addAction(actArrayRight);

    m_wView = new QGraphicsView();
    m_wView->setScene(nullptr);
    //垂直布局（编辑工具栏，编辑区域）
    QVBoxLayout *playout = new QVBoxLayout();
    playout->addWidget(tb, 0);
    playout->addWidget(m_wView, 1);
    playout->setSpacing(0);
    playout->setContentsMargins(0, 0, 0, 0);
    setLayout(playout);

    //    setStyleSheet("border:2px;solid:#000000;");
    connect(actScaleUp,      SIGNAL(triggered(bool)),   this,   SLOT(onScaleUp()));
    connect(actScaleDown,    SIGNAL(triggered(bool)),   this,   SLOT(onScaleDown()));
    connect(actScaleOrg,     SIGNAL(triggered(bool)),   this,   SLOT(onScaleOrg()));
    connect(actDelete,       SIGNAL(triggered(bool)),   this,   SLOT(onDelete()));
    connect(actClean,        SIGNAL(triggered(bool)),   this,   SLOT(onClean()));
    connect(actCut,          SIGNAL(triggered(bool)),   this,   SLOT(onCut()));
    connect(actLayerUp,      SIGNAL(triggered(bool)),   this,   SLOT(onLayerUp()));
    connect(actLayerDown,    SIGNAL(triggered(bool)),   this,   SLOT(onLayerDown()));
    connect(actLayerTop,     SIGNAL(triggered(bool)),   this,   SLOT(onLayerTop()));
    connect(actLayerBottom,  SIGNAL(triggered(bool)),   this,   SLOT(onLayerBottom()));
    connect(actTileFull,     SIGNAL(triggered(bool)),   this,   SLOT(onTileFull()));
    connect(actTileH,        SIGNAL(triggered(bool)),   this,   SLOT(onTileH()));
    connect(actTileV,        SIGNAL(triggered(bool)),   this,   SLOT(onTileV()));
    connect(actArrayTop,     SIGNAL(triggered(bool)),   this,   SLOT(onArrayTop()));
    connect(actArrayHCenter, SIGNAL(triggered(bool)),   this,   SLOT(onArrayHCenter()));
    connect(actArrayBottom,  SIGNAL(triggered(bool)),   this,   SLOT(onArrayBottom()));
    connect(actArrayLeft,    SIGNAL(triggered(bool)),   this,   SLOT(onArrayLeft()));
    connect(actArrayVCenter, SIGNAL(triggered(bool)),   this,   SLOT(onArrayVCenter()));
    connect(actArrayRight,   SIGNAL(triggered(bool)),   this,   SLOT(onArrayRight()));
    connect(this,            SIGNAL(scaleChanged(int)), wScale, SLOT(setValue(int)));
    connect(this,            SIGNAL(scaleChanged(int)), this,   SLOT(adjustTrans()));
    setScale(100);
}

void wDrawingBoard::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Delete:   onDelete();      break;
    case Qt::Key_Home:     onLayerTop();    break;
    case Qt::Key_End:      onLayerBottom(); break;
    case Qt::Key_PageUp:   onLayerUp();     break;
    case Qt::Key_PageDown: onLayerDown();   break;
    case Qt::Key_Left:     onSelectionLeft();break;
    case Qt::Key_Right:     onSelectionRight();break;
    case Qt::Key_Up:     onSelectionTop();break;
    case Qt::Key_Down:     onSelectionBottom();break;
    default:break;
    }
}
void wDrawingBoard::wheelEvent(QWheelEvent*event)
{
    if(event->delta()>0){//如果滚轮往上滚
        onScaleUp();

    }else{//同样的
        onScaleDown();

    }
}

QList<eObject*> wDrawingBoard::getElementList()
{
    QList<eObject*> list;
    QGraphicsScene *scene = m_wView->scene();
    if(nullptr == scene) return list;
    QList<QGraphicsItem *> tmp_list = scene->items();
    if(tmp_list.count() < 2) return list;
    foreach(QGraphicsItem *item, tmp_list) {
        if(item->type() > QGraphicsItem::UserType) {
            eObject *element = static_cast<eObject*>(item);
            if(element->interactiveType() == eObject::Dynamic)
                list.append(element);
        }
    }
    std::sort(list.begin(), list.end(), [](eObject *a, eObject *b) {
        return a->zValue() < b->zValue();
    });
    return list;
}

QList<eObject*> wDrawingBoard::getElementSelectedList()
{
    QList<eObject*> list;
    QGraphicsScene *scene = m_wView->scene();
    if(nullptr == scene) return list;
    QList<QGraphicsItem *> tmp_list = scene->selectedItems();
    if(tmp_list.count() == 0) return list;
    foreach(QGraphicsItem *item, tmp_list) {
        if(item->type() > QGraphicsItem::UserType) {
            eObject *element = static_cast<eObject*>(item);
            if(element->interactiveType() == eObject::Dynamic)
                list.append(element);
        }
    }
    std::sort(list.begin(), list.end(), [](eObject *a, eObject *b) {
        return a->zValue() < b->zValue();
    });

    return list;
}

eObject* wDrawingBoard::getElementSelected()
{
    eObject *element = nullptr;
    QGraphicsScene *scene = m_wView->scene();
    if(nullptr == scene) return element;
    QList<QGraphicsItem *> list = scene->selectedItems();
    if(list.count() != 1) return element;
    element = static_cast<eObject*>(list.at(0));
    return element;
}

void wDrawingBoard::refreshOrder()
{
    int i = 0;
    QList<eObject*> list = getElementList();
    foreach(eObject *element, list) {
        element->setZValue(i++);
    }
}

void wDrawingBoard::setElementGeometry(eObject *element, const QRectF &r)
{
    element->setGeometry(r);
    emit element->geometryChanged(r);
    update();

}

void wDrawingBoard::onCreatElement(int type)
{

    QGraphicsScene *scene = m_wView->scene();
    if(nullptr == scene) return;
    int order = getElementList().count();
    eObject *element = nullptr;

    int iNewWidth=m_resolution.width();
    int iNewHeight=m_resolution.height();
    if(iNewWidth>64)
        iNewWidth=iNewWidth*2/3;
    if(iNewHeight>64)
        iNewHeight=iNewHeight*2/3;
    if(m_xOffset+iNewWidth>m_resolution.width())
    {
        m_xOffset=0;
    }
    if(m_yOffset+iNewHeight>m_resolution.height()){
        m_yOffset=0;
    }

    switch (type)
    {
    case eObject::Text :
    {
        if(iNewHeight>80&&(m_resolution.width()>=m_resolution.height()))
            iNewHeight=80;
        QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);
        element = new eText(rt);
    }
        break;
    case eObject::Photo :
    {
        //          QString file = LoAppTools::getInstance()->selectFile(ePhoto::filters(), this);
        //          if(!file.isNull())
        //          {
        //              QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);
        //              element = new ePhoto(rt,file);
        //          }
        QStringList fileList = LoAppTools::getInstance()->selectPhotoFile(ePhoto::filters(), this);

        if(fileList.count()>0)
        {
            for(int i=0;i<fileList.count();i++)
            {
                QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);
                eObject *element1 = nullptr;
                element1 = new ePhoto(rt,m_resolution,fileList.at(i));
                if(nullptr != element1) {
                    scene->addItem(element1);
                    element1->setZValue(order);
                    order++;
                    element1->setRLimit(QRectF(0, 0, m_resolution.width(), m_resolution.height()));
                    m_xOffset+=16;
                    m_yOffset+=16;

                }
                if(i==fileList.count()-1)
                {
                    QGraphicsScene *scene = m_wView->scene();
                    if(nullptr != scene)
                    {
                        QList<QGraphicsItem *> list = scene->selectedItems();
                        if(list.count() == 1)
                        {
                            static_cast<eObject*>(list.at(0))->setSelected(false);
                            static_cast<eObject*>(list.at(0))->stopElectment();
                        }

                    }
                    element1->setSelected(true);
                }
                if(nullptr != element1)
                {
                    connect(element1,SIGNAL(sigCiTie(bool,int)),this,SLOT(OnCiTieProcess(bool,int)));
                }
            }

        }

    }
        break;
    case eObject::Gif :
    {
        //            QString file = LoAppTools::getInstance()->selectFile(eGif::filters(), this);
        //            if(!file.isNull())
        //            {
        //                QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);
        //                element = new eGif(rt,file);
        //            }

        QStringList fileList = LoAppTools::getInstance()->selectPhotoFile(eGif::filters(), this);

        if(fileList.count()>0)
        {
            for(int i=0;i<fileList.count();i++)
            {
                QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);
                eObject *element1 = nullptr;
                element1 = new eGif(rt,m_resolution,fileList.at(i));
                if(nullptr != element1) {
                    scene->addItem(element1);
                    element1->setZValue(order);
                    order++;
                    element1->setRLimit(QRectF(0, 0, m_resolution.width(), m_resolution.height()));
                    m_xOffset+=16;
                    m_yOffset+=16;

                }
                if(i==fileList.count()-1)
                {
                    QGraphicsScene *scene = m_wView->scene();
                    if(nullptr != scene)
                    {
                        QList<QGraphicsItem *> list = scene->selectedItems();
                        if(list.count() == 1)
                        {
                            static_cast<eObject*>(list.at(0))->setSelected(false);
                            static_cast<eObject*>(list.at(0))->stopElectment();
                        }

                    }
                    element1->setSelected(true);
                }
                if(nullptr != element1)
                {
                    connect(element1,SIGNAL(sigCiTie(bool,int)),this,SLOT(OnCiTieProcess(bool,int)));
                }
            }

        }
    }
        break;
    case eObject::Movie :
    {
        QString file = LoAppTools::getInstance()->selectFile(eMovie::filters(), this,true);
        if(!file.isNull())
        {
            QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);
            element = new eMovie(rt,file,this);
        }
    }
        break;

    case eObject::DClock :
    {
        if(iNewHeight>80&&(m_resolution.width()>=m_resolution.height()))
            iNewHeight=80;
        QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);
        element = new eDClock(rt);
    }
        break;
    case eObject::AClock :
    {
        if(iNewWidth>120)
            iNewWidth=120;
        if(iNewHeight>120)
            iNewHeight=120;
        QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);
        element = new eAClock(rt);
    }
        break;
    case eObject::Office :
        break;
    case eObject::Temp :
    {

        QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);
        element = new eTemp(rt);
    }
        break;
    case eObject::Weather :
    {
        QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);

        element = new eWeather(rt);
    }
        break;
    case eObject::Rss :
        break;
    case eObject::Timer :
    {
        QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);

        element = new eTimer(rt);
    }
        break;
    case eObject::ColorText :
    {
    }
        break;
    case eObject::Audio :
    {
        QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);

        // element = new eAudio(rt);
    }
        break;
    case eObject::Window :
    {
        QRectF rt(m_xOffset,m_yOffset,iNewWidth,iNewHeight);

        element = new eWindow(rt);
    }
        break;
    default:
        break;
    }
    if(nullptr != element && type!=eObject::Photo) {
        scene->addItem(element);
        element->setZValue(order);
        element->setRLimit(QRectF(0, 0, m_resolution.width(), m_resolution.height()));
        m_xOffset+=8;
        m_yOffset+=8;
        QGraphicsScene *scene = m_wView->scene();
        if(nullptr != scene)
        {
            QList<QGraphicsItem *> list = scene->selectedItems();
            if(list.count() == 1)
            {
                static_cast<eObject*>(list.at(0))->setSelected(false);
                static_cast<eObject*>(list.at(0))->stopElectment();

            }

        }
        element->setSelected(true);

    }
    if(nullptr != element)
    {
        connect(element,SIGNAL(sigCiTie(bool,int)),this,SLOT(OnCiTieProcess(bool,int)));
    }
}

void wDrawingBoard::onScaleUp()
{
    int s = scale();
    if(s <= 790) {
        s += 10;
    } else {
        s = 800;
    }
    setScale(s);
}

void wDrawingBoard::onScaleDown()
{
    int s = scale();
    if(s >= 10) {
        s -= 10;
    } else {
        s = 0;
    }
    setScale(s);
}

void wDrawingBoard::onScaleOrg()
{
    setScale(100);
}

void wDrawingBoard::onDelete()
{
    QGraphicsScene *scene = m_wView->scene();
    if(nullptr == scene) return;
    QList<eObject *> list = getElementSelectedList();
    foreach(QGraphicsItem *item, list) {
        scene->removeItem(item);

        delete item;
    }
    eObject* e=getElementSelected();
    if(e!=nullptr)
    {
        switch(e->m_iType)
        {
        case eObject::Photo:
            dynamic_cast<ePhoto *>(e)->deleteContent();
            break;
        case eObject::Movie:
            dynamic_cast<eMovie *>(e)->deleteContent();
            break;
        case eObject::Gif:
            dynamic_cast<eGif *>(e)->deleteContent();
            break;
        }

        delete e;

    }
    refreshOrder();
}

void wDrawingBoard::onClean()
{
    X_UIMsgBoxOkCancel *dlg = new X_UIMsgBoxOkCancel(tr("Tip Info"),tr("Clear all medias?"), this);
    if(dlg->exec() == QDialog::Accepted)
    {
        QList<eObject*> list = getElementList();
        foreach(eObject *element, list) {
            m_wView->scene()->removeItem(element);
            delete element;
        }
    }
    else {

    }

}
void wDrawingBoard::onCut()
{
    //this->parentWidget()->parentWidget()->showMinimized();
    eObject *element = getElementSelected();
    if(element!=nullptr)
    {
        if(element->m_iType==eObject::Photo)
        {
            QImage * pImage=dynamic_cast<ePhoto *>(element)->GetCurImage();

            ImageCutWidget *pImageCutWidget=new ImageCutWidget(pImage);
            pImageCutWidget->setWindowModality(Qt::WindowModal);//阻塞父窗口，所有祖先窗口及其子窗口
            connect(pImageCutWidget,SIGNAL(sigSaveRect(QRect)),this,SLOT(OnCropImage(QRect)));
            pImageCutWidget->show();
        }
    }


}
void wDrawingBoard::OnCropImage(QRect rt)
{
    eObject *element = getElementSelected();
    if(element!=nullptr)
    {
        if(element->m_iType==eObject::Photo)
        {
            dynamic_cast<ePhoto *>(element)->CropCurImage(rt);
        }
    }

}
void wDrawingBoard::onLayerUp()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QList<eObject*> list = getElementList();
    int n = list.count();
    int o = static_cast<int>(element->zValue());
    if(o < n-1) {
        element->setZValue(o+1);
        list.at(o+1)->setZValue(o);
    }
}

void wDrawingBoard::onSelectionLeft()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF rt=element->geometry();
    element->setX(rt.x()-1);
}
void wDrawingBoard::onSelectionRight()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF rt=element->geometry();
    element->setX(rt.x()+1);
}
void wDrawingBoard::onSelectionTop()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF rt=element->geometry();
    element->setY(rt.y()-1);
}
void wDrawingBoard::onSelectionBottom()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF rt=element->geometry();
    element->setY(rt.y()+1);
}
void wDrawingBoard::onLayerDown()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QList<eObject*> list = getElementList();
    int o = static_cast<int>(element->zValue());
    if(o > 0) {
        element->setZValue(o-1);
        list.at(o-1)->setZValue(o);
    }
}
void wDrawingBoard::onLayerTop()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QList<eObject*> list = getElementList();
    int n = list.count();
    int o = static_cast<int>(element->zValue());
    if(o < n-1) {
        for(int i=o+1; i<n; i++)
            list.at(i)->setZValue(i-1);
        element->setZValue(n-1);
    }
}

void wDrawingBoard::onLayerBottom()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QList<eObject*> list = getElementList();
    int o = static_cast<int>(element->zValue());
    if(o > 0) {
        for(int i=0; i<o; i++)
            list.at(i)->setZValue(i+1);
        element->setZValue(0);
    }
}

void wDrawingBoard::onTileFull()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF r = QRectF(0, 0, m_resolution.width(), m_resolution.height());
    setElementGeometry(element, r);
}

void wDrawingBoard::onTileH()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF r = element->geometry();
    r.setX(0);
    r.setWidth(m_resolution.width());
    setElementGeometry(element, r);
}

void wDrawingBoard::onTileV()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF r = element->geometry();
    r.setY(0);
    r.setHeight(m_resolution.height());
    setElementGeometry(element, r);
}

void wDrawingBoard::onArrayTop()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF r = element->geometry();
    qreal h = r.height();
    r.setY(0);
    r.setHeight(h);
    setElementGeometry(element, r);
}

void wDrawingBoard::onArrayHCenter()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF r = element->geometry();
    qreal h = r.height();
    r.setY(floor((m_resolution.height() - r.height()) / 2));
    r.setHeight(h);
    setElementGeometry(element, r);
}

void wDrawingBoard::onArrayBottom()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF r = element->geometry();
    qreal h = r.height();
    r.setY(m_resolution.height() - r.height() + 1);
    r.setHeight(h);
    setElementGeometry(element, r);
}

void wDrawingBoard::onArrayLeft()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF r = element->geometry();
    qreal w = r.width();
    r.setX(0);
    r.setWidth(w);
    setElementGeometry(element, r);
}

void wDrawingBoard::onArrayVCenter()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF r = element->geometry();
    qreal w = r.width();
    r.setX(floor((m_resolution.width() - r.width()) / 2));
    r.setWidth(w);
    setElementGeometry(element, r);
}

void wDrawingBoard::onArrayRight()
{
    eObject *element = getElementSelected();
    if(nullptr == element) return;
    QRectF r = element->geometry();
    qreal w = r.width();
    r.setX(m_resolution.width() - r.width() + 1);
    r.setWidth(w);
    setElementGeometry(element, r);
}

void wDrawingBoard::adjustTrans()
{
    qreal sr = static_cast<qreal>(scale()) / 100;
    m_wView->resetTransform();
    m_wView->scale(sr, sr);
}

void wDrawingBoard::onSlotPageSelected(wPageItem *page)
{
    QGraphicsScene *scene = m_wView->scene();
    if(scene != nullptr) {
        disconnect(scene, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
        page->disconnectCiTieSignal(this);
        if(nullptr != scene)
        {
            QList<QGraphicsItem *> tmp_list = scene->items();
            foreach(QGraphicsItem *item, tmp_list)
            {
                if(item->type() > QGraphicsItem::UserType)
                {
                    eObject *element1 = static_cast<eObject*>(item);
                    element1->stopElectment();
                }
            }
        }
    }
    m_page = page;
    LoAppTools::getInstance()->setCurPage(page);
    if(page == nullptr)
    {
        m_wView->setScene(nullptr);
    }
    else
    {
        setScale(100);
        scene = page->scene();
        m_wView->setScene(scene);
        connect(scene, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
        page->connectCiTieSignal(this);
    }
}

void wDrawingBoard::onSelectionChanged()
{
    eObject *element = nullptr;
    QGraphicsScene *scene = m_wView->scene();
    QList<QGraphicsItem *> list = scene->selectedItems();
    if(list.count() == 1) {
        element = static_cast<eObject*>(list.at(0));

        if(nullptr != scene)
        {
            QList<QGraphicsItem *> tmp_list = scene->items();
            if(tmp_list.count() != 0)
            {
                foreach(QGraphicsItem *item, tmp_list) {
                    if(item->type() > QGraphicsItem::UserType) {
                        eObject *element1 = static_cast<eObject*>(item);
                        if(element1!=element)
                        {
                            element1->stopElectment();
                        }
                    }
                }
            }
            element->playElectment();

        }



    }


    emit sigElementSelected(element);
}
void wDrawingBoard::OnCiTieProcess(bool b,int iTuoZhuaiDir)
{
    if(b)
    {
        bool bNeedFlag=false;
        int iCiTieSpace=8;
        eObject *selectedElement = getElementSelected();
        if(selectedElement!=nullptr)
        {
            QRectF selectedERt=selectedElement->geometry();
            QList<eObject*> list = getElementList();
            foreach(eObject *element, list) {//左竖线，左竖线
                QRectF eRt=element->geometry();
                if(element!=selectedElement)
                {
                    if(fabs(selectedERt.x()-eRt.x())<=iCiTieSpace)
                    {
                        if((selectedERt.x()<iCiTieSpace&&selectedERt.x()>=0)||(selectedERt.right()>=m_iScreen.width()-iCiTieSpace))
                        {

                        }
                        else {
                            if(iTuoZhuaiDir==0)
                            {
                                if(eRt.x()>=0)
                                    selectedElement->setX(eRt.x())  ;
                                else
                                    selectedElement->setX(0)  ;

                            }

                        }

                        selectedElement->setBrightBianLeft(true)  ;
                        element->setBrightBianLeft(true);
                        bNeedFlag=true;
                        break;
                    }
                    else
                    {
                        selectedElement->setBrightBianLeft(false);
                        element->setBrightBianLeft(false);
                    }
                }
            }
            foreach(eObject *element, list) {//上横线，上横线
                QRectF eRt=element->geometry();
                if(element!=selectedElement)
                {
                    if(fabs(selectedERt.y()-eRt.y())<=iCiTieSpace)
                    {
                        if((selectedERt.y()<=iCiTieSpace&&selectedERt.y()>=0)||(selectedERt.bottom()>=m_iScreen.width()-iCiTieSpace))
                        {

                        }
                        else {
                            if(iTuoZhuaiDir==0)
                            {
                                if(eRt.y()>=0)
                                    selectedElement->setY(eRt.y())  ;
                                else
                                    selectedElement->setY(0)  ;

                            }
                        }
                        selectedElement->setBrightBianTop(true)  ;
                        element->setBrightBianTop(true);
                        bNeedFlag=true;
                        break;
                    }
                    else
                    {
                        selectedElement->setBrightBianTop(false);
                        element->setBrightBianTop(false);
                    }
                }

            }
            foreach(eObject *element, list) {//右竖线，右竖线
                QRectF eRt=element->geometry();
                if(element!=selectedElement)
                {
                    if(fabs(selectedERt.right()-eRt.right())<=iCiTieSpace)
                    {
                        if((selectedERt.right()>=m_iScreen.width()-iCiTieSpace)||selectedERt.left()<=iCiTieSpace)
                        {

                        }
                        else {
                            if(iTuoZhuaiDir==0)
                            {
                                if(eRt.right()-selectedERt.width()>=0)
                                    selectedElement->setX(eRt.right()-selectedERt.width())  ;
                                else
                                    selectedElement->setX(0)  ;

                            }

                        }
                        selectedElement->setBrightBianRight(true)  ;
                        element->setBrightBianRight(true);
                        bNeedFlag=true;
                        break;
                    }
                    else
                    {
                        selectedElement->setBrightBianRight(false);
                        element->setBrightBianRight(false);
                    }
                }

            }
            foreach(eObject *element, list) {//下横线，下横线
                QRectF eRt=element->geometry();
                if(element!=selectedElement)
                {
                    if(fabs(selectedERt.bottom()-eRt.bottom())<=iCiTieSpace)
                    {
                        if((selectedERt.bottom()>=m_iScreen.height()-iCiTieSpace)||(selectedERt.y()<=iCiTieSpace))
                        {

                        }
                        else {
                            if(iTuoZhuaiDir==0)
                            {
                                if(eRt.bottom()-selectedERt.height()>=0)
                                    selectedElement->setY(eRt.bottom()-selectedERt.height())  ;
                                else
                                    selectedElement->setY(0);

                            }

                        }
                        selectedElement->setBrightBianbottom(true)  ;
                        element->setBrightBianbottom(true);
                        bNeedFlag=true;
                        break;
                    }
                    else
                    {
                        selectedElement->setBrightBianbottom(false);
                        element->setBrightBianbottom(false);
                    }
                }

            }
            foreach(eObject *element, list) {//左竖线，右竖线
                QRectF eRt=element->geometry();
                if(element!=selectedElement)
                {
                    if(fabs(selectedERt.x()-eRt.right())<=iCiTieSpace)
                    {
                        if(iTuoZhuaiDir==0)
                        {
                            if(selectedERt.right()>=m_iScreen.width()-iCiTieSpace)
                            {

                            }
                            else {
                                if(eRt.right()+1>=0)
                                {
                                    if(eRt.right()+1+selectedERt.width()<=m_iScreen.width())
                                        selectedElement->setX(eRt.right()+1)  ;
                                }
                                else
                                    selectedElement->setX(0)  ;

                            }

                        }
                        selectedElement->setBrightBianLeft(true)  ;
                        element->setBrightBianRight(true);
                        break;
                    }
                    else
                    {
                        if(!bNeedFlag)
                        {
                            selectedElement->setBrightBianLeft(false);
                            element->setBrightBianRight(false);

                        }

                    }
                }
            }
            foreach(eObject *element, list) {//上横线，下横线
                QRectF eRt=element->geometry();
                if(element!=selectedElement)
                {
                    if(fabs(selectedERt.y()-eRt.bottom())<=iCiTieSpace)
                    {
                        if(iTuoZhuaiDir==0)
                        {
                            if(selectedERt.bottom()>=m_iScreen.height()-iCiTieSpace)
                            {

                            }
                            else {
                                if(eRt.bottom()+1>=0)
                                {
                                    if(eRt.bottom()+1+selectedERt.height()<=m_iScreen.height())
                                        selectedElement->setY(eRt.bottom()+1)  ;
                                }
                                else
                                    selectedElement->setY(0)  ;

                            }

                        }
                        selectedElement->setBrightBianTop(true)  ;
                        element->setBrightBianbottom(true);
                        break;
                    }
                    else
                    {
                        if(!bNeedFlag)
                        {
                            selectedElement->setBrightBianTop(false);
                            element->setBrightBianbottom(false);
                        }
                    }
                }

            }

            foreach(eObject *element, list) {//右横线，左横线
                QRectF eRt=element->geometry();
                if(element!=selectedElement)
                {
                    if(fabs(selectedERt.right()-eRt.left())<=iCiTieSpace)
                    {
                        if(selectedERt.left()<=iCiTieSpace)
                        {

                        }
                        else {
                            if(iTuoZhuaiDir==0)
                            {
                                if(eRt.left()-selectedERt.width()-1>=0)
                                    selectedElement->setX(eRt.left()-selectedERt.width()-1)  ;
                                else
                                    selectedElement->setX(0)  ;

                            }

                        }
                        selectedElement->setBrightBianRight(true)  ;
                        element->setBrightBianLeft(true);
                        break;
                    }
                    else
                    {
                        if(!bNeedFlag)
                        {
                            selectedElement->setBrightBianRight(false);
                            element->setBrightBianLeft(false);
                        }
                    }
                }

            }

            foreach(eObject *element, list) {//下横线，上横线
                QRectF eRt=element->geometry();
                if(element!=selectedElement)
                {
                    if(fabs(selectedERt.bottom()-eRt.top())<=iCiTieSpace)
                    {
                        if(selectedERt.top()<=iCiTieSpace)
                        {

                        }
                        else {

                            if(iTuoZhuaiDir==0)
                            {
                                if(eRt.top()-selectedERt.height()-1>=0)
                                    selectedElement->setY(eRt.top()-selectedERt.height()-1)  ;
                                else
                                    selectedElement->setY(0)  ;

                            }
                        }

                        selectedElement->setBrightBianbottom(true)  ;
                        element->setBrightBianTop(true);
                        break;
                    }
                    else
                    {
                        if(!bNeedFlag)
                        {
                            selectedElement->setBrightBianbottom(false);
                            element->setBrightBianTop(false);
                        }
                    }
                }

            }
        }
        else {

            QList<eObject*> list = getElementList();
            foreach(eObject *element, list) {
                element->setBrightBianLeft(false);
                element->setBrightBianTop(false);
                element->setBrightBianRight(false);
                element->setBrightBianbottom(false);


            }
        }
    }
    else {
        QList<eObject*> list = getElementList();
        foreach(eObject *element, list) {
            element->setBrightBianLeft(false);
            element->setBrightBianTop(false);
            element->setBrightBianRight(false);
            element->setBrightBianbottom(false);


        }
    }


}
