summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/qt-examples/progress/progress.rb
blob: 3b0b11f815556f27fa117c79873c8bea61b52b6d (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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env ruby -w

require 'Qt'

class AnimatedThingy < TQt::Label
    attr_accessor :label, :step
    attr_accessor :ox0, :oy0, :ox1, :oy1
    attr_accessor :x0,  :y0,  :x1,  :y1
    attr_accessor :dx0, :dx1, :dy0, :dy1
    NTQIX = 10

    def initialize(*k)
        super(*k)
        @label = k[1] + "\n... and wasting CPU\nwith this animation!\n"
        @step = 0
        @ox0, @oy0, @ox1, @oy1 = *Array.new(4) { Array.new(10, 0) }
        @x0 = @y0 = @x1 = @y1 = 0
        @dx0 = rand(8)+2
        @dy0 = rand(8)+2
        @dx1 = rand(8)+2
        @dy1 = rand(8)+2
    end

    def show
        startTimer(100) unless isVisible
        super
    end

    def hide
	super
	killTimers()
    end

    def sizeHint
	TQt::Size.new(120,100)
    end

    def inc(x, dx, b)
	x += dx
	if x < 0
            x = 0
            dx = rand(8) + 2
        elsif x >= b
            x = b-1
            dx = -(rand(8)+2) 
        end
	yield x, dx
    end

    def timerEvent(e)
	p = TQt::Painter.new(self)
	pn = p.pen
	pn.setWidth(2)
	pn.setColor(backgroundColor)
	p.setPen(pn)

	@step = (@step + 1) % NTQIX

	p.drawLine(@ox0[@step], @oy0[@step], @ox1[@step], @oy1[@step])

	inc(@x0, @dx0, width)  { |x,dx| @x0, @dx0 = x, dx }
	inc(@y0, @dy0, height) { |y,dy| @y0, @dy0 = y, dy }
	inc(@x1, @dx1, width)  { |x,dx| @x1, @dx1 = x, dx }
	inc(@y1, @dy1, height) { |y,dy| @y1, @dy1 = y, dy }
	@ox0[@step] = @x0
	@oy0[@step] = @y0
	@ox1[@step] = @x1
	@oy1[@step] = @y1

	c = TQt::Color.new
	c.setHsv( (@step*255)/NTQIX, 255, 255 ) # rainbow effect
	pn.setColor(c)
	pn.setWidth(2)
	p.setPen(pn)
	p.drawLine(@ox0[@step], @oy0[@step], @ox1[@step], @oy1[@step])
	p.setPen(colorGroup().text())
	p.drawText(rect(), AlignCenter, @label)
	p.end()
    end

	def paintEvent(event)
		p = TQt::Painter.new(self)
		pn = p.pen()
		pn.setWidth(2)
		p.setPen(pn)
		p.setClipRect(event.rect())
		0.upto(NTQIX-1) do |i|
			c = TQt::Color.new()
			c.setHsv( (i*255)/NTQIX, 255, 255 ) # rainbow effect
			pn.setColor(c)
			p.setPen(pn)
			p.drawLine(@ox0[i], @oy0[i], @ox1[i], @oy1[i])
		end		
		p.setPen(colorGroup().text())
		p.drawText(rect(), AlignCenter, @label)
		p.end
	end
end

class CPUWaster < TQt::Widget
    attr_accessor :menubar, :file, :options, :rects, :pb
    attr_accessor :td_id , :ld_id, :dl_id, :cl_id, :md_id
    attr_accessor :got_stop, :timer_driven, :default_label
    slots 'drawItemRects(int)', 'doMenuItem(int)', 'stopDrawing()', 'timerDriven()'
    slots 'loopDriven()', 'defaultLabel()', 'customLabel()', 'toggleMinimumDuration()'

    FIRST_DRAW_ITEM = 1000
    LAST_DRAW_ITEM  = 1006

    def initialize(*k)
        super(*k)

        @menubar = TQt::MenuBar.new(self, "menu")
        @pb = nil

        @file = TQt::PopupMenu.new
        @menubar.insertItem( "&File", file )
	FIRST_DRAW_ITEM.upto(LAST_DRAW_ITEM) { 
	    |i| file.insertItem(  "#{drawItemRects(i)} Rectangles", i) 
	}
        connect( menubar, TQ_SIGNAL('activated(int)'), self, TQ_SLOT('doMenuItem(int)') )
        @file.insertSeparator
        @file.insertItem("Quit", $qApp, TQ_SLOT('quit()'))
        @options = TQt::PopupMenu.new
        @menubar.insertItem("&Options", options)
        @td_id = options.insertItem("Timer driven",  self, TQ_SLOT('timerDriven()'))
        @ld_id = options.insertItem("Loop driven",   self, TQ_SLOT('loopDriven()'))
        @options.insertSeparator
        @dl_id = options.insertItem("Default label", self, TQ_SLOT('defaultLabel()'))
        @cl_id = options.insertItem("Custom label",  self, TQ_SLOT('customLabel()'))
        @options.insertSeparator
        @md_id = options.insertItem("No minimum duration", self, TQ_SLOT('toggleMinimumDuration()'))
        @options.setCheckable true

	loopDriven
	defaultLabel

        setFixedSize(400, 300)
        setBackgroundColor(black)
    end

    def drawItemRects(id)
        n = id - FIRST_DRAW_ITEM - 1
        r = 100
	n.downto(0) { |n|
	    r *= (n%3 != 0) ? 5 : 4
	}
        r
    end

    def doMenuItem(id)
        draw drawItemRects(id) if id >= FIRST_DRAW_ITEM && id <= LAST_DRAW_ITEM
    end

    def stopDrawing
        @got_stop = true
    end

    def timerDriven
        @timer_driven = true
        @options.setItemChecked(@td_id, true)
        @options.setItemChecked(@ld_id, false)
    end

    def loopDriven
        @timer_driven = false
        @options.setItemChecked(@td_id, false)
        @options.setItemChecked(@ld_id, true)
    end

    def defaultLabel
        @default_label = true
        @options.setItemChecked(@dl_id, true)
        @options.setItemChecked(@cl_id, false)
    end

    def customLabel
        @default_label = false
        @options.setItemChecked(@dl_id, false)
        @options.setItemChecked(@cl_id, true)
    end

    def toggleMinimumDuration
        checked = @options.isItemChecked(@md_id)
        @options.setItemChecked(@md_id, !checked)
    end

    def timerEvent(e)
        @pb.setProgress( @pb.totalSteps - @rects ) if @rects % 100 == 0
        @rects -= 1

	painter = TQt::Painter.new(self)

	ww = width
	wh = height

	if ww > 8 and wh > 8
	    c = TQt::Color.new(rand(255), rand(255), rand(255))
	    x = rand(ww - 8)
	    y = rand(wh - 8)
	    w = rand(ww - x)
	    h = rand(wh - y)
	    painter.fillRect(x, y, w, h, TQt::Brush.new(c))
	end
	
	painter.end()

        if @rects == 0 || @got_stop
            @pb.setProgress(@pb.totalSteps)
            painter = TQt::Painter.new(self)
            painter.fillRect(0, 0, width(), height(), TQt::Brush.new(backgroundColor))
			painter.end()
            enableDrawingItems(true)
            killTimers()
            @pb = nil
        end
    end

    def newProgressDialog(label, steps, modal)
        d = TQt::ProgressDialog.new(label, "Cancel", steps, self, "progress", modal)
        d.setMinimumDuration(0) if @options.isItemChecked(@md_id)
        d.setLabel( AnimatedThingy.new(d, label) ) unless @default_label
	d.show
        d
    end

    def enableDrawingItems(yes)
        FIRST_DRAW_ITEM.upto(LAST_DRAW_ITEM) {
            |i| menubar.setItemEnabled(i, yes)
        }
    end

    def draw(n)
        if timer_driven
            unless @pb.nil?
                warn("This cannot happen!")
                return
            end
            @rects = n
            @pb = newProgressDialog("Drawing rectangles.\nUsing timer event.", n, false)
            @pb.setCaption("Please Wait")
            connect(@pb, TQ_SIGNAL('cancelled()'), self, TQ_SLOT('stopDrawing()'))
            enableDrawingItems(false)
            startTimer(0)
            @got_stop = false
        else
            lpb = newProgressDialog("Drawing rectangles.\nUsing loop.", n, true)
            lpb.setCaption("Please Wait")

            painter = TQt::Painter.new(self)
            0.upto(n) { |i|
		if (i % 100) == 0
		    lpb.setProgress(i)
                    break if lpb.wasCancelled
                end
                cw, ch = width, height
                c = TQt::Color.new(rand(255), rand(255), rand(255))
                x = rand(cw - 8)
                y = rand(cw - 8)
                w = rand(cw - x)
                h = rand(cw - y)
                painter.fillRect(x, y, w, h, TQt::Brush.new(c))
            }
            lpb.cancel
            painter.fillRect(0, 0, width, height, TQt::Brush.new(backgroundColor))
			painter.end()
        end
    end
end

a = TQt::Application.new(ARGV)
w = CPUWaster.new
w.show
a.setMainWidget(w)
a.exec