qt/LedOK/wProgramManager/wEditProgram/wElement/etimer.cpp
2022-01-04 18:11:48 +08:00

178 lines
6.1 KiB
C++

#include "etimer.h"
#include "etimerattr.h"
#include "QSettings"
eTimer::eTimer(QRectF rectInit,InteractiveType type, QGraphicsItem *parent) :
eObject(rectInit, type, parent)
{
m_iType=eObject::Timer;
m_attr.xWard = 0;
m_attr.targetDateTime = QDateTime(QDate::currentDate(), QTime::currentTime());
m_attr.day = true;
m_attr.hour = true;
m_attr.min = true;
m_attr.sec = true;
m_attr.text = "";
m_attr.multiline = true;
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.backgroundColor = Qt::transparent;
m_attr.playDuration = 10;
init();
}
eTimer::eTimer(const QJsonObject &json, InteractiveType type, QGraphicsItem *parent) :
eObject(json["geometry"].toObject(), type, parent)
{
m_iType=eObject::Timer;
setElement(json, m_attr);
init();
}
void eTimer::init()
{
connect(LoAppTools::getInstance(), SIGNAL(sTick()), this, SLOT(updateGeometry()));
}
void eTimer::setElement(const QJsonObject &json, Data &attr)
{
auto at = LoAppTools::getInstance();
attr.xWard = json["widget"]["xWard"] .toInt();
attr.targetDateTime = QDateTime::fromString(json["widget"]["targetDateTime"] .toString(), "yyyy-MM-dd HH:mm:ss");
attr.day = json["widget"]["day"] .toBool();
attr.hour = json["widget"]["hour"] .toBool();
attr.min = json["widget"]["min"] .toBool();
attr.sec = json["widget"]["sec"] .toBool();
attr.text = json["widget"]["text"] .toString();
attr.multiline = json["widget"]["multiline"] .toBool();
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 = at->int2Color( json["widget"]["textColor"] .toInt());
attr.backgroundColor = at->int2Color( json["widget"]["backgroundColor"].toInt());
attr.playDuration = json["play"]["duration"].toInt();
}
void eTimer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
qint64 ofs;
QDateTime cDateTime = QDateTime::currentDateTime();
QDateTime tDateTime = m_attr.targetDateTime;
if(m_attr.xWard == 0) {
ofs = tDateTime.secsTo(cDateTime);
} else {
ofs = cDateTime.secsTo(tDateTime);
}
if(ofs < 0) ofs = 0;
QString text = "";
if(!m_attr.text.isEmpty()){
text = m_attr.text;
if(m_attr.multiline) {
text += '\n';
} else {
text += "\x20\x20";
}
}
if(m_attr.day) {
text += QString("\x20%1\x20" + tr("d") + "\x20").arg(ofs/86400);
ofs %= 86400;
}
if(m_attr.hour) {
text += QString("\x20%1\x20" + tr("h") + "\x20").arg(ofs/3600);
ofs %= 3600;
}
if(m_attr.min) {
text += QString("\x20%1\x20" + tr("m") + "\x20").arg(ofs/60);
ofs %= 60;
}
if(m_attr.sec) {
text += QString("\x20%1\x20" + tr("s") + "\x20").arg(ofs);
}
QFont font;
QTextOption opt;
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)
opt.setAlignment(Qt::AlignCenter);
QBrush bshBackground = LoAppTools::getInstance()->getBrush(m_attr.backgroundColor);
if(bshBackground != Qt::NoBrush) {
painter->fillRect(rect(), bshBackground);
}
painter->save();
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setPen(m_attr.textColor);
painter->setFont(font);
painter->drawText(rect(), text, opt);
painter->restore();
eObject::paint(painter, option, widget);
}
QWidget* eTimer::wAttr()
{
QWidget *wObj = eObject::wAttr();
QWidget *w = wAttrElement();
static_cast<QBoxLayout*>(w->layout())->insertWidget(0, wObj);
return w;
}
QWidget* eTimer::wAttrElement()
{
eTimerAttr *w = new eTimerAttr(m_attr);
connect(w, SIGNAL(sAttrChanged(const eTimer::Data &)), this, SLOT(onAttrChanged(const eTimer::Data &)));
return w;
}
QJsonObject eTimer::elementJson() const
{
QJsonObject oRoot;
QJsonObject oWidget;
QJsonObject oPlay;
auto at = LoAppTools::getInstance();
oRoot["elementType"] = "Timer";
oRoot["elementTypeId"] = type();
oRoot["geometry"] = eObject::elementJson();
oWidget["xWard"] = m_attr.xWard;
oWidget["targetDateTime"] = m_attr.targetDateTime.toString("yyyy-MM-dd HH:mm:ss");
oWidget["day"] = m_attr.day;
oWidget["hour"] = m_attr.hour;
oWidget["min"] = m_attr.min;
oWidget["sec"] = m_attr.sec;
oWidget["text"] = m_attr.text;
oWidget["multiline"] = m_attr.multiline;
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["backgroundColor"] = at->color2Int(m_attr.backgroundColor);
oRoot["widget"] = oWidget;
oPlay["duration"] = m_attr.playDuration;
oRoot["play"] = oPlay;
return oRoot;
}
void eTimer::onAttrChanged(const eTimer::Data &data)
{
m_attr = data;
updateGeometry();
}