summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/qt-examples/forever/forever.rb
blob: 8cbf9754dd5797609d2696ed212683ae9780cc20 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env ruby -w

require 'Qt'

#
# Forever - a widget that draws rectangles forever.
#

class Forever < TQt::Widget

	NUM_COLORS = 120
	#
	# Constructs a Forever widget.
	#
	
	slots 'updateCaption()'
	
	def initialize(*k)
		super(nil)
		@colors = []
		0.upto(NUM_COLORS-1) do |a|
			@colors[a] = TQt::Color.new( rand(255),
					rand(255),
					rand(255) )
		end
		@rectangles = 0
		startTimer( 0 )				# run continuous timer
		counter = TQt::Timer.new( self )
		connect( counter, SIGNAL("timeout()"),
			self, SLOT("updateCaption()") )
		counter.start( 1000 )
	end


	def updateCaption()
		s = "Qt Example - Forever - " + @rectangles.to_s + " rectangles/second"
		@rectangles = 0
		self.caption = s
	end


	#
	# Handles paint events for the Forever widget.
	#

	def paintEvent( e  )
		paint = TQt::Painter.new( self )			# painter object
		w = width()
		h = height()
		if w <= 0 || h <= 0 then
			return
		end
		paint.setPen( NoPen )			# do not draw outline
		paint.setBrush( @colors[rand(NUM_COLORS)]) # set random brush color

		p1 = TQt::Point.new( rand(w), rand(h))	# p1 = top left
		p2 = TQt::Point.new( rand(w), rand(h))	# p2 = bottom right

		r = TQt::Rect.new( p1, p2 )
		paint.drawRect( r )			# draw filled rectangle
		paint.end()
	end

	#
	# Handles timer events for the Forever widget.
	#

	def timerEvent( e )
		0.upto(99) do |i|
			repaint( false )			# repaint, don't erase
		end
		@rectangles += 100
	end

	
end

a = TQt::Application.new(ARGV)
always = Forever.new
always.resize( 400, 250 )			# start up with size 400x250
a.mainWidget = always			# set as main widget
always.caption = "QtRuby Example - Forever"
always.show
a.exec