source: trunk/gui/scripts/progress.tcl @ 23

Last change on this file since 23 was 23, checked in by mmc, 19 years ago

Fixed the output processing to recognize messages
tagged with the prefix =RAPPTURE-???=> as Rappture
directives. The following directives are now recognized:

=RAPPTURE-PROGRESS=>percent message
=RAPPTURE-ERROR=>message
=RAPPTURE-RUN=>runfile

Also, added the Rappture::exec command to make it easy
to exec a tool within a wrapper script and handle stdout
and stderr messages properly.

File size: 5.6 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: progress - progress meter for long-running apps
3#
4#  This widget acts as a progress meter for long-running applications.
5#  It accepts progress reports as a combination of a percentage and
6#  a message, and then displays the percentage on a bar 0-100%.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2005
10#  Purdue Research Foundation, West Lafayette, IN
11# ======================================================================
12package require Itk
13
14option add *Progress.borderWidth 2 widgetDefault
15option add *Progress.relief sunken widgetDefault
16option add *Progress.length 2i widgetDefault
17option add *Progress.barBackground white widgetDefault
18option add *Progress.barColor blue widgetDefault
19option add *Progress.font -*-helvetica-medium-r-normal-*-*-120-* widgetDefault
20
21itcl::class Rappture::Progress {
22    inherit itk::Widget
23
24    itk_option define -barcolor barColor BarColor ""
25    itk_option define -barbackground barBackground BarBackground ""
26    itk_option define -length length Length 0
27
28    constructor {args} { # defined below }
29    public method settings {args}
30
31    protected method _redraw {}
32
33    private variable _message ""  ;# status message
34    private variable _percent 0   ;# status percentage
35}
36
37itk::usual Progress {
38    keep -cursor -font -foreground -background
39}
40
41# ----------------------------------------------------------------------
42# CONSTRUCTOR
43# ----------------------------------------------------------------------
44itcl::body Rappture::Progress::constructor {args} {
45    itk_component add bar {
46        canvas $itk_interior.bar -highlightthickness 0
47    } {
48        usual
49        keep -borderwidth -relief
50        ignore -highlightthickness -highlightbackground -highlightcolor
51    }
52    pack $itk_component(bar) -expand yes -fill both
53    bind $itk_component(bar) <Configure> [itcl::code $this _redraw]
54
55    itk_component add message {
56        label $itk_interior.mesg -anchor w
57    }
58
59    eval itk_initialize $args
60    component hull configure -borderwidth 0
61
62    set h [font metrics $itk_option(-font) -linespace]
63    $itk_component(bar) configure -height [expr {$h+4}]
64}
65
66# ----------------------------------------------------------------------
67# USAGE: settings ?-percent <val>? ?-message <string>?
68#
69# Clients use this to query/set the settings shown in the progress
70# meter.  With no args, it returns a list of the form "-percent v
71# -message str".  Otherwise, it interprets the args and updates
72# the values.
73# ----------------------------------------------------------------------
74itcl::body Rappture::Progress::settings {args} {
75    if {[llength $args] == 0} {
76        return [list -percent $_percent -message $_message]
77    }
78
79    Rappture::getopts args params {
80        value -percent ""
81        value -message "__ignore__"
82    }
83
84    set changed 0
85    if {$params(-percent) != ""} {
86        if {![string is double $params(-percent)]
87              || $params(-percent) < 0 || $params(-percent) > 100} {
88            error "bad value \"$params(-percent)\": should be 0-100"
89        }
90        set _percent $params(-percent)
91        set changed 1
92    }
93    if {$params(-message) != "__ignore__"} {
94        set _message $params(-message)
95        set changed 1
96    }
97
98    if {$changed} {
99        _redraw
100        update idletasks
101    }
102}
103
104# ----------------------------------------------------------------------
105# USAGE: _redraw
106#
107# Used internally to redraw the progress meter on the internal canvas.
108# ----------------------------------------------------------------------
109itcl::body Rappture::Progress::_redraw {} {
110    set w [winfo width $itk_component(bar)]
111    set h [winfo height $itk_component(bar)]
112
113    if {[string length $_message] > 0} {
114        $itk_component(message) configure -text $_message
115        pack $itk_component(message) -anchor w
116    } else {
117        pack forget $itk_component(message)
118    }
119
120    if {[$itk_component(bar) find all] == ""} {
121        $itk_component(bar) create rectangle 0 0 1 1 \
122            -outline "" -fill $itk_option(-barbackground) -tags barbg
123        $itk_component(bar) create rectangle 0 0 1 1 \
124            -outline "" -fill $itk_option(-barcolor) -tags bar
125        $itk_component(bar) create text 0 0 \
126            -anchor center -text "" -font $itk_option(-font) -tags number
127    }
128
129    set msg [format "%3.0f%%" $_percent]
130    $itk_component(bar) itemconfigure number -text $msg
131
132    set barw [expr {0.01*$_percent*$w}]
133    $itk_component(bar) coords number [expr {$w/2}] [expr {$h/2}]
134    $itk_component(bar) coords bar 0 0 $barw $h
135    $itk_component(bar) coords barbg 0 0 $w $h
136}
137
138# ----------------------------------------------------------------------
139# CONFIGURATION OPTION: -barcolor
140# ----------------------------------------------------------------------
141itcl::configbody Rappture::Progress::barcolor {
142    $itk_component(bar) itemconfigure bar -fill $itk_option(-barcolor)
143}
144
145# ----------------------------------------------------------------------
146# CONFIGURATION OPTION: -barbackground
147# ----------------------------------------------------------------------
148itcl::configbody Rappture::Progress::barbackground {
149    $itk_component(bar) itemconfigure barbg -fill $itk_option(-barbackground)
150}
151
152# ----------------------------------------------------------------------
153# CONFIGURATION OPTION: -length
154# ----------------------------------------------------------------------
155itcl::configbody Rappture::Progress::length {
156    set w [winfo pixels $itk_component(hull) $itk_option(-length)]
157    component hull configure -width $w
158}
Note: See TracBrowser for help on using the repository browser.