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

Last change on this file since 3394 was 3330, checked in by gah, 11 years ago

merge (by hand) with Rappture1.2 branch

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