qt/LedOK/player/eletimer.cpp
2023-10-23 14:58:29 +08:00

58 lines
2.2 KiB
C++

#include "eletimer.h"
#include "tools.h"
#include "globaldefine.h"
#include <QPainter>
EleTimer::EleTimer(const JValue &json, QWidget *parent) : QWidget{parent} {
isDown = json["isDown"].toBool();
targetTime = QDateTime::fromString(json["targetTime"].toString(), "yyyy-MM-dd HH:mm:ss");
hasDay = json["hasDay"].toBool();
hasHour = json["hasHour"].toBool();
hasMin = json["hasMin"].toBool();
hasSec = json["hasSec"].toBool();
title = json["text"].toString();
isMultiline = json["isMultiline"].toBool();
font = QFont(json["font"].toString());
font.setPixelSize(json["fontSize"].toInt());
font.setBold(json["fontBold"].toBool());
font.setItalic(json["fontItalic"].toBool());
font.setUnderline(json["fontUnderline"].toBool());
font.setStyleStrategy(gTextAntialiasing ? QFont::PreferAntialias : QFont::NoAntialias);
textColor = json["textColor"].toString();
auto color = json["backColor"].toString();
backColor = color.isEmpty() ? QColor(0,0,0,0) : color;
connect(Tools::getInstance(), &Tools::sTick, this, [this]() {
QDateTime cDateTime = QDateTime::currentDateTime();
qint64 ofs = isDown ? cDateTime.secsTo(targetTime) : targetTime.secsTo(cDateTime);
if(ofs < 0) ofs = 0;
secs = ofs;
update();
});
}
void EleTimer::paintEvent(QPaintEvent *){
auto text = this->title;
if(! text.isEmpty()) text += isMultiline ? '\n' : ' ';
int secs = this->secs;
if(hasDay) {
text.append(QString::number(secs/86400)).append(" ").append(tr("day")).append(" ");
secs %= 86400;
}
if(hasHour) {
text.append(QString::asprintf("%02d ", secs/3600)).append(tr("hour")).append(" ");
secs %= 3600;
}
if(hasMin) {
text.append(QString::asprintf("%02d ", secs/60)).append(tr("min")).append(" ");
secs %= 60;
}
if(hasSec) text.append(QString::asprintf("%02d ", secs)).append(tr("sec")).append(" ");
text = text.trimmed();
QPainter painter(this);
if(backColor.alpha() != 0) painter.fillRect(rect(), backColor);
painter.setFont(font);
painter.setPen(textColor);
painter.drawText(rect(), text, QTextOption(Qt::AlignCenter));
}