#include "eletimer.h" #include "tools.h" #include "globaldefine.h" #include #include 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(); font = json["font"].toString(); fontSize = json["fontSize"].toInt(); fontBold = json["fontBold"].toBool(); fontItalic = json["fontItalic"].toBool(); fontUnderline = json["fontUnderline"].toBool(); 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(); QFont font(this->font, fontSize); font.setBold(fontBold); font.setItalic(fontItalic); font.setUnderline(fontUnderline); font.setStyleStrategy(gTextAntialiasing ? QFont::PreferAntialias : QFont::NoAntialias); QPainter painter(this); if(backColor.alpha() != 0) painter.fillRect(rect(), backColor); painter.setFont(font); painter.setPen(textColor); painter.drawText(rect(), text, QTextOption(Qt::AlignCenter)); }