-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample02.lua
More file actions
95 lines (88 loc) · 2.69 KB
/
example02.lua
File metadata and controls
95 lines (88 loc) · 2.69 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
local lqtk = require("lqtk")
lqtk.import("Q.*", "newClass")
local AnalogClock = newClass(QWidget)
do
local hourHand = {
QPoint(5, 14),
QPoint(-5, 14),
QPoint(-4, -71),
QPoint(4, -71)
}
local minuteHand = {
QPoint(4, 14),
QPoint(-4, 14),
QPoint(-3, -89),
QPoint(3, -89)
}
local secondsHand = {
QPoint(1, 14),
QPoint(-1, 14),
QPoint(-1, -89),
QPoint(1, -89)
}
function AnalogClock:new(parent)
QWidget.new(self, parent)
self:setWindowTitle("Analog Clock")
self:resize(200, 200)
self.timer = QTimer()
self.timer:setTimerType(Qt.PreciseTimer)
self.timer:connect("timeout()", self, "update")
self.timer:start()
end
function AnalogClock:update(...)
QWidget.update(self, ...)
local now = QTime.currentTime()
local next = now:addMSecs(1000 - now:msec())
self.timer:setInterval(now:msecsTo(next))
end
function AnalogClock:paintEvent(event)
local palette = self:palette()
local hourColor = palette:color(QPalette.Text)
local minuteColor = palette:color(QPalette.Text)
local secondsColor = palette:color(QPalette.Accent)
local w, h = self:width(), self:height()
local side = w < h and w or h
local painter = QPainter(self)
painter:setRenderHint(QPainter.Antialiasing)
local time = QTime.currentTime()
painter:translate(w / 2, h / 2)
painter:scale(side / 200.0, side / 200.0)
painter:setPen(Qt.NoPen)
painter:setBrush(hourColor)
do
painter:save()
painter:rotate(30.0 * ((time:hour() + time:minute() / 60.0)))
painter:drawConvexPolygon(hourHand)
painter:restore()
end
do
painter:save()
for i = 0, 12 do
painter:drawRect(73, -3, 16, 6);
painter:rotate(30.0);
end
painter:restore()
end
painter:setBrush(minuteColor)
do
painter:save()
painter:rotate(6.0 * time:minute());
painter:drawConvexPolygon(minuteHand);
painter:restore()
end
painter:setBrush(secondsColor)
do
painter:save()
painter:rotate(6.0 * time:second());
painter:drawConvexPolygon(secondsHand);
painter:drawEllipse(-3, -3, 6, 6);
painter:drawEllipse(-5, -68, 10, 10);
painter:restore()
end
painter:finish()
end
end
QApplication(arg)
local clock = AnalogClock()
clock:show()
QApplication.exec()