#include "setworkarea.h"
#include "ui_setworkarea.h"

SetWorkArea::SetWorkArea(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetWorkArea)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::NonModal );//用以设置窗口模态类型   ,ApplicationModal阻塞整个应用程序的所有窗口
    initWidget();
    initControl();
    initArabControlPos();
}

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

void SetWorkArea::initWidget()
{
     m_headValue= 0;
     m_area = 2;
     ui->frameWorkArea->show();

     //蓝色
      buttonStyle1 = "QPushButton{outline: none;color:rgb(10,10,10);background-color:rgb(0,151,226);border:0px solid rgb(0,0,0);border-style:inset;border-radius:5px;}";
     //白色
      buttonStyle2 = "QPushButton{outline: none;color:rgb(10,10,10);background-color:rgb(250,250,250);border:0px solid rgb(0,0,0);border-style:inset;border-radius:5px;}";
     ui->buttonX1->setText(tr("X +"));
     ui->buttonX2->setText(tr("X  -"));
     ui->buttonY1->setText(tr("Y +"));
     ui->buttonY2->setText(tr("Y -"));

     ui->buttonX1->setStyleSheet(buttonStyle1);//初始化默认都是白色，根据从主控上读上来的当前位置值，显示哪个是蓝色
     ui->buttonX2->setStyleSheet(buttonStyle2);
     ui->buttonY1->setStyleSheet(buttonStyle2);
     ui->buttonY2->setStyleSheet(buttonStyle2);
}

void SetWorkArea::initControl()
{
    //根据不同分辨率设置控件的位置和尺寸
    switch (g_emResolut)
    {
    case resolution1910:
        initResolution1910();
        break;
    case resolution1006:
        initResolution1006();
        break;
    default:
        this->resize(1920,1080);
        break;
    }
     initControlStyle();//初始化窗体控件样式
}
void SetWorkArea::initResolution1910()
{
    //上状态栏
    this->resize(896,497);
    this->move((1920-this->width())/2+g_mainWidgetPos.x(),(1080-this->height())/2+g_mainWidgetPos.y());

    ui->frameBack->setGeometry(0,0,946,564);
    ui->labelTitle->setGeometry(40,2,754,56);
    //设置可移动区域
    ui->frameWorkArea->setGeometry(40,78,904,340);
    ui->buttonX1->setGeometry(300,110,160,58);//x+
    ui->buttonX2->setGeometry(10,110,160,58);//x-
    ui->buttonY1->setGeometry(150,10,160,58);//y+
    ui->buttonY2->setGeometry(150,205,160,58);//y-

    ui->buttonOk->setGeometry(522,400,174,70);
    ui->buttonCancel->setGeometry(704,400,174,70);
}
void SetWorkArea::initResolution1006()
{
    //上状态栏

    this->resize(480,268);
    this->move((1024-this->width())/2+g_mainWidgetPos.x(),(600-this->height())/2+g_mainWidgetPos.y());

    ui->frameBack->setGeometry(0,0,508,304);
    ui->labelTitle->setGeometry(5,-10,400,56);

    ui->frameWorkArea->setGeometry(40,58,440,161);
    ui->buttonX1->setGeometry(170,60,70,38);
    ui->buttonX2->setGeometry(10,60,70,38);
    ui->buttonY1->setGeometry(90,10,70,38);//y+
    ui->buttonY2->setGeometry(90,120,70,38);

    ui->buttonOk->setGeometry(274,216,96,40);
    ui->buttonCancel->setGeometry(380,216,96,40);
}
//初始化窗体控件样式
void SetWorkArea::initControlStyle()
{
    SetControlStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());

    //设置控件图标
    //背景图
    //ui->SetWorkArea->setStyleSheet(TRANSPARENTSTYLE);
   // ui->frameWorkArea->setStyleSheet(TRANSPARENTSTYLE);
    //ui->frameBack->setStyleSheet(TRANSPARENTSTYLE);
   // this->setStyleSheet(LABELWHITESTYLE);
    ui->frameBack->setStyleSheet(setControlStyle.getStyleSheet(this->objectName()));
    ui->labelTitle->setStyleSheet(LABELWHITESTYLE);
    ui->labelTitle->setFont(fontNormal_1);
    ui->labelTitle->setAlignment(Qt::AlignLeft);
    ui->labelTitle->setAlignment(Qt::AlignVCenter);
    //设置可工作区域
    ui->frameWorkArea->setStyleSheet(TRANSPARENTSTYLE);
    ui->buttonX1->setFont(fontNormal_1);
    ui->buttonX2->setFont(fontNormal_1);
    ui->buttonY1->setFont(fontNormal_1);
    ui->buttonY2->setFont(fontNormal_1);

    ui->buttonOk->setWhiteBlueBottomStyle();
    ui->buttonOk->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonOk->objectName()+"_1"),12);
    ui->buttonCancel->setWhiteBlueBottomStyle();
    ui->buttonCancel->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonCancel->objectName()+"_1"),12);
}

void SetWorkArea::initArabControlPos()
{
    int language = g_pSettings->readFromInHMIiFile("HMI/language").toInt();
    if(language != arabic)
    {
        return;
    }

    ui->labelTitle->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
}

//设置标题
void SetWorkArea::setTitleStr(QString str)
{
    ui->labelTitle->setText(str);
}
void SetWorkArea::show()
{
   QWidget::show();
}
void SetWorkArea::setHead(int m_head)
{
    m_headValue= m_head;
}
void SetWorkArea::on_buttonCancel_clicked()
{

    this->hide();
}

void SetWorkArea::on_buttonOk_clicked()
{
    if(g_pMachine != NULL)
    {
        g_pMachine->manualAction(SET_WORK_AREA,m_headValue,m_area);
    }

    //this->hide();
}

//X+
void SetWorkArea::on_buttonX1_clicked()
{
    ui->buttonX1->setStyleSheet(buttonStyle1);
    ui->buttonX2->setStyleSheet(buttonStyle2);
    ui->buttonY1->setStyleSheet(buttonStyle2);
    ui->buttonY2->setStyleSheet(buttonStyle2);

    m_area = 2;
}

//Y+
void SetWorkArea::on_buttonY1_clicked()
{
    ui->buttonX1->setStyleSheet(buttonStyle2);
    ui->buttonX2->setStyleSheet(buttonStyle2);
    ui->buttonY1->setStyleSheet(buttonStyle1);
    ui->buttonY2->setStyleSheet(buttonStyle2);
    m_area = 4;
}

//Y-
void SetWorkArea::on_buttonY2_clicked()
{
    ui->buttonX1->setStyleSheet(buttonStyle2);
    ui->buttonX2->setStyleSheet(buttonStyle2);
    ui->buttonY1->setStyleSheet(buttonStyle2);
    ui->buttonY2->setStyleSheet(buttonStyle1);
    m_area = 3;
}

void SetWorkArea::on_buttonX2_clicked()
{
    ui->buttonX1->setStyleSheet(buttonStyle2);
    ui->buttonX2->setStyleSheet(buttonStyle1);
    ui->buttonY1->setStyleSheet(buttonStyle2);
    ui->buttonY2->setStyleSheet(buttonStyle2);
    m_area =1;
}
