/usr/share/tcltk/tklib0.6/controlwidget/voltmeter.tcl is in tklib 0.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | # voltmeter.tcl --
#
# Adapted by Arjen Markus (snitified), july 2010
#
#
#
#
# Part of: The TCL'ers Wiki
# Contents: a voltmeter-like widget
# Date: Fri Jun 13, 2003
#
# Abstract
#
#
#
# Copyright (c) 2003 Marco Maggi
#
# The author hereby grant permission to use, copy, modify, distribute,
# and license this software and its documentation for any purpose,
# provided that existing copyright notices are retained in all copies
# and that this notice is included verbatim in any distributions. No
# written agreement, license, or royalty fee is required for any of the
# authorized uses. Modifications to this software may be copyrighted by
# their authors and need not follow the licensing terms described here,
# provided that the new terms are clearly indicated on the first page of
# each file where they apply.
#
# IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY PARTY
# FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
# ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
# DERIVATIVES THEREOF, EVEN IF THE AUTHOR HAVE BEEN ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
# NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
# AND THE AUTHOR AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
# MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#
# $Id: voltmeter.tcl,v 1.3 2010/09/10 17:16:29 andreas_kupries Exp $
#
package require Tk 8.5
package require snit
package provide voltmeter 0.1
namespace eval controlwidget {
namespace export voltmeter
}
# voltmeter --
# Voltmeter-like widget
#
snit::widget controlwidget::voltmeter {
#
# widget default values
#
option -borderwidth -default 1
option -background -default gray
option -dialcolor -default white
option -needlecolor -default black
option -scalecolor -default black
option -indexid -default {}
option -variable -default {} -configuremethod VariableName
option -min -default 0.0
option -max -default 100.0
option -labelcolor -default black
option -titlecolor -default black
option -labelfont -default {Helvetica 8}
option -titlefont -default {Helvetica 9}
option -labels -default {}
option -title -default {}
option -width -default 50m
option -height -default 25m
option -highlightthickness -default 0
option -relief -default raised
variable pi [expr {3.14159265359/180.0}]
variable motion
variable xc
variable yc
constructor args {
#
# Configure the widget
#
$self configurelist $args
canvas $win.c -background $options(-background) -width $options(-width) -height $options(-height) \
-relief $options(-relief) -borderwidth $options(-borderwidth)
grid $win.c -sticky news -padx 2m -pady 2m
if {$options(-variable) ne ""} {
trace add variable ::$options(-variable) write [mymethod tracer $options(-variable)]
}
set width [$win.c cget -width]
set height [$win.c cget -height]
set xcentre [expr {$width*0.5}]
set ycentre [expr {$width*1.4}]
set t 1.15
set t1 1.25
$win.c create arc \
[expr {$xcentre-$width*$t}] [expr {$ycentre-$width*$t}] \
[expr {$xcentre+$width*$t}] [expr {$ycentre+$width*$t}] \
-start 70.5 -extent 37 -style arc -outline lightgray \
-width [expr {$ycentre*0.245}]
$win.c create arc \
[expr {$xcentre-$width*$t}] [expr {$ycentre-$width*$t}] \
[expr {$xcentre+$width*$t}] [expr {$ycentre+$width*$t}] \
-start 71 -extent 36 -style arc -outline $options(-dialcolor) \
-width [expr {$ycentre*0.23}]
$win.c create arc \
[expr {$xcentre-$width*$t1}] [expr {$ycentre-$width*$t1}] \
[expr {$xcentre+$width*$t1}] [expr {$ycentre+$width*$t1}] \
-start 75 -extent 30 \
-fill black -outline $options(-scalecolor) -style arc -width 0.5m
set num [llength $options(-labels)]
set angle 255.0
set delta [expr {30.0/($num-1)}]
set l1 [expr {$width*$t1}]
set l2 [expr {$width*$t1*0.95}]
set l3 [expr {$width*$t1*0.92}]
for {set i 0} {$i < $num} {incr i} {
set a [expr {($angle+$delta*$i)*$pi}]
set x1 [expr {$xcentre+$l1*cos($a)}]
set y1 [expr {$ycentre+$l1*sin($a)}]
set x2 [expr {$xcentre+$l2*cos($a)}]
set y2 [expr {$ycentre+$l2*sin($a)}]
$win.c create line $x1 $y1 $x2 $y2 -fill $options(-scalecolor) -width 0.5m
set x1 [expr {$xcentre+$l3*cos($a)}]
set y1 [expr {$ycentre+$l3*sin($a)}]
set label [lindex $options(-labels) $i]
if { [string length $label] } {
$win.c create text $x1 $y1 \
-anchor center -justify center -fill $options(-labelcolor) \
-text $label -font $options(-labelfont)
}
}
set title $options(-title)
if { [string length $title] } {
$win.c create text $xcentre [expr {$ycentre-$width*1.05}] \
-anchor center -justify center -fill $options(-titlecolor) \
-text $title -font $options(-titlefont)
}
rivet $win.c 10 10
rivet $win.c [expr {$width-10}] 10
rivet $win.c 10 [expr {$height-10}]
rivet $win.c [expr {$width-10}] [expr {$height-10}]
set motion 0
set xc $xcentre
set yc $ycentre
bind $win.c <ButtonRelease> [list $self needleRelease %W]
bind $win.c <Motion> [list $self needleMotion %W %x %y]
set value 0
$self drawline $win $value
}
method destructor {} {
set varname ::$options(-variable)]
trace remove variable $varname write \
[namespace code "mymethod tracer $win $varname"]
}
#
# public methods --
#
method set {newValue} {
if { $options(-variable) != "" } {
set ::$options(-variable) $newValue ;#! This updates the dial too
} else {
set options(-value) $newValue
$self draw $win.c $options(-value)
}
}
method get {} {
return $options(-value)
}
#
# private methods --
#
method VariableName {option name} {
# Could be still constructing in which case
# $win.c does not exist:
if {![winfo exists $win.c]} {
set options(-variable) $name
return;
}
# Remove any old traces
if {$options(-variable) ne ""} {
trace remove variable ::$options(-variable) write [mymethod tracer $options(-variable)]
}
# Set new trace if appropriate and update value.
set options(-variable) $name
if {$options(-variable) ne ""} {
trace add variable ::$options(-variable) write [mymethod tracer $options(-variable)]
$self drawline $win.c [set ::$options(-variable)]
}
}
method tracer { varname args } \
{
set options(-value) [set ::$varname]
$self drawline $win [set ::$varname]
}
method drawline { widget value } {
set id $options(-indexid)
set min $options(-min)
set max $options(-max)
set c $widget.c
set v [expr { ($value <= ($max*1.05))? $value : ($max*1.05) }]
set angle [expr {((($v-$min)/(1.0*($max-$min)))*30.0+165.0)*$pi}]
set width [$c cget -width]
set xcentre [expr {$width/2.0}]
set ycentre [expr {$width*1.4}]
set l1 [expr {$ycentre*0.85}]
set l2 [expr {$ycentre*0.7}]
set xl [expr {$xcentre-$l1*sin($angle)}]
set yl [expr {$ycentre+$l1*cos($angle)}]
set xs [expr {$xcentre-$l2*sin($angle)}]
set ys [expr {$ycentre+$l2*cos($angle)}]
catch {$c delete $id}
set id [$c create line $xs $ys $xl $yl -fill $options(-needlecolor) -width 0.6m]
$c bind $id <ButtonPress> [list $self needlePress %W]
set options(-indexid) $id
}
method needlePress {w} \
{
set motion 1
}
method needleRelease {w} \
{
set motion 0
}
method needleMotion {w x y} \
{
if {! $motion} { return }
if {$y == $yc && $x == $xc} { return }
#
# Compute the angle with the positive y-axis - easier to examine!
#
set angle [expr {atan2($xc - $x,$yc - $y) / $pi}]
if { $angle >= 15.0 } {
set angle 15.0
}
if { $angle < -15.0 } {
set angle -15.0
}
set ::$options(-variable) [expr {$options(-min) + ($options(-max)-$options(-min))*(15.0-$angle) / 30.0}]
}
proc rivet { c xc yc } {
shadowcircle $c \
[expr {$xc-4}] [expr {$yc-4}] [expr {$xc+4}] [expr {$yc+4}] \
5 0.5m -45.0
}
proc shadowcircle { canvas x1 y1 x2 y2 ticks width orient } {
set radius [expr {($x2-$x1)/2.0}]
set angle $orient
set delta [expr {180.0/$ticks}]
for {set i 0} {$i <= $ticks} {incr i} {
set a [expr {($angle+$i*$delta)}]
set b [expr {($angle-$i*$delta)}]
set color [expr {40+$i*(200/$ticks)}]
set color [format "#%x%x%x" $color $color $color]
$canvas create arc $x1 $y1 $x2 $y2 -start $a -extent $delta \
-style arc -outline $color -width $width
$canvas create arc $x1 $y1 $x2 $y2 -start $b -extent $delta \
-style arc -outline $color -width $width
}
}
}
if {0} {
# main --
# Demonstration of the voltmeter object
#
proc main { argc argv } {
global forever
wm withdraw .
wm title . "A voltmeter-like widget"
wm geometry . +10+10
::controlwidget::voltmeter .t1 -variable value1 -labels { 0 50 100 } -title "Voltmeter (V)"
scale .s1 -command "set ::value1" -variable value1
::controlwidget::voltmeter .t2 -variable value2 -labels { 0 {} 2.5 {} 5 } \
-width 80m -height 40m -title "Ampere (mA)" -dialcolor lightgreen -scalecolor white \
-min 0 -max 5
scale .s2 -command "set ::value2" -variable value2
button .b -text Quit -command "set ::forever 1"
grid .t1 .s1 .t2 .s2 .b
wm deiconify .
vwait forever
.t1 destructor
.t2 destructor
exit 0
}
main $argc $argv
}
### end of file
# Local Variables:
# mode: tcl
# page-delimiter: "^#PAGE"
# End:
|