#include "commondata.h"

//! 省略显示label实现方法
LabelFotmatText::LabelFotmatText()
{

}

/* 设置...字样 */
QString LabelFotmatText::SetFormatBody(QString text, QLabel *CopyDataLabal)
{
    setWidgetFont();
    QFontMetrics fontMetrics(CopyDataLabal->font());
    int LableWidth = CopyDataLabal->width();
    int fontSize = fontMetrics.width(text);
    m_pLabel = CopyDataLabal;
    m_pFormatBody = text;
    if(fontSize > (LableWidth)) {
        //说明只存在一行，在最后面加...就行
        QString format = m_pFormatBody + "aa";
        format = fontMetrics.elidedText(format, Qt::ElideRight, LableWidth - 10);
        m_bTooltips = true;
        return format;
    }
    return m_pFormatBody;
}

/* 监听gsetting，修改所有的字体 */
void LabelFotmatText::setWidgetFont()
{
    connect(StyleGsetting::getInstance(),&StyleGsetting::Sig_styleGsettingChanged,[=](QString key){
        if (UKUI_STYLE_FONT_KEY == key || UKUI_STYLE_FONT_SIZE_KEY == key) {
            QTimer *startTimer = new QTimer;
            startTimer->setSingleShot(true);
            QObject::connect(startTimer,&QTimer::timeout,[=](){
                initLabelText();
            });
            startTimer->start(5);
        }
    });
}

void LabelFotmatText::initLabelText()
{
    if (m_bTooltips) {
        m_pLabel->setToolTip(m_pFormatBody);
    }
    m_pLabel->setText(this->SetFormatBody(m_pFormatBody, m_pLabel));
}


//! 反转图片颜色
PictureToWhite::PictureToWhite(QObject *parent) : QObject(parent)
{
    initGsettingValue();
}

//初始化获取主题，并连接监听信号
void PictureToWhite::initGsettingValue()
{
    updateStyle();

    connect(StyleGsetting::getInstance(),&StyleGsetting::Sig_styleGsettingChanged,this,[=](const QString &key){
        if (key == UKUI_STYLE_NAME_KEY) {
            updateStyle();
        }
    });
}

void PictureToWhite::updateStyle()
{
    // 仅 ukui-dark 为黑色
    if (StyleGsetting::getInstance()->getStyleName() == DARK_STYLE){
        //黑色主题
        tray_icon_color = TRAY_ICON_COLOR_DRAK;
    } else {
        //白色主题
        tray_icon_color = TRAY_ICON_COLOR_LOGHT;
    }
}

QPixmap PictureToWhite::drawSymbolicColoredPixmap(const QPixmap &source)
{
    QColor gray(128,128,128);
    QColor standard (31,32,34);
    QImage img = source.toImage();
    for (int x = 0; x < img.width(); x++) {
        for (int y = 0; y < img.height(); y++) {
            auto color = img.pixelColor(x, y);
            if (color.alpha() > 0) {
                if (qAbs(color.red()-gray.red()) < 255 && qAbs(color.green()-gray.green()) < 255 && qAbs(color.blue()-gray.blue()) < 255) {
                    color.setRed(tray_icon_color);
                    color.setGreen(tray_icon_color);
                    color.setBlue(tray_icon_color);
                    //color.setAlphaF(0.75);
                    img.setPixelColor(x, y, color);
                } else if (qAbs(color.red()-standard.red()) < 255 && qAbs(color.green()-standard.green()) < 255 && qAbs(color.blue()-standard.blue()) < 255) {
                    color.setRed(tray_icon_color);
                    color.setGreen(tray_icon_color);
                    color.setBlue(tray_icon_color);
                    //color.setAlphaF(0.75);
                    img.setPixelColor(x, y, color);
                } else
                    //color.setAlphaF(0.75);
                    img.setPixelColor(x, y, color);
            }
        }
    }
    return QPixmap::fromImage(img);
}
