qt/LedOK/player/eletimer.cpp

57 lines
2.2 KiB
C++
Raw Permalink Normal View History

2023-04-18 14:14:46 +08:00
#include "eletimer.h"
2024-08-07 18:18:37 +08:00
#include "main.h"
2023-04-18 14:14:46 +08:00
#include <QPainter>
2023-10-23 14:58:29 +08:00
EleTimer::EleTimer(const JValue &json, QWidget *parent) : QWidget{parent} {
2023-04-18 14:14:46 +08:00
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();
2023-10-23 14:58:29 +08:00
title = json["text"].toString();
2023-04-18 14:14:46 +08:00
isMultiline = json["isMultiline"].toBool();
2023-04-27 15:06:24 +08:00
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);
2023-04-18 14:14:46 +08:00
textColor = json["textColor"].toString();
auto color = json["backColor"].toString();
backColor = color.isEmpty() ? QColor(0,0,0,0) : color;
2024-08-07 18:18:37 +08:00
connect(gTick, &Tick::secChanged, this, [this](const QDateTime &cur) {
auto sss = isDown ? cur.secsTo(targetTime) : targetTime.secsTo(cur);
if(sss < 0) sss = 0;
if(secs==sss) return;
secs = sss;
2023-04-18 14:14:46 +08:00
update();
});
}
void EleTimer::paintEvent(QPaintEvent *){
2023-10-23 14:58:29 +08:00
auto text = this->title;
if(! text.isEmpty()) text += isMultiline ? '\n' : ' ';
2023-04-18 14:14:46 +08:00
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));
}