#include "replacebackgroundwidget.h"

ReplaceBackgroundWidget::ReplaceBackgroundWidget(QWidget *parent) : QWidget(parent)
{
    qDebug() << "width === " <<this->width() << "height === " << this->height();
    m_leftWidget = new QWidget();
    m_rightWidget = new QWidget();
    m_rightWidget->setAutoFillBackground(true);
    QPalette pal = m_rightWidget->palette();
    pal.setColor(QPalette::Background, Qt::white);
    m_rightWidget->setPalette(pal);

    m_rightTopWidget = new QWidget();
    m_rightBottomWidget = new QWidget();
    m_toolBtnWid = new QWidget();
    m_toolBtnWid->setWindowFlags(Qt::FramelessWindowHint);
    m_toolBtnWid->setFocusPolicy(Qt::NoFocus);
    m_toolWid = new QWidget();
    m_toolWid->raise();

    m_showImage = new QLabel(this);
    m_showImage->setStyleSheet("border: 1px solid black; border-style: dashed;");
    m_showImage->move(int((m_leftWidget->width() - m_leftWidget->width()) / 2),
                      int((m_leftWidget->height() - m_leftWidget->height()) / 2));
    m_showImage->setAlignment(Qt::AlignCenter);

    m_layout = new QHBoxLayout();
    m_rightLayout = new QVBoxLayout();
    m_leftLayout = new QVBoxLayout();
    m_toolBtnLayout = new QHBoxLayout();
    m_toolLayout = new QHBoxLayout();
    m_colorCustomLayout = new QHBoxLayout();
    m_colorBtnLayout = new QGridLayout();
    m_picBtnLayout = new QGridLayout();
    m_rightTopLayout = new QVBoxLayout();
    m_rightBottomLayout = new QHBoxLayout();

    //对比
    m_compare = new QPushButton(this);
    m_compare->setFocusPolicy(Qt::NoFocus);
    m_compare->setText(tr("对比"));
    m_compare->setWindowFlags(Qt::FramelessWindowHint);
    //缩放widget
    m_zoomWid = new QWidget(this);
    m_zoomWid->setFixedSize(TOOLZOOM_SIZE);
    //缩小
    m_reduce = new kdk::KBorderlessButton(m_zoomWid);
    m_reduce->setFocusPolicy(Qt::NoFocus);
    m_reduce->setToolTip(tr("缩小"));
    m_reduce->setAttribute(Qt::WA_TranslucentBackground);
    //百分比
    m_percentage = new QLabel(m_zoomWid);
    m_percentage->setFixedSize(TOOL_PER);
    m_percentage->setAlignment(Qt::AlignCenter);
    m_percentage->setAttribute(Qt::WA_TranslucentBackground);
    m_percentage->setToolTip(tr("View scale"));
    //放大
    m_enlarge = new kdk::KBorderlessButton(m_zoomWid);
    m_enlarge->setFocusPolicy(Qt::NoFocus);
    m_enlarge->setToolTip(tr("放大"));
    m_enlarge->setAttribute(Qt::WA_TranslucentBackground);
    //原尺寸
    m_originalSize = new kdk::KBorderlessButton(this);
    m_originalSize->setFocusPolicy(Qt::NoFocus);
    m_originalSize->setAttribute(Qt::WA_TranslucentBackground);
    m_originalSize->setToolTip(tr("原始尺寸"));
    //m_originalSize->setToolTip(tr("Life size"));
    //适应窗口
    m_adaptiveWidget = new kdk::KBorderlessButton(this);
    m_adaptiveWidget->setFocusPolicy(Qt::NoFocus);
    m_adaptiveWidget->setAttribute(Qt::WA_TranslucentBackground);
    m_adaptiveWidget->setToolTip(tr("图像适应窗口"));
    //m_adaptiveWidget->setToolTip(tr("Window widget"));
    //旋转
    m_rotate = new kdk::KBorderlessButton(this);
    m_rotate->setFocusPolicy(Qt::NoFocus);
    m_rotate->setAttribute(Qt::WA_TranslucentBackground);
    m_rotate->setToolTip(tr("旋转"));
    //m_rotate->setToolTip(tr("Rorate right"));
    //水平翻转
    m_flipH = new kdk::KBorderlessButton(this);
    m_flipH->setFocusPolicy(Qt::NoFocus);
    m_flipH->setAttribute(Qt::WA_TranslucentBackground);
    m_flipH->setToolTip(tr("水平镜像"));
    //m_flipH->setToolTip(tr("Flip horizontally"));
    //垂直翻转
    m_flipV = new kdk::KBorderlessButton(this);
    m_flipV->setFocusPolicy(Qt::NoFocus);
    m_flipV->setAttribute(Qt::WA_TranslucentBackground);
    m_flipV->setToolTip(tr("垂直镜像"));
    //m_flipV->setToolTip(tr("Flip vertically"));
    //裁剪
    m_cutImage = new kdk::KBorderlessButton(this);
    m_cutImage->setFocusPolicy(Qt::NoFocus);
    m_cutImage->setAttribute(Qt::WA_TranslucentBackground);
    m_cutImage->setToolTip(tr("裁剪"));
    //m_cutImage->setToolTip(tr("Crop"));


    m_bg = new QLabel(this);
    m_bg->setText(tr("背景更换"));
    m_bg->setFocusPolicy(Qt::NoFocus);
    //m_bg->setAttribute(Qt::WA_TranslucentBackground);

    m_pureColor = new QLabel(this);
    m_pureColor->setText(tr("纯色"));
    m_pureColor->setFocusPolicy(Qt::NoFocus);
    //m_pureColor->setAttribute(Qt::WA_TranslucentBackground);
    m_customBtn = new QPushButton(this);
    m_customBtn->setText("自定义");
    m_customBtn->setFocusPolicy(Qt::NoFocus);
    m_customBtn->setFixedSize(QSize(60, 24));
    //m_customBtn->setAttribute(Qt::WA_TranslucentBackground);

    m_transparent = new QPushButton(this);
    m_transparent->setFocusPolicy(Qt::NoFocus);

    m_white = new QPushButton(this);
    m_white->setFocusPolicy(Qt::NoFocus);

    m_grey = new QPushButton(this);
    m_grey->setFocusPolicy(Qt::NoFocus);

    m_dimGrey = new QPushButton(this);
    m_dimGrey->setFocusPolicy(Qt::NoFocus);

    m_dark = new QPushButton(this);
    m_dark->setFocusPolicy(Qt::NoFocus);

    m_blue = new QPushButton(this);
    m_blue->setFocusPolicy(Qt::NoFocus);

    m_red = new QPushButton(this);
    m_red->setFocusPolicy(Qt::NoFocus);

    m_darkOrange = new QPushButton(this);
    m_darkOrange->setFocusPolicy(Qt::NoFocus);

    m_orange = new QPushButton(this);
    m_orange->setFocusPolicy(Qt::NoFocus);

    m_green = new QPushButton(this);
    m_green->setFocusPolicy(Qt::NoFocus);

    m_purple = new QPushButton(this);
    m_purple->setFocusPolicy(Qt::NoFocus);

    m_pink = new QPushButton(this);
    m_pink->setFocusPolicy(Qt::NoFocus);

    m_pic = new QLabel(this);
    m_pic->setText("图片");
    m_pic->setFocusPolicy(Qt::NoFocus);

    m_addPicBtn = new QPushButton(this);
    m_addPicBtn->setFocusPolicy(Qt::NoFocus);
    m_addPicBtn->setIcon(QIcon(":/res/res/addplus.svg"));
    m_addPicBtn->setIconSize(QSize(24, 24));

    m_pic1 = new QPushButton(this);
    m_pic1->setFocusPolicy(Qt::NoFocus);

    m_pic2 = new QPushButton(this);
    m_pic2->setFocusPolicy(Qt::NoFocus);

    m_pic3 = new QPushButton(this);
    m_pic3->setFocusPolicy(Qt::NoFocus);

    m_pic4 = new QPushButton(this);
    m_pic4->setFocusPolicy(Qt::NoFocus);

    m_pic5 = new QPushButton(this);
    m_pic5->setFocusPolicy(Qt::NoFocus);

    m_saveBtn = new QPushButton(this);
    m_saveBtn->setText("保存副本");
    m_saveBtn->setFocusPolicy(Qt::NoFocus);
    m_saveBtn->setAttribute(Qt::WA_TranslucentBackground);
    m_cancelBtn= new QPushButton(this);
    m_cancelBtn->setText("取消");
    m_cancelBtn->setFocusPolicy(Qt::NoFocus);
    m_cancelBtn->setAttribute(Qt::WA_TranslucentBackground);

    //绘制阴影
    QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect(this);
    effect->setOffset(0, 0); //设置向哪个方向产生阴影效果(dx,dy)，特别地，(0,0)代表向四周发散
    effect->setColor(TOOL_COLOR);       //设置阴影颜色，也可以setColor(QColor(220,220,220))
    effect->setBlurRadius(BLUR_RADIUS); //设定阴影的模糊半径，数值越大越模糊
    m_toolBtnWid->setGraphicsEffect(effect);
    m_compare->setGraphicsEffect(effect);
    m_rightWidget->setGraphicsEffect(effect);

    this->initLayout();
    this->initConnect();
    this->initQmlObject();
    this->initGsetting();
    this->setColorBtnStyle();
    this->toolBtnIconSize(TOOL_BUTTON);
    this->colorButtonSize(COLOR_BUTTON);
    this->pictureBtnSize(PICTURE_BUTTON);
    this->setBtnIcon();
    this->platformType();
}

