#include "etemp.h"
#include "etempattr.h"
#include "loapptools.h"
#include "QSettings"
#include "QThread"
eTemp::eTemp(QRectF rectInit,InteractiveType type, QGraphicsItem *parent):
    eObject(rectInit, type, parent)
{
    m_iType=eObject::Temp;
    m_attr.cBackground=Qt::transparent;
    m_attr.bTemperature         = true;
    m_attr.compensation          = 0;
    m_attr.temperatureType      = 0;
    m_attr.labelTemperature     = tr("Temperature:");
    m_attr.bTitle               = false;
    m_attr.labelTitle          = "";
    m_attr.bHumidity= true;
    m_attr.labelHumidity= tr("Humidity:");

    m_attr.bNoise= true;
    m_attr.labelNoise= tr("Noise:");

    m_attr.bWindSpeed= true;
    m_attr.labelWindSpeed= tr("Wind Speed:");

    m_attr.bWindDirection= true;
    m_attr.labelWindDirectiton= tr("Wind Direction:");

    m_attr.bPM25= true;
    m_attr.labelPm25= tr("PM25:");

    m_attr.bPM10= true;
    m_attr.labelPm10= tr("PM10:");

    m_attr.iAlignType=0;
    m_attr.fontFamily    = "Arial";
    m_attr.fontSize      = 9;
    m_attr.fontBold      = false;
    m_attr.fontItalics   = false;
    m_attr.fontUnderline = false;
    m_attr.textColor     = Qt::red;
    m_attr.playRefresh   = 10;
    m_attr.playDuration  = MACRO_DEFAULT_PLAYDURATION;
    m_attr.bSingleScroll = false;
    m_attr.iScrollSpeed = 10;
    m_pScrollTimer=new QTimer(this);
    if(m_pRenderPixmap!=nullptr)
         delete m_pRenderPixmap;
    m_pRenderPixmap=new QPixmap(rect().width(),rect().height());
    m_pRenderPixmap->fill(m_attr.cBackground);
    //m_pRenderPixmap->fill(m_attr.);
    connect(m_pScrollTimer,SIGNAL(timeout()),this,SLOT(TimerOutPro()));
    connect(this, SIGNAL(rectChanged(const QRectF &)), this, SLOT(OnRectChanged()));

}

eTemp::eTemp(const QJsonObject &json, InteractiveType type, QGraphicsItem *parent):
    eObject(json["geometry"].toObject(), type, parent)
{
    m_iType=eObject::Temp;
    setElement(json, m_attr);
    m_pScrollTimer=new QTimer(this);
     if(m_pRenderPixmap!=nullptr)
         delete m_pRenderPixmap;
    m_pRenderPixmap=new QPixmap(rect().width(),rect().height());
    m_pRenderPixmap->fill(m_attr.cBackground);
    connect(m_pScrollTimer,SIGNAL(timeout()),this,SLOT(TimerOutPro()));
    connect(this, SIGNAL(rectChanged(const QRectF &)), this, SLOT(OnRectChanged()));
}

