Changeset 1929 for trunk/gui/scripts/radiodial.tcl
- Timestamp:
- Oct 22, 2010, 4:06:10 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gui/scripts/radiodial.tcl
r1422 r1929 62 62 public method current {args} 63 63 public method color {value} 64 64 65 65 protected method _setCurrent {val} 66 66 protected method _redraw {} … … 82 82 private variable _vwidth 0 ;# width allocated for values 83 83 } 84 84 85 85 itk::usual Radiodial { 86 86 keep -background -foreground -cursor -font … … 92 92 itcl::body Rappture::Radiodial::constructor {args} { 93 93 itk_component add dial { 94 94 canvas $itk_interior.dial 95 95 } 96 96 pack $itk_component(dial) -expand yes -fill both … … 127 127 itcl::body Rappture::Radiodial::add {label {value ""}} { 128 128 if {"" == $value} { 129 129 set value [llength $_values] 130 130 } 131 131 … … 141 141 142 142 if {"" == $_current} { 143 143 _setCurrent $value 144 144 } 145 145 … … 177 177 itcl::body Rappture::Radiodial::get {args} { 178 178 Rappture::getopts args params { 179 179 value -format "label" 180 180 } 181 181 if {[llength $args] > 1} { 182 182 error "wrong # args: should be \"get ?-format f? ?current|@index\"" 183 183 } 184 184 set index [lindex $args 0] 185 185 if {"" == $index} { 186 187 188 189 186 set ilist "" 187 for {set i 0} {$i < [llength $_values]} {incr i} { 188 lappend ilist $i 189 } 190 190 } elseif {"current" == $index} { 191 192 193 194 191 set ilist [lsearch -exact $_values $_current] 192 if {$ilist < 0} { 193 set ilist "" 194 } 195 195 } elseif {[regexp {^@([0-9]+|end)$} $index match i]} { 196 196 set ilist $i 197 197 } 198 198 if {[llength $ilist] == 1} { 199 199 set op set 200 200 } else { 201 201 set op lappend 202 202 } 203 203 204 204 set rlist "" 205 205 foreach i $ilist { 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 206 switch -- $params(-format) { 207 label { 208 set v [lindex $_values $i] 209 $op rlist [lindex $_val2label($v) 0] 210 } 211 value { 212 $op rlist [lindex $_values $i] 213 } 214 position { 215 foreach {min max} [_limits] break 216 set v [lindex $_values $i] 217 set frac [expr {double($v-$min)/($max-$min)}] 218 $op rlist $frac 219 } 220 all { 221 set v [lindex $_values $i] 222 foreach {min max} [_limits] break 223 set frac [expr {double($v-$min)/($max-$min)}] 224 224 set l [lindex $_val2label($v) 0] 225 226 227 228 229 230 225 $op rlist [list $l $v $frac] 226 } 227 default { 228 error "bad value \"$v\": should be label, value, position, all" 229 } 230 } 231 231 } 232 232 return $rlist … … 240 240 itcl::body Rappture::Radiodial::current {args} { 241 241 if {[llength $args] == 0} { 242 242 return $_current 243 243 } elseif {[llength $args] == 1} { 244 244 set newval [lindex $args 0] 245 245 set n [_findLabel $newval] 246 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 247 # Don't use expr (?:) because it evaluates the resulting string. 248 # For example, it changes -0.020 to -0.02. 249 if { $n >= 0 } { 250 set rawval [lindex $_values $n] 251 } else { 252 set rawval "" 253 } 254 _setCurrent $rawval 255 256 after cancel [itcl::code $this _redraw] 257 after idle [itcl::code $this _redraw] 258 event generate $itk_component(hull) <<Value>> 259 260 return $_current 261 261 } 262 262 error "wrong # args: should be \"current ?newval?\"" … … 273 273 274 274 if {"" != $_spectrum} { 275 276 277 275 foreach {min max} [_limits] break 276 set frac [expr {double($value-$min)/($max-$min)}] 277 set color [$_spectrum get $frac] 278 278 } else { 279 280 281 282 283 279 if {$value == $_current} { 280 set color $_activecolor 281 } else { 282 set color $itk_option(-linecolor) 283 } 284 284 } 285 285 return $color … … 295 295 set _current $value 296 296 if {"" != $_variable} { 297 298 299 300 301 302 297 upvar #0 $_variable var 298 if {[info exists _val2label($value)]} { 299 set var [lindex $_val2label($value) 0] 300 } else { 301 set var $value 302 } 303 303 } 304 304 } … … 323 323 324 324 if {"" != $_knob} { 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 325 set kw [image width $_knob] 326 set kh [image height $_knob] 327 328 switch -- $itk_option(-knobposition) { 329 n@top - nw@top - ne@top { 330 set extra [expr {$t-$kh}] 331 if {$extra < 0} {set extra 0} 332 set y1 [expr {$h-$extra-1}] 333 } 334 n@middle - nw@middle - ne@middle { 335 set extra [expr {int(ceil($kh-0.5*$t))}] 336 if {$extra < 0} {set extra 0} 337 set y1 [expr {$h-$extra-1}] 338 } 339 n@bottom - nw@bottom - ne@bottom { 340 set y1 [expr {$h-$kh-1}] 341 } 342 343 e@top - w@top - center@top - 344 e@bottom - w@bottom - center@bottom { 345 set extra [expr {int(ceil(0.5*$kh))}] 346 set y1 [expr {$h-$extra-1}] 347 } 348 e@middle - w@middle - center@middle { 349 set extra [expr {int(ceil(0.5*($kh-$t)))}] 350 if {$extra < 0} {set extra 0} 351 set y1 [expr {$h-$extra-1}] 352 } 353 354 s@top - sw@top - se@top - 355 s@middle - sw@middle - se@middle - 356 s@bottom - sw@bottom - se@bottom { 357 set y1 [expr {$h-2}] 358 } 359 } 360 360 } 361 361 set y0 [expr {$y1-$t}] … … 366 366 # draw the background rectangle 367 367 $c create rectangle $x0 $y0 $x1 $y1 \ 368 369 368 -outline $itk_option(-dialoutlinecolor) \ 369 -fill $itk_option(-dialfillcolor) 370 370 371 371 # draw the optional progress bar, from start to current 372 372 if {"" != $itk_option(-dialprogresscolor) 373 374 375 376 377 378 379 380 381 373 && [llength $_values] > 0 && "" != $_current} { 374 if {$max != $min} { 375 set frac [expr {double($_current-$min)/($max-$min)}] 376 } else { 377 set frac 0. 378 } 379 set xx1 [expr {$frac*($x1-$x0) + $x0}] 380 $c create rectangle [expr {$x0+1}] [expr {$y0+3}] $xx1 [expr {$y1-2}] \ 381 -outline "" -fill $itk_option(-dialprogresscolor) 382 382 } 383 383 384 384 # draw lines for all values 385 385 if {$max > $min} { 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 386 foreach v $_values { 387 set frac [expr {double($v-$min)/($max-$min)}] 388 if {"" != $_spectrum} { 389 set color [$_spectrum get $frac] 390 } else { 391 if {$v == $_current} { 392 set color $_activecolor 393 } else { 394 set color $itk_option(-linecolor) 395 } 396 } 397 set thick [expr {($v == $_current) ? 3 : 1}] 398 399 if {"" != $color} { 400 set x [expr {$frac*($x1-$x0) + $x0}] 401 $c create line $x [expr {$y0+1}] $x $y1 \ 402 -fill $color -width $thick 403 } 404 } 405 406 if {"" != $_current} { 407 set x [expr {double($_current-$min)/($max-$min)*($x1-$x0) + $x0}] 408 regexp {([nsew]+|center)@} $itk_option(-knobposition) match anchor 409 switch -glob -- $itk_option(-knobposition) { 410 *@top { set kpos $y0 } 411 *@middle { set kpos [expr {int(ceil(0.5*($y1+$y0)))}] } 412 *@bottom { set kpos $y1 } 413 } 414 $c create image $x $kpos -anchor $anchor -image $_knob 415 } 416 416 } 417 417 … … 419 419 set vw $itk_option(-valuewidth) 420 420 if {$vw > 0 && "" != $_current} { 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 421 set str [lindex $_val2label($_current) 0] 422 if {[string length $str] >= $vw} { 423 set str "[string range $str 0 [expr {$vw-3}]]..." 424 } 425 426 set dy [expr {([font metrics $itk_option(-font) -linespace] 427 - [font metrics $itk_option(-font) -ascent])/2}] 428 429 set id [$c create text [expr {$x1+4}] [expr {($y1+$y0)/2+$dy}] \ 430 -anchor w -text $str -font $itk_option(-font) -foreground $fg] 431 foreach {x0 y0 x1 y1} [$c bbox $id] break 432 set x0 [expr {$x0 + 10}] 433 434 # set up a tooltip so you can mouse over truncated values 435 Rappture::Tooltip::text $c [lindex $_val2label($_current) 0] 436 $c bind $id <Enter> \ 437 [list ::Rappture::Tooltip::tooltip pending %W +$x0,$y1] 438 $c bind $id <Leave> \ 439 [list ::Rappture::Tooltip::tooltip cancel] 440 $c bind $id <ButtonPress> \ 441 [list ::Rappture::Tooltip::tooltip cancel] 442 $c bind $id <KeyPress> \ 443 [list ::Rappture::Tooltip::tooltip cancel] 444 444 } 445 445 } … … 465 465 foreach {min max} [_limits] break 466 466 if {$max > $min && $x >= $x0 && $x <= $x1} { 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 467 set dmin $w 468 set xnearest 0 469 set vnearest "" 470 foreach v $_values { 471 set xv [expr {double($v-$min)/($max-$min)*($x1-$x0) + $x0}] 472 if {abs($xv-$x) < $dmin} { 473 set dmin [expr {abs($xv-$x)}] 474 set xnearest $xv 475 set vnearest $v 476 } 477 } 478 479 if {$vnearest != $_current} { 480 _setCurrent $vnearest 481 _redraw 482 483 event generate $itk_component(hull) <<Value>> 484 } 485 485 } 486 486 } … … 497 497 set index [lsearch -exact $_values $_current] 498 498 if {$index >= 0} { 499 500 501 502 503 504 505 506 507 508 509 510 511 512 499 incr index $offset 500 if {$index >= [llength $_values]} { 501 set index [expr {[llength $_values]-1}] 502 } elseif {$index < 0} { 503 set index 0 504 } 505 506 set newval [lindex $_values $index] 507 if {$newval != $_current} { 508 _setCurrent $newval 509 _redraw 510 511 event generate $itk_component(hull) <<Value>> 512 } 513 513 } 514 514 } … … 523 523 itcl::body Rappture::Radiodial::_limits {} { 524 524 if {[llength $_values] == 0} { 525 526 525 set min 0 526 set max 0 527 527 } else { 528 529 530 531 532 533 534 535 536 528 set min [lindex $_values 0] 529 set max $min 530 foreach v [lrange $_values 1 end] { 531 if {$v < $min} { set min $v } 532 if {$v > $max} { set max $v } 533 } 534 set del [expr {$max-$min}] 535 set min [expr {$min-$itk_option(-valuepadding)*$del}] 536 set max [expr {$max+$itk_option(-valuepadding)*$del}] 537 537 } 538 538 539 539 if {"" != $itk_option(-min)} { 540 540 set min $itk_option(-min) 541 541 } 542 542 if {"" != $itk_option(-max)} { 543 543 set max $itk_option(-max) 544 544 } 545 545 return [list $min $max] … … 578 578 579 579 if {"" != $_knob} { 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 580 set kh [image height $_knob] 581 582 switch -- $itk_option(-knobposition) { 583 n@top - nw@top - ne@top - 584 s@bottom - sw@bottom - se@bottom { 585 if {$kh > $h} { set h $kh } 586 } 587 n@middle - nw@middle - ne@middle - 588 s@middle - sw@middle - se@middle { 589 set h [expr {int(ceil(0.5*$h + $kh))}] 590 } 591 n@bottom - nw@bottom - ne@bottom - 592 s@top - sw@top - se@top { 593 set h [expr {$h + $kh}] 594 } 595 e@middle - w@middle - center@middle { 596 set h [expr {(($h > $kh) ? $h : $kh) + 1}] 597 } 598 n@middle - ne@middle - nw@middle - 599 s@middle - se@middle - sw@middle { 600 set extra [expr {int(ceil($kh-0.5*$h))}] 601 if {$extra < 0} { set extra 0 } 602 set h [expr {$h+$extra}] 603 } 604 } 605 605 } 606 606 incr h 1 … … 610 610 # if the -valuewidth is > 0, then make room for the value 611 611 if {$itk_option(-valuewidth) > 0} { 612 613 614 612 set charw [font measure $itk_option(-font) "n"] 613 set _vwidth [expr {$itk_option(-valuewidth)*$charw}] 614 set w [expr {$w+$_vwidth+4}] 615 615 } else { 616 616 set _vwidth 0 617 617 } 618 618 … … 629 629 itcl::body Rappture::Radiodial::_fixValue {args} { 630 630 if {"" == $itk_option(-variable)} { 631 631 return 632 632 } 633 633 upvar #0 $itk_option(-variable) var … … 669 669 itcl::configbody Rappture::Radiodial::valuewidth { 670 670 if {![string is integer $itk_option(-valuewidth)]} { 671 671 error "bad value \"$itk_option(-valuewidth)\": should be integer" 672 672 } 673 673 _fixSize … … 722 722 set val $itk_option(-activelinecolor) 723 723 if {[catch {$val isa ::Rappture::Spectrum} valid] == 0 && $valid} { 724 725 724 set _spectrum $val 725 set _activecolor "" 726 726 } elseif {[catch {winfo rgb $itk_component(hull) $val}] == 0} { 727 728 727 set _spectrum "" 728 set _activecolor $val 729 729 } elseif {"" != $val} { 730 730 error "bad value \"$val\": should be Spectrum object or color" 731 731 } 732 732 after cancel [itcl::code $this _redraw] … … 739 739 itcl::configbody Rappture::Radiodial::knobimage { 740 740 if {[regexp {^image[0-9]+$} $itk_option(-knobimage)]} { 741 741 set _knob $itk_option(-knobimage) 742 742 } elseif {"" != $itk_option(-knobimage)} { 743 743 set _knob [Rappture::icon $itk_option(-knobimage)] 744 744 } else { 745 745 set _knob "" 746 746 } 747 747 _fixSize … … 756 756 itcl::configbody Rappture::Radiodial::knobposition { 757 757 if {![regexp {^([nsew]+|center)@(top|middle|bottom)$} $itk_option(-knobposition)]} { 758 758 error "bad value \"$itk_option(-knobposition)\": should be anchor@top|middle|bottom" 759 759 } 760 760 _fixSize … … 770 770 itcl::configbody Rappture::Radiodial::padding { 771 771 if {[catch {winfo pixels $itk_component(hull) $itk_option(-padding)}]} { 772 772 error "bad value \"$itk_option(-padding)\": should be size in pixels" 773 773 } 774 774 } … … 780 780 itcl::configbody Rappture::Radiodial::valuepadding { 781 781 if {![string is double $itk_option(-valuepadding)] 782 783 782 || $itk_option(-valuepadding) < 0} { 783 error "bad value \"$itk_option(-valuepadding)\": should be >= 0.0" 784 784 } 785 785 } … … 790 790 itcl::configbody Rappture::Radiodial::variable { 791 791 if {"" != $_variable} { 792 793 792 upvar #0 $_variable var 793 trace remove variable var write [itcl::code $this _fixValue] 794 794 } 795 795 … … 797 797 798 798 if {"" != $_variable} { 799 800 801 802 803 804 805 806 } 807 } 799 upvar #0 $_variable var 800 trace add variable var write [itcl::code $this _fixValue] 801 802 # sync to the current value of this variable 803 if {[info exists var]} { 804 _fixValue 805 } 806 } 807 }
Note: See TracChangeset
for help on using the changeset viewer.