#include "replacebackgroundwidget.h"

ReplaceBackgroundWidget::ReplaceBackgroundWidget(QWidget *parent) : QWidget(parent)
{
    this->resize(QSize(1080, 680));
    m_showImage = new QLabel();
    m_showImage->setStyleSheet("QLabel{border-width:1px;border-style:solid;border-color:grey;}");
    m_showImage->resize(QSize(813, 571));
//    m_showImage->setMinimumSize(QSize(813, 571));
//    QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
//    m_showImage->setSizePolicy(sizePolicy);
    m_showImage->setAlignment(Qt::AlignCenter);

    m_leftWidget = new QWidget();

    m_rightStackWidget = new QStackedWidget();
    m_leftStackWidget = new QStackedWidget();

    m_toolBtnWid = new QWidget();
    m_toolWid = new QWidget();

    m_layout = new QHBoxLayout();
    m_leftLayout = new QVBoxLayout();
    m_toolBtnLayout = new QHBoxLayout();
    m_toolLayout = new QHBoxLayout();

    //对比
    m_compare = new kdk::KPushButton();
    m_compare->setFocusPolicy(Qt::NoFocus);
    m_compare->installEventFilter(this);
    m_compare->setBackgroundColor(Qt::white);
    m_compare->setText(tr("对比"));
    m_compare->setWindowFlags(Qt::FramelessWindowHint);
    m_compare->setToolTip(tr("长按查看原图"));
    //缩放widget
    m_zoomWid = new QWidget();
    m_zoomWid->setFixedSize(TOOLZOOM_SIZE);
    //缩小
    m_reduce = new kdk::KBorderlessButton(m_zoomWid);
    m_reduce->setFocusPolicy(Qt::NoFocus);
    m_reduce->setToolTip(tr("Zoom out"));
    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("Zoom in"));
    m_enlarge->setAttribute(Qt::WA_TranslucentBackground);
    //原尺寸
    m_originalSize = new kdk::KBorderlessButton();
    m_originalSize->setFocusPolicy(Qt::NoFocus);
    m_originalSize->setAttribute(Qt::WA_TranslucentBackground);
    m_originalSize->setToolTip(tr("Life size"));
    //适应窗口
    m_adaptiveWidget = new kdk::KBorderlessButton();
    m_adaptiveWidget->setFocusPolicy(Qt::NoFocus);
    m_adaptiveWidget->setAttribute(Qt::WA_TranslucentBackground);
    m_adaptiveWidget->setToolTip(tr("Window widget"));
    //旋转
    m_rotate = new kdk::KBorderlessButton();
    m_rotate->setFocusPolicy(Qt::NoFocus);
    m_rotate->setAttribute(Qt::WA_TranslucentBackground);
    m_rotate->setToolTip(tr("Rorate right"));
    //水平翻转
    m_flipH = new kdk::KBorderlessButton();
    m_flipH->setFocusPolicy(Qt::NoFocus);
    m_flipH->setAttribute(Qt::WA_TranslucentBackground);
    m_flipH->setToolTip(tr("Flip horizontally"));
    //垂直翻转
    m_flipV = new kdk::KBorderlessButton();
    m_flipV->setFocusPolicy(Qt::NoFocus);
    m_flipV->setAttribute(Qt::WA_TranslucentBackground);
    m_flipV->setToolTip(tr("Flip vertically"));
    //裁剪
    m_cutImage = new kdk::KBorderlessButton();
    m_cutImage->setFocusPolicy(Qt::NoFocus);
    m_cutImage->setAttribute(Qt::WA_TranslucentBackground);
    m_cutImage->setToolTip(tr("Crop"));

    this->initMattingWidget();
    this->initCutWidget();

    //绘制阴影
    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_mattingWidget->setGraphicsEffect(effect);
    m_cutWidget->setGraphicsEffect(effect);
    m_rightStackWidget->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();
    this->setFormatedText();
}

