-
Notifications
You must be signed in to change notification settings - Fork 1
/
example5.cpp
98 lines (78 loc) · 2.31 KB
/
example5.cpp
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
// https://www.djmaster.com/freepascal/demos/clock.php
#include <QtMath>
#include <QApplication>
#include <QTime>
#include <QTimer>
#include "cairo/cairo.h"
#include "CairoWidget.hpp"
class ClockWidget final: public CairoWidget
{
public:
explicit ClockWidget()
{
init([](auto const cr, int const w, int const h) noexcept
{
cairo_set_antialias(cr, CAIRO_ANTIALIAS_BEST);
if (w >= h)
{
cairo_translate(cr, .5 * (w - h), 0.);
cairo_scale(cr, h, h);
}
else
{
cairo_translate(cr, 0., .5 * (h - w));
cairo_scale(cr, w, w);
}
cairo_translate(cr, .5, .5);
}
);
draw([&](auto const cr, int, int)
{
cairo_set_source_rgb(cr, 1., 1., 1.);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
// Set line properties
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_width(cr, .1);
// Draw a black clock outline
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_arc(cr, 0, 0, .4, 0, 2 * M_PI);
cairo_stroke(cr);
qreal h, m, s;
{
auto const ct(QTime::currentTime());
h = (ct.hour() + ct.minute() / 60) * M_PI / 6;
m = ct.minute() * M_PI / 30;
s = ct.second() * M_PI / 30;
}
// Draw a white dot on the current second
cairo_set_source_rgba(cr, 1, 1, 1, .6);
cairo_arc(cr, qSin(s) * .4, -qCos(s) * .4, .05, 0, 2 * M_PI);
cairo_fill(cr);
// Draw the minutes indicator
cairo_set_source_rgba(cr, .2, .2, 1., .6);
cairo_move_to(cr, 0, 0);
cairo_line_to(cr, qSin(m) * .4, -qCos(m) * .4);
cairo_stroke(cr);
// Draw the hours indicator
cairo_move_to(cr, 0, 0);
cairo_line_to(cr, qSin(h) * .2, -qCos(h) * .2);
cairo_stroke(cr);
}
);
//
auto t(new QTimer(this));
connect(t, &QTimer::timeout, this, QOverload<>::of(&ClockWidget::update));
t->start(1000);
}
};
//////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
ClockWidget w;
w.resize(w.height(), w.height());
w.show();
return app.exec();
}