62
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
50
|
12 |
|
|
13 |
View subclass:#ProgressIndicator
|
183
|
14 |
instanceVariableNames:'percentage showPercentage fgColor connectedTop connectedLabel'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Views-Misc'
|
50
|
18 |
!
|
|
19 |
|
|
20 |
!ProgressIndicator class methodsFor:'documentation'!
|
|
21 |
|
62
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
|
25 |
All Rights Reserved
|
|
26 |
|
|
27 |
This software is furnished under a license and may be used
|
|
28 |
only in accordance with the terms of that license and with the
|
|
29 |
inclusion of the above copyright notice. This software may not
|
|
30 |
be provided or otherwise made available to, or used by, any
|
|
31 |
other person. No title to or ownership of the software is
|
|
32 |
hereby transferred.
|
|
33 |
"
|
|
34 |
!
|
|
35 |
|
50
|
36 |
documentation
|
|
37 |
"
|
|
38 |
a view showing a rectangle filled according the percentage value.
|
|
39 |
Can be used as a progress indicator a la MSwindows.
|
183
|
40 |
|
|
41 |
[author:]
|
|
42 |
Claus Gittinger
|
|
43 |
|
|
44 |
[see also:]
|
|
45 |
ActionWaitBox AnimatedLabel
|
50
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
examples
|
|
50 |
"
|
62
|
51 |
basic (internal) interface
|
|
52 |
(if progress indicator is to be used in a complex box ...):
|
50
|
53 |
|
183
|
54 |
Before you get frustrated - see the convenient-interface examples
|
|
55 |
at the end ;-)
|
|
56 |
|
|
57 |
[exBegin]
|
50
|
58 |
|top p h|
|
|
59 |
|
62
|
60 |
top := ModalBox new.
|
50
|
61 |
top extent:300@100.
|
|
62 |
top label:'Progress'.
|
|
63 |
p := ProgressIndicator in:top.
|
|
64 |
p origin:(0.0@0.5) corner:(1.0@0.5).
|
|
65 |
p level:-1.
|
62
|
66 |
h := p preferredExtent y.
|
50
|
67 |
p topInset:(h // 2) negated;
|
183
|
68 |
bottomInset:(h // 2) negated;
|
|
69 |
leftInset:5;
|
|
70 |
rightInset:5.
|
50
|
71 |
|
|
72 |
[
|
183
|
73 |
1 to:100 do:[:val |
|
|
74 |
(Delay forSeconds:0.05) wait.
|
|
75 |
p percentage:val
|
|
76 |
].
|
|
77 |
top hide.
|
62
|
78 |
] fork.
|
|
79 |
top open.
|
183
|
80 |
[exEnd]
|
50
|
81 |
|
|
82 |
changing colors, turning percentage display off:
|
183
|
83 |
[exBegin]
|
50
|
84 |
|top p h|
|
|
85 |
|
|
86 |
top := StandardSystemView new.
|
|
87 |
top extent:300@100.
|
|
88 |
top label:'Progress'.
|
|
89 |
p := ProgressIndicator in:top.
|
|
90 |
p origin:(0.0@0.5) corner:(1.0@0.5).
|
|
91 |
p level:-1.
|
|
92 |
p showPercentage:false.
|
|
93 |
p foregroundColor:(Color red).
|
|
94 |
h := 10.
|
|
95 |
p topInset:(h // 2) negated;
|
183
|
96 |
bottomInset:(h // 2) negated;
|
|
97 |
leftInset:5;
|
|
98 |
rightInset:5.
|
50
|
99 |
top open.
|
|
100 |
[
|
183
|
101 |
1 to:100 do:[:val |
|
|
102 |
(Delay forSeconds:0.05) wait.
|
|
103 |
p percentage:val
|
|
104 |
]
|
50
|
105 |
] fork
|
183
|
106 |
[exEnd]
|
50
|
107 |
|
|
108 |
with border (2D look):
|
183
|
109 |
[exBegin]
|
50
|
110 |
|top p h|
|
|
111 |
|
|
112 |
top := StandardSystemView new.
|
|
113 |
top extent:300@100.
|
|
114 |
top label:'Progress'.
|
|
115 |
p := ProgressIndicator in:top.
|
|
116 |
p origin:(0.0@0.5) corner:(1.0@0.5).
|
|
117 |
p borderWidth:1.
|
62
|
118 |
h := p preferredExtent y.
|
50
|
119 |
p topInset:(h // 2) negated;
|
183
|
120 |
bottomInset:(h // 2) negated;
|
|
121 |
leftInset:5;
|
|
122 |
rightInset:5.
|
50
|
123 |
top open.
|
|
124 |
[
|
183
|
125 |
1 to:100 do:[:val |
|
|
126 |
(Delay forSeconds:0.05) wait.
|
|
127 |
p percentage:val
|
|
128 |
]
|
50
|
129 |
] fork
|
183
|
130 |
[exEnd]
|
50
|
131 |
|
|
132 |
|
|
133 |
getting progress from a model:
|
183
|
134 |
[exBegin]
|
50
|
135 |
|model top p h|
|
|
136 |
|
|
137 |
model := 0 asValue.
|
|
138 |
|
|
139 |
top := StandardSystemView new.
|
|
140 |
top extent:300@100.
|
|
141 |
top label:'Progress'.
|
|
142 |
p := ProgressIndicator in:top.
|
|
143 |
p model:model.
|
|
144 |
p origin:(0.0@0.5) corner:(1.0@0.5).
|
|
145 |
p level:-1.
|
62
|
146 |
h := p preferredExtent y.
|
50
|
147 |
p topInset:(h // 2) negated;
|
183
|
148 |
bottomInset:(h // 2) negated;
|
|
149 |
leftInset:5;
|
|
150 |
rightInset:5.
|
50
|
151 |
top open.
|
|
152 |
|
|
153 |
[
|
183
|
154 |
1 to:100 do:[:val |
|
|
155 |
(Delay forSeconds:0.05) wait.
|
|
156 |
model value:val
|
|
157 |
]
|
50
|
158 |
] fork
|
183
|
159 |
[exEnd]
|
50
|
160 |
|
62
|
161 |
|
50
|
162 |
concrete example:
|
|
163 |
search all files in the source directory for a string
|
|
164 |
using grep. Show progress while doing so.
|
183
|
165 |
[exBegin]
|
50
|
166 |
|top p h names done|
|
|
167 |
|
|
168 |
top := StandardSystemView new.
|
|
169 |
top extent:300@100.
|
|
170 |
top label:'Searching ...'.
|
|
171 |
p := ProgressIndicator in:top.
|
|
172 |
p origin:(0.0@0.5) corner:(1.0@0.5).
|
|
173 |
p level:-1.
|
62
|
174 |
h := p preferredExtent y.
|
50
|
175 |
p topInset:(h // 2) negated;
|
183
|
176 |
bottomInset:(h // 2) negated;
|
|
177 |
leftInset:5;
|
|
178 |
rightInset:5.
|
50
|
179 |
top openWithPriority:(Processor activePriority + 1).
|
|
180 |
|
|
181 |
names := 'source' asFilename directoryContents.
|
|
182 |
done := 0.
|
|
183 |
names do:[:aName |
|
183
|
184 |
|fn stream line|
|
50
|
185 |
|
183
|
186 |
p percentage:(done / names size * 100).
|
|
187 |
fn := ('source/' , aName) asFilename.
|
|
188 |
fn isDirectory ifFalse:[
|
|
189 |
stream := fn readStream.
|
|
190 |
[stream atEnd] whileFalse:[
|
|
191 |
line := stream nextLine.
|
|
192 |
(line findString:'subclass:') ~~ 0 ifTrue:[
|
|
193 |
Transcript showCr:line
|
|
194 |
].
|
|
195 |
].
|
|
196 |
stream close.
|
|
197 |
].
|
|
198 |
done := done + 1
|
50
|
199 |
].
|
|
200 |
|
|
201 |
top destroy
|
183
|
202 |
[exEnd]
|
62
|
203 |
|
|
204 |
|
|
205 |
using the convenient inBox-interface
|
|
206 |
|
|
207 |
(this creates a box and an activity label and evaluates a block
|
|
208 |
to indicate ...)
|
|
209 |
|
|
210 |
basic interface demonstration:
|
183
|
211 |
[exBegin]
|
62
|
212 |
|p|
|
|
213 |
|
|
214 |
p := ProgressIndicator
|
183
|
215 |
inBoxWithLabel:'doing something ...'
|
|
216 |
abortable:true.
|
62
|
217 |
p showProgressOf:
|
183
|
218 |
[:progressValue :currentAction |
|
62
|
219 |
|
183
|
220 |
1 to:100 do:[:val |
|
|
221 |
(Delay forSeconds:0.05) wait.
|
|
222 |
val == 25 ifTrue:[
|
|
223 |
currentAction value:'still going ...'
|
|
224 |
].
|
|
225 |
val == 50 ifTrue:[
|
|
226 |
currentAction value:'halfway through ...'
|
|
227 |
].
|
|
228 |
val == 75 ifTrue:[
|
|
229 |
currentAction value:'almost finished ...'
|
|
230 |
].
|
|
231 |
progressValue value:val
|
|
232 |
]
|
|
233 |
]
|
|
234 |
[exEnd]
|
62
|
235 |
|
|
236 |
|
|
237 |
above search example using this convenient interface:
|
183
|
238 |
[exBegin]
|
62
|
239 |
|p|
|
|
240 |
|
183
|
241 |
p := ProgressIndicator
|
|
242 |
inBoxWithLabel:'searching files ...'
|
|
243 |
abortable:false.
|
62
|
244 |
p showProgressOf:
|
183
|
245 |
[:progressValue :currentAction |
|
|
246 |
|names nDone|
|
62
|
247 |
|
183
|
248 |
names := 'source' asFilename directoryContents.
|
|
249 |
nDone := 0.
|
|
250 |
names do:[:aName |
|
|
251 |
|fn stream line|
|
|
252 |
|
|
253 |
progressValue value:(nDone / names size * 100).
|
|
254 |
currentAction value:'searching ' , 'source/' , aName , ' ...'.
|
62
|
255 |
|
183
|
256 |
fn := ('source/' , aName) asFilename.
|
|
257 |
fn isDirectory ifFalse:[
|
|
258 |
stream := fn readStream.
|
|
259 |
[stream atEnd] whileFalse:[
|
|
260 |
line := stream nextLine.
|
|
261 |
(line findString:'subclass:') ~~ 0 ifTrue:[
|
|
262 |
Transcript showCr:line
|
|
263 |
].
|
|
264 |
].
|
|
265 |
stream close.
|
|
266 |
].
|
|
267 |
nDone := nDone + 1
|
|
268 |
].
|
|
269 |
].
|
|
270 |
[exEnd]
|
62
|
271 |
|
|
272 |
|
|
273 |
a nice example: copying files a la windows ...
|
183
|
274 |
the following copies all files to /dev/null.
|
|
275 |
[exBegin]
|
62
|
276 |
|p|
|
|
277 |
|
|
278 |
(ProgressIndicator
|
183
|
279 |
inBoxWithLabel:'copy files to /dev/null ...'
|
|
280 |
abortable:true)
|
|
281 |
showProgressOf:
|
|
282 |
[:progressValue :currentAction |
|
|
283 |
|files nFiles nDone|
|
62
|
284 |
|
183
|
285 |
files := '.' asFilename directoryContents.
|
|
286 |
nFiles := files size.
|
|
287 |
nDone := 0.
|
|
288 |
files do:[:aFileName |
|
|
289 |
|percent|
|
50
|
290 |
|
183
|
291 |
nDone := nDone + 1.
|
|
292 |
percent := nDone / nFiles * 100.
|
|
293 |
progressValue value:percent.
|
|
294 |
aFileName asFilename isDirectory ifTrue:[
|
|
295 |
Transcript showCr:('skipping ' , aFileName , ' ...').
|
|
296 |
currentAction value:('skipping ' , aFileName , ' ...').
|
|
297 |
] ifFalse:[
|
|
298 |
Transcript showCr:('copying ' , aFileName , ' ...').
|
|
299 |
currentAction value:('copying ' , aFileName , ' ...').
|
|
300 |
Object errorSignal handle:[:ex |
|
|
301 |
self warn:'an error occurred while copying ' , aFileName.
|
|
302 |
ex return
|
|
303 |
] do:[
|
|
304 |
aFileName asFilename copyTo:'/dev/null'.
|
|
305 |
]
|
|
306 |
].
|
|
307 |
].
|
|
308 |
].
|
|
309 |
[exEnd]
|
|
310 |
"
|
50
|
311 |
! !
|
|
312 |
|
62
|
313 |
!ProgressIndicator class methodsFor:'instance creation'!
|
|
314 |
|
|
315 |
inBox
|
|
316 |
^ self inBoxWithLabel:'executing ...' abortable:false
|
|
317 |
!
|
|
318 |
|
|
319 |
inBoxWithLabel:aLabel
|
|
320 |
"create a topView containing an instance of myself,
|
|
321 |
for later use with #showProgressOf:"
|
|
322 |
|
|
323 |
^ self inBoxWithLabel:aLabel abortable:false
|
|
324 |
!
|
|
325 |
|
|
326 |
inBoxWithLabel:aLabel abortable:abortable
|
|
327 |
"create a topView containing an instance of myself,
|
|
328 |
for later use with #showProgressOf:"
|
|
329 |
|
|
330 |
|top p l h|
|
|
331 |
|
|
332 |
top := Dialog new.
|
|
333 |
top extent:300@100; sizeFixed:true.
|
|
334 |
top label:aLabel.
|
|
335 |
top cursor:(Cursor wait).
|
|
336 |
|
|
337 |
l := top addTextLabel:''.
|
|
338 |
l borderWidth:0.
|
74
|
339 |
l adjust:#left.
|
62
|
340 |
l cursor:(Cursor wait).
|
|
341 |
|
|
342 |
top addVerticalSpace.
|
|
343 |
|
|
344 |
p := ProgressIndicator new.
|
|
345 |
p extent:(1.0 @ p preferredExtent y).
|
|
346 |
p level:-1.
|
|
347 |
p leftInset:5;
|
|
348 |
rightInset:5.
|
|
349 |
p cursor:(Cursor wait).
|
|
350 |
p connectToTop:top label:l.
|
|
351 |
|
|
352 |
top addComponent:p.
|
|
353 |
|
|
354 |
abortable ifTrue:[
|
|
355 |
top addVerticalSpace.
|
|
356 |
top addAbortButton
|
|
357 |
].
|
|
358 |
|
|
359 |
^ p
|
|
360 |
! !
|
|
361 |
|
183
|
362 |
!ProgressIndicator methodsFor:'accessing'!
|
|
363 |
|
|
364 |
foregroundColor:aColor
|
|
365 |
fgColor := aColor
|
|
366 |
!
|
|
367 |
|
|
368 |
percentage:aNumber
|
|
369 |
|newPercentage|
|
|
370 |
|
|
371 |
newPercentage := ((aNumber max:0) min:100) rounded.
|
|
372 |
newPercentage ~~ percentage ifTrue:[
|
|
373 |
percentage := newPercentage.
|
|
374 |
shown ifTrue:[self redraw].
|
|
375 |
]
|
|
376 |
!
|
|
377 |
|
|
378 |
showPercentage:aBoolean
|
|
379 |
showPercentage := aBoolean
|
|
380 |
! !
|
|
381 |
|
|
382 |
!ProgressIndicator methodsFor:'change & update'!
|
|
383 |
|
|
384 |
update:aspect with:aParameter from:changedObject
|
|
385 |
(aspect == aspectMsg
|
|
386 |
and:[changedObject == model]) ifTrue:[
|
|
387 |
self percentage:(model perform:aspectMsg).
|
|
388 |
^ self
|
|
389 |
].
|
|
390 |
^ super update:aspect with:aParameter from:changedObject
|
|
391 |
! !
|
|
392 |
|
|
393 |
!ProgressIndicator methodsFor:'drawing'!
|
|
394 |
|
|
395 |
redraw
|
|
396 |
|s rx sx sy sw m w h|
|
|
397 |
|
|
398 |
m := margin + 1.
|
|
399 |
w := width - (m*2).
|
|
400 |
h := height - (m*2).
|
|
401 |
|
|
402 |
s := percentage printString , ' %'.
|
|
403 |
sw := font widthOf:s.
|
|
404 |
sx := (width - sw) // 2.
|
|
405 |
sy := height // 2 + font descent + 2.
|
|
406 |
|
|
407 |
rx := (w * percentage / 100) rounded.
|
|
408 |
|
|
409 |
self paint:Color white.
|
|
410 |
self fillRectangleX:m y:m width:w height:h.
|
|
411 |
|
|
412 |
showPercentage ifTrue:[
|
|
413 |
rx <= (sx+sw) ifTrue:[
|
|
414 |
self paint:Color black.
|
|
415 |
self displayString:s x:sx y:sy.
|
|
416 |
]
|
|
417 |
].
|
|
418 |
|
|
419 |
self paint:fgColor.
|
|
420 |
self fillRectangleX:m y:m width:rx height:h.
|
|
421 |
|
|
422 |
showPercentage ifTrue:[
|
|
423 |
rx >= sx ifTrue:[
|
|
424 |
self clipRect:(m@m corner:rx+1 @ h).
|
|
425 |
self paint:Color white.
|
|
426 |
self displayString:s x:sx y:sy.
|
|
427 |
self clipRect:nil
|
|
428 |
]
|
|
429 |
]
|
|
430 |
! !
|
|
431 |
|
|
432 |
!ProgressIndicator methodsFor:'initialization'!
|
|
433 |
|
|
434 |
initialize
|
|
435 |
super initialize.
|
|
436 |
viewBackground := styleSheet colorAt:'progressIndicatorViewBackground' default:Color white.
|
|
437 |
fgColor := styleSheet colorAt:'progressIndicatorForegroundColor' default:Color blue.
|
|
438 |
percentage := 0.
|
|
439 |
showPercentage := true.
|
|
440 |
! !
|
|
441 |
|
|
442 |
!ProgressIndicator methodsFor:'queries'!
|
|
443 |
|
|
444 |
preferredExtent
|
|
445 |
^ 100 @ (font height + font descent + ((margin + 1) * 2))
|
|
446 |
! !
|
|
447 |
|
62
|
448 |
!ProgressIndicator methodsFor:'showing progress'!
|
|
449 |
|
|
450 |
connectToTop:top label:label
|
|
451 |
connectedTop := top.
|
|
452 |
connectedLabel := label
|
|
453 |
!
|
|
454 |
|
|
455 |
showProgressOf:aBlock
|
|
456 |
"show progress, while evaluating aBlock.
|
|
457 |
If the receiver has been created with inBox, show the
|
|
458 |
box centered on the screen. If not, the view is assumed to
|
|
459 |
be contained in another view, and no special startup actions
|
|
460 |
are performed.
|
|
461 |
|
|
462 |
The block is passed two arguments, the progressValue,
|
|
463 |
which should be set to the percentage from time-to-time
|
|
464 |
within the block and an action value, which should be set to
|
|
465 |
the currently performed action (a string) from time to time.
|
|
466 |
The second valueHolder can be left unchanged.
|
|
467 |
|
|
468 |
Caveat: cannot (currently) suppress close of the box ..."
|
|
469 |
|
|
470 |
|progressValue labelValue p|
|
|
471 |
|
|
472 |
progressValue := 0 asValue.
|
|
473 |
labelValue := '' asValue.
|
|
474 |
|
|
475 |
connectedLabel notNil ifTrue:[
|
|
476 |
connectedLabel
|
|
477 |
model:labelValue;
|
|
478 |
aspect:#value;
|
|
479 |
labelMessage:#value.
|
|
480 |
].
|
|
481 |
|
|
482 |
self model:progressValue.
|
|
483 |
|
|
484 |
p := [
|
|
485 |
[
|
|
486 |
aBlock value:progressValue value:labelValue
|
|
487 |
] valueNowOrOnUnwindDo:[
|
|
488 |
p := nil.
|
|
489 |
connectedTop notNil ifTrue:[
|
|
490 |
connectedTop hide
|
|
491 |
]
|
|
492 |
]
|
|
493 |
] fork.
|
|
494 |
|
|
495 |
connectedTop notNil ifTrue:[
|
|
496 |
Processor activeProcess
|
|
497 |
withPriority:(Processor activePriority + 1)
|
|
498 |
do:[
|
|
499 |
self topView show.
|
|
500 |
].
|
|
501 |
p notNil ifTrue:[p terminate].
|
|
502 |
] ifFalse:[
|
|
503 |
self halt:'not yet implemented'.
|
|
504 |
]
|
|
505 |
|
|
506 |
|
|
507 |
"
|
|
508 |
|p|
|
|
509 |
|
|
510 |
p := ProgressIndicator inBox.
|
|
511 |
p showProgressOf:
|
|
512 |
[:progressValue :currentAction |
|
|
513 |
1 to:100 do:[:percent |
|
|
514 |
(Delay forSeconds:0.05) wait.
|
|
515 |
progressValue value:percent
|
|
516 |
].
|
|
517 |
].
|
|
518 |
|
|
519 |
'it can be reused ...'.
|
|
520 |
p showProgressOf:
|
|
521 |
[:progressValue :currentAction |
|
|
522 |
1 to:100 by:5 do:[:percent |
|
|
523 |
(Delay forSeconds:0.05) wait.
|
|
524 |
progressValue value:percent
|
|
525 |
].
|
|
526 |
].
|
|
527 |
|
|
528 |
"
|
|
529 |
! !
|
|
530 |
|
183
|
531 |
!ProgressIndicator class methodsFor:'documentation'!
|
50
|
532 |
|
183
|
533 |
version
|
|
534 |
^ '$Header: /cvs/stx/stx/libwidg2/ProgressIndicator.st,v 1.5 1996-05-18 11:09:56 cg Exp $'
|
50
|
535 |
! !
|