Changeset 1929 for trunk/gui/scripts/moleculeViewer.tcl
- Timestamp:
- Oct 22, 2010, 4:06:10 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gui/scripts/moleculeViewer.tcl
r1839 r1929 58 58 private variable _download "";# snapshot for download 59 59 } 60 60 61 61 itk::usual MoleculeViewer { 62 62 } … … 75 75 $_dispatcher register !fixsize 76 76 $_dispatcher dispatch $this !fixsize \ 77 77 "[itcl::code $this emblems fixPosition]; list" 78 78 79 79 itk_option add hull.width hull.height … … 103 103 104 104 itk_component add controls { 105 105 frame $itk_interior.cntls 106 106 } { 107 108 107 usual 108 rename -background -controlbackground controlBackground Background 109 109 } 110 110 pack $itk_component(controls) -side right -fill y 111 111 112 112 itk_component add reset { 113 114 115 116 113 button $itk_component(controls).reset \ 114 -borderwidth 1 -padx 1 -pady 1 \ 115 -bitmap [Rappture::icon reset] \ 116 -command [itcl::code $this _zoom reset] 117 117 } { 118 119 120 118 usual 119 ignore -borderwidth 120 rename -highlightbackground -controlbackground controlBackground Background 121 121 } 122 122 pack $itk_component(reset) -padx 4 -pady 4 … … 124 124 125 125 itk_component add zoomin { 126 127 128 129 126 button $itk_component(controls).zin \ 127 -borderwidth 1 -padx 1 -pady 1 \ 128 -bitmap [Rappture::icon zoomin] \ 129 -command [itcl::code $this _zoom in] 130 130 } { 131 132 133 131 usual 132 ignore -borderwidth 133 rename -highlightbackground -controlbackground controlBackground Background 134 134 } 135 135 pack $itk_component(zoomin) -padx 4 -pady 4 … … 137 137 138 138 itk_component add zoomout { 139 140 141 142 139 button $itk_component(controls).zout \ 140 -borderwidth 1 -padx 1 -pady 1 \ 141 -bitmap [Rappture::icon zoomout] \ 142 -command [itcl::code $this _zoom out] 143 143 } { 144 145 146 144 usual 145 ignore -borderwidth 146 rename -highlightbackground -controlbackground controlBackground Background 147 147 } 148 148 pack $itk_component(zoomout) -padx 4 -pady 4 … … 150 150 151 151 itk_component add labels { 152 153 154 152 label $itk_component(controls).labels \ 153 -borderwidth 1 -padx 1 -pady 1 \ 154 -bitmap [Rappture::icon atoms] 155 155 } { 156 157 158 156 usual 157 ignore -borderwidth 158 rename -highlightbackground -controlbackground controlBackground Background 159 159 } 160 160 pack $itk_component(labels) -padx 4 -pady 8 -ipadx 1 -ipady 1 161 161 Rappture::Tooltip::for $itk_component(labels) "Show/hide the labels on atoms" 162 162 bind $itk_component(labels) <ButtonPress> \ 163 163 [itcl::code $this emblems toggle] 164 164 165 165 # … … 167 167 # 168 168 itk_component add area { 169 169 frame $itk_interior.area 170 170 } 171 171 pack $itk_component(area) -expand yes -fill both 172 172 bind $itk_component(area) <Configure> \ 173 173 [list $_dispatcher event -idle !fixsize] 174 174 175 175 itk_component add renderer { 176 176 vtkTkRenderWidget $itk_component(area).ren -rw $this-renWin 177 177 } { 178 178 } … … 184 184 blt::busy hold $itk_component(area) -cursor left_ptr 185 185 bind $itk_component(area)_Busy <ButtonPress> \ 186 186 [itcl::code $this _move click %x %y] 187 187 bind $itk_component(area)_Busy <B1-Motion> \ 188 188 [itcl::code $this _move drag %x %y] 189 189 bind $itk_component(area)_Busy <ButtonRelease> \ 190 190 [itcl::code $this _move release %x %y] 191 191 192 192 emblems on … … 220 220 itcl::body Rappture::MoleculeViewer::add {dataobj {settings ""}} { 221 221 array set params { 222 223 224 225 226 227 228 222 -color auto 223 -brightness 0 224 -width 1 225 -raise 0 226 -linestyle solid 227 -description "" 228 -param "" 229 229 } 230 230 foreach {opt val} $settings { 231 232 233 234 231 if {![info exists params($opt)]} { 232 error "bad settings \"$opt\": should be [join [lsort [array names params]] {, }]" 233 } 234 set params($opt) $val 235 235 } 236 236 … … 238 238 239 239 if {$pos < 0} { 240 241 242 240 if {![Rappture::library isvalid $dataobj]} { 241 error "bad value \"$dataobj\": should be Rappture::library object" 242 } 243 243 244 245 246 247 248 249 250 251 252 253 254 244 set emblem [$dataobj get components.molecule.about.emblems] 245 if {$emblem == "" || ![string is boolean $emblem] || !$emblem} { 246 emblems off 247 } else { 248 emblems on 249 } 250 251 lappend _dlist $dataobj 252 set _dobj2raise($dataobj) $params(-raise) 253 254 $_dispatcher event -idle !redraw 255 255 } 256 256 } … … 266 266 set dlist $_dlist 267 267 foreach obj $dlist { 268 269 270 271 272 273 274 268 if {[info exists _dobj2raise($obj)] && $_dobj2raise($obj)} { 269 set i [lsearch -exact $dlist $obj] 270 if {$i >= 0} { 271 set dlist [lreplace $dlist $i $i] 272 lappend dlist $obj 273 } 274 } 275 275 } 276 276 return $dlist … … 285 285 itcl::body Rappture::MoleculeViewer::delete {args} { 286 286 if {[llength $args] == 0} { 287 287 set args $_dlist 288 288 } 289 289 … … 291 291 set changed 0 292 292 foreach dataobj $args { 293 294 295 296 297 298 293 set pos [lsearch -exact $_dlist $dataobj] 294 if {$pos >= 0} { 295 set _dlist [lreplace $_dlist $pos $pos] 296 catch {unset _dobj2raise($dataobj)} 297 set changed 1 298 } 299 299 } 300 300 301 301 # if anything changed, then rebuild the plot 302 302 if {$changed} { 303 303 $_dispatcher event -idle !redraw 304 304 } 305 305 } … … 317 317 itcl::body Rappture::MoleculeViewer::download {option args} { 318 318 switch $option { 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 319 coming { 320 if {[catch {blt::winop snap $itk_component(area) $_download}]} { 321 $_download configure -width 1 -height 1 322 $_download put #000000 323 } 324 } 325 controls { 326 # no controls for this download yet 327 return "" 328 } 329 now { 330 # Get the image data (as base64) and decode it back to binary. 331 # This is better than writing to temporary files. When we switch 332 # to the BLT picture image it won't be necessary to decode the 333 # image data. 334 set bytes [$_download data -format "jpeg -quality 100"] 335 set bytes [Rappture::encoding::decode -as b64 $bytes] 336 return [list .jpg $bytes] 337 } 338 default { 339 error "bad option \"$option\": should be coming, controls, now" 340 } 341 341 } 342 342 } … … 349 349 itcl::body Rappture::MoleculeViewer::_clear {} { 350 350 foreach a $_actors { 351 352 351 $this-ren RemoveActor $a 352 rename $a "" 353 353 } 354 354 set _actors "" … … 356 356 357 357 foreach lim {xmin xmax ymin ymax zmin zmax} { 358 358 set _limits($lim) "" 359 359 } 360 360 … … 376 376 set dev [lindex [get] end] 377 377 if {"" != $dev} { 378 379 380 381 382 383 384 385 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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 378 set lib [Rappture::library standard] 379 380 set counter 0 381 foreach atom [$dev children -type atom components.molecule] { 382 set symbol [$dev get components.molecule.$atom.symbol] 383 set xyz [$dev get components.molecule.$atom.xyz] 384 regsub {,} $xyz {} xyz 385 386 # update overall limits for molecules along all axes 387 foreach axis {x y z} val $xyz { 388 if {"" == $_limits(${axis}min)} { 389 set _limits(${axis}min) $val 390 set _limits(${axis}max) $val 391 } else { 392 if {$val < $_limits(${axis}min)} { 393 set _limits(${axis}min) $val 394 } 395 if {$val > $_limits(${axis}max)} { 396 set _limits(${axis}max) $val 397 } 398 } 399 } 400 401 # create an actor for each atom 402 set aname $this-actor[incr counter] 403 vtkActor $aname 404 $aname SetMapper $this-map 405 eval $aname SetPosition $xyz 406 $this-ren AddActor $aname 407 408 set sfac 0.7 409 set scale [$lib get elements.($symbol).scale] 410 if {$scale != ""} { 411 $aname SetScale [expr {$sfac*$scale}] 412 } 413 set color [$lib get elements.($symbol).color] 414 if {$color != ""} { 415 eval [$aname GetProperty] SetColor [_color2rgb $color] 416 } 417 418 lappend _actors $aname 419 420 # create a label for each atom 421 set lname $this-label$counter 422 vtkTextActor $lname 423 $lname SetInput "$counter $symbol" 424 $lname ScaledTextOff 425 426 set tprop [$lname GetTextProperty] 427 $tprop SetJustificationToCentered 428 $tprop SetVerticalJustificationToCentered 429 $tprop ShadowOn 430 $tprop SetColor 1 1 1 431 432 set _label2atom($lname) $aname 433 lappend _actors $lname 434 } 435 if {[$itk_component(labels) cget -relief] == "sunken"} { 436 emblems on 437 } 438 _zoom reset 439 439 } 440 440 $this-ren ResetCamera … … 454 454 itcl::body Rappture::MoleculeViewer::_zoom {option} { 455 455 switch -- $option { 456 457 458 459 460 461 462 463 464 465 466 456 in { 457 [$this-ren GetActiveCamera] Zoom 1.25 458 } 459 out { 460 [$this-ren GetActiveCamera] Zoom 0.8 461 } 462 reset { 463 $this-ren ResetCamera 464 [$this-ren GetActiveCamera] SetViewAngle 30 465 _3dView 45 45 0 466 } 467 467 } 468 468 $_dispatcher event -later !fixsize … … 480 480 itcl::body Rappture::MoleculeViewer::_move {option x y} { 481 481 switch -- $option { 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 482 click { 483 blt::busy configure $itk_component(area) -cursor fleur 484 set _click(x) $x 485 set _click(y) $y 486 set _click(theta) $_view(theta) 487 set _click(phi) $_view(phi) 488 set _click(psi) $_view(psi) 489 } 490 drag { 491 if {[array size _click] == 0} { 492 _move click $x $y 493 } else { 494 set w [winfo width $itk_component(renderer)] 495 set h [winfo height $itk_component(renderer)] 496 if {$w <= 0 || $h <= 0} { 497 return 498 } 499 500 if {[catch { 501 # this fails sometimes for no apparent reason 502 set dx [expr {double($x-$_click(x))/$w}] 503 set dy [expr {double($y-$_click(y))/$h}] 504 }]} { 505 return 506 } 507 508 # 509 # Rotate the camera in 3D 510 # 511 if {$_view(psi) > 90 || $_view(psi) < -90} { 512 # when psi is flipped around, theta moves backwards 513 set dy [expr {-$dy}] 514 } 515 set theta [expr {$_view(theta) - $dy*180}] 516 while {$theta < 0} { set theta [expr {$theta+180}] } 517 while {$theta > 180} { set theta [expr {$theta-180}] } 518 #if {$theta < 2} { set theta 2 } 519 #if {$theta > 178} { set theta 178 } 520 521 if {$theta > 45 && $theta < 135} { 522 set phi [expr {$_view(phi) - $dx*360}] 523 while {$phi < 0} { set phi [expr {$phi+360}] } 524 while {$phi > 360} { set phi [expr {$phi-360}] } 525 set psi $_view(psi) 526 } else { 527 set phi $_view(phi) 528 set psi [expr {$_view(psi) - $dx*360}] 529 while {$psi < -180} { set psi [expr {$psi+360}] } 530 while {$psi > 180} { set psi [expr {$psi-360}] } 531 } 532 533 _3dView $theta $phi $psi 534 emblems fixPosition 535 $_dispatcher event -idle !render 536 537 set _click(x) $x 538 set _click(y) $y 539 } 540 } 541 release { 542 _move drag $x $y 543 blt::busy configure $itk_component(area) -cursor left_ptr 544 catch {unset _click} 545 } 546 default { 547 error "bad option \"$option\": should be click, drag, release" 548 } 549 549 } 550 550 } … … 566 566 set blank 0 567 567 foreach lim {xmin xmax ymin ymax zmin zmax} { 568 569 570 571 568 if {"" == $_limits($lim)} { 569 set blank 1 570 break 571 } 572 572 } 573 573 if {$blank} { 574 575 576 574 set xm 0 575 set ym 0 576 set zm 0 577 577 } else { 578 579 580 578 set xm [expr {0.5*($_limits(xmax)+$_limits(xmin))}] 579 set ym [expr {0.5*($_limits(ymax)+$_limits(ymin))}] 580 set zm [expr {0.5*($_limits(zmax)+$_limits(zmin))}] 581 581 } 582 582 … … 613 613 itcl::body Rappture::MoleculeViewer::emblems {option} { 614 614 switch -- $option { 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 615 on { 616 set state 1 617 } 618 off { 619 set state 0 620 } 621 toggle { 622 if {[$itk_component(labels) cget -relief] == "sunken"} { 623 set state 0 624 } else { 625 set state 1 626 } 627 } 628 fixPosition { 629 foreach lname [array names _label2atom] { 630 set aname $_label2atom($lname) 631 set xyz [$aname GetPosition] 632 eval $this-xyzconv SetValue $xyz 633 set xy [$this-xyzconv GetComputedViewportValue $this-ren] 634 eval $lname SetDisplayPosition $xy 635 } 636 return 637 } 638 default { 639 error "bad option \"$option\": should be on, off, toggle, fixPosition" 640 } 641 641 } 642 642 643 643 if {$state} { 644 645 646 647 648 644 $itk_component(labels) configure -relief sunken 645 foreach lname [array names _label2atom] { 646 catch {$this-ren AddActor2D $lname} 647 } 648 emblems fixPosition 649 649 } else { 650 651 652 653 650 $itk_component(labels) configure -relief raised 651 foreach lname [array names _label2atom] { 652 catch {$this-ren RemoveActor $lname} 653 } 654 654 } 655 655 $_dispatcher event -idle !render … … 683 683 itcl::configbody Rappture::MoleculeViewer::device { 684 684 if {"" != $itk_option(-device) 685 686 685 && ![Rappture::library isvalid $itk_option(-device)]} { 686 error "bad value \"$itk_option(-device)\": should be Rappture::library object" 687 687 } 688 688 delete 689 689 690 690 if {"" != $itk_option(-device)} { 691 692 693 694 695 696 697 691 add $itk_option(-device) 692 set state [$itk_option(-device) get components.molecule.about.emblems] 693 if {$state == "" || ![string is boolean $state] || !$state} { 694 emblems off 695 } else { 696 emblems on 697 } 698 698 } 699 699 $_dispatcher event -idle !redraw
Note: See TracChangeset
for help on using the changeset viewer.