clock.cpp
2.99 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "clock.h"
#include <QTime>
#include <QPainter>
#include <QtDebug>
#include <QDateTime>
#include <QTimeZone>
#include <QTimer>
Clock::Clock(QWidget *parent) : QWidget(parent)
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(33);
}
void Clock::paintEvent(QPaintEvent */*event*/)
{
QTime time = QDateTime::currentDateTime().time();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
int side = qMin(width(), height());
painter.scale(side / 200.0, side / 200.0);
QPen hourHand(Qt::white, 6, Qt::SolidLine, Qt::RoundCap);
QPen hourArm(Qt::white, 2);
QPen hourLine(Qt::white, 3);
QPen minHand(hourHand);
QPen minArm(hourArm);
QPen minLine(Qt::white, 1);
QPen secHand(Qt::red, 2);
QPen capPen(Qt::white, 3);
QBrush capBrush(Qt::black, Qt::SolidPattern);
QFont numberFont;
numberFont.setPixelSize(20);
QFontMetrics numberFontMetrics(numberFont);
QPen numberPen(Qt::white);
numberPen.setWidth(2);
// Lines
painter.setPen(minLine);
for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter.drawLine(92, 0, 96, 0);
painter.rotate(6.0);
}
painter.setPen(hourLine);
for (int i = 0; i < 12; ++i) {
// painter.drawLine(88, 0, 96, 0);
painter.drawLine(92, 0, 96, 0);
painter.rotate(30.0);
}
// Numbers
painter.setPen(numberPen);
painter.setFont(numberFont);
QTransform transform;
QPoint targetPoint(0, -75);
QPoint transformedTarget(targetPoint);
QString strings[] = { "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" };
for (int i = 0; i < 12; i++) {
QString string = strings[i];
QRect boundingRect = numberFontMetrics.boundingRect(string);
boundingRect.moveCenter(transformedTarget);
painter.drawText(boundingRect, Qt::AlignCenter, string);
transform.rotate(30.0);
transformedTarget = transform.map(targetPoint);
}
// Needles
qreal second = time.second() + time.msec() / 1000.0;
if (/* ticking == */ false)
second = time.second();
qreal minute = time.minute() + second / 60.0;
qreal hour = time.hour() + minute / 60.0;
// Sec
painter.save();
painter.setPen(secHand);
painter.rotate(6.0 * second);
painter.drawLine(0, 15, 0, -80);
painter.restore();
// Min
painter.save();
painter.rotate(6.0 * minute);
painter.setPen(minArm);
painter.drawLine(0, 0, 0, -15);
painter.setPen(minHand);
painter.drawLine(0, -15, 0, -75);
painter.restore();
// Hour
painter.save();
painter.rotate(30.0 * hour);
painter.setPen(hourArm);
painter.drawLine(0, 0, 0, -15);
painter.setPen(hourHand);
painter.drawLine(0, -15, 0, -50);
painter.restore();
// Cap
painter.setPen(capPen);
painter.setBrush(capBrush);
painter.drawEllipse(-5, -5, 10, 10);
}