/*
* Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/&gt;.
*
*/

#include "appmsg.h"

AppMsg::AppMsg(NotificationPlugin *parent, QString strAppName) : m_strAppName(strAppName)
{
    /* 初始化最大消息数量 */
    initMaxCount();

    /* 初始化gsetting */
    initGsetting();

    /* 初始化界面 */
    initUI();

    /* 连接删除此类消息的信号 */
    connect(this, SIGNAL(Sig_onDeleteAppMsg(AppMsg*)), parent, SLOT(onClearAppMsg(AppMsg*)));

    return;
}

/**
 * @brief AppMsg::initMaxCount
 * 初始化最大消息数量
 */
void AppMsg::initMaxCount()
{
    m_nMaxCount = 3;
}

/**
 * @brief AppMsg::initUI
 * 初始化界面
 */
void AppMsg::initUI()
{
    this->setFixedWidth(352);

    /* App信息中的总的垂直布局器 */
    m_pMainVLaout = new QVBoxLayout();
    m_pMainVLaout->setContentsMargins(0,0,0,0);

    /*
     * 如果只有m_pMainVLaout一个管理所有消息列表，当应用消息动态展开时，0号消息会出现闪屏的情况
     * 为了规避上述情况，m_pMainVLaout中只防止0号消息，1号消息起全部放置m_pIndexFromOneVLaout中
     */
    m_pIndexFromOneVLaout = new QVBoxLayout();
    m_pIndexFromOneVLaout->setContentsMargins(0,0,0,0);
    m_pIndexFromOneVLaout->setSpacing(4);

    QVBoxLayout* pBaseMapVLaout = new QVBoxLayout();
    pBaseMapVLaout->setContentsMargins(0,0,0,0);
    pBaseMapVLaout->setSpacing(4);

    /* 当出现多条消息时，增加底图 */
    m_pAppBaseMapWidget = new QWidget;
    m_pAppBaseMapWidget->setAttribute(Qt::WA_TranslucentBackground);
    m_pAppBaseMapWidget->setLayout(pBaseMapVLaout);
    m_pAppBaseMapWidget->setVisible(false);

    m_pMainVLaout->addLayout(m_pIndexFromOneVLaout);
    m_pMainVLaout->addWidget(m_pAppBaseMapWidget, 0 , Qt::AlignHCenter);

    /* 折叠按钮和删除按钮部分 */
    m_foldBtn = new FoldBtn();
    m_delBtn  = new DelBtn();

    connect(m_foldBtn,&QPushButton::clicked,this,&AppMsg::onFoldAppWidget);
    connect(m_delBtn,&QPushButton::clicked,this,&AppMsg::onDelAppMsg);

    QHBoxLayout *m_pFoldBtnHLaout = new QHBoxLayout();
    m_pFoldBtnHLaout->setContentsMargins(0,0,0,0);
    m_pFoldBtnHLaout->addWidget(m_foldBtn,Qt::AlignVCenter);
    m_pFoldBtnHLaout->addItem(new QSpacerItem(256, 10, QSizePolicy::Expanding));
    m_pFoldBtnHLaout->addWidget(m_delBtn,Qt::AlignVCenter);
    m_pFoldBtnHLaout->addItem(new QSpacerItem(15, 7, QSizePolicy::Fixed));

    m_pFoldBtnWid = new QWidget(this);
    m_pFoldBtnWid->setFixedSize(352,36);
    m_pFoldBtnWid->setContentsMargins(0,0,0,0);
    m_pFoldBtnWid->setLayout(m_pFoldBtnHLaout);

    if (m_bFold || (m_listSingleMsg.count()<=1)) {
        m_pFoldBtnWid->setVisible(false);
    } else {
        m_pFoldBtnWid->setVisible(true);
    }

    m_pMainWid = new QWidget(this);
    m_pMainWid->setContentsMargins(0,0,0,0);
    m_pMainWid->setLayout(m_pMainVLaout);

    m_pMainBaseVLaout = new QVBoxLayout();
    m_pMainBaseVLaout->setContentsMargins(0,0,0,0);
    m_pMainBaseVLaout->addWidget(m_pFoldBtnWid);
    m_pMainBaseVLaout->addWidget(m_pMainWid);
    this->setLayout(m_pMainBaseVLaout);
}

/**
 * @brief AppMsg::paintEvent
 */
