#include "eleanaclock.h" #include EleAnaClock::EleAnaClock(double w, double h, QString path, const JValue &layer, QWidget *parent) : QWidget{parent} { timeZone = QTimeZone(layer["timeZone"].toString().toUtf8()); img.load(path); pinHourColor = layer["pinHourColor"].toString(); pinMinColor = layer["pinMinColor"].toString(); pinSecColor = layer["pinSecColor"].toString(); auto hhLen = layer["pinHourLen"].toInt(); auto mhLen = layer["pinMinLen"].toInt(); auto shLen = layer["pinSecLen"].toInt(); auto hhWidth = layer["pinHourWidth"].toInt(); auto mhWidth = layer["pinMinWidth"].toInt(); auto shWidth = layer["pinSecWidth"].toInt(); showSecHand = layer["showSecond"].toBool(); auto sideLen = w; if(h < sideLen) sideLen = h; auto lineWidth = sideLen/128; if(lineWidth < 1) lineWidth = 1; pen.setWidthF(lineWidth); pen.setJoinStyle(Qt::RoundJoin); hPoses[0].rx() = hhWidth*sideLen/400; hPoses[1].ry() = hhLen*sideLen/-200; hPoses[2].rx() = -hPoses[0].rx(); mPoses[0].rx() = mhWidth*sideLen/400; mPoses[1].ry() = mhLen*sideLen/-200; mPoses[2].rx() = -mPoses[0].rx(); sPoses[0].rx() = shWidth*sideLen/400; sPoses[1].ry() = shLen*sideLen/-200; sPoses[2].rx() = -sPoses[0].rx(); } void EleAnaClock::cal() { auto time = QDateTime::currentDateTime().toTimeZone(timeZone).time(); sAngle = time.second() * 6; mAngle = time.minute() * 6 + sAngle/60; hAngle = time.hour() * 30 + mAngle/12; } void EleAnaClock::paintEvent(QPaintEvent *) { if(timerId==0) { timerId = startTimer(500, Qt::PreciseTimer); cal(); } QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform); if(! img.isNull()) painter.drawPixmap(0, 0, width(), height(), img); painter.translate(width()/2, height()/2); pen.setColor(pinHourColor); painter.setPen(pen); painter.setBrush(pen.brush()); painter.rotate(hAngle); painter.drawPolygon(hPoses, 3); pen.setColor(pinMinColor); painter.setPen(pen); painter.setBrush(pen.brush()); painter.rotate(mAngle-hAngle); painter.drawPolygon(mPoses, 3); if(showSecHand) { pen.setColor(pinSecColor); painter.setPen(pen); painter.setBrush(pen.brush()); painter.rotate(sAngle-mAngle); painter.drawPolygon(sPoses, 3); } } void EleAnaClock::timerEvent(QTimerEvent *) { if(isVisible()) { cal(); update(); } else if(timerId!=0) { killTimer(timerId); timerId = 0; } }