#include "eledigiclock.h" EleDigiClock::EleDigiClock(QString prefix, const JValue &layer, QWidget *parent) : QWidget{parent} { timeZone = QTimeZone(layer["timeZone"].toString().toUtf8()); auto spaceWidth = layer["spaceWidth"].toDouble(); auto pics = layer["arrayPics"]; for(auto &pic : pics) imgs[pic["name"].toString()] = QPixmap(prefix+pic["id"].toString()); int dateStyle = layer["dateStyle"].toInt(); isSingleMonth = dateStyle==1||dateStyle==2||dateStyle==4||dateStyle==6||dateStyle==8||dateStyle==10||dateStyle==12; auto& timeSep = imgs["maohao"]; weekly = layer["weekly"].toBool(); auto hour12 = layer["hour12"].toBool(); auto AmPm = hour12 ? layer["AmPm"].toBool() : false; timeptn = hour12 ? "hhmmssa" : "HHmmss"; auto hour = layer["hour"].toBool(); auto min = layer["min"].toBool(); auto sec = layer["sec"].toBool(); multiline = layer["multiline"].toBool(); if(multiline) { auto vBox = new VBox(this); vBox->setAlignment(Qt::AlignCenter); vBox->setContentsMargins(0,0,0,0); vBox->setSpacing(0); vBox->addStretch(); auto hBox = new HBox(vBox); hBox->addStretch(); addDate(dateStyle, layer, hBox); hBox->addStretch(); if(weekly) { hBox = new HBox(vBox); hBox->addStretch(); hBox->addWidget(weekComp); hBox->addStretch(); } hBox = new HBox(vBox); hBox->addStretch(); if(AmPm) { hBox->addWidget(ampmComp); hBox->addSpacing(spaceWidth); } if(hour) { hBox->addWidget(hourComps[0]); hBox->addWidget(hourComps[1]); } if(hour&&min) hBox->addWidget(new ImgWgt(timeSep)); if(min) { hBox->addWidget(minComps[0]); hBox->addWidget(minComps[1]); } if(min&&sec) hBox->addWidget(new ImgWgt(timeSep)); if(sec) { hBox->addWidget(secComps[0]); hBox->addWidget(secComps[1]); } hBox->addStretch(); vBox->addStretch(); } else { auto hBox = new HBox(this); hBox->setContentsMargins(0,0,0,0); hBox->setSpacing(0); hBox->addStretch(); addDate(dateStyle, layer, hBox); if(hBox->count()>1) hBox->addSpacing(spaceWidth*2); if(weekly) { hBox->addWidget(weekComp); hBox->addSpacing(spaceWidth*2); } if(AmPm) { hBox->addWidget(ampmComp); hBox->addSpacing(spaceWidth); } if(hour) { hBox->addWidget(hourComps[0]); hBox->addWidget(hourComps[1]); } if(hour&&min) hBox->addWidget(new ImgWgt(timeSep)); if(min) { hBox->addWidget(minComps[0]); hBox->addWidget(minComps[1]); } if(min&&sec) hBox->addWidget(new ImgWgt(timeSep)); if(sec) { hBox->addWidget(secComps[0]); hBox->addWidget(secComps[1]); } hBox->addStretch(); } } void EleDigiClock::addDate(int dateStyle, const JValue &layer, HBox *tar) { if(dateStyle==0 || dateStyle==1) { addYear(layer, tar, imgs["YEAR"]); if(layer["month"].toBool()) { tar->addWidget(monthComps[0]); tar->addWidget(monthComps[1]); tar->addWidget(new ImgWgt(imgs["MONTH"])); } if(layer["day"].toBool()) { tar->addWidget(dayComps[0]); tar->addWidget(dayComps[1]); tar->addWidget(new ImgWgt(imgs["DAY"])); } } else if(dateStyle==2 || dateStyle==3) { auto& sep = imgs["xiegang"]; if(layer["month"].toBool()) { tar->addWidget(monthComps[0]); tar->addWidget(monthComps[1]); tar->addWidget(new ImgWgt(sep)); } if(layer["day"].toBool()) { tar->addWidget(dayComps[0]); tar->addWidget(dayComps[1]); tar->addWidget(new ImgWgt(sep)); } addYear(layer, tar, QPixmap()); } else if(dateStyle==4 || dateStyle==5) { auto& sep = imgs["xiegang"]; if(layer["day"].toBool()) { tar->addWidget(dayComps[0]); tar->addWidget(dayComps[1]); tar->addWidget(new ImgWgt(sep)); } if(layer["month"].toBool()) { tar->addWidget(monthComps[0]); tar->addWidget(monthComps[1]); tar->addWidget(new ImgWgt(sep)); } addYear(layer, tar, QPixmap()); } else if(dateStyle==6 || dateStyle==7) { auto& sep = imgs["xiegang"]; addYear(layer, tar, sep); if(layer["month"].toBool()) { tar->addWidget(monthComps[0]); tar->addWidget(monthComps[1]); tar->addWidget(new ImgWgt(sep)); } if(layer["day"].toBool()) { tar->addWidget(dayComps[0]); tar->addWidget(dayComps[1]); } } else if(dateStyle==8 || dateStyle==9) { auto& sep = imgs["hengxian"]; if(layer["month"].toBool()) { tar->addWidget(monthComps[0]); tar->addWidget(monthComps[1]); tar->addWidget(new ImgWgt(sep)); } if(layer["day"].toBool()) { tar->addWidget(dayComps[0]); tar->addWidget(dayComps[1]); tar->addWidget(new ImgWgt(sep)); } addYear(layer, tar, QPixmap()); } else if(dateStyle==10 || dateStyle==11) { auto& sep = imgs["hengxian"]; if(layer["day"].toBool()) { tar->addWidget(dayComps[0]); tar->addWidget(dayComps[1]); tar->addWidget(new ImgWgt(sep)); } if(layer["month"].toBool()) { tar->addWidget(monthComps[0]); tar->addWidget(monthComps[1]); tar->addWidget(new ImgWgt(sep)); } addYear(layer, tar, QPixmap()); } else if(dateStyle==12 || dateStyle==13) { auto& sep = imgs["hengxian"]; addYear(layer, tar, sep); if(layer["month"].toBool()) { tar->addWidget(monthComps[0]); tar->addWidget(monthComps[1]); tar->addWidget(new ImgWgt(sep)); } if(layer["day"].toBool()) { tar->addWidget(dayComps[0]); tar->addWidget(dayComps[1]); } } } void EleDigiClock::addYear(const JValue &layer, HBox *tar, const QPixmap &sep) { if(layer["year"].toBool()) { if(layer["fullYear"].toBool()) { tar->addWidget(yearComps[0]); tar->addWidget(yearComps[1]); } tar->addWidget(yearComps[2]); tar->addWidget(yearComps[3]); if(!sep.isNull()) tar->addWidget(new ImgWgt(sep)); } } void EleDigiClock::cal() { auto dt = QDateTime::currentDateTime().toTimeZone(timeZone); auto time = dt.time(); auto hms = time.toString(timeptn); *ampmComp = imgs[time.hour()<12?"AM":"PM"]; *hourComps[0] = imgs[hms.mid(0,1)]; *hourComps[1] = imgs[hms.mid(1,1)]; *minComps[0] = imgs[hms.mid(2,1)]; *minComps[1] = imgs[hms.mid(3,1)]; *secComps[0] = imgs[hms.mid(4,1)]; *secComps[1] = imgs[hms.mid(5,1)]; if(yearComps[0]->img.isNull() || (time.hour()==0 && time.second()==0)) { auto date = dt.date(); if(weekly) *weekComp = imgs[weeks[date.dayOfWeek()-1]]; auto ymd = date.toString("yyyyMMdd"); *yearComps[0] = imgs[ymd.mid(0,1)]; *yearComps[1] = imgs[ymd.mid(1,1)]; *yearComps[2] = imgs[ymd.mid(2,1)]; *yearComps[3] = imgs[ymd.mid(3,1)]; *monthComps[0] = isSingleMonth && ymd.at(4)=='0' ? QPixmap() : imgs[ymd.mid(4,1)]; *monthComps[1] = imgs[ymd.mid(5,1)]; *dayComps[0] = isSingleMonth && ymd.at(6)=='0' ? QPixmap() : imgs[ymd.mid(6,1)]; *dayComps[1] = imgs[ymd.mid(7,1)]; } } void EleDigiClock::showEvent(QShowEvent *) { if(timerId==0) { timerId = startTimer(1000, Qt::PreciseTimer); cal(); } } void EleDigiClock::timerEvent(QTimerEvent *) { if(isVisible()) { cal(); update(); } else if(timerId) { killTimer(timerId); timerId = 0; } }