void AppMsg::paintEvent(QPaintEvent *)
{
    int messageWidth = 6;
    int rectSize = 6;

    QPainter p(this);
    QRect rect = this->rect();
    rect.setWidth(rect.width() - messageWidth);
    rect.setHeight(rect.height() - 1);
    p.setRenderHint(QPainter::Antialiasing);

    QColor m_Color;
    if (m_pStyleName == "ukui-dark") {
        m_Color = QColor(28,28,28,200);
    } else {
        m_Color = QColor(255, 255, 255, 100);
    }

    if ((1 < m_listSingleMsg.count()) && getFoldFlag()) {
        p.setBrush(QBrush(m_Color));
    } else {
        p.setBrush(QBrush(QColor(220,220,220,0)));
    }

    p.setPen(Qt::transparent);
    p.drawRoundedRect(rect,rectSize,rectSize);

}

/**
 * @brief AppMsg::setMaxNumMsg
 * 设置最大消息通知数
 * @param nMaxNum
 * 最大消息通知数
 */
void AppMsg::setMaxNumMsg(int nMaxNum)
{
    return;
}

/**
 * @brief AppMsg::onFoldAppWidget
 * 折叠整个应用的消息
 * App消息集合窗口上移
 * 折叠按钮窗口上移
 */
void AppMsg::onFoldAppWidget()
{
    m_bFold = true;
    int widthFoldWid = m_pFoldBtnWid->width();
    int heightFoldWid = m_pFoldBtnWid->height();
    QPropertyAnimation* pAnimation2 = new QPropertyAnimation(this, "btnWidFold");
    connect(pAnimation2, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value){
        QRect Rect = value.value<QRect>();
        int x1, y1, x2, y2;
        Rect.getRect(&x1, &y1, &x2, &y2);
        m_pFoldBtnWid->setGeometry(x1, y1, x2, y2);
    });
    connect(pAnimation2, &QPropertyAnimation::finished, this,[=](){
        m_pFoldBtnWid->setVisible(false);
        m_pMainBaseVLaout->removeWidget(m_pFoldBtnWid);
        emit Sig_foldAnimationFinish();
    });
    pAnimation2->setDuration(30);
    pAnimation2->setStartValue(QRect(0, 0, widthFoldWid, heightFoldWid));
    pAnimation2->setEndValue(QRect(0, 0-heightFoldWid, widthFoldWid, heightFoldWid));
    pAnimation2->start(QAbstractAnimation::DeleteWhenStopped);

    int widthAppWid = m_pMainWid->width();
    int heightAppWid = m_pMainWid->height();
    QPropertyAnimation* pAnimation1 = new QPropertyAnimation(this, "appMainFold");
    connect(pAnimation1, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value){
        QRect Rect = value.value<QRect>();
        int x1, y1, x2, y2;
        Rect.getRect(&x1, &y1, &x2, &y2);
        m_pMainWid->setGeometry(x1, y1, x2, y2);
    });
    connect(pAnimation1, &QPropertyAnimation::finished, this,[=](){
        /* 折叠时,索引从第1条开始,消息全部不可见 */
        SingleMsg* pFristSingleMsg = m_listSingleMsg.at(0);
        pFristSingleMsg->setBodyLabelWordWrap(false);
        pFristSingleMsg->setFoldFlag(true);
        for(int i = 1; i < m_listSingleMsg.count(); i++)
        {
            SingleMsg* pTmpSingleMsg = m_listSingleMsg.at(i);
            pTmpSingleMsg->setBodyLabelWordWrap(false);
            pTmpSingleMsg->setFoldFlag(true);
            pTmpSingleMsg->startAnimationFold();
        }
        m_pMainBaseVLaout->removeWidget(m_pFoldBtnWid);
        m_pFoldBtnWid->setVisible(false);
    });
    pAnimation1->setDuration(30);
    pAnimation1->setStartValue(QRect(0, m_pFoldBtnWid->height(), widthAppWid, heightAppWid));
    pAnimation1->setEndValue(QRect(0, 0, widthAppWid, heightAppWid));
    pAnimation1->start(QAbstractAnimation::DeleteWhenStopped);
}

/**
 * @brief AppMsg::onDelAppMsg
 * 删除整个应用的消息
 * 先折叠，再左移动画，最后上移
 */
void AppMsg::onDelAppMsg()
{
    SingleMsg* pFristSingleMsg = m_listSingleMsg.at(0);
    connect(this,&AppMsg::Sig_foldAnimationFinish,this,[=](){
        /* 设置一个单次定时器,只为延迟200毫秒,等待折叠动画完成 */
        QTimer *timer = new QTimer();
        timer->setSingleShot(true);
        connect(timer, &QTimer::timeout, this, [=](){
            /* 开启左移动画，上移动画 */
            pFristSingleMsg->onDele();
        });
        timer->start(200);
    });

    onFoldAppWidget();
}

