#include "custom_widget.h"

#include <QPainter>
#include <QStyleOption>

#include "common.h"
#include "gsettings_monitor.h"

CustomWidget::CustomWidget(QWidget *parent)
    : QWidget(parent)
{
    this->setProperty("useSystemStyleBlur",true);
    this->setAttribute(Qt::WA_TranslucentBackground,true);
    m_tran = GsettingsMonitor::getInstance()->sysTransparencyGet();
    this->initConnect();
}

CustomWidget::~CustomWidget()
{
}

void CustomWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter p(this);
    // p.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    QPainterPath rectPath;
    rectPath.addRoundedRect(this->rect(), 0, 0); // 左上右下
//    double tran=m_pGsettingControlTrans->get("transparency").toDouble()*255;
    QStyleOption opt;
    opt.init(this);

    QColor mainColor;

    if (QColor(255,255,255) == opt.palette.color(QPalette::Base) || QColor(248,248,248) == opt.palette.color(QPalette::Base)) {
        mainColor = QColor(242, 242, 242, m_tran);
    } else {
        mainColor = QColor(20, 20, 20, m_tran);
    }

    p.fillPath(rectPath,QBrush(mainColor));
    QWidget::paintEvent(event);
}

void CustomWidget::transChange(int transparency)
{
    m_tran = transparency * 2.55;
    this->update();
}

//读取主题配置文件
void CustomWidget::initConnect()
{
    connect(GsettingsMonitor::getInstance(), &GsettingsMonitor::sysTransparencyUpdate,
            this,                            &CustomWidget::transChange);
}