2023-04-18 14:14:46 +08:00
|
|
|
#include "eletimer.h"
|
|
|
|
#include "tools.h"
|
|
|
|
#include "globaldefine.h"
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
|
|
|
EleTimer::EleTimer(const QJsonObject &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();
|
|
|
|
text = json["text"].toString();
|
|
|
|
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;
|
|
|
|
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 *){
|
|
|
|
QString text;
|
|
|
|
if(! this->text.isEmpty()) {
|
|
|
|
text += this->text;
|
|
|
|
if(isMultiline) text += '\n';
|
|
|
|
else text += " ";
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|