summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/qt-examples/tooltip/tooltip.rb
blob: 181816da9d401385584ddd0a06a2d0d43be91c8a (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
85
86
87
88
89
90
91
92
93
94
95
require 'Qt'

class DynamicTip < Qt::ToolTip
	def initialize(p)
		super(p)
	end

	def maybeTip(p)
		if !parentWidget.inherits('TellMe')
			return
		end

		r = parentWidget.tip(p)
		if !r.isValid
			return
		end

		s = 'position: ' + r.center.x.to_s + ', ' + r.center.y.to_s
		tip(r,s)
	end
end

class TellMe < Qt::Widget

	def initialize
		super

		setMinimumSize(30, 30)

		@r1 = randomRect
		@r2 = randomRect
		@r3 = randomRect

		@t = DynamicTip.new(self)

		Qt::ToolTip.add(self, @r3, 'this color is called red') #TT says this is helpful, I'm not so sure
	end

	def tip(point)
		if (@r1.contains(point))
			@r1
		elsif (@r2.contains(point))
			@r2
		else
			Qt::Rect.new(0,0, -1, -1)
		end
	end

	def paintEvent(e)
		p = Qt::Painter.new(self)

		if (e.rect.intersects(@r1))
			p.setBrush(Qt::blue)
			p.drawRect(@r1)
		end

		if (e.rect.intersects(@r2))
			p.setBrush(Qt::blue)
			p.drawRect(@r2)
		end

		if (e.rect.intersects(@r3))
			p.setBrush(Qt::red)
			p.drawRect(@r3)
		end
		
		p.end
	end

	def mousePressEvent (e)
		if (@r1.contains(e.pos))
			@r1 = randomRect
		end

		if (@r2.contains(e.pos))
			@r2 = randomRect
		end
		
		repaint
	end

	def resizeEvent(e)
		unless rect.contains(@r1)
			@r1 = randomRect
		end

		unless rect.contains(@r2)
			@r2 = randomRect
		end
	end

	def randomRect
		Qt::Rect.new(rand(width - 20), rand(height - 20), 20, 20)
	end
end