void eTemp::setElement(const QJsonObject &json, Data &attr)
{
    auto apptool = LoAppTools::getInstance();
    attr.bTemperature          =               json["widget"]["bTemperature"].toBool();
    attr.compensation          =               json["widget"]["temperatureCompensation"].toInt();
    attr.temperatureType      =               json["widget"]["temperatureStyle"].toInt();
    attr.labelTemperature     =               json["widget"]["labelTemperature"].toString();
    attr.labelTitle          =               json["widget"]["labelTitle"].toString();
    attr.bTitle               =  json["widget"]["bTitle"].toBool();

    attr.bHumidity=  json["widget"]["bHumidity"].toBool();
    attr.labelHumidity= json["widget"]["labelHumidity"].toString();

    attr.bNoise= json["widget"]["bNoise"].toBool();
    attr.labelNoise= json["widget"]["labelNoise"].toString();

    attr.bWindSpeed= json["widget"]["bWindSpeed"].toBool();
    attr.labelWindSpeed= json["widget"]["labelWindSpeed"].toString();

    attr.bWindDirection= json["widget"]["bWindDirection"].toBool();
    attr.labelWindDirectiton= json["widget"]["labelWindDirection"].toString();

    attr.bPM25= json["widget"]["bPM25"].toBool();
    attr.labelPm25= json["widget"]["labelPM25"].toString();

    attr.bPM10= json["widget"]["bPM10"].toBool();
    attr.labelPm10= json["widget"]["labelPM10"].toString();

    attr.bSingleScroll= json["widget"]["bPaomadeng"].toBool();
    attr.iScrollSpeed =json["widget"]["scrollSpeed"].toInt();
    attr.cBackground       = apptool->int2Color(json["widget"]["cBackground"].toInt());

    attr.iAlignType    =                json["widget"]["alignType"]     .toInt();
    attr.fontFamily    =               json["widget"]["fontFamily"]   .toString();
    attr.fontSize      =               json["widget"]["fontSize"]     .toInt();
    attr.fontBold      =               json["widget"]["fontBold"]     .toBool();
    attr.fontItalics   =               json["widget"]["fontItalics"]  .toBool();
    attr.fontUnderline =               json["widget"]["fontUnderline"].toBool();
    attr.textColor     = apptool->int2Color(json["widget"]["textColor"]    .toInt());
    attr.playRefresh   =               json["play"]["refresh"]        .toInt();
    attr.playDuration  =               json["play"]["duration"]       .toInt();
}
void eTemp::TimerOutPro()
{
    if(m_pRenderPixmap==nullptr)
        return;
    m_iScrollOffset--;
    QPainter painter;
    m_pRenderPixmap->fill(m_attr.cBackground);

    painter.begin(m_pRenderPixmap);
    QString strOutputString="";
    if(m_attr.bTitle)
    {
         strOutputString+=m_attr.labelTitle+" ";
    }
    QString sUnit;
    if(m_attr.bTemperature)
    {
        if(m_attr.temperatureType == 0) {
            sUnit = QStringLiteral("℃");
        } else {
            sUnit = QStringLiteral("℉");
        }
        strOutputString+=QString(m_attr.labelTemperature + "%1" + sUnit).arg(00 + m_attr.compensation)+" ";
    }
    if(m_attr.bHumidity)
    {
        strOutputString+=QString(m_attr.labelHumidity+"%1"+"RH").arg(00)+" ";
    }
    if(m_attr.bNoise)
    {
        strOutputString+=QString(m_attr.labelNoise+"%1"+"dB").arg(00)+" ";
    }
    if(m_attr.bWindSpeed)
    {
        strOutputString+=QString(m_attr.labelWindSpeed+"%1"+"m/s").arg(00)+" ";
    }
    if(m_attr.bWindDirection)
    {
        strOutputString+=QString(m_attr.labelWindDirectiton+"%1").arg(00)+" ";
    }
    if(m_attr.bPM10)
    {
        strOutputString+=QString(m_attr.labelPm10+"%1"+"μg/m³").arg(00)+" ";
     }
    if(m_attr.bPM25)
    {
        strOutputString+=QString(m_attr.labelPm25+"%1"+"μg/m³").arg(00)+" ";
     }
    QTextOption opt;
    opt.setAlignment(Qt::AlignLeft | Qt:: AlignVCenter);
    opt.setWrapMode(QTextOption::NoWrap);
    QFont font;
    font.setFamily(m_attr.fontFamily);
    font.setPointSize(m_attr.fontSize);
    font.setBold(m_attr.fontBold);
    font.setItalic(m_attr.fontItalics);
    font.setUnderline(m_attr.fontUnderline);
    MACRO_FONT_FANG_JUCHI_2(font)
    painter.setFont(font);
    painter.setPen(m_attr.textColor);
    painter.setRenderHint(QPainter::Antialiasing, false);

    QFontMetrics fm(font);
    QRect rec = fm.boundingRect(strOutputString);
    if(m_iScrollOffset<-1*rec.width())
        m_iScrollOffset=rect().width();
    QRectF rRect;
    rRect=QRectF(m_iScrollOffset,0, rec.width(), rect().height());
    painter.drawText(rRect, strOutputString, opt);
    painter.end();
    updateGeometry();

}
void eTemp::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QString sUnit;
    QRectF rRect;
    int iCountItem=0;
    int iYOffset=0;
    int iYHeight=0;
    QTextOption opt;
    opt.setAlignment(Qt::AlignLeft | Qt:: AlignVCenter);

    if(m_attr.bTitle)
        iCountItem++;
    if(m_attr.bTemperature)
        iCountItem++;
    if(m_attr.bHumidity)
        iCountItem++;
    if(m_attr.bNoise)
        iCountItem++;
    if(m_attr.bWindSpeed)
        iCountItem++;
    if(m_attr.bWindDirection)
        iCountItem++;
    if(m_attr.bPM10)
        iCountItem++;
    if(m_attr.bPM25)
        iCountItem++;
    rRect=INVALID_RECT;
    if(iCountItem>0)
        iYHeight=rect().height() / iCountItem;
    else {
        return;
    }
    QFont font;
    font.setFamily(m_attr.fontFamily);
    font.setPointSize(m_attr.fontSize);
    font.setBold(m_attr.fontBold);
    font.setItalic(m_attr.fontItalics);
    font.setUnderline(m_attr.fontUnderline);
    MACRO_FONT_FANG_JUCHI_2(font)
    painter->setFont(font);
    painter->setPen(m_attr.textColor);
    painter->setRenderHint(QPainter::Antialiasing, false);
    if(m_attr.bSingleScroll)
    {
        if(!m_pScrollTimer->isActive())
        {
            m_iScrollOffset=rect().width();
            m_pScrollTimer->start(m_attr.iScrollSpeed);
        }
         painter->save();
         MACRO_FAN_JUCHI(painter)
         painter->drawPixmap(rect(), *m_pRenderPixmap,rect());
         painter->restore();
    }
    else {
        if(m_pScrollTimer->isActive())
            m_pScrollTimer->stop();
        if(m_attr.iAlignType==0)
            opt.setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
        else if(m_attr.iAlignType==1)
            opt.setAlignment(Qt::AlignCenter);
        else if(m_attr.iAlignType==2)
            opt.setAlignment(Qt::AlignRight|Qt::AlignVCenter);
        painter->fillRect(rect(),m_attr.cBackground);
        if(m_attr.bTitle)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, m_attr.labelTitle, opt);

            iYOffset+=iYHeight;
        }
        if(m_attr.bTemperature)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            if(m_attr.temperatureType == 0) {
                sUnit = QStringLiteral("℃");
            } else {
                sUnit = QStringLiteral("℉");
            }
            painter->drawText(rRect, QString(m_attr.labelTemperature + "%1" + sUnit).arg(00 + m_attr.compensation), opt);
            iYOffset+=iYHeight;
        }
        if(m_attr.bHumidity)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, QString(m_attr.labelHumidity+"%1"+"RH").arg(00), opt);
             iYOffset+=iYHeight;
        }
        if(m_attr.bNoise)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, QString(m_attr.labelNoise+"%1"+"dB").arg(00), opt);
             iYOffset+=iYHeight;
        }
        if(m_attr.bWindSpeed)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, QString(m_attr.labelWindSpeed+"%1"+"m/s").arg(00), opt);
             iYOffset+=iYHeight;
        }
        if(m_attr.bWindDirection)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, QString(m_attr.labelWindDirectiton+"%1").arg(00), opt);
             iYOffset+=iYHeight;
        }
        if(m_attr.bPM10)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, QString(m_attr.labelPm10+"%1"+"μg/m³").arg(00), opt);
             iYOffset+=iYHeight;
        }
        if(m_attr.bPM25)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, QString(m_attr.labelPm25+"%1"+"μg/m³").arg(00), opt);
             iYOffset+=iYHeight;
        }
    }

    eObject::paint(painter, option, widget);
}

