#include "eleanaclock.h" #include #include #include EleAnaClock::EleAnaClock(QString path, QJsonObject 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(); } void EleAnaClock::cal() { QTime time = QDateTime::currentDateTime().toTimeZone(timeZone).time(); double rad = time.second()*M_PI/30; double rsin = sin(rad); double rcos = cos(rad); double sideLen = width(); if(height() < sideLen) sideLen = height(); sPath = QPainterPath(QPointF(sideLen*0.55*rsin, sideLen*-0.55*rcos)); double pinWidth = sideLen/36; double sx = pinWidth*rcos; double sy = pinWidth*rsin; sPath.lineTo(sx, sy); sPath.lineTo(-sx, -sy); sPath.closeSubpath(); rad = time.minute()*M_PI/30 + rad/60; rsin = sin(rad); rcos = cos(rad); mPath = QPainterPath(QPointF(sideLen*0.45*rsin, sideLen*-0.45*rcos)); pinWidth = sideLen/30; sx = pinWidth*rcos; sy = pinWidth*rsin; mPath.lineTo(sx, sy); mPath.lineTo(-sx, -sy); mPath.closeSubpath(); rad = time.hour()*M_PI/6 + rad/12; rsin = sin(rad); rcos = cos(rad); hPath = QPainterPath(QPointF(sideLen*0.3*rsin, sideLen*-0.3*rcos)); pinWidth = sideLen/18; sx = pinWidth*rcos; sy = pinWidth*rsin; hPath.lineTo(sx, sy); hPath.lineTo(-sx, -sy); hPath.closeSubpath(); } void EleAnaClock::paintEvent(QPaintEvent *) { if(timerId==0) { timerId = startTimer(1000, 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); painter.fillPath(hPath, QBrush(pinHourColor)); painter.fillPath(mPath, QBrush(pinMinColor)); painter.fillPath(sPath, QBrush(pinSecColor)); painter.translate(-width()/2, -height()/2); } void EleAnaClock::timerEvent(QTimerEvent *) { if(isVisible()) { cal(); repaint(); } else if(timerId!=0) { killTimer(timerId); timerId = 0; } }