/**
 * @brief AppMsg::statisticLeftItem
 * 统计应用剩余显示条数
 */
void AppMsg::statisticLeftItem()
{
    /* 小于1时,表示列表为空,直接返回 */
    if(m_listSingleMsg.count() < 1) {
        return;
    }
    int nShowLeftCount = m_listSingleMsg.count() - 1;
    /* 当应用处于折叠状态，且剩余条数大于0时,应用底图部件显示 */
    if((true == m_bFold) && (nShowLeftCount > 0)) {
        m_pAppBaseMapWidget->setVisible(true);
    } else {
        m_pAppBaseMapWidget->setVisible(false);
    }

    SingleMsg* pTopSingleMsg = m_listSingleMsg.at(0);
    pTopSingleMsg->setLeftItem(nShowLeftCount);

    SidebarNotificationDbus::getInstance()->addNotificationNum();

    if(nShowLeftCount+1>0){
        NotificationGsetting::getInstance()->setState(true);
    } else {
        NotificationGsetting::getInstance()->setState(false);
    }
    return;
}

/**
 * @brief AppMsg::deleteExceedingMsg
 * 当显示的最大消息大于设置的消息数目时删除最早的消息
 */
void AppMsg::deleteExceedingMsg()
{
    int nIndex = -1;
    while (m_listSingleMsg.count() > m_nMaxCount){
        nIndex = m_listSingleMsg.count() - 1;
        if (-1 == nIndex){
            return;
        }

        SingleMsg* pSingleMsg = m_listSingleMsg.at(nIndex);
        onDeleSingleMsg(pSingleMsg);
    }
}

/**
 * @brief AppMsg::initGsetting
 * 初始化 锁屏、消息通知、主题gsetting
 */
void AppMsg::initGsetting()
{
    m_pStyleName =  StyleGsetting::getInstance()->getStyleName();
    connect(StyleGsetting::getInstance(),&StyleGsetting::Sig_styleGsettingChanged,[=](QString key){
        if (key == "styleName") {
            m_pStyleName =  StyleGsetting::getInstance()->getStyleName();
            this->update();
        }
    });
    return;
}

/**
 * @brief AppMsg::addSingleMsg
 * 新增单条消息至通知列表，新类别消息需要new，然后添加至列表最上面
 * @param strIconPath 图标地址
 * @param strSummary 消息摘要
 * @param dateTime 消息时间
 * @param strBody 消息内容
 */
void AppMsg::addSingleMsg(QString strIconPath, QString strSummary, QDateTime dateTime, QString strBody, QString strUrl, QString strAction)
{
    SingleMsg* pSingleMsg = new SingleMsg(this, strIconPath, m_strAppName, strSummary, dateTime, strBody, strUrl, strAction);

    int uIndex = m_listSingleMsg.count();
    for(int i = m_listSingleMsg.count() - 1; i >= 0; i--){
        SingleMsg* pTmpSingleMsg = m_listSingleMsg.at(i);
        if(pSingleMsg->getPushTime() < pTmpSingleMsg->getPushTime()){
            break;
        }
        uIndex = i;
    }

    if((0 == uIndex) && (m_listSingleMsg.count() > 0)){
        /* 当新增的消息将插入最顶部时,将已有列表最顶部的老主消息正文设置一行缩略显示 */
        SingleMsg* pFirstMsg = m_listSingleMsg.at(0);
        pFirstMsg->setMainFlag(false);
        pFirstMsg->setShowLeftItemFlag(false);

        if(true == m_bFold){
            /* 只有已经折叠的才需要将现有的正文设置为缩略显示 */
            pFirstMsg->setBodyLabelWordWrap(false);
            pFirstMsg->setVisible(false);
            /* 将SingleMsg底部设置0px空隙,当其展开时，让他完全从0展开 */
            pFirstMsg->setSingleMsgContentsMargins(0, 0, 0, 0);
        }

        /* 如果新增的消息将插入最顶部，则将老的顶部消息，先从m_pMainVLaout移除，再插入m_pIndexFromOneVLaout顶部 */
        m_pMainVLaout->removeWidget(pFirstMsg);
        m_pIndexFromOneVLaout->insertWidget(0, pFirstMsg);
    }

    if((0 == uIndex) && (false == m_bFold)){
        /* 如果插入第0条,并且已展开,则将新增消息设置自动换行,并且折叠标志设为false */
        pSingleMsg->setBodyLabelWordWrap(true);
        pSingleMsg->setFoldFlag(m_bFold);
    }

    if(0 != uIndex){
        /* 如果插入的不是第0条,则自己不能为主 */
        pSingleMsg->setMainFlag(false);
        if(true == m_bFold){
            /* 并且已经折叠的自己不可见 */
            pSingleMsg->setVisible(false);
        }
    }

    m_listSingleMsg.insert(uIndex, pSingleMsg);
    if(0 != uIndex){
        /* 假如新增消息不是插入最顶部，则直接插入m_pIndexFromOneVLaout中的uIndex - 1 */
        m_pIndexFromOneVLaout->insertWidget(uIndex - 1, pSingleMsg);
    } else {
        /* 假如新增的消息将插入最顶部，由于上面已把老的顶部消息插入m_pIndexFromOneVLaout的顶部，所以此时直接插入m_pMainVLaout顶部 */
        m_pMainVLaout->insertWidget(0, pSingleMsg);
    }

    /* 当单个应用的消息达到设置的最大数目时，删除最早的消息并统计数目 */
    deleteExceedingMsg();

    /* 将该应用中最顶上的一条消息的时间赋给应用 */
    SingleMsg* pTopSingleMsg = m_listSingleMsg.at(0);
    m_uNotifyTime = pTopSingleMsg->getPushTime();
    m_dateTime = pTopSingleMsg->getPushDateTime();

    /* 统计应用剩余显示条数 */
    statisticLeftItem();

    return;
}