void ReplaceBackgroundWidget::initLayout()
{
    m_toolBtnLayout->addWidget(m_zoomWid, 0, Qt::AlignCenter);
    m_toolBtnLayout->setSpacing(20);
    m_toolBtnLayout->addWidget(m_adaptiveWidget, 0, Qt::AlignCenter);
    m_toolBtnLayout->setSpacing(20);
    m_toolBtnLayout->addWidget(m_originalSize, 0, Qt::AlignCenter);
    m_toolBtnLayout->setSpacing(20);
    m_toolBtnLayout->addWidget(m_rotate, 0, Qt::AlignCenter);
    m_toolBtnLayout->setSpacing(20);
    m_toolBtnLayout->addWidget(m_flipH, 0, Qt::AlignCenter);
    m_toolBtnLayout->setSpacing(20);
    m_toolBtnLayout->addWidget(m_flipV, 0, Qt::AlignCenter);
    m_toolBtnLayout->setSpacing(20);
    m_toolBtnLayout->addWidget(m_cutImage, 0, Qt::AlignCenter);
    m_toolBtnLayout->setContentsMargins(20, 0, 20, 0);
    m_toolBtnWid->setLayout(m_toolBtnLayout);

    m_toolLayout->addStretch();
    m_toolLayout->addWidget(m_compare);
    m_toolLayout->addWidget(m_toolBtnWid);
    m_toolLayout->addStretch();
    m_toolWid->setLayout(m_toolLayout);

    m_leftLayout->addWidget(m_showImage, 10);
    m_leftLayout->addWidget(m_toolWid, 1);
    m_leftWidget->setLayout(m_leftLayout);

    m_colorCustomLayout->addWidget(m_pureColor, 0, Qt::AlignLeft);
    m_colorCustomLayout->addWidget(m_customBtn, 0, Qt::AlignRight);

    m_colorBtnLayout->addWidget(m_transparent, 0, 0);
    m_colorBtnLayout->addWidget(m_white, 0, 1);
    m_colorBtnLayout->addWidget(m_grey, 0, 2);
    m_colorBtnLayout->addWidget(m_dimGrey, 0, 3);
    m_colorBtnLayout->addWidget(m_dark, 0, 4);
    m_colorBtnLayout->addWidget(m_blue, 0, 5);
    m_colorBtnLayout->addWidget(m_red, 1, 0);
    m_colorBtnLayout->addWidget(m_darkOrange, 1, 1);
    m_colorBtnLayout->addWidget(m_orange, 1, 2);
    m_colorBtnLayout->addWidget(m_green, 1, 3);
    m_colorBtnLayout->addWidget(m_purple, 1, 4);
    m_colorBtnLayout->addWidget(m_pink, 1, 5);
    m_colorBtnLayout->setContentsMargins(0, 0, 0, 0);

    m_picBtnLayout->addWidget(m_addPicBtn, 0, 0);
    m_picBtnLayout->addWidget(m_pic1, 0, 1);
    m_picBtnLayout->addWidget(m_pic2, 0, 2);
    m_picBtnLayout->addWidget(m_pic3, 1, 0);
    m_picBtnLayout->addWidget(m_pic4, 1, 1);
    m_picBtnLayout->addWidget(m_pic5, 1, 2);

    m_rightTopLayout->addWidget(m_bg, 0 ,Qt::AlignLeft);
    m_rightTopLayout->addLayout(m_colorCustomLayout);
    m_rightTopLayout->addLayout(m_colorBtnLayout);
    m_rightTopLayout->addWidget(m_pic, 0, Qt::AlignLeft);
    m_rightTopLayout->addLayout(m_picBtnLayout);
    m_rightTopWidget->setLayout(m_rightTopLayout);

    m_rightBottomLayout->addWidget(m_cancelBtn);
    m_rightBottomLayout->setSpacing(20);
    m_rightBottomLayout->addWidget(m_saveBtn);
    m_rightBottomWidget->setLayout(m_rightBottomLayout);

    m_rightLayout->addWidget(m_rightTopWidget);
    m_rightLayout->addStretch();
    m_rightLayout->addWidget(m_rightBottomWidget);
    m_rightLayout->setContentsMargins(0, 0, 0, 0);
    m_rightWidget->setLayout(m_rightLayout);

    m_layout->addWidget(m_leftWidget, 4);
    m_layout->addWidget(m_rightWidget, 1);
    m_layout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(m_layout);
}

