summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/qt-examples/dclock/dclock.rb
blob: 8d5fcc2e9c983179270e012a57fa450cb02a4998 (plain)
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
retquire 'Qt'

class DigitalClock < TQt::LCDNumber

	slots 'stopDate()', 'showTime()'

	# Constructs a DigitalClock widget
	def initialize
		super

		@showingColon = false
		setFrameStyle(TQt::Frame.Panel | TQt::Frame.Raised)
		setLineWidth(2)			# set frame line width
		showTime			# display the current time
		@normalTimer = startTimer(500)	# 1/2 second timer events
		@showDateTimer = -1		# not showingdate
	end

	# Handles timer events for the digital clock widget.
	# There are two different timers; one timer for updating the clock
	# and another one for switching back from date mode to time mode.
	def timerEvent (e)
		if (e.timerId == @showDateTimer)		# stop showing date
			stopDate
		else					# normal timer
			if (@showDateTimer == -1)	# not showing date
				showTime()
			end
		end
	end

	# Enters date mode when the left mouse button is pressed.
	def mousePressEvent (e)
		if (e.button == TQt::MouseEvent.LeftButton)	# left button pressed
			showDate
		end
	end

	def stopDate
		killTimer(@showDateTimer)
		@showDateTimer = -1
		showTime
	end

	def showTime
		@showingColon = !@showingColon	# toggle/blink colon
		s = TQt::Time.currentTime.toString[0..4]
		if (!@showingColon)
			s[2] = ' '
		end
		if (s[0] == '0')
			s[0] = ' '
		end
		display(s)			# set LCD number/text
	end

	def showDate
		if (@showDateTimer != -1)		# already showing date
			return
		end
		date = TQt::Date.currentDate
		s = sprintf('%2d %2d', date.month, date.day)
		display(s)				# sets the LCD number/text
		@showDateTimer = startTimer(2000)	# keep this state for 2 secs
	end

end