#include "freqlabel.h"
#include <QGSettings>
#include <QPainter>
#include <QVariant>

#define FREQLABLE_HIGHT 18
#define FREQLABLE_MARGINS 4,0,4,0
#define RADIUS 6.0

FreqLabel::FreqLabel(QWidget *parent) : QLabel(parent)
{
    const QByteArray id("org.ukui.style");
    QGSettings * fontSetting = new QGSettings(id, QByteArray(), this);
    if(QGSettings::isSchemaInstalled(id)){
        connect(fontSetting, &QGSettings::changed,[=](QString key) {
            if ("systemFontSize" ==key) {
                changedFontSlot();
            }
        });
    }
    changedFontSlot();
    setText("...");
    setFixedHeight(FREQLABLE_HIGHT);
    setContentsMargins(FREQLABLE_MARGINS);
}

void FreqLabel::changedFontSlot()
{
    const QByteArray id("org.ukui.style");
    if(QGSettings::isSchemaInstalled(id)){
        QGSettings * fontSetting = new QGSettings(id, QByteArray(), this);
        QVariant fontVariant =  fontSetting->get("systemFontSize");
        QFont font;
        font.setPointSize(fontVariant.toInt()*0.85);
        this->setFont(font);
    }
}

void FreqLabel::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing); //抗锯齿效果
    auto rect = this->rect();
    painter.drawRoundedRect(rect, RADIUS, RADIUS);
    QLabel::paintEvent(event);
}
