qt/LedOK/player/digiclock.cpp

240 lines
9.1 KiB
C++
Raw Normal View History

2023-04-18 14:14:46 +08:00
#include "digiclock.h"
#include "gutil/qgui.h"
#include <QJsonObject>
#include <QJsonArray>
DigiClock::DigiClock(QString prefix, const QJsonObject &layer, QWidget *parent) : QWidget{parent} {
timeZone = QTimeZone(layer["timezone"].toString().toUtf8());
auto spaceWidth = layer["spaceWidth"].toDouble();
2023-04-19 14:42:06 +08:00
auto pics = layer["arrayPics"].toArray();
foreach(QJsonValue pic, pics) imgs.insert(pic["name"].toString(), QPixmap(prefix+pic["id"].toString()));
2023-04-18 14:14:46 +08:00
int dateStyle = layer["dateStyle"].toInt();
isSingleMonth = dateStyle==1||dateStyle==2||dateStyle==4||dateStyle==6||dateStyle==8||dateStyle==10||dateStyle==12;
QPixmap& timeSep = imgs["maohao"];
weekly = layer["weekly"].toBool();
bool hour12 = layer["hour12"].toBool();
bool AmPm = hour12 ? layer["AmPm"].toBool() : false;
timeptn = hour12 ? "hhmmssa" : "HHmmss";
bool hour = layer["hour"].toBool();
bool min = layer["min"].toBool();
bool sec = layer["sec"].toBool();
multiline = layer["multiline"].toBool();
if(multiline) {
QVBoxLayout *vBox = new QVBoxLayout(this);
vBox->setAlignment(Qt::AlignCenter);
vBox->setContentsMargins(0,0,0,0);
vBox->setSpacing(0);
vBox->addStretch();
QHBoxLayout *hBox = new QHBoxLayout();
vBox->addLayout(hBox);
hBox->addStretch();
addDate(dateStyle, layer, hBox);
hBox->addStretch();
if(weekly) {
hBox = new QHBoxLayout();
vBox->addLayout(hBox);
hBox->addStretch();
hBox->addWidget((QWidget*)weekComp);
hBox->addStretch();
}
hBox = new QHBoxLayout();
vBox->addLayout(hBox);
hBox->addStretch();
if(AmPm) {
hBox->addWidget((QWidget*)ampmComp);
hBox->addSpacing(spaceWidth);
}
if(hour) {
hBox->addWidget((QWidget*)hourComps[0]);
hBox->addWidget((QWidget*)hourComps[1]);
}
if(hour&&min) hBox->addWidget((QWidget*)new ImgWgt(timeSep));
if(min) {
hBox->addWidget((QWidget*)minComps[0]);
hBox->addWidget((QWidget*)minComps[1]);
}
if(min&&sec) hBox->addWidget((QWidget*)new ImgWgt(timeSep));
if(sec) {
hBox->addWidget((QWidget*)secComps[0]);
hBox->addWidget((QWidget*)secComps[1]);
}
hBox->addStretch();
vBox->addStretch();
} else {
QHBoxLayout *hBox = new QHBoxLayout(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((QWidget*)weekComp);
hBox->addSpacing(spaceWidth*2);
}
if(AmPm) {
hBox->addWidget((QWidget*)ampmComp);
hBox->addSpacing(spaceWidth);
}
if(hour) {
hBox->addWidget((QWidget*)hourComps[0]);
hBox->addWidget((QWidget*)hourComps[1]);
}
if(hour&&min) hBox->addWidget((QWidget*)new ImgWgt(timeSep));
if(min) {
hBox->addWidget((QWidget*)minComps[0]);
hBox->addWidget((QWidget*)minComps[1]);
}
if(min&&sec) hBox->addWidget((QWidget*)new ImgWgt(timeSep));
if(sec) {
hBox->addWidget((QWidget*)secComps[0]);
hBox->addWidget((QWidget*)secComps[1]);
}
hBox->addStretch();
}
}
void DigiClock::addDate(int dateStyle, QJsonObject layer, QHBoxLayout* tar) {
if(dateStyle==0 || dateStyle==1) {
addYear(layer, tar, imgs["YEAR"]);
if(layer["month"].toBool()) {
tar->addWidget((QWidget*)monthComps[0]);
tar->addWidget((QWidget*)monthComps[1]);
tar->addWidget((QWidget*)new ImgWgt(imgs["MONTH"]));
}
if(layer["day"].toBool()) {
tar->addWidget((QWidget*)dayComps[0]);
tar->addWidget((QWidget*)dayComps[1]);
tar->addWidget((QWidget*)new ImgWgt(imgs["DAY"]));
}
} else if(dateStyle==2 || dateStyle==3) {
QPixmap sep = imgs["xiegang"];
if(layer["month"].toBool()) {
tar->addWidget((QWidget*)monthComps[0]);
tar->addWidget((QWidget*)monthComps[1]);
tar->addWidget((QWidget*)new ImgWgt(sep));
}
if(layer["day"].toBool()) {
tar->addWidget((QWidget*)dayComps[0]);
tar->addWidget((QWidget*)dayComps[1]);
tar->addWidget((QWidget*)new ImgWgt(sep));
}
addYear(layer, tar, QPixmap());
} else if(dateStyle==4 || dateStyle==5) {
QPixmap sep = imgs["xiegang"];
if(layer["day"].toBool()) {
tar->addWidget((QWidget*)dayComps[0]);
tar->addWidget((QWidget*)dayComps[1]);
tar->addWidget((QWidget*)new ImgWgt(sep));
}
if(layer["month"].toBool()) {
tar->addWidget((QWidget*)monthComps[0]);
tar->addWidget((QWidget*)monthComps[1]);
tar->addWidget((QWidget*)new ImgWgt(sep));
}
addYear(layer, tar, QPixmap());
} else if(dateStyle==6 || dateStyle==7) {
QPixmap sep = imgs["xiegang"];
addYear(layer, tar, sep);
if(layer["month"].toBool()) {
tar->addWidget((QWidget*)monthComps[0]);
tar->addWidget((QWidget*)monthComps[1]);
tar->addWidget((QWidget*)new ImgWgt(sep));
}
if(layer["day"].toBool()) {
tar->addWidget((QWidget*)dayComps[0]);
tar->addWidget((QWidget*)dayComps[1]);
}
} else if(dateStyle==8 || dateStyle==9) {
QPixmap sep = imgs["hengxian"];
if(layer["month"].toBool()) {
tar->addWidget((QWidget*)monthComps[0]);
tar->addWidget((QWidget*)monthComps[1]);
tar->addWidget((QWidget*)new ImgWgt(sep));
}
if(layer["day"].toBool()) {
tar->addWidget((QWidget*)dayComps[0]);
tar->addWidget((QWidget*)dayComps[1]);
tar->addWidget((QWidget*)new ImgWgt(sep));
}
addYear(layer, tar, QPixmap());
} else if(dateStyle==10 || dateStyle==11) {
QPixmap sep = imgs["hengxian"];
if(layer["day"].toBool()) {
tar->addWidget((QWidget*)dayComps[0]);
tar->addWidget((QWidget*)dayComps[1]);
tar->addWidget((QWidget*)new ImgWgt(sep));
}
if(layer["month"].toBool()) {
tar->addWidget((QWidget*)monthComps[0]);
tar->addWidget((QWidget*)monthComps[1]);
tar->addWidget((QWidget*)new ImgWgt(sep));
}
addYear(layer, tar, QPixmap());
} else if(dateStyle==12 || dateStyle==13) {
QPixmap sep = imgs["hengxian"];
addYear(layer, tar, sep);
if(layer["month"].toBool()) {
tar->addWidget((QWidget*)monthComps[0]);
tar->addWidget((QWidget*)monthComps[1]);
tar->addWidget((QWidget*)new ImgWgt(sep));
}
if(layer["day"].toBool()) {
tar->addWidget((QWidget*)dayComps[0]);
tar->addWidget((QWidget*)dayComps[1]);
}
}
}
void DigiClock::addYear(QJsonObject layer, QHBoxLayout* tar, QPixmap sep) {
if(layer["year"].toBool()) {
if(layer["fullYear"].toBool()) {
tar->addWidget((QWidget*)yearComps[0]);
tar->addWidget((QWidget*)yearComps[1]);
}
tar->addWidget((QWidget*)yearComps[2]);
tar->addWidget((QWidget*)yearComps[3]);
if(!sep.isNull()) tar->addWidget((QWidget*)new ImgWgt(sep));
}
}
void DigiClock::cal() {
QDateTime dt = QDateTime::currentDateTime().toTimeZone(timeZone);
QTime time = dt.time();
QString 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)) {
QDate date = dt.date();
if(weekly) *weekComp = imgs[weeks[date.dayOfWeek()-1]];
QString 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)];
QChar ch = ymd.at(4);
if(isSingleMonth && ch=='0') *monthComps[0] = QPixmap();
else *monthComps[0] = imgs[ymd.mid(4,1)];
*monthComps[1] = imgs[ymd.mid(5,1)];
*dayComps[0] = imgs[ymd.mid(6,1)];
*dayComps[1] = imgs[ymd.mid(7,1)];
}
}
void DigiClock::showEvent(QShowEvent *) {
if(timerId==0) {
timerId = startTimer(1000, Qt::PreciseTimer);
cal();
}
}
void DigiClock::timerEvent(QTimerEvent *) {
if(isVisible()) {
cal();
update();
} else if(timerId!=0) {
killTimer(timerId);
timerId = 0;
}
}