QWidget* eTemp::wAttr()
{
    QWidget *wObj = eObject::wAttr();
    QWidget *w = wAttrElement();
    static_cast<QBoxLayout*>(w->layout())->insertWidget(0, wObj);
    return w;
}

QWidget* eTemp::wAttrElement()
{
    eTempAttr *w = new eTempAttr(m_attr);
    connect(w, SIGNAL(sAttrChanged(const eTemp::Data &)), this, SLOT(onAttrChanged(const eTemp::Data &)));
    connect(w, SIGNAL(sBackgroundColorChanged(const QColor&)), this, SLOT(onBackgroundColorChanged(const QColor&)));

    return w;
}

QJsonObject eTemp::elementJson() const
{
    auto apptool = LoAppTools::getInstance();
    QJsonObject oRoot;
    QJsonObject oWidget;
    QJsonObject oPlay;
    auto at = LoAppTools::getInstance();

    oRoot["elementType"]   = "Temp";
    oRoot["elementTypeId"] = type();
    oRoot["geometry"]      = eObject::elementJson();

    oWidget["bTemperature"]          = m_attr.bTemperature;
    oWidget["temperatureCompensation"]          = m_attr.compensation;
    oWidget["temperatureStyle"]     = m_attr.temperatureType;
    oWidget["labelTemperature"]     = m_attr.labelTemperature;

    oWidget["bTitle"]          = m_attr.bTitle;
    oWidget["labelTitle"]          = m_attr.labelTitle;

    oWidget["bHumidity"]          = m_attr.bHumidity;
    oWidget["labelHumidity"]          = m_attr.labelHumidity;

    oWidget["bNoise"]          = m_attr.bNoise;
    oWidget["labelNoise"]          = m_attr.labelNoise;

    oWidget["bWindSpeed"]          = m_attr.bWindSpeed;
    oWidget["labelWindSpeed"]          = m_attr.labelWindSpeed;

    oWidget["bWindDirection"]          = m_attr.bWindDirection;
    oWidget["labelWindDirection"]          = m_attr.labelWindDirectiton;

    oWidget["bPM25"]          = m_attr.bPM25;
    oWidget["labelPM25"]          = m_attr.labelPm25;

    oWidget["bPM10"]          = m_attr.bPM10;
    oWidget["labelPM10"]          = m_attr.labelPm10;

    oWidget["bPaomadeng"]          = m_attr.bSingleScroll;
    oWidget["scrollSpeed"]          = m_attr.iScrollSpeed;
    oWidget["alignType"]    = m_attr.iAlignType;
    oWidget["fontFamily"]    = m_attr.fontFamily;
    oWidget["fontSize"]      = m_attr.fontSize;
    oWidget["fontBold"]      = m_attr.fontBold;
    oWidget["fontItalics"]   = m_attr.fontItalics;
    oWidget["fontUnderline"] = m_attr.fontUnderline;
    oWidget["textColor"]     = at->color2Int(m_attr.textColor);
    oWidget["cBackground"] = apptool->color2Int(m_attr.cBackground);
    QRectF r1 = geometry();
    QRect r=r1.toRect();
    QString strDir=QString("%1%2%3%4%5").arg(zValue()).arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height());
    oWidget.insert("idDir",strDir);
    oRoot["widget"]          = oWidget;

    oPlay["refresh"]  = m_attr.playRefresh;
    oPlay["duration"] = m_attr.playDuration;
    oRoot["play"]     = oPlay;

    return  oRoot;
}