/**
 * @brief AppMsg::updateAppPushTime
 * 更新应用最新的推送时间
 */
void AppMsg::updateAppPushTime()
{
    for(int i = 0; i < m_listSingleMsg.count(); i++){
        SingleMsg* pTmpSingleMsg = m_listSingleMsg.at(i);
        pTmpSingleMsg->updatePushTime();
    }
    return;
}

/**
 * @brief AppMsg::getSingleMsgCount
 * 获取应用消息数
 * @return 应用消息数量
 */
int AppMsg::getSingleMsgCount()
{
    return m_listSingleMsg.count();
}

/**
 * @brief AppMsg::onDeleteAppMsg
 * 删除一个应用消息
 */
void AppMsg::onDeleteAppMsg()
{
    emit Sig_onDeleteAppMsg(this);
    return;
}

/**
 * @brief AppMsg::onDeleSingleMsg
 * 删除单条消息
 * @param pSingleMsg 需要删除的 SingleMsg 指针
 */
void AppMsg::onDeleSingleMsg(SingleMsg* pSingleMsg)
{
    int nIndex = m_listSingleMsg.indexOf(pSingleMsg);
    if(-1 == nIndex){
        qDebug()<<"AppMsg::onDeleSingleMsg 在通知链表中未找到pSingleMsg指针";
        return;
    }

    m_listSingleMsg.removeAt(nIndex);
    if(0 == nIndex){
        m_pMainVLaout->removeWidget(pSingleMsg);
        if(m_listSingleMsg.count() > 0)
        {
            SingleMsg* pTopSingleMsg = m_listSingleMsg.at(0);
            m_pIndexFromOneVLaout->removeWidget(pTopSingleMsg);
            m_pMainVLaout->insertWidget(0, pTopSingleMsg);
        }
    } else {
        m_pIndexFromOneVLaout->removeWidget(pSingleMsg);
    }
//    pSingleMsg->deleteLater();

    /* 当本次删除为应用首条时,且该应用不止一条,则需将新的首条设置为顶部消息状态 */
    if(0 == nIndex) {
        setTopWithSecondItem();
    }

    /* 统计剩余显示条数,看情况显示 */
    statisticLeftItem();

    /* 当删除一条消息后,如果应用列表为空 */
    if(0 == m_listSingleMsg.count()){
        emit Sig_onDeleteAppMsg(this);
    }
    return;
}

/**
 * @brief AppMsg::setTopWithSecondItem
 * 当应用最顶条被删除后,将第二条置顶
 */
void AppMsg::setTopWithSecondItem()
{
    if (m_listSingleMsg.count() > 0) {
        SingleMsg* pFirstMsg = m_listSingleMsg.at(0);
        pFirstMsg->setMainFlag(true);
        pFirstMsg->setFoldFlag(m_bFold);
        pFirstMsg->setBodyLabelWordWrap(true);
        pFirstMsg->setVisible(true);
    }
    return;
}