void ReplaceBackgroundWidget::initMattingWidget()
{
    m_mattingWidget = new QWidget();
    m_mattingWidget->setAutoFillBackground(true);
    QPalette pal = m_mattingWidget->palette();
    pal.setColor(QPalette::Background, Qt::white);
    m_mattingWidget->setPalette(pal);

    m_mattingTopWidget = new QWidget();
    m_mattingMidWidget = new QWidget();
    m_mattingBottomWidget = new QWidget();

    m_mattingLayout = new QVBoxLayout();
    m_mattingTopLayout = new QHBoxLayout();
    m_mattingMidLayout = new QVBoxLayout();
    m_mattingBottomLayout = new QHBoxLayout();
    m_customBtnLayout = new QHBoxLayout();
    m_colorCustomLayout = new QHBoxLayout();
    m_colorBtnLayout = new QGridLayout();
    m_picBtnLayout = new QGridLayout();

    m_bgLabel1 = new QLabel();
    m_bgLabel1->setPixmap(QPixmap(":/res/res/ai/changebackground.svg"));
    m_bgLabel2 = new QLabel();
    m_bgLabel2->setText(tr("背景更换"));

    m_pullDownBtnBg = new QPushButton();
    m_pullDownBtnBg->setFocusPolicy(Qt::NoFocus);
    m_pullDownBtnBg->setFixedSize(16, 16);
    m_pullDownBtnBg->setAttribute(Qt::WA_TranslucentBackground);

    m_pureColor = new QLabel();
    m_pureColor->setText(tr("纯色"));
    m_pureColor->setFocusPolicy(Qt::NoFocus);
    m_pureColor->setAttribute(Qt::WA_TranslucentBackground);
    m_pureColor->setStyleSheet("QLabel{color:grey;}");

    m_customBtn = new QPushButton();
    m_customBtn->setFocusPolicy(Qt::NoFocus);
    m_customBtn->setFixedSize(QSize(60, 24));
    m_customBtn->setAttribute(Qt::WA_TranslucentBackground);

    m_customBtnLeft = new QLabel();
    m_customBtnLeft->setFocusPolicy(Qt::NoFocus);
    m_customBtnLeft->setFixedSize(QSize(24, 24));

    m_customBtnRight = new QLabel();
    m_customBtnRight->setFocusPolicy(Qt::NoFocus);
    m_customBtnRight->setFixedSize(QSize(36, 24));

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

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

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

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

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

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

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

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

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

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

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

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

    m_pic = new QLabel();
    m_pic->setText("图片");
    m_pic->setFocusPolicy(Qt::NoFocus);
    m_pic->setStyleSheet("QLabel{color:grey;}");

    m_addPicBtn = new kdk::KPushButton();
    m_addPicBtn->setFocusPolicy(Qt::NoFocus);
    m_addPicBtn->setIcon(QIcon(":/res/res/addplus.svg"));
    m_addPicBtn->setIconSize(QSize(24, 24));
    m_addPicBtn->setBackgroundColor(QColor(232, 232, 232));

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

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

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

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

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

    m_list << m_addPicBtn << m_pic1 << m_pic2 << m_pic3 << m_pic4 << m_pic5;

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

void ReplaceBackgroundWidget::initCutWidget()
{
    m_cutWidget = new QWidget();
    m_cutWidget->setAutoFillBackground(true);
    QPalette pal = m_cutWidget->palette();
    pal.setColor(QPalette::Background, Qt::white);
    m_cutWidget->setPalette(pal);

    m_cutTopWid = new QWidget();
    m_cutMidWid = new QWidget();
    m_cutBottomWid = new QWidget();
    m_sizeWid = new QListWidget();
    m_scaleWid = new QListWidget();

    m_cutTopLayout = new QHBoxLayout();
    m_cutMidLayout = new QVBoxLayout();
    m_sizeLayout = new QVBoxLayout();
    m_cutBottomLayout = new QVBoxLayout();
    m_scaleLayout = new QVBoxLayout();
    m_cutLayout = new QVBoxLayout();
    m_cutBtnLayout = new QHBoxLayout();
    m_cutToolBtnLayout = new QHBoxLayout();


    m_cutLabel1 = new QLabel();
    m_cutLabel1->setPixmap(QPixmap(":/res/res/ai/cutImage.svg"));
    m_cutLabel2 = new QLabel();
    m_cutLabel2->setText(tr("尺寸裁剪"));

    m_pullDownBtnCut = new QPushButton();
    m_pullDownBtnCut->setFocusPolicy(Qt::NoFocus);
    m_pullDownBtnCut->setFixedSize(16, 16);
    m_pullDownBtnCut->setAttribute(Qt::WA_TranslucentBackground);

    m_sizeLabel = new QLabel();
    m_sizeLabel->setText("尺寸");
    m_sizeLabel->setFocusPolicy(Qt::NoFocus);
    m_sizeLabel->setStyleSheet("QLabel{color:grey;}");

    m_lifeSize = new QListWidgetItem(m_sizeWid);
    m_lifeSize->setText(tr("原图尺寸"));
    m_lifeSize->setSizeHint(QSize(0, 36));
    m_freeSize = new QListWidgetItem(m_sizeWid);
    m_freeSize->setText(tr("自由尺寸"));
    m_freeSize->setSizeHint(QSize(0, 36));
    m_standardOneInch = new QListWidgetItem(m_sizeWid);
    m_standardOneInch->setText(tr("标准1寸：295*413px"));
    m_standardOneInch->setSizeHint(QSize(0, 36));
    m_standardTwoInch = new QListWidgetItem(m_sizeWid);
    m_standardTwoInch->setText(tr("标准2寸：413*626px"));
    m_standardTwoInch->setSizeHint(QSize(0, 36));
    m_smallOneInch = new QListWidgetItem(m_sizeWid);
    m_smallOneInch->setText(tr("小一寸：259*377px"));
    m_smallOneInch->setSizeHint(QSize(0, 36));
    m_smallTwoInch = new QListWidgetItem(m_sizeWid);
    m_smallTwoInch->setText(tr("小二寸：413*531px"));
    m_smallTwoInch->setSizeHint(QSize(0, 36));
    m_idCardSize = new QListWidgetItem(m_sizeWid);
    m_idCardSize->setText(tr("身份证照：358*441px"));
    m_idCardSize->setSizeHint(QSize(0, 36));
    m_sizeWid->setFixedHeight(255);
    m_sizeWid->setStyleSheet("QListWidget{background:rgb(240, 240, 240);border:none}");

    m_scaleLabel = new QLabel();
    m_scaleLabel->setText("比例");
    m_scaleLabel->setFocusPolicy(Qt::NoFocus);
    m_scaleLabel->setStyleSheet("QLabel{color:grey;}");

    m_originalScale = new QListWidgetItem(m_scaleWid);
    m_originalScale->setText(tr("原比例"));
    m_originalScale->setSizeHint(QSize(0, 36));
    m_fourToThree = new QListWidgetItem(m_scaleWid);
    m_fourToThree->setText(tr("4:3 标准横屏"));
    m_fourToThree->setSizeHint(QSize(0, 36));
    m_threeTofour = new QListWidgetItem(m_scaleWid);
    m_threeTofour->setText(tr("3:4 标准竖屏"));
    m_threeTofour->setSizeHint(QSize(0, 36));
    m_sixteenToNine = new QListWidgetItem(m_scaleWid);
    m_sixteenToNine->setText(tr("16:9 视频横屏"));
    m_sixteenToNine->setSizeHint(QSize(0, 36));
    m_nineToSixteen = new QListWidgetItem(m_scaleWid);
    m_nineToSixteen->setText(tr("9:16 视屏竖屏"));
    m_nineToSixteen->setSizeHint(QSize(0, 36));
    m_oneToOne = new QListWidgetItem(m_scaleWid);
    m_oneToOne->setText(tr("1:1 正方形"));
    m_oneToOne->setSizeHint(QSize(0, 36));
    m_scaleWid->setFixedHeight(220);
    m_scaleWid->setStyleSheet("QListWidget{background:rgb(240, 240, 240);border:none}");

    m_cutToolBtnWid = new QWidget();
    m_cutToolBtnWid->hide();
    m_cutBtnWid = new QWidget();
    m_cutBtnWid->setFixedSize(80, 40);
    m_cutCancel = new QPushButton();
    m_cutCancel->setFixedSize(24, 24);
    m_cutCancel->setToolTip(tr("Cancel"));
    m_cutCancel->setStyleSheet(
        "QPushButton{border:0px;border-radius:4px;background:transparent;border-image: url(:/res/res/cancel.png);}"
        "QPushButton::hover{border:0px;border-radius:4px;background:transparent;border-image: "
        "url(:/res/res/cancel_hover.png);}"
        "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;border-image: "
        "url(:/res/res/cancel_hover.png);}");
    m_cutSave = new QPushButton();
    m_cutSave->setFixedSize(24, 24);
    m_cutSave->setToolTip(tr("Save"));
    m_cutSave->setStyleSheet(
        "QPushButton{border:0px;border-radius:4px;background:transparent;border-image: url(:/res/res/save.png);}"
        "QPushButton::hover{border:0px;border-radius:4px;background:transparent;border-image: "
        "url(:/res/res/save_hover.png);}"
        "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;border-image: "
        "url(:/res/res/save_hover.png);}");

}

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_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_cutBtnLayout->addWidget(m_cutCancel);
    m_cutBtnLayout->addWidget(m_cutSave);
    m_cutBtnWid->setLayout(m_cutBtnLayout);
    m_cutToolBtnLayout->addStretch();
    m_cutToolBtnLayout->addWidget(m_cutBtnWid);
    m_cutToolBtnLayout->addStretch();
    m_cutToolBtnWid->setLayout(m_cutToolBtnLayout);

    m_leftStackWidget->addWidget(m_toolWid);
    m_leftStackWidget->addWidget(m_cutToolBtnWid);

    m_leftLayout->addWidget(m_showImage, 10);
    m_leftLayout->addWidget(m_leftStackWidget, 1);
    m_leftLayout->setContentsMargins(20, 30, 10, 10);
    m_leftWidget->setLayout(m_leftLayout);

    m_mattingTopLayout->addWidget(m_bgLabel1);
    m_mattingTopLayout->addWidget(m_bgLabel2);
    m_mattingTopLayout->addStretch();
    m_mattingTopLayout->addWidget(m_pullDownBtnBg);
    m_mattingTopLayout->setContentsMargins(10, 0, 10, 0);
    m_mattingTopWidget->setLayout(m_mattingTopLayout);

    m_customBtnLayout->addWidget(m_customBtnLeft);
    m_customBtnLayout->addWidget(m_customBtnRight);
    m_customBtnLayout->setSpacing(0);
    m_customBtnLayout->setContentsMargins(0, 0, 0, 0);
    m_customBtn->setLayout(m_customBtnLayout);

    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_picBtnLayout->setContentsMargins(0, 0, 0, 0);

    m_mattingMidLayout->addLayout(m_colorCustomLayout);
    m_mattingMidLayout->addLayout(m_colorBtnLayout);
    m_mattingMidLayout->addWidget(m_pic, 0, Qt::AlignLeft);
    m_mattingMidLayout->addLayout(m_picBtnLayout);
    m_mattingMidLayout->setContentsMargins(10, 0, 10, 0);
    m_mattingMidWidget->setLayout(m_mattingMidLayout);

    m_mattingBottomLayout->addWidget(m_cancelBtn);
    m_mattingBottomLayout->setSpacing(20);
    m_mattingBottomLayout->addWidget(m_saveBtn);
    m_mattingBottomLayout->setContentsMargins(10, 0, 10, 10);
    m_mattingBottomWidget->setLayout(m_mattingBottomLayout);

    m_mattingLayout->addWidget(m_mattingTopWidget);
    m_mattingLayout->addWidget(m_mattingMidWidget);
    m_mattingLayout->addStretch();
    m_mattingLayout->addWidget(m_mattingBottomWidget);
    m_mattingLayout->setContentsMargins(0, 0, 0, 0);
    m_mattingWidget->setLayout(m_mattingLayout);

    m_cutTopLayout->addWidget(m_cutLabel1);
    m_cutTopLayout->addWidget(m_cutLabel2);
    m_cutTopLayout->addStretch();
    m_cutTopLayout->addWidget(m_pullDownBtnCut);
    m_cutTopLayout->setContentsMargins(10, 0, 10, 0);
    m_cutTopWid->setLayout(m_cutTopLayout);

    m_cutMidLayout->addWidget(m_sizeLabel, 0, Qt::AlignLeft);
    m_cutMidLayout->addWidget(m_sizeWid);
    m_cutMidLayout->setContentsMargins(10, 0, 10, 0);
    m_cutMidWid->setLayout(m_cutMidLayout);

    m_cutBottomLayout->addWidget(m_scaleLabel, 0, Qt::AlignLeft);
    m_cutBottomLayout->addWidget(m_scaleWid);
    m_cutBottomLayout->setContentsMargins(10, 0, 10, 0);
    m_cutBottomWid->setLayout(m_cutBottomLayout);

    m_cutLayout->addWidget(m_cutTopWid);
    m_cutLayout->addWidget(m_cutMidWid);
    m_cutLayout->addWidget(m_cutBottomWid);
    m_cutLayout->addStretch();
    m_cutLayout->setSpacing(10);
    m_cutLayout->setContentsMargins(0, 0, 0, 0);
    m_cutWidget->setLayout(m_cutLayout);

    m_rightStackWidget->addWidget(m_mattingWidget);
    m_rightStackWidget->addWidget(m_cutWidget);

    m_layout->addWidget(m_leftWidget, 4);
    m_layout->addWidget(m_rightStackWidget, 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);
    //收起or显示切换背景按钮
    connect(m_pullDownBtnBg, &QPushButton::clicked, this, &ReplaceBackgroundWidget::toHideOrShow);
    //添加图片
    connect(m_addPicBtn, &kdk::KPushButton::clicked, this, &ReplaceBackgroundWidget::addPicture);
    //纯色
    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::returnReplaceAndOrigImage, this, &ReplaceBackgroundWidget::getReplaceAndOrigImage);

    connect(m_sizeWid, &QListWidget::itemClicked, this, &ReplaceBackgroundWidget::onSizeItemClicked);
    connect(m_scaleWid, &QListWidget::itemClicked, this, &ReplaceBackgroundWidget::onScaleItemClicked);
    connect(m_cutCancel, &QPushButton::clicked, this, &ReplaceBackgroundWidget::toMattingPage);
}

