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

Last change on this file since 2035 was 1929, checked in by gah, 14 years ago
File size: 5.4 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-2005  Purdue Research Foundation
9#
10#  See the file "license.terms" for information on usage and
11#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12# ======================================================================
13package require Itk
14
15option add *TemperatureGauge.sampleWidth 30 widgetDefault
16option add *TemperatureGauge.sampleHeight 20 widgetDefault
17option add *TemperatureGauge.textBackground #cccccc widgetDefault
18option add *TemperatureGauge.valuePosition "right" widgetDefault
19option add *TemperatureGauge.editable yes widgetDefault
20option add *TemperatureGauge.state normal widgetDefault
21
22itcl::class Rappture::TemperatureGauge {
23    inherit Rappture::Gauge
24
25    constructor {args} { # defined below }
26
27    protected method _redraw {}
28    protected method _resize {}
29
30    # create a spectrum to use for all temperature widgets
31    private common _spectrum [Rappture::Spectrum [namespace current]::#auto {
32        0.0    blue
33        300.0  red
34        500.0  yellow
35    } -units K]
36}
37
38itk::usual TemperatureGauge {
39}
40
41# ----------------------------------------------------------------------
42# CONSTRUCTOR
43# ----------------------------------------------------------------------
44itcl::body Rappture::TemperatureGauge::constructor {args} {
45    eval itk_initialize -spectrum $_spectrum -units K $args
46}
47
48# ----------------------------------------------------------------------
49# USAGE: _redraw
50#
51# Used internally to redraw the gauge on the internal canvas based
52# on the current value and the size of the widget.  For this temperature
53# gauge, we draw something that looks like a thermometer.
54# ----------------------------------------------------------------------
55itcl::body Rappture::TemperatureGauge::_redraw {} {
56    set c $itk_component(icon)
57    set w [winfo width $c]
58    set h [winfo height $c]
59
60    if {"" == [$c find all]} {
61        # first time around, create the items
62        $c create oval 0 0 1 1 -outline "" -tags bulbfill
63        $c create oval 0 0 1 1 -outline black -tags bulboutline
64        $c create oval 0 0 1 1 -outline "" -fill "" -stipple gray50 -tags {bulbscreen screen}
65        $c create rect 0 0 1 1 -outline black -fill white -tags stickoutline
66        $c create rect 0 0 1 1 -outline "" -tags stickfill
67        $c create rect 0 0 1 1 -outline "" -fill "" -stipple gray50 -tags {stickscreen screen}
68        $c create image 0 0 -anchor w -image "" -tags bimage
69    }
70
71    if {"" != $itk_option(-spectrum)} {
72        set color [$itk_option(-spectrum) get [value]]
73        set frac [$itk_option(-spectrum) get -fraction [value]]
74    } else {
75        set color ""
76        set frac 0
77    }
78
79    # update the items based on current values
80    set x 1
81    set y [expr {0.5*$h}]
82    $c coords bimage 0 $y
83    if {$itk_option(-image) != ""} {
84        set x [expr {$x + [image width $itk_option(-image)] + 2}]
85    }
86
87    set avail [expr {$w-$x}]
88    if {$avail > 0} {
89        #
90        # If we have any space left over, draw the thermometer
91        # as a mercury bulb on the left and a stick to the right.
92        #
93        set bsize [expr {0.2*$avail}]
94        if {$bsize > 0.5*$h-2} {set bsize [expr {0.5*$h-2}]}
95        set ssize [expr {0.5*$bsize}]
96
97        $c coords bulboutline $x [expr {$y-$bsize}] \
98            [expr {$x+2*$bsize}] [expr {$y+$bsize}]
99        $c coords bulbscreen [expr {$x-1}] [expr {$y-$bsize-1}] \
100            [expr {$x+2*$bsize+1}] [expr {$y+$bsize+1}]
101        $c coords bulbfill $x [expr {$y-$bsize}] \
102            [expr {$x+2*$bsize}] [expr {$y+$bsize}]
103
104        set x0 [expr {$x+2*$bsize+1}]
105        set x1 [expr {$w-2}]
106        set xr [expr {($x1-$x0)*$frac + $x0}]
107        $c coords stickoutline [expr {$x0-2}] [expr {$y-$ssize}] \
108            $x1 [expr {$y+$ssize}]
109        $c coords stickscreen [expr {$x0-2}] [expr {$y-$ssize}] \
110            [expr {$x1+1}] [expr {$y+$ssize+1}]
111        $c coords stickfill [expr {$x0-2}] [expr {$y-$ssize+1}] \
112            $xr [expr {$y+$ssize}]
113
114        $c itemconfigure bulbfill -fill $color
115        $c itemconfigure stickfill -fill $color
116    }
117
118    if {$itk_option(-state) == "disabled"} {
119        $c itemconfigure screen -fill white
120    } else {
121        $c itemconfigure screen -fill ""
122    }
123}
124
125# ----------------------------------------------------------------------
126# USAGE: _resize
127#
128# Used internally to resize the internal canvas based on the -image
129# option or the size of the text.
130# ----------------------------------------------------------------------
131itcl::body Rappture::TemperatureGauge::_resize {} {
132    if {$itk_option(-samplewidth) > 0} {
133        set w $itk_option(-samplewidth)
134    } else {
135        set w [winfo reqheight $itk_component(value)]
136    }
137    if {$itk_option(-image) != ""} {
138        set w [expr {$w+[image width $itk_option(-image)]+4}]
139    }
140
141    if {$itk_option(-sampleheight) > 0} {
142        set h $itk_option(-sampleheight)
143    } else {
144        if {$itk_option(-image) != ""} {
145            set h [expr {[image height $itk_option(-image)]+4}]
146        } else {
147            set h [winfo reqheight $itk_component(value)]
148        }
149    }
150
151    $itk_component(icon) configure -width $w -height $h
152}
Note: See TracBrowser for help on using the repository browser.