source: branches/r9/gui/scripts/mapviewer.tcl @ 4919

Last change on this file since 4919 was 4919, checked in by gah, 6 years ago
File size: 66.8 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: mapviewer - Map object viewer
4#
5#  It connects to the GeoVis server running on a rendering farm,
6#  transmits data, and displays the results.
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# ======================================================================
14package require Itk
15package require BLT
16#package require Img
17
18option add *MapViewer.width 4i widgetDefault
19option add *MapViewer*cursor crosshair widgetDefault
20option add *MapViewer.height 4i widgetDefault
21option add *MapViewer.foreground black widgetDefault
22option add *MapViewer.controlBackground gray widgetDefault
23option add *MapViewer.controlDarkBackground #999999 widgetDefault
24option add *MapViewer.plotBackground black widgetDefault
25option add *MapViewer.plotForeground white widgetDefault
26option add *MapViewer.font \
27    -*-helvetica-medium-r-normal-*-12-* widgetDefault
28
29# must use this name -- plugs into Rappture::resources::load
30proc MapViewer_init_resources {} {
31    Rappture::resources::register \
32        geovis_server Rappture::MapViewer::SetServerList
33}
34
35itcl::class Rappture::MapViewer {
36    inherit Rappture::VisViewer
37
38    itk_option define -plotforeground plotForeground Foreground ""
39    itk_option define -plotbackground plotBackground Background ""
40
41    private variable _layersFrame "";   # Name of layers frame widget
42    private variable _mapsettings;      # Global map settings
43
44    constructor { hostlist args } {
45        Rappture::VisViewer::constructor $hostlist
46    } {
47        # defined below
48    }
49    destructor {
50        # defined below
51    }
52    public proc SetServerList { namelist } {
53        Rappture::VisViewer::SetServerList "geovis" $namelist
54    }
55    public method add {dataobj {settings ""}}
56    public method camera {option args}
57    public method delete {args}
58    public method disconnect {}
59    public method download {option args}
60    public method get {args}
61    public method isconnected {}
62    public method limits { colormap }
63    public method parameters {title args} {
64        # do nothing
65    }
66    public method scale {args}
67
68    protected method AdjustSetting {what {value ""}}
69    protected method Connect {}
70    protected method CurrentLayers {args}
71    protected method Disconnect {}
72    protected method DoResize {}
73    protected method DoRotate {}
74    protected method InitSettings { args  }
75    protected method KeyPress { key }
76    protected method KeyRelease { key }
77    protected method MouseClick { button x y }
78    protected method MouseDoubleClick { button x y }
79    protected method MouseDrag { button x y }
80    protected method MouseMotion {}
81    protected method MouseRelease { button x y }
82    protected method MouseScroll { direction }
83    protected method Pan {option x y}
84    protected method Rebuild {}
85    protected method ReceiveMapInfo { args }
86    protected method ReceiveImage { args }
87    protected method Rotate {option x y}
88    protected method Zoom {option {x 0} {y 0}}
89
90    # The following methods are only used by this class.
91    private method BuildCameraTab {}
92    private method BuildDownloadPopup { widget command }
93    private method BuildLayerTab {}
94    private method BuildTerrainTab {}
95    private method ChangeLayerVisibility { dataobj layer }
96    private method EventuallyHandleMotionEvent { x y }
97    private method EventuallyResize { w h }
98    private method EventuallyRotate { dx dy }
99    private method GetImage { args }
100    private method GetNormalizedMouse { x y }
101    private method MapIsGeocentric {}
102    private method SetLayerStyle { dataobj layer }
103    private method SetTerrainStyle { style }
104    private method SetOpacity { dataset }
105    private method UpdateLayerControls {}
106    private method EarthFile {}
107
108    private variable _dlist "";         # list of data objects
109    private variable _obj2datasets
110    private variable _obj2ovride;       # maps dataobj => style override
111    private variable _layers;           # Contains the names of all the
112                                        # layer in the server.
113    private variable _click;            # info used for rotate operations
114    private variable _view;             # view params for 3D view
115    private variable _settings
116    private variable _visibility
117    private variable _style;            # Array of current component styles.
118    private variable _initialStyle;     # Array of initial component styles.
119    private variable _reset 1;          # Indicates that server was reset and
120                                        # needs to be reinitialized.
121    private variable _initCamera 1;
122    private variable _haveTerrain 0
123
124    private variable _first ""     ;# This is the topmost dataset.
125    private variable _start 0
126    private variable _title ""
127
128    common _downloadPopup          ;# download options from popup
129    private common _hardcopy
130    private variable _width 0
131    private variable _height 0
132    private variable _resizePending 0
133    private variable _rotatePending 0
134    private variable _rotateDelay 150
135    private variable _motion
136    private variable _sendEarthFile 0
137    private variable _useServerManip 0
138}
139
140itk::usual MapViewer {
141    keep -background -foreground -cursor -font
142    keep -plotbackground -plotforeground
143}
144
145# ----------------------------------------------------------------------
146# CONSTRUCTOR
147# ----------------------------------------------------------------------
148itcl::body Rappture::MapViewer::constructor {hostlist args} {
149    set _serverType "geovis"
150
151    if { [catch {
152       
153    # Rebuild event
154    $_dispatcher register !rebuild
155    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
156
157    # Resize event
158    $_dispatcher register !resize
159    $_dispatcher dispatch $this !resize "[itcl::code $this DoResize]; list"
160
161    # Rotate event
162    $_dispatcher register !rotate
163    $_dispatcher dispatch $this !rotate "[itcl::code $this DoRotate]; list"
164
165    # <Motion> event
166    $_dispatcher register !motion
167    $_dispatcher dispatch $this !motion "[itcl::code $this MouseMotion]; list"
168
169    #
170    # Populate parser with commands handle incoming requests
171    #
172    $_parser alias image    [itcl::code $this ReceiveImage]
173    $_parser alias map      [itcl::code $this ReceiveMapInfo]
174    $_parser alias camera   [itcl::code $this camera]
175
176    # Settings for mouse motion events: these are required
177    # to update the Lat/Long coordinate display
178    array set _motion {
179        compress        0
180        delay           100
181        enable          0
182        pending         0
183        x               0
184        y               0
185    }
186    # This array holds the Viewpoint parameters that the
187    # server sends on "camera get".
188    array set _view {
189        distance        1.0
190        heading         0.0
191        pitch           -89.9
192        srs             ""
193        verticalDatum   ""
194        x               0.0
195        y               0.0
196        z               0.0
197    }
198
199    # Note: grid types are "geodetic", "utm" and "mgrs"
200    # Currently only work in geocentric maps
201    array set _settings [subst {
202        camera-throw           0
203        coords-precision       5
204        coords-units           "latlong_decimal_degrees"
205        coords-visible         1
206        grid                   0
207        grid-type              "geodetic"
208        legend                 1
209        terrain-edges          0
210        terrain-lighting       0
211        terrain-vertscale      1.0
212        terrain-wireframe      0
213    }]
214    itk_component add view {
215        canvas $itk_component(plotarea).view \
216            -highlightthickness 0 -borderwidth 0
217    } {
218        usual
219        ignore -highlightthickness -borderwidth  -background
220    }
221
222    set c $itk_component(view)
223    #bind $c <KeyPress-Left>  [list %W xview scroll 10 units]
224    #bind $c <KeyPress-Right> [list %W xview scroll -10 units]
225    #bind $c <KeyPress-Up>    [list %W yview scroll 10 units]
226    #bind $c <KeyPress-Down>  [list %W yview scroll -10 units]
227    bind $c <Enter> "focus %W"
228    bind $c <Control-F1> [itcl::code $this ToggleConsole]
229
230    # Fix the scrollregion in case we go off screen
231    $c configure -scrollregion [$c bbox all]
232
233    set _map(id) [$c create image 0 0 -anchor nw -image $_image(plot)]
234    set _map(cwidth) -1
235    set _map(cheight) -1
236    set _map(zoom) 1.0
237    set _map(original) ""
238
239    set f [$itk_component(main) component controls]
240    itk_component add reset {
241        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
242            -highlightthickness 0 \
243            -image [Rappture::icon reset-view] \
244            -command [itcl::code $this camera reset]
245    } {
246        usual
247        ignore -highlightthickness
248    }
249    pack $itk_component(reset) -side top -padx 2 -pady 2
250    Rappture::Tooltip::for $itk_component(reset) \
251        "Reset the view to the default zoom level"
252
253    itk_component add zoomin {
254        button $f.zin -borderwidth 1 -padx 1 -pady 1 \
255            -highlightthickness 0 \
256            -image [Rappture::icon zoom-in] \
257            -command [itcl::code $this Zoom in]
258    } {
259        usual
260        ignore -highlightthickness
261    }
262    pack $itk_component(zoomin) -side top -padx 2 -pady 2
263    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
264
265    itk_component add zoomout {
266        button $f.zout -borderwidth 1 -padx 1 -pady 1 \
267            -highlightthickness 0 \
268            -image [Rappture::icon zoom-out] \
269            -command [itcl::code $this Zoom out]
270    } {
271        usual
272        ignore -highlightthickness
273    }
274    pack $itk_component(zoomout) -side top -padx 2 -pady 2
275    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
276
277    BuildLayerTab
278    BuildTerrainTab
279    BuildCameraTab
280
281    # Legend
282
283    set _image(legend) [image create photo]
284    itk_component add legend {
285        canvas $itk_component(plotarea).legend -width 50 -highlightthickness 0
286    } {
287        usual
288        ignore -highlightthickness
289        rename -background -plotbackground plotBackground Background
290    }
291
292    # Hack around the Tk panewindow.  The problem is that the requested
293    # size of the 3d view isn't set until an image is retrieved from
294    # the server.  So the panewindow uses the tiny size.
295    set w 10000
296    pack forget $itk_component(view)
297    blt::table $itk_component(plotarea) \
298        0,0 $itk_component(view) -fill both -reqwidth $w
299    blt::table configure $itk_component(plotarea) c1 -resize none
300
301    bind $itk_component(view) <Configure> \
302        [itcl::code $this EventuallyResize %w %h]
303
304    if {$_useServerManip} {
305        # Bindings for keyboard events
306        bind $itk_component(view) <KeyPress> \
307            [itcl::code $this KeyPress %N]
308        bind $itk_component(view) <KeyRelease> \
309            [itcl::code $this KeyRelease %N]
310
311        # Bindings for rotation via mouse
312        bind $itk_component(view) <ButtonPress-1> \
313            [itcl::code $this MouseClick 1 %x %y]
314        bind $itk_component(view) <Double-1> \
315            [itcl::code $this MouseDoubleClick 1 %x %y]
316        bind $itk_component(view) <B1-Motion> \
317            [itcl::code $this MouseDrag 1 %x %y]
318        bind $itk_component(view) <ButtonRelease-1> \
319            [itcl::code $this MouseRelease 1 %x %y]
320
321        # Bindings for panning via mouse
322        bind $itk_component(view) <ButtonPress-2> \
323            [itcl::code $this MouseClick 2 %x %y]
324        bind $itk_component(view) <Double-2> \
325            [itcl::code $this MouseDoubleClick 2 %x %y]
326        bind $itk_component(view) <B2-Motion> \
327            [itcl::code $this MouseDrag 2 %x %y]
328        bind $itk_component(view) <ButtonRelease-2> \
329            [itcl::code $this MouseRelease 2 %x %y]
330
331        bind $itk_component(view) <ButtonPress-3> \
332            [itcl::code $this MouseClick 3 %x %y]
333        bind $itk_component(view) <Double-3> \
334            [itcl::code $this MouseDoubleClick 3 %x %y]
335        bind $itk_component(view) <B3-Motion> \
336            [itcl::code $this MouseDrag 3 %x %y]
337        bind $itk_component(view) <ButtonRelease-3> \
338            [itcl::code $this MouseRelease 3 %x %y]
339
340        # Binding for mouse motion events
341        if {$_motion(enable)} {
342            bind $itk_component(view) <Motion> \
343                [itcl::code $this EventuallyHandleMotionEvent %x %y]
344        }
345    } else {
346        # Bindings for panning via mouse
347        bind $itk_component(view) <ButtonPress-1> \
348            [itcl::code $this Pan click %x %y]
349        bind $itk_component(view) <B1-Motion> \
350            [itcl::code $this Pan drag %x %y]
351        bind $itk_component(view) <ButtonRelease-1> \
352            [itcl::code $this Pan release %x %y]
353        bind $itk_component(view) <Button-1> \
354            +[itcl::code $this SendCmd "map setpos %x %y"]
355        bind $itk_component(view) <Double-1> \
356            [itcl::code $this camera go %x %y 0.4]
357
358        # Bindings for rotation via mouse
359        bind $itk_component(view) <ButtonPress-2> \
360            [itcl::code $this Rotate click %x %y]
361        bind $itk_component(view) <B2-Motion> \
362            [itcl::code $this Rotate drag %x %y]
363        bind $itk_component(view) <ButtonRelease-2> \
364            [itcl::code $this Rotate release %x %y]
365
366        # Bindings for zoom via mouse
367        bind $itk_component(view) <ButtonPress-3> \
368            [itcl::code $this Zoom click %x %y]
369        bind $itk_component(view) <B3-Motion> \
370            [itcl::code $this Zoom drag %x %y]
371        bind $itk_component(view) <ButtonRelease-3> \
372            [itcl::code $this Zoom release %x %y]
373        bind $itk_component(view) <Double-3> \
374            [itcl::code $this camera go %x %y 2.5]
375        bind $itk_component(view) <Double-3> \
376            +[itcl::code $this SendCmd "map setpos %x %y"]
377
378        # Bindings for panning via keyboard
379        bind $itk_component(view) <KeyPress-Left> \
380            [itcl::code $this Pan set 10 0]
381        bind $itk_component(view) <KeyPress-Right> \
382            [itcl::code $this Pan set -10 0]
383        bind $itk_component(view) <KeyPress-Up> \
384            [itcl::code $this Pan set 0 -10]
385        bind $itk_component(view) <KeyPress-Down> \
386            [itcl::code $this Pan set 0 10]
387
388        # Binding for mouse motion events
389        set _motion(compress) 1
390        if {$_motion(enable)} {
391            bind $itk_component(view) <Motion> \
392                [itcl::code $this EventuallyHandleMotionEvent %x %y]
393        }
394    }
395
396    bind $itk_component(view) <Shift-KeyPress-Left> \
397        [itcl::code $this Pan set 2 0]
398    bind $itk_component(view) <Shift-KeyPress-Right> \
399        [itcl::code $this Pan set -2 0]
400    bind $itk_component(view) <Shift-KeyPress-Up> \
401        [itcl::code $this Pan set 0 -2]
402    bind $itk_component(view) <Shift-KeyPress-Down> \
403        [itcl::code $this Pan set 0 2]
404
405    # Bindings for zoom via keyboard
406    bind $itk_component(view) <KeyPress-Prior> \
407        [itcl::code $this Zoom out]
408    bind $itk_component(view) <KeyPress-Next> \
409        [itcl::code $this Zoom in]
410
411    bind $itk_component(view) <Enter> "focus $itk_component(view)"
412
413    if {[string equal "x11" [tk windowingsystem]]} {
414        # Bindings for zoom via mouse
415        if {$_useServerManip} {
416            bind $itk_component(view) <4> [itcl::code $this MouseScroll up]
417            bind $itk_component(view) <5> [itcl::code $this MouseScroll down]
418        } else {
419            bind $itk_component(view) <4> [itcl::code $this Zoom out]
420            bind $itk_component(view) <5> [itcl::code $this Zoom in]
421        }
422    }
423
424    set _image(download) [image create photo]
425
426    eval itk_initialize $args
427    Connect
428} errs] != 0 } {
429        puts stderr errs=$errs
430    }
431}
432
433# ----------------------------------------------------------------------
434# DESTRUCTOR
435# ----------------------------------------------------------------------
436itcl::body Rappture::MapViewer::destructor {} {
437    Disconnect
438    $_dispatcher cancel !rebuild
439    $_dispatcher cancel !resize
440    $_dispatcher cancel !rotate
441    image delete $_image(plot)
442    image delete $_image(download)
443}
444
445itcl::body Rappture::MapViewer::DoResize {} {
446    set sendResize 1
447    if { $_width < 2 } {
448        set _width 500
449        set sendResize 0
450    }
451    if { $_height < 2 } {
452        set _height 500
453        set sendResize 0
454    }
455    set _start [clock clicks -milliseconds]
456    if {$sendResize} {
457        SendCmd "screen size $_width $_height"
458    }
459    set _resizePending 0
460}
461
462itcl::body Rappture::MapViewer::DoRotate {} {
463    SendCmd "camera rotate $_view(azimuth) $_view(elevation)"
464    set _rotatePending 0
465}
466
467itcl::body Rappture::MapViewer::EventuallyResize { w h } {
468    set _width $w
469    set _height $h
470    if { !$_resizePending } {
471        set _resizePending 1
472        $_dispatcher event -after 200 !resize
473    }
474}
475
476itcl::body Rappture::MapViewer::EventuallyRotate { dx dy } {
477    set _view(azimuth) $dx
478    set _view(elevation) $dy
479    if { !$_rotatePending } {
480        set _rotatePending 1
481        $_dispatcher event -after $_rotateDelay !rotate
482    }
483}
484
485# ----------------------------------------------------------------------
486# USAGE: add <dataobj> ?<settings>?
487#
488# Clients use this to add a data object to the plot.  The optional
489# <settings> are used to configure the plot.  Allowed settings are
490# -color, -brightness, -width, -linestyle, and -raise.
491# ----------------------------------------------------------------------
492itcl::body Rappture::MapViewer::add {dataobj {settings ""}} {
493    array set params {
494        -color auto
495        -width 1
496        -linestyle solid
497        -brightness 0
498        -raise 0
499        -description ""
500        -param ""
501        -type ""
502    }
503    array set params $settings
504    set params(-description) ""
505    set params(-param) ""
506    array set params $settings
507
508    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
509        # can't handle -autocolors yet
510        set params(-color) black
511    }
512    set pos [lsearch -exact $_dlist $dataobj]
513    if {$pos < 0} {
514        #if {[llength $_dlist] > 0} {
515        #    error "Can't add more than 1 map to mapviewer"
516        #}
517        lappend _dlist $dataobj
518    }
519    set _obj2ovride($dataobj-color) $params(-color)
520    set _obj2ovride($dataobj-width) $params(-width)
521    set _obj2ovride($dataobj-raise) $params(-raise)
522    $_dispatcher event -idle !rebuild
523}
524
525# ----------------------------------------------------------------------
526# USAGE: delete ?<dataobj1> <dataobj2> ...?
527#
528#       Clients use this to delete a dataobj from the plot.  If no dataobjs
529#       are specified, then all dataobjs are deleted.  No data objects are
530#       deleted.  They are only removed from the display list.
531#
532# ----------------------------------------------------------------------
533itcl::body Rappture::MapViewer::delete {args} {
534    if { [llength $args] == 0} {
535        set args $_dlist
536    }
537    # Delete all specified dataobjs
538    set changed 0
539    foreach dataobj $args {
540        set pos [lsearch -exact $_dlist $dataobj]
541        if { $pos < 0 } {
542            continue;                   # Don't know anything about it.
543        }
544        # When a map is marked deleted, we hide its layers.
545        foreach layer [$dataobj layers] {
546            SendCmd "map layer visible 0 $layer"
547            set _visibility($layer) 0
548        }
549        # Remove it from the dataobj list.
550        set _dlist [lreplace $_dlist $pos $pos]
551        array unset _obj2ovride $dataobj-*
552        array unset _settings $dataobj-*
553        set changed 1
554    }
555    # If anything changed, then rebuild the plot
556    if { $changed } {
557        $_dispatcher event -idle !rebuild
558    }
559}
560
561# ----------------------------------------------------------------------
562# USAGE: get ?-objects?
563# USAGE: get ?-visible?
564# USAGE: get ?-image view?
565#
566# Clients use this to query the list of objects being plotted, in
567# order from bottom to top of this result.  The optional "-image"
568# flag can also request the internal images being shown.
569# ----------------------------------------------------------------------
570itcl::body Rappture::MapViewer::get {args} {
571    if {[llength $args] == 0} {
572        set args "-objects"
573    }
574
575    set op [lindex $args 0]
576    switch -- $op {
577        "-objects" {
578            # put the dataobj list in order according to -raise options
579            set dlist {}
580            foreach dataobj $_dlist {
581                if { ![$dataobj isvalid] } {
582                    continue
583                }
584                if {[info exists _obj2ovride($dataobj-raise)] &&
585                    $_obj2ovride($dataobj-raise)} {
586                    set dlist [linsert $dlist 0 $dataobj]
587                } else {
588                    lappend dlist $dataobj
589                }
590            }
591            return $dlist
592        }
593        "-visible" {
594            set dlist {}
595            foreach dataobj $_dlist {
596                if { ![$dataobj isvalid] } {
597                    continue
598                }
599                if { ![info exists _obj2ovride($dataobj-raise)] } {
600                    # No setting indicates that the object isn't visible.
601                    continue
602                }
603                # Otherwise use the -raise parameter to put the object to
604                # the front of the list.
605                if { $_obj2ovride($dataobj-raise) } {
606                    set dlist [linsert $dlist 0 $dataobj]
607                } else {
608                    lappend dlist $dataobj
609                }
610            }
611            return $dlist
612        }           
613        -image {
614            if {[llength $args] != 2} {
615                error "wrong # args: should be \"get -image view\""
616            }
617            switch -- [lindex $args end] {
618                view {
619                    return $_image(plot)
620                }
621                default {
622                    error "bad image name \"[lindex $args end]\": should be view"
623                }
624            }
625        }
626        default {
627            error "bad option \"$op\": should be -objects or -image"
628        }
629    }
630}
631
632itcl::body Rappture::MapViewer::MapIsGeocentric {} {
633    if { [info exists _mapsettings(type)] } {
634        return [expr {$_mapsettings(type) eq "geocentric"}]
635    } else {
636        return 0
637    }
638}
639
640# ----------------------------------------------------------------------
641# USAGE: scale ?<data1> <data2> ...?
642#
643# Sets the default limits for the overall plot according to the
644# limits of the data for all of the given <data> objects.  This
645# accounts for all objects--even those not showing on the screen.
646# Because of this, the limits are appropriate for all objects as
647# the user scans through data in the ResultSet viewer.
648# ----------------------------------------------------------------------
649itcl::body Rappture::MapViewer::scale {args} {
650    array unset _mapsettings
651    set _haveTerrain 0
652
653    # Verify that all the maps have the same global settings. For example,
654    # you can't have one map type "geocentric" and the other "projected".
655
656    foreach dataobj $args {
657        if { ![$dataobj isvalid] } {
658            continue
659        }
660        array unset hints
661        array set hints [$dataobj hints]
662        if { ![info exists _mapsettings(label)] } {
663            set _mapsettings(label) $hints(label)
664        }
665        if { ![info exists _mapsettings(style)] } {
666            set _mapsettings(style) $hints(style)
667        }
668        if { ![info exists _mapsettings(type)] } {
669            set _mapsettings(type) $hints(type)
670        } elseif { $hints(type) != $_mapsettings(type) } {
671            error "maps \"$hints(label)\" have differing types"
672        }
673        if { ![info exists _mapsettings(projection)] } {
674            set _mapsettings(projection) $hints(projection)
675        } elseif { $hints(projection) != $_mapsettings(projection) } {
676            error "maps \"$hints(label)\" have differing projections"
677        }
678        if { $hints(extents) != "" } {
679            if { ![info exists _mapsettings(extents)] } {
680                set _mapsettings(extents) $hints(extents)
681            }
682            foreach {x1 y1 x2 y2} $hints(extents) break
683            if { ![info exists _mapsettings(x1)] || $x1 < $_mapsettings(x1) } {
684                set _mapsettings(x1) $x1
685            }
686            if { ![info exists _mapsettings(y1)] || $y1 < $_mapsettings(y1) } {
687                set _mapsettings(y1) $y1
688            }
689            if { ![info exists _mapsettings(x2)] || $x2 > $_mapsettings(x2) } {
690                set _mapsettings(x2) $x2
691            }
692            if { ![info exists _mapsettings(y2)] || $y2 > $_mapsettings(y2) } {
693                set _mapsettings(y2) $y2
694            }
695        }
696        foreach layer [$dataobj layers] {
697            if { [$dataobj type $layer] == "elevation" } {
698                set _haveTerrain 1
699                break
700            }
701        }
702    }
703    if { $_haveTerrain } {
704        if { [$itk_component(main) exists "Terrain Settings"] } {
705            # TODO: Enable controls like vertical scale that only have
706            # an effect when terrain is present
707        }
708    }
709}
710
711# ----------------------------------------------------------------------
712# USAGE: download coming
713# USAGE: download controls <downloadCommand>
714# USAGE: download now
715#
716# Clients use this method to create a downloadable representation
717# of the plot.  Returns a list of the form {ext string}, where
718# "ext" is the file extension (indicating the type of data) and
719# "string" is the data itself.
720# ----------------------------------------------------------------------
721itcl::body Rappture::MapViewer::download {option args} {
722    switch $option {
723        coming {
724            if {[catch {
725                blt::winop snap $itk_component(plotarea) $_image(download)
726            }]} {
727                $_image(download) configure -width 1 -height 1
728                $_image(download) put #000000
729            }
730        }
731        controls {
732            set popup .mapviewerdownload
733            if { ![winfo exists .mapviewerdownload] } {
734                set inner [BuildDownloadPopup $popup [lindex $args 0]]
735            } else {
736                set inner [$popup component inner]
737            }
738            set _downloadPopup(image_controls) $inner.image_frame
739            set num [llength [get]]
740            set num [expr {($num == 1) ? "1 result" : "$num results"}]
741            set word [Rappture::filexfer::label downloadWord]
742            $inner.summary configure -text "$word $num in the following format:"
743            update idletasks            ;# Fix initial sizes
744            return $popup
745        }
746        now {
747            set popup .mapviewerdownload
748            if {[winfo exists .mapviewerdownload]} {
749                $popup deactivate
750            }
751            switch -- $_downloadPopup(format) {
752                "image" {
753                    return [$this GetImage [lindex $args 0]]
754                }
755            }
756            return ""
757        }
758        default {
759            error "bad option \"$option\": should be coming, controls, now"
760        }
761    }
762}
763
764# ----------------------------------------------------------------------
765# USAGE: Connect ?<host:port>,<host:port>...?
766#
767# Clients use this method to establish a connection to a new
768# server, or to reestablish a connection to the previous server.
769# Any existing connection is automatically closed.
770# ----------------------------------------------------------------------
771itcl::body Rappture::MapViewer::Connect {} {
772    global readyForNextFrame
773    set readyForNextFrame 1
774    set _reset 1
775    set _hosts [GetServerList "geovis"]
776    if { "" == $_hosts } {
777        return 0
778    }
779    set result [VisViewer::Connect $_hosts]
780    if { $result } {
781        if { $_reportClientInfo }  {
782            # Tell the server the viewer, hub, user and session.
783            # Do this immediately on connect before buffering any commands
784            global env
785
786            set info {}
787            set user "???"
788            if { [info exists env(USER)] } {
789                set user $env(USER)
790            }
791            set session "???"
792            if { [info exists env(SESSION)] } {
793                set session $env(SESSION)
794            }
795            lappend info "hub" [exec hostname]
796            lappend info "client" "mapviewer"
797            lappend info "user" $user
798            lappend info "session" $session
799            SendCmd "clientinfo [list $info]"
800        }
801
802        set w [winfo width $itk_component(view)]
803        set h [winfo height $itk_component(view)]
804        EventuallyResize $w $h
805    }
806    return $result
807}
808
809#
810# isconnected --
811#
812#       Indicates if we are currently connected to the visualization server.
813#
814itcl::body Rappture::MapViewer::isconnected {} {
815    return [VisViewer::IsConnected]
816}
817
818#
819# disconnect --
820#
821itcl::body Rappture::MapViewer::disconnect {} {
822    Disconnect
823    set _reset 1
824}
825
826#
827# Disconnect --
828#
829#       Clients use this method to disconnect from the current rendering
830#       server.
831#
832itcl::body Rappture::MapViewer::Disconnect {} {
833    VisViewer::Disconnect
834
835    $_dispatcher cancel !rebuild
836    $_dispatcher cancel !resize
837    $_dispatcher cancel !rotate
838    $_dispatcher cancel !motion
839    # disconnected -- no more data sitting on server
840    array unset _layers
841    array unset _layersFrame
842    global readyForNextFrame
843    set readyForNextFrame 1
844}
845
846# ----------------------------------------------------------------------
847# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
848#
849# Invoked automatically whenever the "image" command comes in from
850# the rendering server.  Indicates that binary image data with the
851# specified <size> will follow.
852# ----------------------------------------------------------------------
853itcl::body Rappture::MapViewer::ReceiveImage { args } {
854    global readyForNextFrame
855    set readyForNextFrame 1
856    array set info {
857        -token "???"
858        -bytes 0
859        -type image
860    }
861    array set info $args
862    set bytes [ReceiveBytes $info(-bytes)]
863    if { $info(-type) == "image" } {
864        $_image(plot) configure -data $bytes
865    } elseif { $info(type) == "print" } {
866        set tag $this-print-$info(-token)
867        set _hardcopy($tag) $bytes
868    }
869}
870
871#
872# ReceiveMapInfo --
873#
874itcl::body Rappture::MapViewer::ReceiveMapInfo { args } {
875    if { ![isconnected] } {
876        return
877    }
878    set option [lindex $args 0]
879    switch -- $option {
880        "coords" {
881            set len [llength $args]
882            if {$len < 4} {
883                puts stderr "Coords out of range"
884            } elseif {$len < 6} {
885                foreach { x y z } [lrange $args 1 end] break
886                puts stderr "Coords: $x $y $z"
887            } else {
888                foreach { x y z pxX pxY } [lrange $args 1 end] break
889                puts stderr "Coords($pxX,$pxY): $x $y $z"
890            }
891        }
892        "names" {
893            foreach { name } [lindex $args 1] {
894                puts stderr "layer: $name"
895            }
896        }
897        default {
898            error "unknown map option \"$option\" from server"
899        }
900    }
901}
902
903# ----------------------------------------------------------------------
904# USAGE: Rebuild
905#
906# Called automatically whenever something changes that affects the
907# data in the widget.  Clears any existing data and rebuilds the
908# widget to display new data.
909# ----------------------------------------------------------------------
910itcl::body Rappture::MapViewer::Rebuild {} {
911    set w [winfo width $itk_component(view)]
912    set h [winfo height $itk_component(view)]
913    if { $w < 2 || $h < 2 } {
914        $_dispatcher event -idle !rebuild
915        return
916    }
917
918    # Turn on buffering of commands to the server.  We don't want to be
919    # preempted by a server disconnect/reconnect (which automatically
920    # generates a new call to Rebuild).
921    StartBufferingCommands
922
923    if { $_reset } {
924        set _width $w
925        set _height $h
926        DoResize
927
928        if { [info exists _mapsettings(type)] } {
929            # The map must be reset once before any layers are added This
930            # should not be done more than once as it is very expensive.
931            if {$_sendEarthFile} {
932                set bytes [EarthFile]
933                if {0} {
934                    set f [open "/tmp/map.earth" "w"]
935                    puts $f $bytes
936                    close $f
937                }
938                set length [string length $bytes]
939                SendCmd "map load data follows $length"
940                append _outbuf $bytes
941            } else {
942                if { $_mapsettings(type) == "geocentric" } {
943                    SendCmd "map reset geocentric"
944                }  else {
945                    set proj $_mapsettings(projection)
946                    if { $proj == "" } {
947                        SendCmd "map reset projected global-mercator"
948                    } elseif { ![info exists _mapsettings(extents)] || $_mapsettings(extents) == "" } {
949                        SendCmd [list map reset "projected" $proj]
950                    } else {
951                        #foreach {x1 y1 x2 y2} $_mapsettings(extents) break
952                        foreach key "x1 y1 x2 y2" {
953                            set $key $_mapsettings($key)
954                        }
955                        SendCmd [list map reset "projected" $proj $x1 $y1 $x2 $y2]
956                    }
957                }
958                # XXX: Remove these after implementing batch load of layers with reset
959                SendCmd "map layer delete base"
960            }
961
962            # Most terrain settings are global to the map and apply even
963            # if there is no elevation layer.  The exception is the
964            # vertical scale, which only applies if there is an elevation
965            # layer
966            if { [info exists _mapsettings(style)] } {
967                SetTerrainStyle $_mapsettings(style)
968            } else {
969                InitSettings terrain-edges terrain-lighting \
970                    terrain-vertscale terrain-wireframe
971            }
972            InitSettings coords-visible
973        } else {
974            error "No map settings on reset"
975        }
976    }
977
978    set _first ""
979    set count 0
980
981    foreach dataobj [get -objects] {
982        set _obj2datasets($dataobj) ""
983        foreach layer [$dataobj layers] {
984            array unset info
985            array set info [$dataobj layer $layer]
986            if { ![info exists _layers($layer)] } {
987                if { $_reportClientInfo }  {
988                    set cinfo {}
989                    lappend cinfo "tool_id"       [$dataobj hints toolId]
990                    lappend cinfo "tool_name"     [$dataobj hints toolName]
991                    lappend cinfo "tool_version"  [$dataobj hints toolRevision]
992                    lappend cinfo "tool_title"    [$dataobj hints toolTitle]
993                    lappend cinfo "dataset_label" [$dataobj hints label]
994                    lappend cinfo "dataset_tag"   $layer
995                    SendCmd [list "clientinfo" $cinfo]
996                }
997                set _layers($layer) 1
998                SetLayerStyle $dataobj $layer
999            }
1000            lappend _obj2datasets($dataobj) $layer
1001            # FIXME: This is overriding all layers' initial visibility setting
1002            if { [info exists _obj2ovride($dataobj-raise)] } {
1003                SendCmd "map layer visible 1 $layer"
1004                set _visibility($layer) 1
1005                #SetLayerOpacity $layer
1006            }
1007        }
1008    }
1009
1010    if {$_reset} {
1011        if {$_initCamera} {
1012            # If this is the first Rebuild, we need to
1013            # set up the initial view settings if there
1014            # are any
1015            if { [info exists _mapsettings(camera)] } {
1016                set location $_mapsettings(camera)
1017                if { $location != "" } {
1018                    array set _view $location
1019                    camera set all
1020                }
1021            }
1022            set _initCamera 0
1023        } else {
1024            # Restore view from before reconnect
1025            camera set all
1026        }
1027    }
1028
1029    UpdateLayerControls
1030    set _reset 0
1031    global readyForNextFrame
1032    set readyForNextFrame 0;            # Don't advance to the next frame
1033                                        # until we get an image.
1034
1035    # Actually write the commands to the server socket.  If it fails, we
1036    # don't care.  We're finished here.
1037    blt::busy hold $itk_component(hull)
1038    StopBufferingCommands
1039    blt::busy release $itk_component(hull)
1040}
1041
1042# ----------------------------------------------------------------------
1043# USAGE: CurrentLayers ?-all -visible? ?dataobjs?
1044#
1045# Returns a list of server IDs for the current datasets being displayed.
1046# This is normally a single ID, but it might be a list of IDs if the
1047# current data object has multiple components.
1048# ----------------------------------------------------------------------
1049itcl::body Rappture::MapViewer::CurrentLayers {args} {
1050    set flag [lindex $args 0]
1051    switch -- $flag {
1052        "-all" {
1053            if { [llength $args] > 1 } {
1054                error "CurrentLayers: can't specify dataobj after \"-all\""
1055            }
1056            set dlist [get -objects]
1057        }
1058        "-visible" {
1059            if { [llength $args] > 1 } {
1060                set dlist {}
1061                set args [lrange $args 1 end]
1062                foreach dataobj $args {
1063                    if { [info exists _obj2ovride($dataobj-raise)] } {
1064                        lappend dlist $dataobj
1065                    }
1066                }
1067            } else {
1068                set dlist [get -visible]
1069            }
1070        }           
1071        default {
1072            set dlist $args
1073        }
1074    }
1075    set rlist ""
1076    foreach dataobj $dlist {
1077        foreach layer [$dataobj layers] {
1078            if { [info exists _layers($layer)] && $_layers($layer) } {
1079                lappend rlist $layer
1080            }
1081        }
1082    }
1083    return $rlist
1084}
1085
1086itcl::body Rappture::MapViewer::KeyPress {k} {
1087    SendCmd "key press $k"
1088}
1089
1090itcl::body Rappture::MapViewer::KeyRelease {k} {
1091    SendCmd "key release $k"
1092}
1093
1094itcl::body Rappture::MapViewer::GetNormalizedMouse {x y} {
1095    set w [winfo width $itk_component(view)]
1096    set h [winfo height $itk_component(view)]
1097    set x [expr {(2.0 * double($x)/$w) - 1.0}]
1098    set y [expr {(2.0 * double($y)/$h) - 1.0}]
1099    return [list $x $y]
1100}
1101
1102itcl::body Rappture::MapViewer::MouseClick {button x y} {
1103    SendCmd "mouse click $button $x $y"
1104}
1105
1106itcl::body Rappture::MapViewer::MouseDoubleClick {button x y} {
1107    SendCmd "mouse dblclick $button $x $y"
1108}
1109
1110itcl::body Rappture::MapViewer::MouseDrag {button x y} {
1111    SendCmd "mouse drag $button $x $y"
1112}
1113
1114itcl::body Rappture::MapViewer::MouseRelease {button x y} {
1115    SendCmd "mouse release $button $x $y"
1116}
1117
1118itcl::body Rappture::MapViewer::MouseMotion {} {
1119    SendCmd "mouse motion $_motion(x) $_motion(y)"
1120    set _motion(pending) 0
1121}
1122
1123itcl::body Rappture::MapViewer::MouseScroll {direction} {
1124    switch -- $direction {
1125        "up" {
1126            SendCmd "mouse scroll 1"
1127        }
1128        "down" {
1129            SendCmd "mouse scroll -1"
1130        }
1131    }
1132}
1133
1134#
1135# EventuallyHandleMotionEvent --
1136#
1137#       This routine compresses (no button press) motion events.  It
1138#       delivers a server mouse command once every 100 milliseconds (if a
1139#       motion event is pending).
1140#
1141itcl::body Rappture::MapViewer::EventuallyHandleMotionEvent {x y} {
1142    set _motion(x) $x
1143    set _motion(y) $y
1144    if { !$_motion(compress) } {
1145        MouseMotion
1146        return
1147    }
1148    if { !$_motion(pending) } {
1149        set _motion(pending) 1
1150        $_dispatcher event -after $_motion(delay) !motion
1151    }
1152}
1153
1154# ----------------------------------------------------------------------
1155# USAGE: Zoom in
1156# USAGE: Zoom out
1157# USAGE: Zoom reset
1158#        $this Zoom click x y
1159#        $this Zoom drag x y
1160#        $this Zoom release x y
1161#
1162# Called automatically when the user clicks on one of the zoom
1163# controls for this widget.  Changes the zoom for the current view.
1164# Also implements mouse zoom.
1165# ----------------------------------------------------------------------
1166itcl::body Rappture::MapViewer::Zoom {option {x 0} {y 0}} {
1167    switch -- $option {
1168        "in" {
1169            # z here is normalized mouse Y delta
1170            set z -0.25
1171            SendCmd "camera zoom $z"
1172        }
1173        "out" {
1174            # z here is normalized mouse Y delta
1175            set z 0.25
1176            SendCmd "camera zoom $z"
1177        }
1178        "reset" {
1179            SendCmd "camera dist $_view(distance)"
1180        }
1181        "click" {
1182            set _click(x) $x
1183            set _click(y) $y
1184            $itk_component(view) configure -cursor hand1
1185        }
1186        "drag" {
1187            if { ![info exists _click(x)] } {
1188                set _click(x) $x
1189            }
1190            if { ![info exists _click(y)] } {
1191                set _click(y) $y
1192            }
1193            set h [winfo height $itk_component(view)]
1194            set dy [expr ($_click(y) - $y)/double($h)]
1195            set _click(x) $x
1196            set _click(y) $y
1197            if {[expr (abs($dy) > 0.0)]} {
1198                SendCmd "camera zoom $dy"
1199            }
1200        }
1201        "release" {
1202            Zoom drag $x $y
1203            $itk_component(view) configure -cursor ""
1204        }
1205    }
1206}
1207
1208# ----------------------------------------------------------------------
1209# USAGE: Rotate click <x> <y>
1210# USAGE: Rotate drag <x> <y>
1211# USAGE: Rotate release <x> <y>
1212#
1213# Called automatically when the user clicks/drags/releases in the
1214# plot area.  Moves the plot according to the user's actions.
1215# ----------------------------------------------------------------------
1216itcl::body Rappture::MapViewer::Rotate {option x y} {
1217    switch -- $option {
1218        "click" {
1219            $itk_component(view) configure -cursor fleur
1220            set _click(x) $x
1221            set _click(y) $y
1222        }
1223        "drag" {
1224            if {[array size _click] == 0} {
1225                Rotate click $x $y
1226            } else {
1227                set w [winfo width $itk_component(view)]
1228                set h [winfo height $itk_component(view)]
1229                if {$w <= 0 || $h <= 0} {
1230                    return
1231                }
1232                set dx [expr ($x - $_click(x))/double($w)]
1233                set dy [expr ($_click(y) - $y)/double($h)]
1234                set _click(x) $x
1235                set _click(y) $y
1236                if {[expr (abs($dx) > 0.0 || abs($dy) > 0.0)]} {
1237                    SendCmd "camera rotate $dx $dy"
1238                    #EventuallyRotate $dx $dy
1239                }
1240            }
1241        }
1242        "release" {
1243            Rotate drag $x $y
1244            $itk_component(view) configure -cursor ""
1245            catch {unset _click}
1246        }
1247        default {
1248            error "bad option \"$option\": should be click, drag, release"
1249        }
1250    }
1251}
1252
1253# ----------------------------------------------------------------------
1254# USAGE: $this Pan set x y
1255#        $this Pan click x y
1256#        $this Pan drag x y
1257#        $this Pan release x y
1258#
1259# Called automatically when the user clicks on one of the zoom
1260# controls for this widget.  Changes the zoom for the current view.
1261# ----------------------------------------------------------------------
1262itcl::body Rappture::MapViewer::Pan {option x y} {
1263    switch -- $option {
1264        "set" {
1265            set w [winfo width $itk_component(view)]
1266            set h [winfo height $itk_component(view)]
1267            set x [expr $x / double($w)]
1268            set y [expr $y / double($h)]
1269            if {[expr (abs($x) > 0.0 || abs($y) > 0.0)]} {
1270                SendCmd "camera pan $x $y"
1271            }
1272            return
1273        }
1274        "click" {
1275            set _click(x) $x
1276            set _click(y) $y
1277            $itk_component(view) configure -cursor hand1
1278        }
1279        "drag" {
1280            if { ![info exists _click(x)] } {
1281                set _click(x) $x
1282            }
1283            if { ![info exists _click(y)] } {
1284                set _click(y) $y
1285            }
1286            set w [winfo width $itk_component(view)]
1287            set h [winfo height $itk_component(view)]
1288            set dx [expr ($x - $_click(x))/double($w)]
1289            set dy [expr ($_click(y) - $y)/double($h)]
1290            set _click(x) $x
1291            set _click(y) $y
1292            if {[expr (abs($dx) > 0.0 || abs($dy) > 0.0)]} {
1293                SendCmd "camera pan $dx $dy"
1294            }
1295        }
1296        "release" {
1297            Pan drag $x $y
1298            $itk_component(view) configure -cursor ""
1299        }
1300        default {
1301            error "unknown option \"$option\": should set, click, drag, or release"
1302        }
1303    }
1304}
1305
1306# ----------------------------------------------------------------------
1307# USAGE: InitSettings <what> ?<value>?
1308#
1309# Used internally to update rendering settings whenever parameters
1310# change in the popup settings panel.  Sends the new settings off
1311# to the back end.
1312# ----------------------------------------------------------------------
1313itcl::body Rappture::MapViewer::InitSettings { args } {
1314    foreach setting $args {
1315        AdjustSetting $setting
1316    }
1317}
1318
1319#
1320# AdjustSetting --
1321#
1322#       Changes/updates a specific setting in the widget.  There are
1323#       usually user-setable option.  Commands are sent to the render
1324#       server.
1325#
1326itcl::body Rappture::MapViewer::AdjustSetting {what {value ""}} {
1327    if { ![isconnected] } {
1328        return
1329    }
1330    switch -- $what {
1331        "coords-visible" - "coords-precision" - "coords-units" {
1332            set bool $_settings(coords-visible)
1333            set units $_settings(coords-units)
1334            set precision $_settings(coords-precision)
1335            SendCmd "map posdisp $bool $units $precision"
1336        }
1337        "grid" - "grid-type" {
1338            set bool $_settings(grid)
1339            set gridType $_settings(grid-type)
1340            SendCmd "map grid $bool $gridType"
1341        }
1342        "camera-throw" {
1343            set bool $_settings($what)
1344            SendCmd "camera throw $bool"
1345        }
1346        "terrain-edges" {
1347            set bool $_settings($what)
1348            SendCmd "map terrain edges $bool"
1349        }
1350        "terrain-lighting" {
1351            set bool $_settings($what)
1352            SendCmd "map terrain lighting $bool"
1353        }
1354        "terrain-palette" {
1355            set cmap [$itk_component(terrainpalette) value]
1356            #SendCmd "map terrain colormap $cmap"
1357        }
1358        "terrain-vertscale" {
1359            set val $_settings($what)
1360            SendCmd "map terrain vertscale $val"
1361        }
1362        "terrain-wireframe" {
1363            set bool $_settings($what)
1364            SendCmd "map terrain wireframe $bool"
1365        }
1366        default {
1367            error "don't know how to fix $what"
1368        }
1369    }
1370}
1371
1372# ----------------------------------------------------------------------
1373# CONFIGURATION OPTION: -plotbackground
1374# ----------------------------------------------------------------------
1375itcl::configbody Rappture::MapViewer::plotbackground {
1376    if { [isconnected] } {
1377        foreach {r g b} [Color2RGB $itk_option(-plotbackground)] break
1378        SendCmd "screen bgcolor $r $g $b"
1379    }
1380}
1381
1382# ----------------------------------------------------------------------
1383# CONFIGURATION OPTION: -plotforeground
1384# ----------------------------------------------------------------------
1385itcl::configbody Rappture::MapViewer::plotforeground {
1386    if { [isconnected] } {
1387        foreach {r g b} [Color2RGB $itk_option(-plotforeground)] break
1388        #fix this!
1389        #SendCmd "color background $r $g $b"
1390    }
1391}
1392
1393itcl::body Rappture::MapViewer::limits { dataobj } {
1394    error "no limits"
1395}
1396
1397itcl::body Rappture::MapViewer::BuildTerrainTab {} {
1398
1399    set fg [option get $itk_component(hull) font Font]
1400    #set bfg [option get $itk_component(hull) boldFont Font]
1401
1402    set inner [$itk_component(main) insert end \
1403        -title "Terrain Settings" \
1404        -icon [Rappture::icon surface]]
1405    $inner configure -borderwidth 4
1406
1407    checkbutton $inner.posdisp \
1408        -text "Show Coordinate Readout" \
1409        -variable [itcl::scope _settings(coords-visible)] \
1410        -command [itcl::code $this AdjustSetting coords-visible] \
1411        -font "Arial 9" -anchor w
1412
1413    checkbutton $inner.grid \
1414        -text "Show Graticule" \
1415        -variable [itcl::scope _settings(grid)] \
1416        -command [itcl::code $this AdjustSetting grid] \
1417        -font "Arial 9" -anchor w
1418
1419    checkbutton $inner.wireframe \
1420        -text "Show Wireframe" \
1421        -variable [itcl::scope _settings(terrain-wireframe)] \
1422        -command [itcl::code $this AdjustSetting terrain-wireframe] \
1423        -font "Arial 9" -anchor w
1424
1425    checkbutton $inner.lighting \
1426        -text "Enable Lighting" \
1427        -variable [itcl::scope _settings(terrain-lighting)] \
1428        -command [itcl::code $this AdjustSetting terrain-lighting] \
1429        -font "Arial 9" -anchor w
1430
1431    checkbutton $inner.edges \
1432        -text "Show Edges" \
1433        -variable [itcl::scope _settings(terrain-edges)] \
1434        -command [itcl::code $this AdjustSetting terrain-edges] \
1435        -font "Arial 9" -anchor w
1436
1437    label $inner.palette_l -text "Palette" -font "Arial 9" -anchor w
1438    itk_component add terrainpalette {
1439        Rappture::Combobox $inner.palette -width 10 -editable no
1440    }
1441    $inner.palette choices insert end [GetColormapList]
1442
1443    $itk_component(terrainpalette) value "BCGYR"
1444    bind $inner.palette <<Value>> \
1445        [itcl::code $this AdjustSetting terrain-palette]
1446
1447    label $inner.vscale_l -text "Vertical Scale" -font "Arial 9" -anchor w
1448    ::scale $inner.vscale -from 0 -to 10 -orient horizontal \
1449        -variable [itcl::scope _settings(terrain-vertscale)] \
1450        -width 10 \
1451        -resolution 0.1 \
1452        -showvalue on \
1453        -command [itcl::code $this AdjustSetting terrain-vertscale]
1454    $inner.vscale set $_settings(terrain-vertscale)
1455
1456    blt::table $inner \
1457        0,0 $inner.posdisp   -cspan 2  -anchor w -pady 2 \
1458        1,0 $inner.grid      -cspan 2  -anchor w -pady 2 \
1459        2,0 $inner.wireframe -cspan 2  -anchor w -pady 2 \
1460        3,0 $inner.lighting  -cspan 2  -anchor w -pady 2 \
1461        4,0 $inner.edges     -cspan 2  -anchor w -pady 2 \
1462        5,0 $inner.vscale_l  -anchor w -pady 2 \
1463        5,1 $inner.vscale    -fill x   -pady 2 \
1464        6,0 $inner.palette_l -anchor w -pady 2 \
1465        6,1 $inner.palette   -fill x   -pady 2 
1466
1467    blt::table configure $inner r* c* -resize none
1468    blt::table configure $inner r8 c1 -resize expand
1469}
1470
1471itcl::body Rappture::MapViewer::BuildLayerTab {} {
1472
1473    set fg [option get $itk_component(hull) font Font]
1474    #set bfg [option get $itk_component(hull) boldFont Font]
1475
1476    set inner [$itk_component(main) insert end \
1477        -title "Layers" \
1478        -icon [Rappture::icon layers]]
1479    $inner configure -borderwidth 4
1480    set f [frame $inner.layers]
1481    blt::table $inner \
1482        0,0 $f -fill both
1483    set _layersFrame $inner
1484}
1485
1486itcl::body Rappture::MapViewer::BuildCameraTab {} {
1487    set inner [$itk_component(main) insert end \
1488        -title "Camera Settings" \
1489        -icon [Rappture::icon camera]]
1490    $inner configure -borderwidth 4
1491
1492    set row 0
1493
1494    set labels { x y z heading pitch distance }
1495    foreach tag $labels {
1496        label $inner.${tag}label -text $tag -font "Arial 9"
1497        entry $inner.${tag} -font "Arial 9"  -bg white \
1498            -textvariable [itcl::scope _view($tag)]
1499        bind $inner.${tag} <KeyPress-Return> \
1500            [itcl::code $this camera set ${tag}]
1501        bind $inner.${tag} <KP_Enter> \
1502            [itcl::code $this camera set ${tag}]
1503        blt::table $inner \
1504            $row,0 $inner.${tag}label -anchor e -pady 2 \
1505            $row,1 $inner.${tag} -anchor w -pady 2
1506        blt::table configure $inner r$row -resize none
1507        incr row
1508    }
1509    set labels { srs verticalDatum }
1510    foreach tag $labels {
1511        label $inner.${tag}label -text $tag -font "Arial 9"
1512        entry $inner.${tag} -font "Arial 9"  -bg white \
1513            -textvariable [itcl::scope _view($tag)]
1514        bind $inner.${tag} <KeyPress-Return> \
1515            [itcl::code $this camera set ${tag}]
1516        bind $inner.${tag} <KP_Enter> \
1517            [itcl::code $this camera set ${tag}]
1518        blt::table $inner \
1519            $row,0 $inner.${tag}label -anchor e -pady 2 \
1520            $row,1 $inner.${tag} -anchor w -pady 2
1521        blt::table configure $inner r$row -resize none
1522        incr row
1523    }
1524
1525    if {0} {
1526    button $inner.get \
1527        -text "Get Camera Settings" \
1528        -font "Arial 9" \
1529        -command [itcl::code $this SendCmd "camera get"]
1530    blt::table $inner \
1531        $row,0 $inner.get -anchor w -pady 2 -cspan 2
1532    blt::table configure $inner r$row -resize none
1533    incr row
1534
1535    button $inner.set \
1536        -text "Apply Camera Settings" \
1537        -font "Arial 9" \
1538        -command [itcl::code $this camera set all]
1539    blt::table $inner \
1540        $row,0 $inner.set -anchor w -pady 2 -cspan 2
1541    blt::table configure $inner r$row -resize none
1542    incr row
1543    }
1544
1545    if {$_useServerManip} {
1546        checkbutton $inner.throw \
1547            -text "Enable Throw" \
1548            -font "Arial 9" \
1549            -variable [itcl::scope _settings(camera-throw)] \
1550            -command [itcl::code $this AdjustSetting camera-throw]
1551        blt::table $inner \
1552            $row,0 $inner.throw -anchor w -pady 2 -cspan 2
1553        blt::table configure $inner r$row -resize none
1554        incr row
1555    }
1556
1557    blt::table configure $inner c* r* -resize none
1558    blt::table configure $inner c2 -resize expand
1559    blt::table configure $inner r$row -resize expand
1560}
1561
1562#
1563#  camera --
1564#
1565# USAGE: camera get
1566#        This is called by the server to transfer the
1567#        current Viewpoint settings
1568# USAGE: camera reset
1569#        Reset the camera to the default view
1570#
1571itcl::body Rappture::MapViewer::camera {option args} {
1572    switch -- $option {
1573        "get" {
1574            # We got the camera settings from the server
1575            foreach name {x y z heading pitch distance srs verticalDatum} value $args {
1576                set _view($name) $value
1577            }
1578            puts stderr "view: $_view(x), $_view(y), $_view(z), $_view(heading), $_view(pitch), $_view(distance), $_view(srs), $_view(verticalDatum)"
1579        }
1580        "go" {
1581            SendCmd "camera go $args"
1582        }
1583        "reset" {
1584            array set _view {
1585                x               0.0
1586                y               0.0
1587                z               0.0
1588                heading         0.0
1589                pitch           -89.9
1590                distance        1.0
1591                srs             ""
1592                verticalDatum   ""
1593            }
1594            if { [info exists _mapsettings(camera)] } {
1595                # Check if the tool specified a default
1596                set location $_mapsettings(camera)
1597                if { $location != "" } {
1598                    array set _view $location
1599                    set duration 0.0
1600                    SendCmd [list camera set $_view(x) $_view(y) $_view(z) $_view(heading) $_view(pitch) $_view(distance) $duration $_view(srs) $_view(verticalDatum)]
1601                } else {
1602                    SendCmd "camera reset"
1603                    # Retrieve the settings
1604                    #SendCmd "camera get"
1605                }
1606            } else {
1607                SendCmd "camera reset"
1608                # Retrieve the settings
1609               # SendCmd "camera get"
1610            }
1611        }
1612        "set" {
1613            set who [lindex $args 0]
1614            if {$who != "all" && $who != "srs" && $who != "verticalDatum"} {
1615                set val $_view($who)
1616                set code [catch { string is double $val } result]
1617                if { $code != 0 || !$result } {
1618                    return
1619                }
1620            }
1621            switch -- $who {
1622                "distance" {
1623                    SendCmd [list camera dist $_view(distance)]
1624                }
1625                "all" - "x" - "y" - "z" - "heading" - "pitch" - "srs" - "verticalDatum" {
1626                    set duration 0.0
1627                    SendCmd [list camera set $_view(x) $_view(y) $_view(z) $_view(heading) $_view(pitch) $_view(distance) $duration $_view(srs) $_view(verticalDatum)]
1628                }
1629            }
1630        }
1631    }
1632}
1633
1634itcl::body Rappture::MapViewer::GetImage { args } {
1635    if { [image width $_image(download)] > 0 &&
1636         [image height $_image(download)] > 0 } {
1637        set bytes [$_image(download) data -format "jpeg -quality 100"]
1638        set bytes [Rappture::encoding::decode -as b64 $bytes]
1639        return [list .jpg $bytes]
1640    }
1641    return ""
1642}
1643
1644itcl::body Rappture::MapViewer::BuildDownloadPopup { popup command } {
1645    Rappture::Balloon $popup \
1646        -title "[Rappture::filexfer::label downloadWord] as..."
1647    set inner [$popup component inner]
1648    label $inner.summary -text "" -anchor w
1649
1650    radiobutton $inner.image_button -text "Image File" \
1651        -variable [itcl::scope _downloadPopup(format)] \
1652        -value image
1653    Rappture::Tooltip::for $inner.image_button \
1654        "Save as digital image."
1655
1656    button $inner.ok -text "Save" \
1657        -highlightthickness 0 -pady 2 -padx 3 \
1658        -command $command \
1659        -compound left \
1660        -image [Rappture::icon download]
1661
1662    button $inner.cancel -text "Cancel" \
1663        -highlightthickness 0 -pady 2 -padx 3 \
1664        -command [list $popup deactivate] \
1665        -compound left \
1666        -image [Rappture::icon cancel]
1667
1668    blt::table $inner \
1669        0,0 $inner.summary -cspan 2  \
1670        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
1671        4,1 $inner.cancel -width .9i -fill y \
1672        4,0 $inner.ok -padx 2 -width .9i -fill y
1673    blt::table configure $inner r3 -height 4
1674    blt::table configure $inner r4 -pady 4
1675    raise $inner.image_button
1676    $inner.image_button invoke
1677    return $inner
1678}
1679
1680itcl::body Rappture::MapViewer::SetTerrainStyle { style } {
1681    array set settings {
1682        -color white
1683        -edgecolor black
1684        -edges 0
1685        -lighting 0
1686        -linewidth 1.0
1687        -vertscale 1.0
1688        -wireframe 0
1689    }
1690    array set settings $style
1691
1692    SendCmd "map terrain edges $settings(-edges)"
1693    set _settings(terrain-edges) $settings(-edges)
1694    #SendCmd "map terrain color [Color2RGB $settings(-color)]"
1695    #SendCmd "map terrain colormode constant"
1696    SendCmd "map terrain lighting $settings(-lighting)"
1697    set _settings(terrain-lighting) $settings(-lighting)
1698    SendCmd "map terrain linecolor [Color2RGB $settings(-edgecolor)]"
1699    #SendCmd "map terrain linewidth $settings(-linewidth)"
1700    SendCmd "map terrain vertscale $settings(-vertscale)"
1701    set _settings(terrain-vertscale) $settings(-vertscale)
1702    SendCmd "map terrain wireframe $settings(-wireframe)"
1703    set _settings(terrain-wireframe) $settings(-wireframe)
1704}
1705
1706itcl::body Rappture::MapViewer::SetLayerStyle { dataobj layer } {
1707    array set info [$dataobj layer $layer]
1708    set _visibility($layer) 1
1709
1710    switch -- $info(type) {
1711        "image" {
1712            array set settings {
1713                -min_level 0
1714                -max_level 23
1715                -opacity 1.0
1716            }
1717            if { [info exists info(style)] } {
1718                array set settings $info(style)
1719            }
1720            if { [info exists info(opacity)] } {
1721                set settings(-opacity) $info(opacity)
1722            }
1723            if {!$_sendEarthFile} {
1724                switch -- $info(driver)  {
1725                    "debug" {
1726                        SendCmd [list map layer add image debug $layer]
1727                    }
1728                    "gdal" {
1729                        SendCmd [list map layer add image gdal \
1730                                     $info(gdal.url) $layer]
1731                    }
1732                    "tms" {
1733                        SendCmd [list map layer add image tms \
1734                                     $info(tms.url) $layer]
1735                    }
1736                    "wms" {
1737                        SendCmd [list map layer add image wms \
1738                                     $info(wms.url) \
1739                                     $info(wms.layers) \
1740                                     $info(wms.format) \
1741                                     $info(wms.transparent) \
1742                                     $layer]
1743                    }
1744                    "xyz" {
1745                        SendCmd [list map layer add image xyz \
1746                                     $info(xyz.url) \
1747                                     $layer]
1748                    }
1749                }                       
1750            }
1751            SendCmd "map layer opacity $settings(-opacity) $layer"
1752        }
1753        "elevation" {
1754            array set settings {
1755                -min_level 0
1756                -max_level 23
1757            }
1758            if { [info exists info(style)] } {
1759                array set settings $info(style)
1760            }
1761            if {!$_sendEarthFile} {
1762                switch -- $info(driver)  {
1763                    "gdal" {
1764                        SendCmd [list map layer add elevation gdal \
1765                                     $info(gdal.url) $layer]
1766                    }
1767                    "tms" {
1768                        SendCmd [list map layer add elevation tms \
1769                                     $info(tms.url) $layer]
1770                    }
1771                }
1772            }
1773        }
1774        "line" {
1775            array set settings {
1776                -color black
1777                -minbias 1000
1778                -opacity 1.0
1779                -width 1
1780            }
1781            if { [info exists info(style)] } {
1782                array set settings $info(style)
1783            }
1784            if { [info exists info(opacity)] } {
1785                set settings(-opacity) $info(opacity)
1786            }
1787            SendCmd [list map layer add line $info(ogr.url) $layer]
1788            SendCmd "map layer opacity $settings(-opacity) $layer"
1789        }
1790        "polygon" {
1791            array set settings {
1792                -color white
1793                -minbias 1000
1794                -opacity 1.0
1795            }
1796            if { [info exists info(style)] } {
1797                array set settings $info(style)
1798            }
1799            if { [info exists info(opacity)] } {
1800                set settings(-opacity) $info(opacity)
1801            }
1802            SendCmd [list map layer add polygon $info(ogr.url) $layer]
1803            SendCmd "map layer opacity $settings(-opacity) $layer"
1804        }
1805        "label" {
1806            array set settings {
1807                -align "center-center"
1808                -color black
1809                -declutter 1
1810                -font Arial
1811                -fontsize 16.0
1812                -halocolor white
1813                -halowidth 2.0
1814                -layout "ltr"
1815                -minbias 1000
1816                -opacity 1.0
1817                -removedupe 1
1818            }
1819            if { [info exists info(style)] } {
1820                array set settings $info(style)
1821            }
1822            if { [info exists info(opacity)] } {
1823                set settings(-opacity) $info(opacity)
1824            }
1825            set contentExpr $info(content)
1826            if {[info exists info(priority)]} {
1827                set priorityExpr $info(priority)
1828            } else {
1829                set priorityExpr ""
1830            }
1831            SendCmd [list map layer add text $info(ogr.url) $contentExpr $priorityExpr $layer]
1832            SendCmd "map layer opacity $settings(-opacity) $layer"
1833        }
1834    }
1835
1836    if { [info exists info(visible)] } {
1837        if { !$info(visible) } {
1838            set _visibility($layer) 0
1839            SendCmd "map layer visible 0 $layer"
1840        }
1841    }
1842}
1843
1844itcl::body Rappture::MapViewer::SetOpacity { dataset } {
1845    foreach {dataobj layer} [split $dataset -] break
1846    set type [$dataobj type $layer]
1847    set val $_settings(-opacity)
1848    set sval [expr { 0.01 * double($val) }]
1849    if { !$_obj2ovride($dataobj-raise) } {
1850        # This is wrong.  Need to figure out why raise isn't set with 1
1851        #set sval [expr $sval * .6]
1852    }
1853    SendCmd "$type opacity $sval $dataset"
1854}
1855
1856itcl::body Rappture::MapViewer::ChangeLayerVisibility { dataobj layer } {
1857    set bool $_visibility($layer)
1858    SendCmd "map layer visible $bool $layer"
1859}
1860
1861itcl::body Rappture::MapViewer::UpdateLayerControls {} {
1862    set row 0
1863    set inner $_layersFrame
1864    if { [winfo exists $inner.layers] } {
1865        foreach w [winfo children $inner.layers] {
1866            destroy $w
1867        }
1868    }
1869    set f $inner.layers
1870    foreach dataobj [get -objects] {
1871        foreach layer [$dataobj layers] {
1872            array unset info
1873            array set info [$dataobj layer $layer]
1874            checkbutton $f.$layer \
1875                -text $info(label) \
1876                -variable [itcl::scope _visibility($layer)] \
1877                -command [itcl::code $this \
1878                              ChangeLayerVisibility $dataobj $layer] \
1879                    -font "Arial 9" -anchor w
1880            blt::table $f $row,0 $f.$layer -anchor w -pady 2
1881            Rappture::Tooltip::for $f.$layer $info(description)
1882            incr row
1883        }
1884    }
1885    if { $row > 0 } {
1886        blt::table configure $f r* c* -resize none
1887        blt::table configure $f r$row c1 -resize expand
1888    }
1889}
1890
1891#
1892# Generate an OSG Earth file to send to server.  This is inteneded
1893# as a stopgap and testing tool until the protocol is fleshed out.
1894#
1895# Note that the lighting settings are required to be "hard-coded"
1896# as below for the runtime control to work.  Don't make those user
1897# configurable.
1898#
1899# Also note: Use "true"/"false" for boolean settings.  Not sure if
1900# the parser in OSG Earth accepts all of Tcl's forms of boolean vals.
1901#
1902itcl::body Rappture::MapViewer::EarthFile {} {
1903    append out "<map"
1904    append out " name=\"$_mapsettings(label)\""
1905    append out " type=\"$_mapsettings(type)\""
1906    append out " version=\"2\""
1907    append out ">\n"
1908    append out " <options lighting=\"true\">\n"
1909    # FIXME: convert color setting to hex
1910    # array set style $_mapsettings(style)
1911    # if {[info exists style(-color)]} {
1912    #     set color "?"
1913    # }
1914    set color "#ffffffff"
1915    append out "  <terrain lighting=\"false\" color=\"$color\"/>\n"
1916    if { [info exists _mapsettings(projection)] } {
1917        append out "  <profile"
1918        append out " srs=\"$_mapsettings(projection)\""
1919        if { [info exists _mapsettings(extents)] } {
1920            append out " xmin=\"$_mapsettings(x1)\""
1921            append out " ymin=\"$_mapsettings(y1)\""
1922            append out " xmax=\"$_mapsettings(x2)\""
1923            append out " ymax=\"$_mapsettings(y2)\""
1924        }
1925        append out "/>\n"
1926    }
1927    append out " </options>\n"
1928
1929    foreach dataobj [get -objects] {
1930        foreach layer [$dataobj layers] {
1931            set _layers($layer) 1
1932            array unset info
1933            array set info [$dataobj layer $layer]
1934            switch -- $info(type) {
1935                "image" {
1936                    append out " <image"
1937                    append out " name=\"$layer\""
1938                    append out " driver=\"gdal\""
1939                    if { [info exists info(opacity)] } {
1940                        append out " opacity=\"$info(opacity)\""
1941                    }
1942                    if { $info(visible) } {
1943                        append out " visible=\"true\""
1944                    } else {
1945                        append out " visible=\"false\""
1946                    }
1947                    append out ">\n"
1948                    append out "  <url>$info(url)</url>\n"
1949                    append out " </image>\n"
1950                }
1951                "elevation" {
1952                    append out " <elevation"
1953                    append out " name=\"$layer\""
1954                    append out " driver=\"gdal\""
1955                    if { $info(visible) } {
1956                        append out " visible=\"true\""
1957                    } else {
1958                        append out " visible=\"false\""
1959                    }
1960                    append out ">\n"
1961                    append out "  <url>$info(url)</url>\n"
1962                    append out " </elevation>\n"
1963                }
1964                default {
1965                    puts stderr "Type $info(type) not implemented in MapViewer::EarthFile"
1966                }
1967            }
1968        }
1969    }
1970    append out "</map>\n"
1971    return $out
1972}
Note: See TracBrowser for help on using the repository browser.