-
Notifications
You must be signed in to change notification settings - Fork 1
/
example4.cpp
147 lines (120 loc) · 3.92 KB
/
example4.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// https://developer-old.gnome.org/gtkmm-tutorial/stable/sec-drawing-clock-example.html.en
#include <QtMath>
#include <QApplication>
#include <QTime>
#include <QTimer>
#include "cairo/cairo.h"
#include "CairoWidget.hpp"
class ClockWidget final: public CairoWidget
{
qreal const m_radius{.42}, m_line_width{.05};
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_rgba(cr, .337, .612, .117, .9); // green
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_set_source_rgb(cr, .0, .0, .0);
// scale to unit square and translate (0, 0) to be (.5, .5), i.e.
// the center of the window
cairo_set_line_width(cr, m_line_width);
cairo_arc(cr, 0, 0, m_radius, 0, 2 * M_PI);
cairo_save(cr);
cairo_set_source_rgba(cr, 1., 1., 1., .8);
cairo_fill_preserve(cr);
cairo_restore(cr);
cairo_stroke_preserve(cr);
cairo_clip(cr);
//clock ticks
for (int i = 0; i != 12; ++i)
{
qreal inset = .05;
cairo_save(cr);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
if (i % 3)
{
inset *= .8;
cairo_set_line_width(cr, .03);
}
cairo_move_to(
cr,
(m_radius - inset) * qCos (i * M_PI / 6),
(m_radius - inset) * qSin (i * M_PI / 6));
cairo_line_to(
cr,
m_radius * qCos (i * M_PI / 6),
m_radius * qSin (i * M_PI / 6));
cairo_stroke(cr);
cairo_restore(cr); /* stack-pen-size */
}
qreal minutes, hours, seconds;
{
auto const ct(QTime::currentTime());
// compute the angles of the indicators of our clock
minutes = ct.minute() * M_PI / 30;
hours = ct.hour() * M_PI / 6;
seconds= ct.second() * M_PI / 30;
}
cairo_save(cr);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
// draw the seconds hand
cairo_save(cr);
cairo_set_line_width(cr, m_line_width / 3);
cairo_set_source_rgba(cr, .7, .7, .7, .8); // gray
cairo_move_to(cr, 0, 0);
cairo_line_to(cr, qSin(seconds) * (m_radius * .9),
-qCos(seconds) * (m_radius * .9));
cairo_stroke(cr);
cairo_restore(cr);
// draw the minutes hand
cairo_set_source_rgba(cr, .117, .337, .612, .9); // blue
cairo_move_to(cr, 0, 0);
cairo_line_to(cr, qSin(minutes + seconds / 60) * (m_radius * .8),
-qCos(minutes + seconds / 60) * (m_radius * .8));
cairo_stroke(cr);
// draw the hours hand
cairo_set_source_rgba(cr, .337, .612, .117, .9); // green
cairo_move_to(cr, 0, 0);
cairo_line_to(cr, qSin(hours + minutes / 12.) * (m_radius * .5),
-qCos(hours + minutes / 12.) * (m_radius * .5));
cairo_stroke(cr);
cairo_restore(cr);
// draw a little dot in the middle
cairo_arc(cr, 0, 0, m_line_width / 3., 0, 2 * M_PI);
cairo_fill(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();
}