source: trunk/gui/scripts/tempgauge.tcl @ 1

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

initial import

File size: 4.6 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: tempgauge - gauge for temperature values
3#
4#  This is a specialize form of the more general gauge, used for
5#  displaying temperature values.
6# ======================================================================
7#  AUTHOR:  Michael McLennan, Purdue University
8#  Copyright (c) 2004  Purdue Research Foundation, West Lafayette, IN
9# ======================================================================
10package require Itk
11
12option add *TemperatureGauge.width 30 widgetDefault
13option add *TemperatureGauge.height 0 widgetDefault
14option add *TemperatureGauge.textBackground #cccccc widgetDefault
15option add *TemperatureGauge.valuePosition "right" widgetDefault
16option add *TemperatureGauge.editable yes widgetDefault
17
18itcl::class Rappture::TemperatureGauge {
19    inherit Rappture::Gauge
20
21    constructor {args} { # defined below }
22
23    protected method _redraw {}
24    protected method _resize {}
25
26    # create a spectrum to use for all temperature widgets
27    private common _spectrum [Rappture::Spectrum [namespace current]::#auto {
28        0.0    blue
29        300.0  red
30        500.0  yellow
31    } -units K]
32}
33
34itk::usual TemperatureGauge {
35}
36
37# ----------------------------------------------------------------------
38# CONSTRUCTOR
39# ----------------------------------------------------------------------
40itcl::body Rappture::TemperatureGauge::constructor {args} {
41    eval itk_initialize -spectrum $_spectrum -units K $args
42}
43
44# ----------------------------------------------------------------------
45# USAGE: _redraw
46#
47# Used internally to redraw the gauge on the internal canvas based
48# on the current value and the size of the widget.  For this temperature
49# gauge, we draw something that looks like a thermometer.
50# ----------------------------------------------------------------------
51itcl::body Rappture::TemperatureGauge::_redraw {} {
52    set c $itk_component(icon)
53    set w [winfo width $c]
54    set h [winfo height $c]
55
56    if {"" == [$c find all]} {
57        # first time around, create the items
58        $c create oval 0 0 1 1 -outline "" -tags bulbfill
59        $c create oval 0 0 1 1 -outline black -tags bulboutline
60        $c create rect 0 0 1 1 -outline black -fill white -tags stickoutline
61        $c create rect 0 0 1 1 -outline "" -tags stickfill
62        $c create image 0 0 -anchor w -image "" -tags bimage
63    }
64
65    if {"" != $itk_option(-spectrum)} {
66        set color [$itk_option(-spectrum) get [value]]
67        set frac [$itk_option(-spectrum) get -fraction [value]]
68    } else {
69        set color ""
70        set frac 0
71    }
72
73    # update the items based on current values
74    set x 1
75    set y [expr {0.5*$h}]
76    $c coords bimage 0 $y
77    if {$itk_option(-image) != ""} {
78        set x [expr {$x + [image width $itk_option(-image)] + 2}]
79    }
80
81    set avail [expr {$w-$x}]
82    if {$avail > 0} {
83        #
84        # If we have any space left over, draw the thermometer
85        # as a mercury bulb on the left and a stick to the right.
86        #
87        set bsize [expr {0.2*$avail}]
88        if {$bsize > 0.5*$h-2} {set bsize [expr {0.5*$h-2}]}
89        set ssize [expr {0.5*$bsize}]
90
91        $c coords bulboutline $x [expr {$y-$bsize}] \
92            [expr {$x+2*$bsize}] [expr {$y+$bsize}]
93        $c coords bulbfill $x [expr {$y-$bsize}] \
94            [expr {$x+2*$bsize}] [expr {$y+$bsize}]
95
96        set x0 [expr {$x+2*$bsize+1}]
97        set x1 [expr {$w-2}]
98        set xr [expr {($x1-$x0)*$frac + $x0}]
99        $c coords stickoutline [expr {$x0-2}] [expr {$y-$ssize}] \
100            $x1 [expr {$y+$ssize}]
101        $c coords stickfill [expr {$x0-2}] [expr {$y-$ssize+1}] \
102            $xr [expr {$y+$ssize}]
103
104        $c itemconfigure bulbfill -fill $color
105        $c itemconfigure stickfill -fill $color
106    }
107}
108
109# ----------------------------------------------------------------------
110# USAGE: _resize
111#
112# Used internally to resize the internal canvas based on the -image
113# option or the size of the text.
114# ----------------------------------------------------------------------
115itcl::body Rappture::TemperatureGauge::_resize {} {
116    if {$itk_option(-width) > 0} {
117        set w $itk_option(-width)
118    } else {
119        set w [winfo reqheight $itk_component(value)]
120    }
121    if {$itk_option(-image) != ""} {
122        set w [expr {$w+[image width $itk_option(-image)]+4}]
123    }
124
125    if {$itk_option(-height) > 0} {
126        set h $itk_option(-height)
127    } else {
128        if {$itk_option(-image) != ""} {
129            set h [expr {[image height $itk_option(-image)]+4}]
130        } else {
131            set h [winfo reqheight $itk_component(value)]
132        }
133    }
134
135    $itk_component(icon) configure -width $w -height $h
136}
Note: See TracBrowser for help on using the repository browser.