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