clock.cpp
2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "clock.h"
#include <QPainter>
#include <QTime>
#include <QTimer>
#include <QtDebug>
#include <QPaintEvent>
Clock::Clock(QWidget *parent) : QWidget(parent)
{
ticking = true;
background.load(":/images/clock/numbers.png");
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(checkUpdate()));
timer->start(33);
}
void Clock::checkUpdate()
{
if (ticking)
{
QTime cur = QTime::currentTime();
if (cur.second() != last.second())
{
last = cur;
update();
}
}
else
update();
}
void Clock::paintEvent(QPaintEvent */*event*/)
{
QTime time = QTime::currentTime();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawImage(0, 0, background);
painter.translate(width() / 2, height() / 2);
int side = qMin(width(), height());
painter.scale(side / 356.0, side / 355.0);
QPen hourHand(Qt::white, 11, Qt::SolidLine, Qt::RoundCap);
QPen hourArm(Qt::white, 4);
QPen minHand(hourHand);
QPen minArm(hourArm);
QPen secHand(Qt::red, 3);
QPen capPen(Qt::white, 4);
QBrush capBrush(Qt::black, Qt::SolidPattern);
// Needles
qreal sec = time.second() + time.msec() / 1000.0;
if (ticking)
sec = time.second();
qreal min = time.minute() + sec / 60.0;
qreal hour = time.hour() + min / 60.0;
// Sec
painter.save();
painter.rotate(6.0 * sec);
painter.setPen(secHand);
painter.drawLine(0, 28, 0, -155);
painter.restore();
// Min
painter.save();
painter.rotate(6.0 * min);
painter.setPen(minArm);
painter.drawLine(0, 0, 0, -30);
painter.setPen(minHand);
painter.drawLine(0, -27, 0, -125);
painter.restore();
// Hour
painter.save();
painter.rotate(30.0 * hour);
painter.setPen(hourArm);
painter.drawLine(0, 0, 0, -30);
painter.setPen(hourHand);
painter.drawLine(0, -27, 0, -80);
painter.restore();
// Cap
painter.setPen(capPen);
painter.setBrush(capBrush);
painter.drawEllipse(-8, -8, 16, 16);
}