#include <QtGui> #include <cmath> #include "timer.h" #include "ui_timer.h" #ifndef PI #define PI acos(-1) #endif const double DegressPerMinute = 6.0; const double DegressPerSecond = DegressPerMinute / 60; const int MaxMinutes = 60; const int MaxSeconds = MaxMinutes * 60; const int UpdateInterval = 5;
Timer::Timer(QWidget *parent) : QWidget(parent), ui(new Ui::Timer) { ui->setupUi(this); finishTime = QDateTime::currentDateTime(); updateTimer = new QTimer(this); connect(updateTimer,SIGNAL(timeout()),this,SLOT(update())); finishTimer = new QTimer(this); finishTimer->setSingleShot(true); connect(finishTimer,SIGNAL(timeout()),this,SIGNAL(timeout())); connect(finishTimer,SIGNAL(timeout()),updateTimer,SLOT(stop())); QFont font; font.setPointSize(8); setFont(font); }
Timer::~Timer() { delete ui; }
void Timer::setTimer(int secs) { secs = qBound(0, secs, MaxSeconds); finishTime = QDateTime::currentDateTime().addSecs(secs); if(secs > 0) { updateTimer->start(UpdateInterval * 1000); finishTimer->start(secs * 1000); } else { updateTimer->stop(); finishTimer->stop(); } update(); }
int Timer::getTimer() const { int secs = QDateTime::currentDateTime().secsTo(finishTime); if(secs < 0) secs = 0; return secs; }
void Timer::mousePressEvent(QMouseEvent *event) { QPointF point = event->pos() - rect().center(); double angle = std::atan2(-point.x(), -point.y()) * 180.0 /PI; setTimer(getTimer() + int(angle / DegressPerSecond)); update(); }
void Timer::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing,true); int side = qMin(width(),height()); painter.setViewport((width() - side) / 2,(height() - side) / 2, side, side); painter.setWindow(-50,-50,100,100); draw(&painter); }
void Timer::draw(QPainter *painter) { static const int triangle[3][2] = { {-2,-49} ,{2,-49}, {0,-47} }; QPen thickPen(palette().foreground(),1.5); QPen thinPen(palette().foreground(),0.5); painter->setPen(thinPen); painter->setBrush(palette().foreground()); painter->drawPolygon(QPolygon(3,&triangle[0][0])); QConicalGradient coneGradient(0,0,-90.0); coneGradient.setColorAt(0.0,Qt::darkGray); coneGradient.setColorAt(0.2,Qt::blue); coneGradient.setColorAt(0.5,Qt::white); coneGradient.setColorAt(1.0,Qt::darkGray); painter->setBrush(coneGradient); painter->drawEllipse(-46,-46,92,92); QRadialGradient radialGradient(0,0,20,0,0); radialGradient.setColorAt(0.0, Qt::lightGray); radialGradient.setColorAt(0.8,Qt::darkGray); radialGradient.setColorAt(0.9,Qt::white); radialGradient.setColorAt(1.0,Qt::black); painter->setPen(Qt::NoPen); painter->setBrush(radialGradient); painter->drawEllipse(-20,-20,40,40); QLinearGradient linearGradient(-7, -25 ,7 ,-25); linearGradient.setColorAt(0.0, Qt::black); linearGradient.setColorAt(0.3, Qt::lightGray); linearGradient.setColorAt(0.8, Qt::white); linearGradient.setColorAt(1.0, Qt::black); painter->rotate(getTimer() * DegressPerSecond); painter->setBrush(linearGradient); painter->setPen(thinPen); painter->drawRoundRect(-7,-25,14,50,170,150); for (int i = 0; i<=MaxMinutes-1; ++i) { if( i % 5 == 0) { painter->setPen(thickPen); painter->drawLine(0,-41,0,-44); painter->drawText(-15,-41,30,30,Qt::AlignHCenter | Qt::AlignTop,QString::number(i)); } else { painter->setPen(thinPen); painter->drawLine(0,-42,0,-44); } painter->rotate(-DegressPerMinute); } }
|