Changeset 2985 for branches/blt4


Ignore:
Timestamp:
Apr 30, 2012, 3:23:41 PM (13 years ago)
Author:
gah
Message:
 
Location:
branches/blt4/gui/scripts
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/blt4/gui/scripts/drawingentry.tcl

    r2966 r2985  
    2020    private variable _path
    2121    private variable _cname2id
    22     private variable _worldX 0
    23     private variable _worldY 0
    24     private variable _worldWidth 0
    25     private variable _worldHeight 0
    26 
     22    private variable _xMin 0
     23    private variable _yMin 0
     24    private variable _xScale 0
     25    private variable _yScale 0
     26    private variable _xOffset 0
     27    private variable _yOffset 0
     28    private variable _canvasWidth 0
     29    private variable _canvasHeight 0
     30    private variable _xAspect 0
     31    private variable _yAspect 0
    2732    constructor {owner path args} { # defined below }
    2833
     
    3843    private method ParseStringDescription { cname }
    3944    private method ParseNumberDescription { cname }
    40     private method GetCanvasHeight { }
    41     private method GetCanvasWidth { }
     45    private method AdjustDrawingArea { xAspect yAspect }
    4246    private method ScreenX { x }
    4347    private method ScreenY { y }
     
    4549    private method Highlight { tag }
    4650    private method Unhighlight { tag }
     51    private method Redraw {}
    4752}
    4853
     
    150155        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
    151156    }
     157}
     158
     159itcl::body Rappture::DrawingEntry::Redraw {args} {
     160    # Remove exists canvas items and hints
     161    $itk_component(drawing) delete all
     162    # Recompute the size of the canvas/drawing area
     163    set _canvasWidth [winfo width $itk_component(drawing)]
     164    if { $w < 2 } {
     165        set _canvasWidth [winfo reqwidth $itk_component(drawing)]
     166    }
     167    set _canvasHeight [winfo height $itk_component(drawing)]
     168    if { $_canvasHeight < 2 } {
     169        set _canvasHeight [winfo reqheight $itk_component(drawing)]
     170    }
     171    set _drawingWidth $_canvasWidth
     172    set _drawingHeight $_canvasHeight
     173    set _xOffset 0
     174    set _yOffset 0
     175    array set _hints {
     176        set background white
     177    }
     178    ParseDecription
    152179}
    153180
     
    395422
    396423itcl::body Rappture::DrawingEntry::ScreenX { x } {
    397     set norm [expr ($x - $_worldX) / double($_worldWidth)]
    398     puts stderr "ScreenX x=$x, norm=$norm wx=$_worldX ww=$_worldWidth"
    399     set x [expr int($norm * [GetCanvasWidth])]
    400     puts stderr "ScreenX after x=$x cw=[GetCanvasWidth]"
     424    set norm [expr ($x - $_xMin) * $_xScale]
     425    set x [expr int($norm * $_drawingWidth) + $_xOffset]
    401426    return $x
    402427}
    403428
    404429itcl::body Rappture::DrawingEntry::ScreenY { y } {
    405     set norm [expr ($y - $_worldY) / double($_worldWidth)]
    406     set y [expr int($norm * [GetCanvasHeight])]
     430    set norm [expr ($y - $_yMin) * $_yScale]
     431    set y [expr int($norm * $_drawingHeight) + $_yOffset]
    407432    return $y
    408433}
     
    416441}
    417442
    418 itcl::body Rappture::DrawingEntry::GetCanvasWidth { } {
    419     set w [winfo width $itk_component(drawing)]
    420     if { $w < 2 } {
    421         set w [winfo reqwidth $itk_component(drawing)]
    422     }
    423     return $w
    424 }
    425 
    426 itcl::body Rappture::DrawingEntry::GetCanvasHeight { } {
    427     set h [winfo height $itk_component(drawing)]
    428     if { $h < 2 } {
    429         set h [winfo reqheight $itk_component(drawing)]
    430     }
    431     return $h
     443itcl::body Rappture::DrawingEntry::AdjustDrawingArea { xAspect yAspect } {
     444    set _drawingWidth $_canvasWidth
     445    set _drawingHeight $_canvasHeight
     446    if { $xAspect <= 0 || $yAspect <= 0 } {
     447        return
     448    }
     449    set current [expr double($_canvasWidth) / double($_canvasHeight)]
     450    set wanted [expr double($xAspect) / double($yAspect)]
     451    if { $current > $wanted } {
     452        set sw [ expr int($_canvasWidth * $wanted)]
     453        if { $sw < 1 } {
     454            set sw 1
     455        }
     456        set _xOffset [expr $_canvasWidth - $sw]
     457        set _drawingWidth $sw
     458    } else {
     459        set sh [expr int($_canvaseHeight / $wanted)]
     460        if { $sh < 1 }  {
     461            set sh 1
     462        }
     463        set _xOffset [expr $_canvasHeight - $sh]
     464        set _drawingHeight $sh
     465    }
    432466}
    433467
    434468itcl::body Rappture::DrawingEntry::ParseDescription {} {
    435469    puts stderr "ParseDescrption description owner=$_owner path=$_path"
    436     set bbox [$_owner xml get $_path.about.bbox]
    437     puts stderr bbox=$bbox
    438     if { [llength $bbox] != 4 } {
    439         set bbox "0 0 [GetCanvasWidth] [GetCanvasHeight]"
    440     }
    441     foreach { x1 y1 x2 y2 } $bbox break
    442     set _worldWidth [expr $x2 - $x1]
    443     set _worldHeight [expr $y2 - $y1]
    444     set _worldX $x1
    445     set _worldY $y1
     470    ParseBackground
    446471    foreach cname [$_owner xml children $_path.components] {
    447472        switch -glob -- $cname {
     
    476501        -fill ""
    477502}
     503
     504#
     505#      <background>
     506#       <!-- background color of the drawing canvas (default white) -->
     507#       <color>black</color>
     508#       <!-- coordinate system:  x0 y0 ?at screenx screeny? x1 y1
     509#                               ?at screenx screeny?
     510#            The screenx/screeny values are optional, so you can also say
     511#          something like "-.1 0 1.1 1" as you had in your example.
     512#          This lets you put the origin at a specific point on screen,
     513#          and also define the directions of the axes.  We still compute
     514#          the overall bounding box.  In the example below, the bounding
     515#          box goes from -1,1 in the upper-left corner to 1,-1 in the
     516#          lower right.
     517#       -->
     518#       <coordinates>0 0 at 50% 50% 1 1 at 100% 100%</coordinates>
     519
     520#       <!-- aspect ratio:  scales coordinate system so that pixels may not
     521#            be square.  A coordinate system like the one above implies a
     522#          square drawing area, since x and y both go from -1 to 1.  But
     523#          if you set the aspect to 2:1, you'll get something twice as
     524#          wide as it is tall.  This effectively says that x goes from
     525#          -1 to 1 in a certain distance, but y goes from -1 to 1 in half
     526#          that screen distance.  Default is whatever aspect is defined
     527#          by the coordinates.  If x goes 0-3 and y goes 0-1, then the
     528#          drawing (without any other aspect ratio) would be 3x wide and
     529#          1x tall.  The aspect ratio could be used to force it to be
     530#          square instead by setting "1 1" instead.  In that case, x goes
     531#          0-3 over the width, and y goes 0-1 over the same screen distance
     532#          along the height.
     533#       -->
     534#       <aspect>2 1</aspect>
     535#     </background>
     536#
     537
     538itcl::body Rappture::DrawingEntry::ParseCoordinates {} {
     539    set len [llength $values]
     540    if { $len == 4 } {
     541        if { [scan $values "%g %g %g %g" x1 y1 x2 y2] != 4 } {
     542            error "bad coordinates specification \"$values\""
     543        }
     544        set _xScale [expr 1.0 / ($x2 - $x1)]
     545        set _yScale [expr 1.0 / ($y2 - $y2)]
     546        set _xMin $x1
     547        set _yMin $y1
     548    } elseif { $len == 10 } {
     549        if { [scan $values "%g %g %s %d%% %d%% %g %g %s %d%% %d%%" \
     550                  sx1 sy1 at1 x1 y1 sx2 sy2 at2 x2 y2] != 10 } {
     551            error "bad coordinates specification \"$values\""
     552        }
     553        if { $at1 != "at" || $at2 != "at" } {
     554            error "bad coordinates specification \"$values\""
     555        }           
     556        set x1 [expr $x1 / 100.0]
     557        set x2 [expr $x2 / 100.0]
     558        set y1 [expr $y1 / 100.0]
     559        set y2 [expr $y2 / 100.0]
     560        set _xScale [expr ($sx2 - $sx1) / ($x2 - $x1)]
     561        set _yScale [expr ($sy2 - $sy2) / ($y2 - $y2)]
     562        set _xMin $x1
     563        set _yMin $y1
     564    }
     565}
     566
     567itcl::body Rappture::DrawingEntry::ParseBackground {} {
     568    foreach elem [$_owner xml children $_path.background] {
     569        switch -- $elem {
     570            #  Background color of the drawing canvas (default white)
     571            "color" {
     572                set value [$_owner xml get $_path.background.$elem]
     573                set _hints(background) $value
     574            }
     575            "aspect" {
     576                set value [$_owner xml get $_path.background.$elem]
     577                foreach { xAspect yAspect } $value break
     578                AdjustDrawingArea $xAspect $yAspect
     579            }
     580            "coordinates" {
     581                set value [$_owner xml get $_path.background.$elem]
     582                ParseCoordinates $value
     583            }
     584        }
     585    }
     586}
     587
  • branches/blt4/gui/scripts/htmlviewer.tcl

    r2982 r2985  
    348348    }
    349349    puts stderr "Problem in your html: image \"$fileName\" does not exist"
    350     # The htmlwidget assumes it owns the image and will delete it.
    351     # Always create a copy of the image.
    352350    set img [Rappture::icon exclaim]
    353351    # Htmlwidget will free the image itself, so make a duplicate image
Note: See TracChangeset for help on using the changeset viewer.