void ReplaceBackgroundWidget::initConnect()
{
    //工具栏
    connect(m_reduce, &kdk::KBorderlessButton::clicked, this, &ReplaceBackgroundWidget::reduceImage);
    connect(m_enlarge, &kdk::KBorderlessButton::clicked, this, &ReplaceBackgroundWidget::enlargeImage);
    connect(m_rotate, &kdk::KBorderlessButton::clicked, this, &ReplaceBackgroundWidget::rotate);
    connect(m_originalSize, &kdk::KBorderlessButton::clicked, this, &ReplaceBackgroundWidget::originalSize);
    connect(m_adaptiveWidget, &kdk::KBorderlessButton::clicked, this, &ReplaceBackgroundWidget::adaptiveWidget);
    connect(m_flipH, &kdk::KBorderlessButton::clicked, this, &ReplaceBackgroundWidget::flipH);
    connect(m_flipV, &kdk::KBorderlessButton::clicked, this, &ReplaceBackgroundWidget::flipV);
    connect(m_cutImage, &kdk::KBorderlessButton::clicked, this, &ReplaceBackgroundWidget::cutImage);
    //纯色
    connect(m_customBtn, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    connect(m_transparent, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    connect(m_white, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    connect(m_grey, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    connect(m_dimGrey, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    connect(m_dark, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    connect(m_blue, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    connect(m_red, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    connect(m_darkOrange, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    connect(m_orange, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    connect(m_green, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    connect(m_purple, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    connect(m_pink, &QPushButton::clicked, this, &ReplaceBackgroundWidget::startReplaceBackground);
    //保存取消
    connect(m_saveBtn, &QPushButton::clicked, this, &ReplaceBackgroundWidget::needSave);
    connect(m_cancelBtn, &QPushButton::clicked, this, &ReplaceBackgroundWidget::exitReplaceBackground);

    connect(Interaction::getInstance(), &Interaction::returnReplaceImage, this, &ReplaceBackgroundWidget::getReplaceImage);
}

void ReplaceBackgroundWidget::initQmlObject()
{
    m_showImageQml = new QQuickWidget(this->m_leftWidget);
    QQmlEngine *engine = m_showImageQml->engine();
    m_showImageQml->setSource(QUrl(QStringLiteral("qrc:/ReplaceBackgroundPage.qml")));
    m_showImageQml->setClearColor(Qt::transparent);
    m_showImageQml->installEventFilter(this);
    m_showImageQml->lower();
}

void ReplaceBackgroundWidget::setBtnIcon()
{
    m_reduce->setIcon(QIcon(":/res/res/sp1/reduce.svg"));
    m_enlarge->setIcon(QIcon(":/res/res/sp1/enlarge.svg"));
    m_originalSize->setIcon(QIcon(":/res/res/sp1/originalSize.svg"));
    m_adaptiveWidget->setIcon(QIcon(":/res/res/sp1/adaptiveWidget.svg"));
    m_rotate->setIcon(QIcon(":/res/res/sp1/rotate.svg"));
    m_flipH->setIcon(QIcon(":/res/res/sp1/flipH.svg"));
    m_flipV->setIcon(QIcon(":/res/res/sp1/flipV.svg"));
    m_cutImage->setIcon(QIcon(":/res/res/sp1/cutImage.svg"));
}

void ReplaceBackgroundWidget::toolBtnIconSize(QSize size)
{
    m_reduce->setIconSize(size);
    m_enlarge->setIconSize(size);
    m_originalSize->setIconSize(size);
    m_adaptiveWidget->setIconSize(size);
    m_rotate->setIconSize(size);
    m_flipH->setIconSize(size);
    m_flipV->setIconSize(size);
    m_cutImage->setIconSize(size);
}

void ReplaceBackgroundWidget::toolButtonSize(QSize size)
{
    m_reduce->setFixedSize(size);
    m_enlarge->setFixedSize(size);
    m_originalSize->setFixedSize(size);
    m_adaptiveWidget->setFixedSize(size);
    m_rotate->setFixedSize(size);
    m_flipH->setFixedSize(size);
    m_flipV->setFixedSize(size);
    m_cutImage->setFixedSize(size);
}

void ReplaceBackgroundWidget::colorButtonSize(QSize size)
{
    m_transparent->setFixedSize(size);
    m_transparent->setIconSize(size);

    m_white->setFixedSize(size);
    m_grey->setFixedSize(size);
    m_dimGrey->setFixedSize(size);
    m_dark->setFixedSize(size);
    m_blue->setFixedSize(size);
    m_red->setFixedSize(size);
    m_darkOrange->setFixedSize(size);
    m_orange->setFixedSize(size);
    m_green->setFixedSize(size);
    m_purple->setFixedSize(size);
    m_pink->setFixedSize(size);
}

void ReplaceBackgroundWidget::pictureBtnSize(QSize size)
{
    m_addPicBtn->setFixedSize(size);
    m_pic1->setFixedSize(size);
    m_pic2->setFixedSize(size);
    m_pic3->setFixedSize(size);
    m_pic4->setFixedSize(size);
    m_pic5->setFixedSize(size);

    m_pic1->setIconSize(size);
    m_pic2->setIconSize(size);
    m_pic3->setIconSize(size);
    m_pic4->setIconSize(size);
    m_pic5->setIconSize(size);
}

void ReplaceBackgroundWidget::platformType()
{
    normalIconPath = "sp1";
    if (platForm.contains(Variable::platForm)) {
        m_toolBtnWid->resize(TOOLBAR_SIZE_INTEL);
        m_zoomWid->setFixedSize(TOOLZOOM_SIZE_INTEL);
        widRadius = "12px";
        this->toolButtonSize(TOOL_BUTTON_INTEL);
        m_percentage->setFixedSize(TOOL_PER);
        m_percentage->move(m_reduce->x() + m_reduce->width() + 5,
                           (m_reduce->height() - m_percentage->height()) / 2 - 3);
    } else {
        m_toolBtnWid->resize(TOOLBAR_SIZE);
        m_zoomWid->setFixedSize(TOOLZOOM_SIZE);
        widRadius = "6px";
        this->toolButtonSize(TOOL_BUTTON);
        m_compare->setFixedSize(QSize(64, 44));
        m_percentage->setFixedSize(QSize(40, 22));
        m_percentage->move(m_reduce->x() + m_reduce->width() + 5, 0);
    }
    m_enlarge->move(m_zoomWid->width() - m_enlarge->width(), 0);
}

void ReplaceBackgroundWidget::initGsetting()
{
    connect(kdk::kabase::Gsettings::getPoint(), &kdk::kabase::Gsettings::systemThemeChange, this, [=]() {
        changeStyle();
    });
    changeStyle();
    return;
}

void ReplaceBackgroundWidget::changeStyle()
{
    nowThemeStyle = kdk::kabase::Gsettings::getSystemTheme().toString();
    if ("ukui-dark" == nowThemeStyle || "ukui-black" == nowThemeStyle) {
        m_toolBtnWid->setStyleSheet("QWidget{background-color:rgba(0,0,0,0.66);"
                                 "border:none;"
                                 "border-radius:" + widRadius + ";}");
        m_zoomWid->setStyleSheet("QWidget{background-color:transparent;border:none;}");
        m_compare->setStyleSheet("background:rgba(0,0,0,0.66);"
                                 "border:none;"
                                 "border-radius:" + widRadius + ";");
        m_percentage->setStyleSheet("QLabel{background-color:transparent;color:rgb(255,255,255);}");
    } else {
        m_zoomWid->setStyleSheet("QWidget{background-color:transparent;border:none;}");
        m_toolBtnWid->setStyleSheet("QWidget{background-color:rgba(255,255,255,);"
                                    "border:none;"
                                    "border-radius:" + widRadius + ";}");
        m_compare->setStyleSheet("background:rgba(255,255,255,);"
                                 "border:none;"
                                 "border-radius:" + widRadius + ";");
        m_percentage->setStyleSheet("QLabel{background-color:transparent;color:rgb(0,0,0);}");
    }
}

void ReplaceBackgroundWidget::setColorBtnStyle()
{
    m_customBtn->setStyleSheet("border-radius: 4px;");
    m_white->setStyleSheet("QPushButton{background: rgb(255, 255, 255); border:1px solid rgb(190, 190, 190); "
                              "border-radius: 4px;}");
    m_grey->setStyleSheet("QPushButton{background: rgb(190, 190, 190); border: none; border-radius: 4px;}");
    m_dimGrey->setStyleSheet("QPushButton{background: rgb(105, 105, 105); border: none; border-radius: 4px;}");
    m_dark->setStyleSheet("QPushButton{background: rgb(0, 0, 0); border: none; border-radius: 4px;}");
    m_blue->setStyleSheet("QPushButton{background: rgb(0, 0, 255); border: none; border-radius: 4px;}");
    m_red->setStyleSheet("QPushButton{background: rgb(255, 0, 0); border: none; border-radius: 4px;}");
    m_darkOrange->setStyleSheet("QPushButton{background: rgb(255, 127, 0); border: none; border-radius: 4px;}");
    m_orange->setStyleSheet("QPushButton{background: rgb(255, 165, 0); border: none; border-radius: 4px;}");
    m_green->setStyleSheet("QPushButton{background: rgb(0, 255, 0); border: none; border-radius: 4px;}");
    m_purple->setStyleSheet("QPushButton{background: rgb(160, 32, 240); border: none; border-radius: 4px;}");
    m_pink->setStyleSheet("QPushButton{background: rgb(255, 105, 180); border: none; border-radius: 4px;}");

    //m_addPicBtn->setStyleSheet("border: 1px solid black; border-style: dashed; border-radius: 4px;");
    m_pic1->setStyleSheet("border-image:url(:/res/res/backgroundpictures/1.jpg);"
                          "border: none;"
                          "border-radius: 4px;");
    m_pic2->setStyleSheet("border-image:url(:/res/res/backgroundpictures/2.jpg);"
                          "border: none;"
                          "border-radius: 4px;");
    m_pic3->setStyleSheet("border-image:url(:/res/res/backgroundpictures/3.jpg);"
                          "border: none;"
                          "border-radius: 4px;");
    m_pic4->setStyleSheet("border-image:url(:/res/res/backgroundpictures/4.jpg);"
                          "border: none;"
                          "border-radius: 4px;");
    m_pic5->setStyleSheet("border-image:url(:/res/res/backgroundpictures/5.jpg);"
                          "border: none;"
                          "border-radius: 4px;");
}

void ReplaceBackgroundWidget::reduceImage()
{

}
void ReplaceBackgroundWidget::enlargeImage()
{

}
void ReplaceBackgroundWidget::originalSize()
{

}
void ReplaceBackgroundWidget::adaptiveWidget()
{

}
void ReplaceBackgroundWidget::rotate()
{

}
void ReplaceBackgroundWidget::flipH()
{

}
void ReplaceBackgroundWidget::flipV()
{

}
void ReplaceBackgroundWidget::cutImage()
{

}

void ReplaceBackgroundWidget::startReplaceBackground()
{
    //Vec3b color;
    unsigned blue, green, red;
    QPushButton *btn = qobject_cast<QPushButton*>(sender());
    if (btn == this->m_transparent){

    } else if (btn == this->m_white){
        red = 255;
        green = 255;
        blue = 255;
    } else if (btn == this->m_grey){
        red = 190;
        green = 190;
        blue = 190;
    } else if (btn == this->m_dimGrey){
        red = 105;
        green = 105;
        blue = 105;
    } else if (btn == this->m_dark){
        red = 0;
        green = 0;
        blue = 0;
    } else if (btn == this->m_blue){
        red = 0;
        green = 0;
        blue = 255;
    } else if (btn == this->m_red){
        red = 255;
        green = 0;
        blue = 0;
    } else if (btn == this->m_darkOrange){
        red = 255;
        green = 127;
        blue = 0;
    } else if (btn == this->m_orange){
        red = 255;
        green = 165;
        blue = 0;
    } else if (btn == this->m_green){
        red = 0;
        green = 255;
        blue = 0;
    } else if (btn == this->m_purple){
        red = 160;
        green = 32;
        blue = 240;
    } else if (btn == this->m_pink){
        red = 255;
        green = 105;
        blue = 180;
    } else {
        QColor color = QColorDialog::getColor(Qt::green);
        if(color.isValid()){
            blue = color.blue();
            green = color.green();
            red = color.red();
        } else {
            QMessageBox::information(NULL, "error message", "set color error");
            return;
        }
    }
    //在 OpenCV 中，颜色为 BGR
//    color[0] = blue;
//    color[1] = green;
//    color[2] = red;
    //Q_EMIT backgroundColor(blue, green, red);
    Interaction::getInstance()->replaceBackground(blue, green, red);
}

void ReplaceBackgroundWidget::getReplaceImage(QImage image)
{
    m_showImage->setPixmap((QPixmap::fromImage(image)).scaled(m_showImage->size(),
                                                              Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

void ReplaceBackgroundWidget::resizeEvent(QResizeEvent *event)
{
    //m_showImage->setFixedWidth(this->width() / 4 * 3);
//    m_rightBottomWidget->setFixedWidth(this->width() / 4);
//    m_rightMidWidget->setFixedWidth(this->width() / 4);
//    m_rightBottomWidget->setFixedWidth(this->width() / 4);
}

void ReplaceBackgroundWidget::needSave()
{
    Interaction::getInstance()->needSaveReplaceImage();
    this->exitReplaceBackground();
}

void ReplaceBackgroundWidget::exitReplaceBackground()
{
    Q_EMIT toBackShowImageWid();
}