void eTemp::onAttrChanged(const eTemp::Data &data)
{
    bool iReStartTimer=false;
    if(data.bSingleScroll)
    {
        if(m_attr.iScrollSpeed!=data.iScrollSpeed)
            iReStartTimer=true;

    }
    m_attr = data;
    if(data.bSingleScroll)
    {
        if(!m_pScrollTimer->isActive())
        {
            m_iScrollOffset=rect().width();

            m_pScrollTimer->start(m_attr.iScrollSpeed);
        }
        else {
            if(iReStartTimer)
            {
                m_pScrollTimer->stop();
                m_pScrollTimer->start(m_attr.iScrollSpeed);

            }
        }

    }
    else {
        if(m_pScrollTimer->isActive())
            m_pScrollTimer->stop();

    }
    m_attr = data;
    updateGeometry();
}


void eTemp::CreateAllWordPic(QString strPath)
{
    QStringList strWordList;
    strWordList.append(tr("0"));
    strWordList.append(tr("1"));
    strWordList.append(tr("2"));
    strWordList.append(tr("3"));
    strWordList.append(tr("4"));
    strWordList.append(tr("5"));
    strWordList.append(tr("6"));
    strWordList.append(tr("7"));
    strWordList.append(tr("8"));
    strWordList.append(tr("9"));

    strWordList.append(m_attr.labelTitle);
    strWordList.append(m_attr.labelTemperature);
    strWordList.append(m_attr.labelHumidity);
    strWordList.append(m_attr.labelNoise);
    strWordList.append(m_attr.labelWindSpeed);
    strWordList.append(m_attr.labelWindDirectiton);
    strWordList.append(m_attr.labelPm10);
    strWordList.append(m_attr.labelPm25);

    strWordList.append("℃");
    strWordList.append("℉");
    strWordList.append("RH");
    strWordList.append("dB");
    strWordList.append("m/s");
    strWordList.append("μg/m³");
    strWordList.append("μg/m³");
    strWordList.append("-");
 //   strWordList.append(" ");
    strWordList.append(tr("north"));
    strWordList.append(tr("north")+tr("east")+tr("north"));
    strWordList.append(tr("east")+tr("north"));
    strWordList.append(tr("east")+tr("east")+tr("north"));
    strWordList.append(tr("east"));
    strWordList.append(tr("east")+tr("east")+tr("south"));
    strWordList.append(tr("east")+tr("south"));
    strWordList.append(tr("south")+tr("east")+tr("south"));
    strWordList.append(tr("south"));
    strWordList.append(tr("south")+tr("west")+tr("south"));
    strWordList.append(tr("west")+tr("south"));
    strWordList.append(tr("west")+tr("west")+tr("south"));
    strWordList.append(tr("west"));
    strWordList.append(tr("west")+tr("west")+tr("north"));
    strWordList.append(tr("west")+tr("north"));
    strWordList.append(tr("north")+tr("west")+tr("north"));

    QStringList strFileList;
    strFileList.append("0");
    strFileList.append("1");
    strFileList.append("2");
    strFileList.append("3");
    strFileList.append("4");
    strFileList.append("5");
    strFileList.append("6");
    strFileList.append("7");
    strFileList.append("8");
    strFileList.append("9");
    strFileList.append("labeltitle");
    strFileList.append("labeltemperature");
    strFileList.append("labelhumidity");
    strFileList.append("labelnoise");
    strFileList.append("labelwindSpeed");
    strFileList.append("labelwindDirection");
    strFileList.append("labelpm10");
    strFileList.append("labelpm25");

    strFileList.append("unit_celsius");
    strFileList.append("unit_fahrenheit");
    strFileList.append("unit_humidity");
    strFileList.append("unit_noise");
    strFileList.append("unit_windspeed");
    strFileList.append("unit_pm10");
    strFileList.append("unit_pm25");
    strFileList.append("minus_sign");

    strFileList.append("N");
    strFileList.append("NNE");
    strFileList.append("NE");
    strFileList.append("ENE");
    strFileList.append("E");
    strFileList.append("ESE");
    strFileList.append("SE");
    strFileList.append("SSE");
    strFileList.append("S");
    strFileList.append("SSW");
    strFileList.append("SW");
    strFileList.append("WSW");
    strFileList.append("W");
    strFileList.append("WNW");
    strFileList.append("NW");
    strFileList.append("NNW");

    int iPicSuCaiHeight=m_attr.fontSize;
    int iBCount=0;

        if(m_attr.bTitle)
        {
            iBCount++;
        }
        if(m_attr.bTemperature)
        {
            iBCount++;
        }
        if(m_attr.bHumidity)
        {
            iBCount++;
        }
        if(m_attr.bNoise)
        {
            iBCount++;
        }
        if(m_attr.bWindSpeed)
        {
            iBCount++;
        }
        if(m_attr.bWindDirection)
        {
            iBCount++;
        }
        if(m_attr.bPM10)
        {
            iBCount++;
         }
        if(m_attr.bPM25)
        {
            iBCount++;
        }


    int itempHeight=m_attr.fontSize;
    for(int i=0;i<strWordList.count();i++)
    {
        QFont font;
        font.setFamily(m_attr.fontFamily);
        font.setPointSize(m_attr.fontSize);
        font.setBold(m_attr.fontBold);
        font.setUnderline(m_attr.fontUnderline);
        font.setItalic(m_attr.fontItalics);
        MACRO_FONT_FANG_JUCHI_2(font)
        QFontMetrics fm(font);
        QRect rec = fm.boundingRect(strWordList.at(i));
        if(itempHeight<rec.height())
            itempHeight=rec.height();

    }
    if(m_attr.bSingleScroll)
    {
       iPicSuCaiHeight =itempHeight;
    }
    else {
        iPicSuCaiHeight=rect().height()/iBCount;
    }



    for(int i=0;i<strWordList.count();i++)
        CreateWordPic(strWordList.at(i),strFileList.at(i),strPath,iPicSuCaiHeight);
    CreatePreviewPic(strPath);

}
void eTemp::CreateWordPic(QString strWord,QString strFileName,QString strPath,int iPicHeight)
{
    //设置字体
        QFont font;
        font.setFamily(m_attr.fontFamily);
        font.setPointSize(m_attr.fontSize);
//        font.setPixelSize(m_attr.fontSize);
        font.setBold(m_attr.fontBold);
        font.setUnderline(m_attr.fontUnderline);
        font.setItalic(m_attr.fontItalics);
        MACRO_FONT_FANG_JUCHI_2(font)
        QFontMetrics fm(font);
        QRect rec = fm.boundingRect(strWord);
        int iFontHeight=iPicHeight;//rec.height();
        //        int iFontHeight=font.pointSize();
        //        int iFontHeight=m_attr.fontSize;
        //字符串所占的像素宽度,高度
        //int textWidth = rec.width();
        //int leading=fm.leading();
        int linespacing=fm.lineSpacing();
        //int textHeight = rec.height()-leading-linespacing;
        QPixmap *tempPixamp=new QPixmap(rec.width(),iFontHeight);
        tempPixamp->fill(m_attr.cBackground);
        QPainter *painter=new QPainter();
        MACRO_FAN_JUCHI(painter)
        painter->begin(tempPixamp);


        QTextOption opt;
        opt.setAlignment(Qt::AlignTop|Qt::AlignLeft);
        //opt.setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
        painter->setPen(m_attr.textColor);
        painter->setFont(font);
        QRectF aa(0,(iFontHeight-linespacing)/2,rect().width(),rec.height());
        painter->drawText(aa, strWord, opt);
        painter->end();
        delete painter;

        QString strSaveFile=strPath+MACRO_FENGEDANFU+strFileName+".png";
        strSaveFile.replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);
        tempPixamp->save(strSaveFile,"PNG");
        delete  tempPixamp;


}
void eTemp::CreatePreviewPic(QString strPath)
{
    QPixmap *tempPixamp=new QPixmap(rect().width(),rect().height());
    tempPixamp->fill(m_attr.cBackground);
    QPainter *painter=new QPainter();
    painter->begin(tempPixamp);
    QString sUnit;
    QRectF rRect;
    int iCountItem=0;
    int iYOffset=0;
    int iYHeight=0;
    QTextOption opt;
    opt.setAlignment(Qt::AlignLeft | Qt:: AlignVCenter);

    if(m_attr.bTitle)
        iCountItem++;
    if(m_attr.bTemperature)
        iCountItem++;
    if(m_attr.bHumidity)
        iCountItem++;
    if(m_attr.bNoise)
        iCountItem++;
    if(m_attr.bWindSpeed)
        iCountItem++;
    if(m_attr.bWindDirection)
        iCountItem++;
    if(m_attr.bPM10)
        iCountItem++;
    if(m_attr.bPM25)
        iCountItem++;
    rRect=INVALID_RECT;
    if(iCountItem>0)
        iYHeight=rect().height() / iCountItem;
    else {
        return;
    }
    QFont font;
    font.setFamily(m_attr.fontFamily);
    font.setPointSize(m_attr.fontSize);
    font.setBold(m_attr.fontBold);
    font.setItalic(m_attr.fontItalics);
    font.setUnderline(m_attr.fontUnderline);
    MACRO_FONT_FANG_JUCHI_2(font)
    painter->setFont(font);
    painter->setPen(m_attr.textColor);
    painter->setRenderHint(QPainter::Antialiasing, false);
    if(m_attr.bSingleScroll)
    {
        if(!m_pScrollTimer->isActive())
        {
            m_iScrollOffset=rect().width();
            m_pScrollTimer->start(m_attr.iScrollSpeed);
        }
         painter->save();
         MACRO_FAN_JUCHI(painter)
         painter->drawPixmap(rect(), *m_pRenderPixmap,rect());
         painter->restore();
    }
    else {
        if(m_pScrollTimer->isActive())
            m_pScrollTimer->stop();
        if(m_attr.iAlignType==0)
            opt.setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
        else if(m_attr.iAlignType==1)
            opt.setAlignment(Qt::AlignCenter);
        else if(m_attr.iAlignType==2)
            opt.setAlignment(Qt::AlignRight|Qt::AlignVCenter);
        painter->fillRect(rect(),m_attr.cBackground);
        if(m_attr.bTitle)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, m_attr.labelTitle, opt);

            iYOffset+=iYHeight;
        }
        if(m_attr.bTemperature)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            if(m_attr.temperatureType == 0) {
                sUnit = QStringLiteral("℃");
            } else {
                sUnit = QStringLiteral("℉");
            }
            painter->drawText(rRect, QString(m_attr.labelTemperature + "%1" + sUnit).arg(00 + m_attr.compensation), opt);
            iYOffset+=iYHeight;
        }
        if(m_attr.bHumidity)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, QString(m_attr.labelHumidity+"%1"+"RH").arg(00), opt);
             iYOffset+=iYHeight;
        }
        if(m_attr.bNoise)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, QString(m_attr.labelNoise+"%1"+"dB").arg(00), opt);
             iYOffset+=iYHeight;
        }
        if(m_attr.bWindSpeed)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, QString(m_attr.labelWindSpeed+"%1"+"m/s").arg(00), opt);
             iYOffset+=iYHeight;
        }
        if(m_attr.bWindDirection)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, QString(m_attr.labelWindDirectiton+"%1").arg(00), opt);
             iYOffset+=iYHeight;
        }
        if(m_attr.bPM10)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, QString(m_attr.labelPm10+"%1"+"μg/m³").arg(00), opt);
             iYOffset+=iYHeight;
        }
        if(m_attr.bPM25)
        {
            rRect=QRectF(0,iYOffset, rect().width(), iYHeight);
            painter->drawText(rRect, QString(m_attr.labelPm25+"%1"+"μg/m³").arg(00), opt);
             iYOffset+=iYHeight;
        }
    }


    delete painter;

    QString strSaveFile=strPath+MACRO_FENGEDANFU+"previewTmp.png";
    strSaveFile.replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);
    tempPixamp->save(strSaveFile,"PNG");
    delete  tempPixamp;

}
QJsonObject eTemp::save(const QString &pRoot)
{
    QRectF r1 = geometry();
    QRect r=r1.toRect();
    QString strDir=QString("%1%2%3%4%5").arg(zValue()).arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height());
    //保存图片元素到相应目录
    QDir dirTemp(pRoot+MACRO_FENGEFU+strDir);
    if(dirTemp.exists())
    {
        if(dirTemp.removeRecursively())
        {
            int iReTryCount=0;
            QDir dRoot(pRoot);
            while(!dRoot.mkdir(strDir))
            {
                QThread::sleep(1);
                iReTryCount++;
                if(iReTryCount>10)
                    break;
            }
        }
    }
    else {
        int iReTryCount=0;
        QDir dRoot(pRoot);
        while(!dRoot.mkdir(strDir))
        {
            QThread::sleep(1);
            iReTryCount++;
            if(iReTryCount>10)
                break;
        }
    }
    CreateAllWordPic(pRoot+MACRO_FENGEFU+strDir);
    return elementJson();
}
void eTemp::OnRectChanged()
{
    if(m_pScrollTimer->isActive())
        m_pScrollTimer->stop();
    if(m_pRenderPixmap!=nullptr)
         delete m_pRenderPixmap;
    m_pRenderPixmap=new QPixmap(rect().width(),rect().height());
    m_pRenderPixmap->fill(m_attr.cBackground);
    updateGeometry();
}