/**
 * @brief AppMsg::setAppFoldFlag
 * 设置应用折叠标志
 * @param bFlag TRUE 折叠 FALSE 展开
 */
void AppMsg::setAppFoldFlag(bool bFlag)
{
    m_bFold = bFlag;

    //当应用处于展开状态，或者总条数小于等于1时,应用底图部件隐藏
    if ((false == m_bFold) || (m_listSingleMsg.count() <= 1)) {
        m_pAppBaseMapWidget->setVisible(false);
    }

    if (m_listSingleMsg.count() <= 1) {
        return;
    }

    if (false == m_bFold) {
        if (m_listSingleMsg.count()>1) {
            m_pFoldBtnWid->setVisible(true);
        }
        int widthFoldWid = m_pFoldBtnWid->width();
        int heightFoldWid = m_pFoldBtnWid->height();
        QPropertyAnimation* pAnimation2 = new QPropertyAnimation(this, "geometryFold");
        connect(pAnimation2, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value){
            QRect Rect = value.value<QRect>();
            int x1, y1, x2, y2;
            Rect.getRect(&x1, &y1, &x2, &y2);
            m_pFoldBtnWid->setGeometry(x1, y1, x2, y2);
        });
        connect(pAnimation2, &QPropertyAnimation::finished, this,[=](){
            m_pMainBaseVLaout->insertWidget(0,m_pFoldBtnWid);
        });
        pAnimation2->setDuration(30);
        pAnimation2->setStartValue(QRect(0, 0-heightFoldWid, widthFoldWid, heightFoldWid));
        pAnimation2->setEndValue(QRect(0, 0, widthFoldWid, heightFoldWid));
        pAnimation2->start(QAbstractAnimation::DeleteWhenStopped);

        int widthAppWid = m_pMainWid->width();
        int heightAppWid = m_pMainWid->height();
        QPropertyAnimation* pAnimation1 = new QPropertyAnimation(this, "geometryMain");
        connect(pAnimation1, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value){
            QRect Rect = value.value<QRect>();
            int x1, y1, x2, y2;
            Rect.getRect(&x1, &y1, &x2, &y2);
            m_pMainWid->setGeometry(x1, y1, x2, y2);
        });
        connect(pAnimation1, &QPropertyAnimation::finished, this,[=](){
            /* 多条消息展开动画：展开时,索引从第1条开始,消息全部可见 */
            SingleMsg* pFristSingleMsg = m_listSingleMsg.at(0);
            pFristSingleMsg->setBodyLabelWordWrap(true);
            pFristSingleMsg->setFoldFlag(false);
            for (int i = 1; i < m_listSingleMsg.count(); i++) {
                SingleMsg* pTmpSingleMsg = m_listSingleMsg.at(i);
                pTmpSingleMsg->setBodyLabelWordWrap(true);
                pTmpSingleMsg->setFoldFlag(false);
                pTmpSingleMsg->startAnimationUnfold();
            }
        });
        pAnimation1->setDuration(30);
        pAnimation1->setStartValue(QRect(0, 0, widthAppWid, heightAppWid));
        pAnimation1->setEndValue(QRect(0, m_pFoldBtnWid->height(), widthAppWid, heightAppWid));
        pAnimation1->start(QAbstractAnimation::DeleteWhenStopped);

    } else {
        /* 折叠时,索引从第1条开始,消息全部不可见 */
        for (int i = 1; i < m_listSingleMsg.count(); i++) {
            SingleMsg* pTmpSingleMsg = m_listSingleMsg.at(i);
            pTmpSingleMsg->startAnimationFold();
        }
        m_pMainBaseVLaout->removeWidget(m_pFoldBtnWid);
        m_pFoldBtnWid->setVisible(false);
        onFoldAppWidget();
    }
}

/**
 * @brief AppMsg::setAppFold
 * 将app消息设置为折叠状态
 */
void AppMsg::setAppFold()
{
    if (false == m_bFold) {
        /* 假如应用展开，则将应用主消息设置折叠 */
        SingleMsg* pFirstSingleMsg = m_listSingleMsg.at(0);
        pFirstSingleMsg->mainMsgSetShowOrFold(false);
    }
}

/**
 * @brief AppMsg::onShowBaseMap
 * 显示应用底图
 */
