#include "fixpushbutton.h"
#include <QGSettings>

FixPushButton::FixPushButton(QWidget *parent) :
    QPushButton(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 ("systemFont" == key || "systemFontSize" ==key) {
                changedLabelSlot();
            }
        });
    }
}


void FixPushButton::setButtonText(QString text) {

    mStr = text;
    changedLabelSlot();
}

QString FixPushButton::getText(){
    return mStr;
}

void FixPushButton::changedLabelSlot() {
    QFontMetrics  fontMetrics(this->font());
    int fontSize = fontMetrics.width(mStr);
    if (fontSize > 65) {
        setText(fontMetrics.elidedText(mStr, Qt::ElideRight, 65));
        setToolTip(mStr);
    } else {
        setText(mStr);
        setToolTip("");
    }
}