void ReplaceBackgroundWidget::setBtnIcon()
{
    m_transparent->setIcon(QIcon(":/res/res/ai/transparent.svg"));

    m_compare->setIcon(QIcon(":/res/res/ai/compare.svg"));
    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_compare->setIconSize(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(80, 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("QPushButton{background:rgba(0,0,0,0.66);"
                                 "border:none;"
                                 "border-radius:" + widRadius + ";}");
        m_percentage->setStyleSheet("QLabel{background-color:transparent;color:rgb(255,255,255);}");
        m_cutBtnWid->setStyleSheet("QWidget{background-color:rgba(0,0,0,0.72);border-radius:6px;}");
    } else {
        m_zoomWid->setStyleSheet("QWidget{background-color:transparent;border:none;}");
        m_toolBtnWid->setStyleSheet("QWidget{background-color:rgba(255,255,255,1);"
                                    "border:none;"
                                    "border-radius:" + widRadius + ";}");
        m_compare->setStyleSheet("QPushButton{background:rgb(255,255,255);"
                                 "border:none;"
                                 "border-radius:" + widRadius + ";}");
        m_percentage->setStyleSheet("QLabel{background-color:transparent;color:rgb(0,0,0);}");
        m_cutBtnWid->setStyleSheet("QWidget{background-color:rgba(255, 255, 255, 1);border-radius:6px;}");
    }
}

void ReplaceBackgroundWidget::setColorBtnStyle()
{
    m_pullDownBtnBg->setStyleSheet("QPushButton{border-image:url(:/res/res/ai/arrowdown.svg);"
                                 "background-color:transparent;"
                                 "border:none;"
                                 "border-radius: 4px;}");
    m_pullDownBtnCut->setStyleSheet("QPushButton{border-image:url(:/res/res/ai/arrowdown.svg);"
                                    "background-color:transparent;"
                                    "border:none;"
                                    "border-radius: 4px;}");
    m_customBtn->setStyleSheet("QPushButton{background-color:transparent;"
                               "border:none;"
                               "border-radius: 4px;}");
    m_customBtnLeft->setStyleSheet("QLabel{border-image: url(:/res/res/ai/colorwheel.png);"
                                   "background-color: transparent;"
                                   "border: none;"
                                   "}");
    m_customBtnRight->setStyleSheet("QLabel{border-image: url(:/res/res/ai/transparentselector.svg);"
                                    "background-color: transparent;"
                                    "border: none;"
                                    "}");
    m_transparent->setStyleSheet("QPushButton{background-color:transparent;border:none;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("background-color:rgb(232, 232, 232);border:none;border-radius:4px;"
//                               "hover{background-color:rgb(232, 232, 232);border:none;color:#0062F0;border-radius:4px;"
//                               "pressed{background-color:rgb(232, 232, 232);color:#0062F0;border:none;border-radius:4px;");
    m_pic1->setStyleSheet("QPushButton{border-image:url(:/res/res/backgroundpictures/1.jpg);"
                          "border:none;"
                          "border-radius:4px;}");
    m_pic2->setStyleSheet("QPushButton{border-image:url(:/res/res/backgroundpictures/2.jpg);"
                          "border:none;"
                          "border-radius:4px;}");
    m_pic3->setStyleSheet("QPushButton{border-image:url(:/res/res/backgroundpictures/3.jpg);"
                          "border:none;"
                          "border-radius:4px;}");
    m_pic4->setStyleSheet("QPushButton{border-image:url(:/res/res/backgroundpictures/4.jpg);"
                          "border:none;"
                          "border-radius:4px;}");
    m_pic5->setStyleSheet("QPushButton{border-image:url(:/res/res/backgroundpictures/5.jpg);"
                          "border:none;"
                          "border-radius:4px;}");

    m_saveBtn->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:#0062F0;color:white;}"
                             "QPushButton::pressed{border:0px;border-radius:4px;background:#0062F0;color:white;}");

//    m_compare->setStyleSheet("QPushButton{background-color:rgb(255,255,255);border:none;border-radius:" + widRadius + ";}"
//                             "QPushButton:hover{background-color:rgb(255,255,255);color:#0062F0;border:none;border-radius:" + widRadius + ";}"
//                             "QPushButton:pressed{background-color:rgb(255,255,255);color:#0062F0;border:none;border-radius:" + widRadius + ";}");
}

void ReplaceBackgroundWidget::addPicture()
{
    QFileDialog m_fileDialog;
    m_fileDialog.update();
    //设置视图模式
    m_fileDialog.setViewMode(QFileDialog::Detail);
    //设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFiles
    m_fileDialog.setFileMode(QFileDialog::ExistingFiles);
    m_fileDialog.setOption(QFileDialog::HideNameFilterDetails);

    QString file_path;
    QString format;
    QString openFile = tr("添加图片");

    //构造打开条件
    format = "(";
    for (const QString &str : Variable::SUPPORT_FORMATS)
        format += "*." + str + " ";
    format += ");;";
    //所有类型都放在一起，类型名显示不全，故增加分层，将同一类型的放在一起，分层显示
    format = format + Variable::SUPPORT_FORMATS_CLASSIFY;
    file_path = m_fileDialog.getOpenFileName(this, openFile, Variable::getSettings("imagePath").toString(), format);
    if (file_path.isEmpty()) {
        return;
    }

    QPushButton *newPicBtn = new QPushButton(this);
    newPicBtn->setFixedSize(PICTURE_BUTTON);
    newPicBtn->setStyleSheet("QPushButton{border-image:url(" + file_path +");"
                             "border:none;"
                             "border-radius:4px;}");
    int row = 0;
    int col = 0;
    m_list.insert(1, newPicBtn);
    for(int i = 0; i < m_list.size(); i++) {
        m_picBtnLayout->addWidget(m_list[i], row, col++);
        if (col == 3) {
            col = 0;
            row++;
        }
    }
}

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::toHideOrShow()
{

}

void ReplaceBackgroundWidget::reduceImage()
{

}

void ReplaceBackgroundWidget::enlargeImage()
{

}

void ReplaceBackgroundWidget::originalSize()
{

}

void ReplaceBackgroundWidget::adaptiveWidget()
{

}

void ReplaceBackgroundWidget::rotate()
{
    Interaction::getInstance()->mattingRotate();
}

void ReplaceBackgroundWidget::flipH()
{
    Interaction::getInstance()->mattingFlipH();
}

void ReplaceBackgroundWidget::flipV()
{
    Interaction::getInstance()->mattingFlipV();
}

void ReplaceBackgroundWidget::cutImage()
{
    m_rightStackWidget->setCurrentIndex(1);
    m_leftStackWidget->setCurrentIndex(1);
}

void ReplaceBackgroundWidget::toMattingPage()
{
    m_rightStackWidget->setCurrentIndex(0);
    m_leftStackWidget->setCurrentIndex(0);
}

void ReplaceBackgroundWidget::onSizeItemClicked(QListWidgetItem *item)
{
    if(item == m_lifeSize){
        qDebug() << "原图尺寸";

    } else if(item == m_freeSize){
        qDebug() << "自由尺寸";

    } else if(item == m_standardOneInch){
        qDebug() << "标准一寸";

    } else if(item == m_standardTwoInch){
        qDebug() << "标准二寸";

    } else if(item == m_smallOneInch){
        qDebug() << "小一寸";

    } else if(item == m_smallTwoInch){
        qDebug() << "小二寸";

    } else if(item == m_idCardSize){
        qDebug() << "身份证照";

    }
}

void ReplaceBackgroundWidget::onScaleItemClicked(QListWidgetItem *item)
{
    if(item == m_originalScale){
        qDebug() << "原比例";

    } else if(item == m_fourToThree){
        qDebug() << "4：3";

    } else if(item == m_threeTofour){
        qDebug() << "3：4";

    } else if(item == m_sixteenToNine){
        qDebug() << "16：9";

    } else if(item == m_nineToSixteen){
        qDebug() << "9：16";

    } else if(item == m_oneToOne){
        qDebug() << "1：1";

    }
}

void ReplaceBackgroundWidget::setFormatedText()
{
    // 创建遮罩Widget
    m_maskWidget = new QWidget(m_showImage);
    m_maskWidget->setFixedSize(m_showImage->size());
    m_maskWidget->setStyleSheet("QWidget{background-color: rgba(0, 0, 0, 80);}");
    m_maskWidget->hide();

    // 在遮罩Widget上显示文本
    m_loadingLabel = new QLabel(m_maskWidget);
    m_loadingLabel->setText("智能抠图中...");
    m_loadingLabel->setStyleSheet("QLabel{background-color:transparent;border:none;color: white;}");
    m_loadingLabel->move((m_maskWidget->width() - m_loadingLabel->width()) / 2,
                       (m_maskWidget->height() - m_loadingLabel->height()) / 2);
}

void ReplaceBackgroundWidget::startReplaceBackground()
{
    //Vec3b color;
    unsigned blue, green, red;
    QPushButton *btn = qobject_cast<QPushButton*>(sender());
    if (btn != m_customBtn) {
        m_maskWidget->show();
    }
    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 if (btn == this->m_customBtn){
        m_colorDialog = new ColorDialog(this);
        connect(m_colorDialog, &ColorDialog::colorSelected, this, &ReplaceBackgroundWidget::colorSelected);
        QRect customBtnRect = m_customBtn->geometry();
        QPoint globalCustomBtnTopLeft = m_customBtn->mapToGlobal(customBtnRect.topLeft());
        QString platform = QGuiApplication::platformName();
        if (platform.startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) {
            int x = globalCustomBtnTopLeft.x() - m_colorDialog->width();
            int y = globalCustomBtnTopLeft.y();
            kdk::WindowManager::setGeometry(m_colorDialog->windowHandle(),QRect(QPoint(x,y),QSize(m_colorDialog->width(),m_colorDialog->height())));
        } else {
            int x = globalCustomBtnTopLeft.x() - m_colorDialog->width() * 1.5;
            int y = globalCustomBtnTopLeft.y() - 6;
            m_colorDialog->move(x, y);
        }
        m_colorDialog->setFocus();
        m_colorDialog->show();
        return;
    }
    Interaction::getInstance()->replaceBackground(blue, green, red);
}

void ReplaceBackgroundWidget::colorSelected(QColor color)
{
    m_maskWidget->show();
    QString labelQss = QString("QLabel{background: %1;"
                               "border-top-right-radius:4px;"
                               "border-bottom-right-radius:4px}").arg(color.name());
    m_customBtnRight->setStyleSheet(labelQss);

    unsigned blue, green, red;
    blue = color.blue();
    green = color.green();
    red = color.red();
    Interaction::getInstance()->replaceBackground(blue, green, red);
}

void ReplaceBackgroundWidget::getReplaceAndOrigImage(QImage replaceImage, QImage origImage)
{
    m_replaceImage = QPixmap::fromImage(replaceImage);
    m_origImage = QPixmap::fromImage(origImage);
    m_showImage->setPixmap(m_replaceImage.scaled(m_showImage->size(),
                                                  Qt::KeepAspectRatio, Qt::SmoothTransformation));
    m_maskWidget->hide();
}

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

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

void ReplaceBackgroundWidget::resizeEvent(QResizeEvent *event)
{
    m_maskWidget->setFixedSize(m_showImage->size());
    m_loadingLabel->move((m_maskWidget->width() - m_loadingLabel->width()) / 2,
                       (m_maskWidget->height() - m_loadingLabel->height()) / 2);
}

bool ReplaceBackgroundWidget::eventFilter(QObject *obj, QEvent *event)
{
    switch (event->type()) {
    case QEvent::MouseButtonPress:
        if(obj == m_compare) {
            m_showImage->setPixmap(m_origImage.scaled(m_showImage->size(),
                                                      Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }
        break;
    case QEvent::MouseButtonRelease:
        if(obj == m_compare) {
            m_showImage->setPixmap(m_replaceImage.scaled(m_showImage->size(),
                                                      Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }
        break;
    default:
        break;
    }
    return QWidget::eventFilter(obj, event);
}