void AppMsg::onShowBaseMap()
{
    if ((true == m_bFold) && (m_listSingleMsg.count() > 1)) {
        /* 当应用处于折叠状态，且总条数大于1时,应用底图部件显示 */
        SingleMsg* pTmpSingleMsg = m_listSingleMsg.at(0);
        pTmpSingleMsg->setSingleMsgContentsMargins(0, 0, 0, 0);
        pTmpSingleMsg->setShowLeftItemFlag(true);
        m_pAppBaseMapWidget->setVisible(true);
    }
}

/**
 * @brief AppMsg::onHideBaseMap
 * 隐藏应用底图
 */
void AppMsg::onHideBaseMap()
{
    m_pAppBaseMapWidget->setVisible(false);
}


FoldBtn::FoldBtn()
{
    this->setObjectName("fold");
    this->setLayoutDirection(Qt::LeftToRight);
    this->setText(QObject::tr(" fold"));
    this->setFixedSize(88,36);
    setButtonIcon();

    updateStyle(StyleGsetting::getInstance()->getStyleName());
    connect(StyleGsetting::getInstance(),&StyleGsetting::Sig_styleGsettingChanged,[=](QString key){
        if(key == "styleName") {
            updateStyle(StyleGsetting::getInstance()->getStyleName());
            this->update();
        }
    });
}

/**
 * @brief FoldBtn::setButtonIcon
 * 设置折叠按钮
 */
void FoldBtn::setButtonIcon()
{
    m_pPictureToWhite = new PictureToWhite();
    QPixmap pixmap = QIcon::fromTheme("kylin-fold-hover").pixmap(17,17);
    pixmap = m_pPictureToWhite->drawSymbolicColoredPixmap(pixmap);
    this->setIcon(QIcon(pixmap));
}

/**
 * @brief FoldBtn::updateStyle
 * 更新样式
 * @param styleName
 */
void FoldBtn::updateStyle(QString styleName)
{
    if (styleName == "ukui-dark") {
        this->setStyleSheet("QPushButton{background:rgba(0,0,0,222); border:0px; border-radius:6px;}"
                            "QPushButton:hover{background:rgba(0,0,0,200); border:0px; border-radius:6px;}"
                            "QPushButton:pressed{background:rgba(0,0,0,222); border:0px; border-radius:6px;}");
    } else {
        this->setStyleSheet("QPushButton{background:rgba(255,255,255,255); border:0px; border-radius:6px;}"
                            "QPushButton:hover{background:rgba(255,255,255,71); border:0px; border-radius:6px;}"
                            "QPushButton:pressed{background:rgba(255,255,255,41); border:0px; border-radius:6px;}");
    }
    setButtonIcon();
}

//清除按钮
DelBtn::DelBtn()
{
    setButtonIcon();
    this->setFixedSize(24,24);
    updateStyle(StyleGsetting::getInstance()->getStyleName());
    connect(StyleGsetting::getInstance(),&StyleGsetting::Sig_styleGsettingChanged,[=](QString key){
        if(key == "styleName") {
            updateStyle(StyleGsetting::getInstance()->getStyleName());
            this->update();
        }
    });
}

/**
 * @brief DelBtn::setButtonIcon
 * 设置清除按钮
 */
void DelBtn::setButtonIcon()
{
    m_pPictureToWhite = new PictureToWhite();
    QPixmap pixmap = QIcon::fromTheme("edit-clear-symbolic").pixmap(12,12);
    pixmap = m_pPictureToWhite->drawSymbolicColoredPixmap(pixmap);
    this->setIcon(QIcon(pixmap));
}

/**
 * @brief DelBtn::updateStyle
 * 更新样式
 * @param styleName
 */
void DelBtn::updateStyle(QString styleName)
{
    if (styleName == "ukui-dark") {
        this->setStyleSheet("QPushButton{background:rgba(0,0,0,200); border:0px; border-radius:12px;}"
                                 "QPushButton:hover{background:rgba(0,0,0,222); border:0px; border-radius:12px;}"
                                 "QPushButton:pressed{background:rgba(0,0,0,200); border:0px; border-radius:12px;}");
    } else {
        this->setStyleSheet("QPushButton{background:rgba(255,255,255,200); border:0px; border-radius:12px;}"
                                 "QPushButton:hover{background:rgba(255,255,255,222); border:0px; border-radius:12px;}"
                                 "QPushButton:pressed{background:rgba(255,255,255,200); border:0px; border-radius:12px;}");
    }
    setButtonIcon();
}

