#include "toolbar.h"
#include "kyview.h"
#include "sizedate.h"
ToolBar::ToolBar(QWidget *parent) : QWidget(parent)
{

    //    this->resize(678 +4 ,40 + 4);
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setFocusPolicy(Qt::NoFocus);
    //布局
    g_tooleWid = new QFrame(this);
    m_toolLayout = new QHBoxLayout(this);
    //缩放widget
    m_zoomWid = new QWidget(this);
    //缩小
    m_reduce = new QPushButton(m_zoomWid);

    m_reduce->setFocusPolicy(Qt::NoFocus);
    m_reduce->move(0, 0);
    m_reduce->setToolTip(tr("Zoom out"));
    m_reduce->setAttribute(Qt::WA_TranslucentBackground);
    //    m_reduce->setToolTip(tr("缩小图片"));
    //    m_reduce->setAutoRepeat(true);
    //    m_reduce->setAutoRepeatDelay(1000);
    //百分比
    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_percentage->setFont(m_ft);
    //    m_percentage->setText("98%");
    //放大
    m_enlarge = new QPushButton(m_zoomWid);

    m_enlarge->setFocusPolicy(Qt::NoFocus);

    m_enlarge->setToolTip(tr("Zoom in"));
    m_enlarge->setAttribute(Qt::WA_TranslucentBackground);
    //    m_enlarge->setToolTip(tr("放大图片"));
    //    m_enlarge->setAutoRepeat(true);
    //    m_enlarge->setAutoRepeatDelay(15);
    //原始尺寸
    m_originalSize = new QPushButton(this);

    m_originalSize->setFocusPolicy(Qt::NoFocus);
    m_originalSize->setToolTip(tr("Life size"));
    m_originalSize->setAttribute(Qt::WA_TranslucentBackground);
    //    m_originalSize->setToolTip(tr("原始尺寸"));
    //适应窗口
    m_adaptiveWidget = new QPushButton(this);

    m_adaptiveWidget->setFocusPolicy(Qt::NoFocus);
    m_adaptiveWidget->setToolTip(tr("Window widget"));
    m_adaptiveWidget->setAttribute(Qt::WA_TranslucentBackground);
    //    m_adaptiveWidget->setToolTip(tr("图像适应窗口"));
    //旋转
    m_rotate = new QPushButton(this);
    m_rotate->setFocusPolicy(Qt::NoFocus);
    m_rotate->setAttribute(Qt::WA_TranslucentBackground);
    //    m_rotate->setToolTip(tr("旋转"));
    //水平翻转
    m_flipH = new QPushButton(this);

    m_flipH->setFocusPolicy(Qt::NoFocus);
    m_flipH->setToolTip(tr("Flip horizontally"));
    m_flipH->setAttribute(Qt::WA_TranslucentBackground);
    //    m_flipH->setToolTip(tr("水平镜像"));
    //垂直翻转
    m_flipV = new QPushButton(this);
    m_flipV->setFocusPolicy(Qt::NoFocus);
    m_flipV->setToolTip(tr("Flip vertically"));
    m_flipV->setAttribute(Qt::WA_TranslucentBackground);
    //    m_flipV->setToolTip(tr("垂直镜像"));
    //裁剪
    m_cutImage = new QPushButton(this);

    m_cutImage->setFocusPolicy(Qt::NoFocus);
    m_cutImage->setToolTip(tr("Crop"));
    m_cutImage->setAttribute(Qt::WA_TranslucentBackground);
    //    cutImage->setToolTip(tr("裁剪"));

    // OCR
    m_ocr = new QPushButton(this);

    m_ocr->setFocusPolicy(Qt::NoFocus);
    m_ocr->setToolTip(tr("OCR"));
    m_ocr->setAttribute(Qt::WA_TranslucentBackground);

    m_line1 = new QLabel(this);
    m_line1->setFixedSize(TOOL_LINE);
    //滤镜
    //    filter = new QPushButton(this);
    //    filter->setFixedSize(TOOL_BUTTON);
    //    filter->setFocusPolicy(Qt::NoFocus);
    //    filter->setToolTip(tr("Filter"));
    //    filter->setToolTip(tr("滤镜"));
    //标注
    //    labelbar = new QPushButton(this);//
    //    labelbar->setFixedSize(TOOL_BUTTON);
    //    labelbar->setFocusPolicy(Qt::NoFocus);
    //    labelbar->setToolTip(tr("Label"));
    //    labelbar->setToolTip(tr("标注"));

    m_line2 = new QLabel(this);
    m_line2->setFixedSize(TOOL_LINE);
    //侧边栏--相册
    m_sidebar = new QCheckBox(this);

    m_sidebar->setFocusPolicy(Qt::NoFocus);
    m_sidebar->setToolTip(tr("Sidebar"));
    m_sidebar->setAttribute(Qt::WA_TranslucentBackground);
    //    m_sidebar->setToolTip(tr("缩略图侧栏"));
    //信息栏
    m_information = new QCheckBox(this); //
    m_information->setFocusPolicy(Qt::NoFocus);
    m_information->setToolTip(tr("Get info"));
    m_information->setAttribute(Qt::WA_TranslucentBackground);
    //    m_information->setToolTip(tr("信息"));
    //删除
    m_delImage = new QPushButton(this);

    m_delImage->setFocusPolicy(Qt::NoFocus);
    m_delImage->setToolTip(tr("Delete"));
    m_delImage->setAttribute(Qt::WA_TranslucentBackground);
    //    m_delImage->setToolTip(tr("删除"));
    //绘制阴影
    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); //设定阴影的模糊半径，数值越大越模糊
    g_tooleWid->setGraphicsEffect(effect);

    this->platformType();
    m_enlarge->move(m_percentage->x() + m_percentage->width() + 7, 0);
    this->initGsetting();
    this->initControlQss();
    this->initConnect();
    this->initFontSize();
}
//更新改名后的文件信息
void ToolBar::updateFileInfo(QFileInfo fileInfo)
{
    m_imagePath = fileInfo.absoluteFilePath();
}
//布局
void ToolBar::initControlQss()
{

    if (platForm.contains(Variable::platForm)) {
        m_toolLayout->addWidget(m_zoomWid, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(10);
        m_toolLayout->addWidget(m_adaptiveWidget, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(10);
        m_toolLayout->addWidget(m_originalSize, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(10);
        m_toolLayout->addWidget(m_rotate, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(10);
        m_toolLayout->addWidget(m_flipH, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(10);
        m_toolLayout->addWidget(m_flipV, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(10);
        m_toolLayout->addWidget(m_cutImage, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(5);
        m_toolLayout->addWidget(m_line1, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(5);
        //        m_toolLayout->addWidget(m_ocr, 0, Qt::AlignCenter);
        //        m_toolLayout->setSpacing(5);
        //    m_toolLayout->addWidget(m_filter,0,Qt::AlignCenter);
        //    m_toolLayout->setSpacing(20);
        //    m_toolLayout->addWidget(m_labelbar,0,Qt::AlignCenter);
        //    m_toolLayout->setSpacing(16);
        //        m_toolLayout->addWidget(m_line2, 0, Qt::AlignCenter);
        //    m_toolLayout->setSpacing(20);
        m_toolLayout->addWidget(m_sidebar, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(10);
        m_toolLayout->addWidget(m_information, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(10);
        m_toolLayout->addWidget(m_delImage, 0, Qt::AlignCenter);
        m_toolLayout->setContentsMargins(0, 0, 0, 0);
        g_tooleWid->setLayout(m_toolLayout);
        g_tooleWid->move(2, 2);
        g_tooleWid->resize(this->width() - 4, this->height() - 4);
        m_line2->hide();
    } else {
        m_line2->show();
        m_toolLayout->addWidget(m_zoomWid, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(20);
        m_toolLayout->addWidget(m_adaptiveWidget, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(20);
        m_toolLayout->addWidget(m_originalSize, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(20);
        m_toolLayout->addWidget(m_rotate, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(20);
        m_toolLayout->addWidget(m_flipH, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(20);
        m_toolLayout->addWidget(m_flipV, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(20);
        m_toolLayout->addWidget(m_cutImage, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(5);
        m_toolLayout->addWidget(m_line1, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(5);
        m_toolLayout->addWidget(m_ocr, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(5);
        //    m_toolLayout->addWidget(m_filter,0,Qt::AlignCenter);
        //    m_toolLayout->setSpacing(20);
        //    m_toolLayout->addWidget(m_labelbar,0,Qt::AlignCenter);
        //    m_toolLayout->setSpacing(16);
        m_toolLayout->addWidget(m_line2, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(20);
        m_toolLayout->addWidget(m_sidebar, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(20);
        m_toolLayout->addWidget(m_information, 0, Qt::AlignCenter);
        m_toolLayout->setSpacing(20);
        m_toolLayout->addWidget(m_delImage, 0, Qt::AlignCenter);
        m_toolLayout->setContentsMargins(14, 8, 18, 12);
        g_tooleWid->setLayout(m_toolLayout);
        g_tooleWid->move(4, 4);
        g_tooleWid->resize(this->width() - 8, this->height() - 8);
    }
}
//信号槽
void ToolBar::initConnect()
{
    connect(m_reduce, &QPushButton::clicked, this, &ToolBar::reduceImageSlot);
    connect(m_enlarge, &QPushButton::clicked, this, &ToolBar::enlargeImageSlot);
    connect(m_rotate, &QPushButton::clicked, this, &ToolBar::rotate);
    connect(m_originalSize, &QPushButton::clicked, this, &ToolBar::originalSize);
    connect(m_adaptiveWidget, &QPushButton::clicked, this, &ToolBar::adaptiveWidget);
    connect(m_flipH, &QPushButton::clicked, this, &ToolBar::flipH);
    connect(m_flipV, &QPushButton::clicked, this, &ToolBar::flipV);
    connect(m_cutImage, &QPushButton::clicked, this, &ToolBar::cutImage);
    connect(m_ocr, &QPushButton::clicked, this, &ToolBar::getText);
    //    connect(m_filter,&QPushButton::clicked,this,&ToolBar::filter);
    //    connect(m_labelbar,&QPushButton::clicked,this,&ToolBar::labelbar);
    connect(m_sidebar, &QCheckBox::clicked, this, &ToolBar::sidebar);
    connect(m_information, &QCheckBox::clicked, this, &ToolBar::information);
    connect(m_delImage, &QPushButton::clicked, this, &ToolBar::delImage);
}
//改变百分比的数值
void ToolBar::changePerRate(QString num, QString path)
{
    this->m_percentage->setText(num);
    m_imagePath = path;
}
//缩小
void ToolBar::reduceImage(QPoint focus)
{

    Interaction::getInstance()->watchSmallImage(focus);
}
//放大
void ToolBar::enlargeImage(QPoint focus)
{
    Interaction::getInstance()->watchBigImage(focus);
}

void ToolBar::reduceImageSlot()
{
    reduceImage();
}

void ToolBar::enlargeImageSlot()
{
    enlargeImage();
}

//原始大小
void ToolBar::originalSize()
{
    Interaction::getInstance()->watchOriginalImage();
}
//适应窗口
void ToolBar::adaptiveWidget()
{
    Interaction::getInstance()->watchAutoImage();
}
//旋转
void ToolBar::rotate()
{
    if (platForm.contains(Variable::platForm)) {
        Interaction::getInstance()->rotate(false);
    } else {
        Interaction::getInstance()->rotate();
    }
}
//水平镜像
void ToolBar::flipH()
{
    Interaction::getInstance()->flipH();
}
//垂直镜像
void ToolBar::flipV()
{
    Interaction::getInstance()->flipV();
}
//裁剪
void ToolBar::cutImage()
{
    Interaction::getInstance()->cutImage();
    Q_EMIT hideTwoBar();
    Q_EMIT startCutting();
    m_sidebar->setChecked(false);
    m_information->setChecked(false);
}
//获取图片中文字
void ToolBar::getText()
{
    Interaction::getInstance()->getText();
    //    Q_EMIT startGetText();
    Q_EMIT viewOCRMode();
}
//滤镜
void ToolBar::filter()
{
    qDebug() << "滤镜";
}
//标注
void ToolBar::labelbar()
{
    qDebug() << "标注";
}
//侧边栏
void ToolBar::sidebar()
{
    Q_EMIT showSidebar();
    //图片预览-埋点
    kdk::kabase::BuriedPoint buriedPointPicturePreview;
    if (buriedPointPicturePreview.functionBuriedPoint(kdk::kabase::AppName::KylinPhotoViewer,
                                                      kdk::kabase::BuriedPoint::PT::KylinPhotoViewerPicturePreview)) {
        qCritical() << "buried point fail PicturePreview!";
    }
}
//信息栏展示
void ToolBar::information()
{
    Q_EMIT showInfor();
    //查看图片信息-埋点
    kdk::kabase::BuriedPoint buriedPointPictureDetails;
    if (buriedPointPictureDetails.functionBuriedPoint(kdk::kabase::AppName::KylinPhotoViewer,
                                                      kdk::kabase::BuriedPoint::PT::KylinPhotoViewerPictureDetails)) {
        qCritical() << "buried point fail PictureDetails!";
    }
}
//删除图片
void ToolBar::delImage()
{
    QString uri = "file://" + m_imagePath;
    Peony::FileOperation *trash = Peony::FileOperationUtils::trash(QStringList(uri), true);
    connect(
        trash, &Peony::FileOperation::operationFinished, this,
        [=]() {
            if (!trash->hasError()) {
                Interaction::getInstance()->deleteImage();
            }
        },
        Qt::BlockingQueuedConnection);
}
//监听主题
void ToolBar::initGsetting()
{
    connect(kdk::kabase::Gsettings::getPoint(), &kdk::kabase::Gsettings::systemThemeChange, this, [=]() {
        changeStyle();
    });
    changeStyle();
    return;
}
void ToolBar::lable2SetFontSizeSlot(int size)
{
    //默认大小12px,换算成pt为9
    double lable2BaseFontSize = DEFAULT_FONT_SIZE;
    double nowFontSize = lable2BaseFontSize * double(size) / DEFAULT_FONT_SIZE; // 11为系统默认大小
    QFont font;
    font.setPointSizeF(nowFontSize);
    m_percentage->setFont(font);
}
void ToolBar::initFontSize()
{
    //只有非标准字号的控件才需要绑定
    connect(this, &ToolBar::setFontSizeSignal, this, &ToolBar::lable2SetFontSizeSlot);

    connect(kdk::kabase::Gsettings::getPoint(), &kdk::kabase::Gsettings::systemFontSizeChange, this, [=]() {
        //获取字号的值
        int fontSizeKey = kdk::kabase::Gsettings::getSystemFontSize().toInt();
        //发送改变信号
        if (fontSizeKey > 0) {
            if (fontSizeKey > 15) {
                fontSizeKey = 15;
            }
            Q_EMIT setFontSizeSignal(fontSizeKey);
        }
    });
    //启动时设置字号
    int fontSizeKey = DEFAULT_FONT; //系统默认字号
    int fontSizeKeyTmp = kdk::kabase::Gsettings::getSystemFontSize().toInt();
    if (fontSizeKeyTmp > 0) {
        fontSizeKey = fontSizeKeyTmp;
    }
    if (fontSizeKey > 15) {
        fontSizeKey = 15;
    }
    Q_EMIT setFontSizeSignal(fontSizeKey);
}


void ToolBar::changeStyle()
{
    //需要重新梳理此处的QSS
    nowThemeStyle = kdk::kabase::Gsettings::getSystemTheme().toString();
    if ("ukui-dark" == nowThemeStyle || "ukui-black" == nowThemeStyle) {
        this->setStyleSheet("QWidget{border-radius:" + widRadius + ";border:none;}");
        g_tooleWid->setStyleSheet("QWidget{background-color:rgba(0,0,0,0.66);border-radius:" + widRadius
                                  + ";border:none;}");
        m_zoomWid->setStyleSheet("QWidget{background-color:transparent;border:none;}");
        m_percentage->setStyleSheet("QLabel{background-color:transparent;color:rgb(255,255,255);}");
        m_line1->setStyleSheet("QLabel{border: 1px solid #727272;background-color: #727272;}");
        m_line2->setStyleSheet("QLabel{border: 1px solid #727272;background-color: #727272;}");

        m_sidebar->setStyleSheet("QCheckBox{border:0px;border-radius:4px;background:transparent;}"
                                 "QCheckBox::indicator:unchecked{border:0px;border-radius:4px;background:transparent;"
                                 "image:url(:/res/res/"
                                 + normalIconPath
                                 + "/1sidebar.png);}"
                                   "QCheckBox::indicator:hover{border:0px;border-radius:4px;background:transparent;"
                                   "image: url(:/res/res/"
                                 + normalIconPath
                                 + "/1sidebar_hover.png);}"
                                   "QCheckBox::indicator:checked{border:0px;border-radius:4px;background:transparent;"
                                   "image: url(:/res/res/"
                                 + normalIconPath + "/1sidebar_hover.png);}");

        m_information->setStyleSheet(
            "QCheckBox{border:0px;border-radius:4px;background:transparent;}"
            "QCheckBox::indicator:unchecked{border:0px;border-radius:4px;background:transparent;image:"
            " url(:/res/res/"
            + normalIconPath
            + "/1information.png);}"
              "QCheckBox::indicator:hover{border:0px;border-radius:4px;background:transparent;"
              "image: url(:/res/res/"
            + normalIconPath
            + "/1information_hover.png);}"
              "QCheckBox::indicator:checked{border:0px;border-radius:4px;background:transparent;"
              "image: url(:/res/res/"
            + normalIconPath + "/1information_hover.png);}");

        if (!m_isOpenSuccess) {
            btnState();
        } else {
            if (m_imageSuffix != "" && OCR_SUPPORT.contains(m_imageSuffix)) {
                m_ocr->setEnabled(true);
                m_ocr->setStyleSheet(
                    "QPushButton{border:0px;border-radius:4px;background:transparent;background-image: "
                    "url(:/res/res/"
                    + normalIconPath
                    + "/1ocr.png);}"
                      "QPushButton::hover{border:0px;border-radius:4px;background:transparent;background-"
                      "image: url(:/res/res/"
                    + normalIconPath
                    + "/1ocr_hover.png);}"
                      "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;background-"
                      "image: url(:/res/res/"
                    + normalIconPath + "/1ocr_hover.png);}");
            } else {
                QString head = "url(:/res/res/" + damagedIconPath + "/";
                QString num = "1";
                QString headStyle = head + num;
                m_ocr->setStyleSheet(btnStyle(headStyle + "ocr_damaged.png)"));
                m_ocr->setEnabled(false);
            }
            m_reduce->setStyleSheet(
                "QPushButton{border:0px;border-radius:4px;background:transparent;background-image: "
                "url(:/res/res/"
                + normalIconPath
                + "/1reduce.png);}"
                  "QPushButton::hover{border:0px;border-radius:4px;background:transparent;background-"
                  "image: url(:/res/res/"
                + normalIconPath
                + "/1reduce_hover.png);}"
                  "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                  "background-image: url(:/res/res/"
                + normalIconPath + "/1reduce_hover.png);}");

            m_enlarge->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:transparent;background-image:"
                                     " url(:/res/res/"
                                     + normalIconPath
                                     + "/1enlarge.png);}"
                                       "QPushButton::hover{border:0px;border-radius:4px;background:transparent;"
                                       "background-image: url(:/res/res/"
                                     + normalIconPath
                                     + "/1enlarge_hover.png);}"
                                       "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                                       "background-image: url(:/res/res/"
                                     + normalIconPath + "/1enlarge_hover.png);}");

            m_originalSize->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:transparent;background-"
                                          "image: url(:/res/res/"
                                          + normalIconPath
                                          + "/1originalSize.png);}"
                                            "QPushButton::hover{border:0px;border-radius:4px;background:transparent;"
                                            "background-image: url(:/res/res/"
                                          + normalIconPath
                                          + "/1originalSize_hover.png);}"
                                            "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                                            "background-image: url(:/res/res/"
                                          + normalIconPath + "/1originalSize_hover.png);}");

            m_adaptiveWidget->setStyleSheet(
                "QPushButton{border:0px;border-radius:4px;background:transparent;"
                "background-image: url(:/res/res/"
                + normalIconPath
                + "/1adaptiveWidget.png);}"
                  "QPushButton::hover{border:0px;border-radius:4px;background:transparent;"
                  "background-image: url(:/res/res/"
                + normalIconPath
                + "/1adaptiveWidget_hover.png);}"
                  "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                  "background-image: url(:/res/res/"
                + normalIconPath + "/1adaptiveWidget_hover.png);}");
        }
        if ((m_isCanOperate && m_isOpenSuccess)) {
            m_rotate->setStyleSheet(
                "QPushButton{border:0px;border-radius:4px;background:transparent;background-image: "
                "url(:/res/res/"
                + normalIconPath
                + "/1rotate.png);}"
                  "QPushButton::hover{border:0px;border-radius:4px;background:transparent;background-"
                  "image: url(:/res/res/"
                + normalIconPath
                + "/1rotate_hover.png);}"
                  "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                  "background-image: url(:/res/res/"
                + normalIconPath + "/1rotate_hover.png);}");

            m_flipH->setStyleSheet(
                "QPushButton{border:0px;border-radius:4px;background:transparent;background-image: "
                "url(:/res/res/"
                + normalIconPath
                + "/1flipH.png);}"
                  "QPushButton::hover{border:0px;border-radius:4px;background:transparent;background-"
                  "image: url(:/res/res/"
                + normalIconPath
                + "/1flipH_hover.png);}"
                  "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                  "background-image: url(:/res/res/"
                + normalIconPath + "/1flipH_hover.png);}");

            m_flipV->setStyleSheet(
                "QPushButton{border:0px;border-radius:4px;background:transparent;background-image: "
                "url(:/res/res/"
                + normalIconPath
                + "/1flipV.png);}"
                  "QPushButton::hover{border:0px;border-radius:4px;background:transparent;background-"
                  "image: url(:/res/res/"
                + normalIconPath
                + "/1flipV_hover.png);}"
                  "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                  "background-image: url(:/res/res/"
                + normalIconPath + "/1flipV_hover.png);}");
            m_delImage->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:transparent;background-"
                                      "image: url(:/res/res/"
                                      + normalIconPath
                                      + "/1delImage.png);}"
                                        "QPushButton::hover{border:0px;border-radius:4px;background:transparent;"
                                        "background-image: url(:/res/res/"
                                      + normalIconPath
                                      + "/1delImage_hover.png);}"
                                        "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                                        "background-image: url(:/res/res/"
                                      + normalIconPath + "/1delImage_hover.png);}");
            //    m_line2->setStyleSheet("QLabel{border: 1px solid #393939;background-color: #393939;}");

        } else {
            btnState();
        }
        if (!m_cutImage->isEnabled()) {
            getImageType(false);
            return;
        }

        m_cutImage->setStyleSheet(
            "QPushButton{background:transparent;border:0px;border-radius:4px;background-image: "
            "url(:/res/res/"
            + normalIconPath
            + "/1cutImage.png);}"
              "QPushButton::hover{background:transparent;border:0px;border-radius:4px;background-"
              "image: url(:/res/res/"
            + normalIconPath
            + "/1cutImage_hover.png);}"
              "QPushButton::pressed{background:transparent;border:0px;border-radius:4px;background-"
              "image: url(:/res/res/"
            + normalIconPath + "/1cutImage_hover.png);}");

        //    filter->setStyleSheet("QPushButton{border:0px;border-radius:4px;background-image:
        //    url(:/res/res/"+normalIconPath+"/1filter.png);}"
        //                          "QPushButton::hover{border:0px;border-radius:4px;background-image:
        //                          url(:/res/res/"+normalIconPath+"/1filter_hover.png);}"
        //                          "QPushButton::pressed{border:0px;border-radius:4px;background-image:
        //                          url(:/res/res/"+normalIconPath+"/1filter_hover.png);}");

        //    labelbar->setStyleSheet("QPushButton{border:0px;border-radius:4px;background-image:
        //    url(:/res/res/1labelbar.png);}"
        //                            "QPushButton::hover{border:0px;border-radius:4px;background-image:
        //                            url(:/res/res/1labelbar_hover.png);}"
        //                            "QPushButton::pressed{border:0px;border-radius:4px;background-image:
        //                            url(:/res/res/1labelbar_hover.png);}");


    } else {
        this->setStyleSheet("QWidget{border-radius:" + widRadius + ";border:none;}");
        m_zoomWid->setStyleSheet("QWidget{background-color:transparent;border:none;}");
        g_tooleWid->setStyleSheet("QWidget{background-color:rgba(255,255,255,1);border:none;border-radius:" + widRadius
                                  + ";}");
        m_line1->setStyleSheet("QLabel{border: 1px solid #393939;background-color: #393939;}");
        m_line2->setStyleSheet("QLabel{border: 1px solid #393939;background-color: #393939;}");

        m_percentage->setStyleSheet("QLabel{background-color:transparent;color:rgb(0,0,0);}");
        m_sidebar->setStyleSheet(
            "QCheckBox{border:0px;border-radius:4px;background:transparent;}"
            "QCheckBox::indicator:unchecked{border:0px;border-radius:4px;background:transparent;image: "
            "url(:/res/res/"
            + normalIconPath
            + "/sidebar.png);}"
              "QCheckBox::indicator:hover{border:0px;border-radius:4px;background:transparent;"
              "image: url(:/res/res/"
            + normalIconPath
            + "/sidebar_hover.png);}"
              "QCheckBox::indicator:checked{border:0px;border-radius:4px;background:transparent;"
              "image: url(:/res/res/"
            + normalIconPath + "/sidebar_hover.png);}");

        m_information->setStyleSheet(
            "QCheckBox{border:0px;border-radius:4px;background:transparent;}"
            "QCheckBox::indicator:unchecked{border:0px;border-radius:4px;background:"
            "transparent;image: url(:/res/res/"
            + normalIconPath
            + "/information.png);}"
              "QCheckBox::indicator:hover{border:0px;border-radius:4px;background:transparent;"
              "image: url(:/res/res/"
            + normalIconPath
            + "/information_hover.png);}"
              "QCheckBox::indicator:checked{border:0px;border-radius:4px;background:transparent;"
              "image: url(:/res/res/"
            + normalIconPath + "/information_hover.png);}");


        if (!m_isOpenSuccess) {
            btnState();
        } else {
            if (m_imageSuffix != "" && OCR_SUPPORT.contains(m_imageSuffix)) {
                m_ocr->setEnabled(true);
                m_ocr->setStyleSheet(
                    "QPushButton{border:0px;border-radius:4px;background:transparent;background-image: "
                    "url(:/res/res/"
                    + normalIconPath
                    + "/ocr.png);}"
                      "QPushButton::hover{border:0px;border-radius:4px;background:transparent;background-"
                      "image: url(:/res/res/"
                    + normalIconPath
                    + "/ocr_hover.png);}"
                      "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;background-"
                      "image: url(:/res/res/"
                    + normalIconPath + "/ocr_hover.png);}");
            } else {
                QString head = "url(:/res/res/" + damagedIconPath + "/";
                m_ocr->setStyleSheet(btnStyle(head + "ocr_damaged.png)"));
                m_ocr->setEnabled(false);
            }
            m_reduce->setStyleSheet(
                "QPushButton{border:0px;border-radius:4px;background:transparent;background-image: "
                "url(:/res/res/"
                + normalIconPath
                + "/reduce.png);}"
                  "QPushButton::hover{border:0px;border-radius:4px;background:transparent;background-"
                  "image: url(:/res/res/"
                + normalIconPath
                + "/reduce_hover.png);}"
                  "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                  "background-image: url(:/res/res/"
                + normalIconPath + "/reduce_hover.png);}");

            m_enlarge->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:transparent;background-image:"
                                     " url(:/res/res/"
                                     + normalIconPath
                                     + "/enlarge.png);}"
                                       "QPushButton::hover{border:0px;border-radius:4px;background:transparent;"
                                       "background-image: url(:/res/res/"
                                     + normalIconPath
                                     + "/enlarge_hover.png);}"
                                       "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                                       "background-image: url(:/res/res/"
                                     + normalIconPath + "/enlarge_hover.png);}");

            m_originalSize->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:transparent;background-"
                                          "image: url(:/res/res/"
                                          + normalIconPath
                                          + "/originalSize.png);}"
                                            "QPushButton::hover{border:0px;border-radius:4px;background:transparent;"
                                            "background-image: url(:/res/res/"
                                          + normalIconPath
                                          + "/originalSize_hover.png);}"
                                            "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                                            "background-image: url(:/res/res/"
                                          + normalIconPath + "/originalSize_hover.png);}");

            m_adaptiveWidget->setStyleSheet(
                "QPushButton{border:0px;border-radius:4px;background:transparent;"
                "background-image: url(:/res/res/"
                + normalIconPath
                + "/adaptiveWidget.png);}"
                  "QPushButton::hover{border:0px;border-radius:4px;background:transparent;"
                  "background-image: url(:/res/res/"
                + normalIconPath
                + "/adaptiveWidget_hover.png);}"
                  "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                  "background-image: url(:/res/res/"
                + normalIconPath + "/adaptiveWidget_hover.png);}");
        }
        if ((m_isOpenSuccess && m_isCanOperate)) {
            m_rotate->setStyleSheet(
                "QPushButton{border:0px;border-radius:4px;background:transparent;background-image: "
                "url(:/res/res/"
                + normalIconPath
                + "/rotate.png);}"
                  "QPushButton::hover{border:0px;border-radius:4px;background:transparent;background-"
                  "image: url(:/res/res/"
                + normalIconPath
                + "/rotate_hover.png);}"
                  "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                  "background-image: url(:/res/res/"
                + normalIconPath + "/rotate_hover.png);}");

            m_flipH->setStyleSheet(
                "QPushButton{border:0px;border-radius:4px;background:transparent;background-image: "
                "url(:/res/res/"
                + normalIconPath
                + "/flipH.png);}"
                  "QPushButton::hover{border:0px;border-radius:4px;background:transparent;background-"
                  "image: url(:/res/res/"
                + normalIconPath
                + "/flipH_hover.png);}"
                  "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                  "background-image: url(:/res/res/"
                + normalIconPath + "/flipH_hover.png);}");

            m_flipV->setStyleSheet(
                "QPushButton{border:0px;border-radius:4px;background:transparent;background-image: "
                "url(:/res/res/"
                + normalIconPath
                + "/flipV.png);}"
                  "QPushButton::hover{border:0px;border-radius:4px;background:transparent;background-"
                  "image: url(:/res/res/"
                + normalIconPath
                + "/flipV_hover.png);}"
                  "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                  "background-image: url(:/res/res/"
                + normalIconPath + "/flipV_hover.png);}");
            m_delImage->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:transparent;background-"
                                      "image: url(:/res/res/"
                                      + normalIconPath
                                      + "/delImage.png);}"
                                        "QPushButton::hover{border:0px;border-radius:4px;background:transparent;"
                                        "background-image: url(:/res/res/"
                                      + normalIconPath
                                      + "/delImage_hover.png);}"
                                        "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;"
                                        "background-image: url(:/res/res/"
                                      + normalIconPath + "/delImage_hover.png);}");
            //    m_line2->setStyleSheet("QLabel{border: 1px solid #393939;background-color: #393939;}");
        } else {
            btnState();
        }
        if (!m_cutImage->isEnabled()) {
            getImageType(false);
            return;
        }
        m_cutImage->setStyleSheet(
            "QPushButton{background:transparent;border:0px;border-radius:4px;background:"
            "transparent;background-image: url(:/res/res/"
            + normalIconPath
            + "/cutImage.png);}"
              "QPushButton::hover{background:transparent;border:0px;border-radius:4px;background:"
              "transparent;background-image: url(:/res/res/"
            + normalIconPath
            + "/cutImage_hover.png);}"
              "QPushButton::pressed{background:transparent;border:0px;border-radius:4px;background:"
              "transparent;background-image: url(:/res/res/"
            + normalIconPath + "/cutImage_hover.png);}");

        //    filter->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:transparent;background-image:
        //    url(:/res/res/filter.png);}"
        //                          "QPushButton::hover{border:0px;border-radius:4px;background:transparent;background-image:
        //                          url(:/res/res/filter_hover.png);}"
        //                          "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;background-image:
        //                          url(:/res/res/filter_hover.png);}");

        //    labelbar->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:transparent;background-image:
        //    url(:/res/res/labelbar.png);}"
        //                            "QPushButton::hover{border:0px;border-radius:4px;background:transparent;background-image:
        //                            url(:/res/res/labelbar_hover.png);}"
        //                            "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;background-image:
        //                            url(:/res/res/labelbar_hover.png);}");
    }
}

void ToolBar::setButtonState(bool isOpenSuccess, bool isCanOperate)
{
    // isOpenSuccess---是否没有损坏；isCanOperate---是否可操作及其他
    m_isOpenSuccess = isOpenSuccess;
    m_isCanOperate = isCanOperate;
    m_reduce->setEnabled(isOpenSuccess);                   //缩小
    m_enlarge->setEnabled(isOpenSuccess);                  //放大
    m_originalSize->setEnabled(isOpenSuccess);             //原始尺寸
    m_adaptiveWidget->setEnabled(isOpenSuccess);           //适应图片
    m_rotate->setEnabled(isOpenSuccess && isCanOperate);   //旋转
    m_flipH->setEnabled(isOpenSuccess && isCanOperate);    //水平翻转
    m_flipV->setEnabled(isOpenSuccess && isCanOperate);    //垂直翻转
    m_cutImage->setEnabled(isOpenSuccess && isCanOperate); //裁剪
    m_ocr->setEnabled(isOpenSuccess && isCanOperate);      // OCR
    //    m_filter->setEnabled(isOpenSuccess);//滤镜
    //    m_labelbar->setEnabled(isOpenSuccess);//标注栏
    m_delImage->setEnabled(isOpenSuccess && isCanOperate); //删除图片

    //更新一波图标
    changeStyle();
}

void ToolBar::getImageType(bool canCut)
{
    QString head = "url(:/res/res/" + damagedIconPath + "/";
    QString num = "1";
    QString headStyle = head;
    //        m_ocr->setStyleSheet(btnStyle(headStyle + "ocr_damaged.png)"));
    //判断是否能进行裁剪
    if (m_isOpenSuccess) {
        m_cutImage->setEnabled(canCut);
        //        m_ocr->setEnabled(canCut);
    } else {
        m_cutImage->setEnabled(m_isOpenSuccess);
        //        m_ocr->setEnabled(m_isOpenSuccess);
    }
    if (!canCut) {
        if ("ukui-dark" == nowThemeStyle || "ukui-black" == nowThemeStyle) {
            headStyle = head + num;
        } else {
            headStyle = head;
        }
        m_cutImage->setStyleSheet(btnStyle(headStyle + "cutImage_damaged.png)"));
    } else {
        changeStyle();
    }
}

void ToolBar::getSuffix(QString suffix)
{
    m_imageSuffix = suffix;
}

void ToolBar::btnState()
{
    QString head = "url(:/res/res/" + damagedIconPath + "/";
    QString num = "1";
    QString headStyle = head;
    if ("ukui-dark" == nowThemeStyle || "ukui-black" == nowThemeStyle) {
        if (!(m_isOpenSuccess)) {
            m_percentage->setStyleSheet("QLabel{background-color:transparent;color:rgba(255,255,255,0.4);}");
        }
        headStyle = head + num;
    } else {
        if (!(m_isOpenSuccess)) {
            m_percentage->setStyleSheet("QLabel{background-color:transparent;color:rgba(0,0,0,0.4);}");
        }
        headStyle = head;
    }
    if (!m_isOpenSuccess) {
        m_reduce->setStyleSheet(btnStyle(headStyle + "reduce_damaged.png)"));
        m_enlarge->setStyleSheet(btnStyle(headStyle + "enlarge_damaged.png)"));
        m_originalSize->setStyleSheet(btnStyle(headStyle + "origSize_damaged.png)"));
        m_adaptiveWidget->setStyleSheet(btnStyle(headStyle + "adaptWid_damaged.png)"));
    }
    if (!(m_isCanOperate && m_isOpenSuccess)) {
        m_rotate->setStyleSheet(btnStyle(headStyle + "rotate_damaged.png)"));
        m_flipH->setStyleSheet(btnStyle(headStyle + "flipH_damaged.png)"));
        m_flipV->setStyleSheet(btnStyle(headStyle + "flipV_damaged.png)"));
        m_delImage->setStyleSheet(btnStyle(headStyle + "delImage_damaged.png)"));
        m_cutImage->setStyleSheet(btnStyle(headStyle + "cutImage_damaged.png)"));
        m_ocr->setStyleSheet(btnStyle(headStyle + "ocr_damaged.png)"));
    }
}

QString ToolBar::btnStyle(QString iconName)
{
    QString styleBtn;
    QString btnDefault =
        "QPushButton{border:0px;border-radius:4px;background:transparent;border-image: " + iconName + ";}";
    QString btnHover =
        "QPushButton::hover{border:0px;border-radius:4px;background:transparent;border-image: " + iconName + ";}";
    QString btnPress =
        "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;border-image: " + iconName + ";}";
    styleBtn = QString(btnDefault + btnHover + btnPress);
    return styleBtn;
}

void ToolBar::buttonSize(QSize acturalSize)
{
    m_reduce->setFixedSize(acturalSize);
    m_enlarge->setFixedSize(acturalSize);
    m_originalSize->setFixedSize(acturalSize);
    m_adaptiveWidget->setFixedSize(acturalSize);
    m_rotate->setFixedSize(acturalSize);
    m_flipH->setFixedSize(acturalSize);
    m_flipV->setFixedSize(acturalSize);
    m_cutImage->setFixedSize(acturalSize);
    m_sidebar->setFixedSize(acturalSize);
    m_information->setFixedSize(acturalSize);
    m_delImage->setFixedSize(acturalSize);
    m_ocr->setFixedSize(acturalSize);
}

void ToolBar::platformType()
{
    if (platForm.contains(Variable::platForm)) {
        this->resize(TOOLBAR_SIZE_INTEL);
        m_zoomWid->setFixedSize(TOOLZOOM_SIZE_INTEL);
        m_rotate->setToolTip(tr("Rorate left"));
        widRadius = "16px";
        normalIconPath = "intel";
        damagedIconPath = "inteldamaged";
        this->buttonSize(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);
        m_ocr->hide();
    } else {
        this->resize(TOOLBAR_SIZE);
        m_zoomWid->setFixedSize(TOOLZOOM_SIZE);
        m_rotate->setToolTip(tr("Rorate right"));
        widRadius = "6px";
        normalIconPath = "sp1";
        damagedIconPath = "sp1damaged";
        this->buttonSize(TOOL_BUTTON);
        m_percentage->setFixedSize(TOOL_PER);
        m_percentage->move(m_reduce->x() + m_reduce->width() + 5, 0);
        m_ocr->show();
    }
}
