source: trunk/gui/scripts/xyprint.tcl @ 3093

Last change on this file since 3093 was 2918, checked in by gah, 13 years ago

normalize filename before giving to filexfer

File size: 53.1 KB
Line 
1
2# ----------------------------------------------------------------------
3#  COMPONENT: xyprint - Print X-Y plot.
4#
5# ======================================================================
6#  AUTHOR:  Michael McLennan, Purdue University
7#  Copyright (c) 2004-2005  Purdue Research Foundation
8#
9#  See the file "license.terms" for information on usage and
10#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11# ======================================================================
12package require Itk
13package require BLT
14
15#option add *XyPrint.width 3i widgetDefault
16#option add *XyPrint.height 3i widgetDefault
17option add *XyPrint.gridColor #d9d9d9 widgetDefault
18option add *XyPrint.activeColor blue widgetDefault
19option add *XyPrint.dimColor gray widgetDefault
20option add *XyPrint.controlBackground gray widgetDefault
21option add *XyPrint*Font "Arial 9" widgetDefault
22option add *XyPrint*Entry*width "6" widgetDefault
23option add *XyPrint*Entry*background "white" widgetDefault
24
25itcl::class Rappture::XyPrint {
26    inherit itk::Widget
27
28    constructor {args} {}
29    destructor {}
30
31    private variable _graph "";         # Original graph.
32    private variable _clone "";         # Cloned graph.
33    private variable _preview "";       # Preview image.
34    private variable _savedSettings;    # Array of settings.
35
36    private common _oldSettingsFile "~/.rpsettings"
37    private common _settingsFile "~/.rp_settings"
38
39    public method print { graph toolName plotName }
40    public method reset {}
41
42    private method CopyOptions { cmd orig clone {exclude {}}}
43    private method CloneGraph { orig }
44
45    private method BuildGeneralTab {}
46    private method BuildLayoutTab {}
47    private method BuildLegendTab {}
48    private method BuildAxisTab {}
49    private method SetOption { opt }
50    private method SetComponentOption { comp opt }
51    private method SetNamedComponentOption { comp name opt }
52    private method GetAxis {}
53    private method GetElement { args }
54    private method RegeneratePreview {}
55    private method InitClone {}
56    private method Pixels2Inches { pixels }
57    private method Inches2Pixels { inches {defValue ""}}
58    private method Color2RGB { color }
59
60    private method ApplyGeneralSettings {}
61    private method ApplyLegendSettings {}
62    private method ApplyAxisSettings {}
63    private method ApplyElementSettings {}
64    private method ApplyLayoutSettings {}
65    private method InitializeSettings {}
66    private method CreateSettings { toolName plotName }
67    private method RestoreSettings { toolName plotName }
68    private method SaveSettings { toolName plotName }
69    private method DestroySettings {}
70    private method ResetSettings { }
71    private method GetOutput {}
72    private method SetWaitVariable { state }
73    private method SetLayoutOption { option }
74    private method GetAxisType { axis }
75    private method restore { toolName plotName data }
76
77    # Same dialog may be used for different graphs
78    private common _settings
79    private common _wait
80}
81
82# ----------------------------------------------------------------------
83# CONSTRUCTOR
84# ----------------------------------------------------------------------
85itcl::body Rappture::XyPrint::constructor { args } {
86    Rappture::dispatcher _dispatcher
87    $_dispatcher register !rebuild
88    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
89   
90    set w [winfo pixels . 2.5i]
91    set h [winfo pixels . 2i]
92    set _preview [image create photo -width $w -height $h]
93    itk_component add tabs {
94        blt::tabset $itk_interior.tabs \
95            -highlightthickness 0 -tearoff 0 -side top \
96            -bd 0 -gap 0 -tabborderwidth 1 \
97            -outerpad 1 -background grey
98    } {
99        keep -cursor
100        ignore -highlightthickness -borderwidth -background
101    }
102    set inner [frame $itk_interior.frame -bg grey]
103    itk_component add preview {
104        label $inner.preview \
105            -highlightthickness 0 -bd 0 -image $_preview -width 2.5i \
106                -height 2.5i -background grey
107    } {
108        ignore -background
109    }
110    itk_component add ok {
111        button $itk_interior.ok -text "Save" \
112            -highlightthickness 0 -pady 2 -padx 0 \
113            -command [itcl::code $this SetWaitVariable 1] \
114            -compound left \
115            -image [Rappture::icon download]
116    }
117    itk_component add cancel {
118        button $itk_interior.cancel -text "Cancel" \
119            -highlightthickness 0 -pady 2 -padx 0 \
120            -command [itcl::code $this SetWaitVariable 0] \
121            -compound left \
122            -image [Rappture::icon cancel]
123    }
124    blt::table $itk_interior \
125        0,0 $inner -fill both \
126        0,1 $itk_component(tabs) -fill both -cspan 2 \
127        1,2 $itk_component(cancel) -padx 2 -pady 2 -width .9i -fill y \
128        1,1 $itk_component(ok) -padx 2 -pady 2 -width .9i -fill y
129    blt::table $inner \
130        0,0 $itk_component(preview) -fill both -padx 10 -pady 10
131
132    #blt::table configure $itk_interior c1 c2 -resize none
133    blt::table configure $itk_interior c0 -resize both
134    BuildGeneralTab
135    BuildAxisTab
136    BuildLegendTab
137    BuildLayoutTab
138    eval itk_initialize $args
139}
140
141# ----------------------------------------------------------------------
142# DESTRUCTOR
143# ----------------------------------------------------------------------
144itcl::body Rappture::XyPrint::destructor {} {
145    destroy $_clone
146    image delete $_preview
147    array unset _settings $this-*
148}
149
150itcl::body Rappture::XyPrint::DestroySettings {} {
151    destroy $_clone
152    set _clone ""
153    set _graph ""
154}
155
156itcl::body Rappture::XyPrint::reset {} {
157    SetWaitVariable 0
158}
159
160itcl::body Rappture::XyPrint::SetWaitVariable { state } {
161    set _wait($this) $state
162}
163
164itcl::body Rappture::XyPrint::print { graph toolName plotName } {
165    set _graph $graph
166    CloneGraph $graph
167    InitClone
168    RestoreSettings $toolName $plotName
169    InitializeSettings
170    SetWaitVariable 0
171    tkwait variable [itcl::scope _wait($this)]
172    SaveSettings $toolName $plotName
173    set output ""
174    if { $_wait($this) } {
175        set output [GetOutput]
176    }
177    DestroySettings
178    return $output
179}
180
181itcl::body Rappture::XyPrint::GetOutput {} {
182    # Get the selected format of the download.
183    set page $itk_component(graph_page)
184    set format [$page.format current]
185
186    if { $format == "jpg" } {
187        set img [image create photo]
188        $_clone snap $img
189        set bytes [$img data -format "jpeg -quality 100"]
190        set bytes [Rappture::encoding::decode -as b64 $bytes]
191        image delete $img
192        return [list .jpg $bytes]
193    } elseif { $format == "png" } {
194        set img [image create photo]
195        $_clone snap $img
196        set bytes [$img data -format "png"]
197        set bytes [Rappture::encoding::decode -as b64 $bytes]
198        image delete $img
199        return [list .png $bytes]
200    }
201
202    # Handle encapsulated postscript or portable document format.
203
204    # Append an "i" for the graph postscript component.
205    set w $_settings($this-layout-width)i
206    set h $_settings($this-layout-height)i
207
208    $_clone postscript configure \
209        -maxpect false \
210        -decoration yes \
211        -center yes \
212        -width $w -height $h
213   
214    set psdata [$_clone postscript output]
215
216    if 0 {
217        set f [open "junk.raw" "w"]
218        puts -nonewline $f $psdata
219        close $f
220    }
221    if { $format == "eps" } {
222        return [list .$format $psdata]
223    }
224
225    set cmd ""
226    # | eps2eps << $psdata
227    lappend cmd "|" "/usr/bin/gs" \
228        "-q" "-sDEVICE=epswrite" "-sstdout=%stderr" \
229        "-sOutputFile=-" "-dNOPAUSE" "-dBATCH" "-dSAFER" \
230        "-dDEVICEWIDTH=250000" "-dDEVICEHEIGHT=250000" "-" "<<" "$psdata"
231    if { $format == "pdf" } {
232        # | eps2eps << $psdata | ps2pdf
233        lappend cmd "|" "/usr/bin/gs" \
234            "-q" "-sDEVICE=pdfwrite" "-sstdout=%stderr" \
235            "-sOutputFile=-" "-dNOPAUSE" "-dBATCH" "-dSAFER" \
236            "-dCompatibilityLevel=1.4" "-c" ".setpdfwrite" "-f" "-"
237    }
238    if { [catch {
239        set f [open $cmd "r"]
240        fconfigure $f -translation binary -encoding binary
241        set output [read $f]
242        close $f
243    } err ] != 0 } {
244        global errorInfo
245        puts stderr "failed to generate file: $err\n$errorInfo"
246        return ""
247    }
248    if 0 {
249        set f [open "junk.$format" "w"]
250        fconfigure $f -translation binary -encoding binary
251        puts -nonewline $f $output
252        close $f
253    }
254    return [list .$format $output]
255}
256
257itcl::body Rappture::XyPrint::CopyOptions { cmd orig clone {exclude {}} } {
258    set all [eval $orig $cmd]
259    set configLine $clone
260    foreach name $exclude {
261        set ignore($name) 1
262    }
263    foreach arg $cmd {
264        lappend configLine $arg
265    }
266    foreach option $all {
267        if { [llength $option] != 5 } {
268            continue
269        }
270        set switch [lindex $option 0]
271        set initial [lindex $option 3]
272        set current [lindex $option 4]
273        if { [info exists ignore($switch)] } {
274            continue
275        }
276        if { [string compare $initial $current] == 0 } {
277            continue
278        }
279        lappend configLine $switch $current
280    }
281    eval $configLine
282}
283
284itcl::body Rappture::XyPrint::CloneGraph { orig } {
285    set top $itk_interior
286    if { [winfo exists $top.graph] } {
287        destroy $top.graph
288    }
289    set _clone [blt::graph $top.graph]
290    CopyOptions "configure" $orig $_clone
291    # Axis component
292    foreach axis [$orig axis names] {
293        if { $axis == "z" } {
294            continue
295        }
296        if { [$orig axis cget $axis -hide] } {
297            continue
298        }
299        if { [$_clone axis name $axis] == "" } {
300            $_clone axis create $axis
301        }
302        CopyOptions [list axis configure $axis] $orig $_clone
303    }
304    foreach axis { x y x2 y2 } {
305        $_clone ${axis}axis use [$orig ${axis}axis use]
306    }
307    # Pen component
308    foreach pen [$orig pen names] {
309        if { [$_clone pen name $pen] == "" } {
310            $_clone pen create $pen
311        }
312        CopyOptions [list pen configure $pen] $orig $_clone
313    }
314    # Marker component
315    foreach marker [$orig marker names] {
316        $_clone marker create [$orig marker type $marker] -name $marker
317        CopyOptions [list marker configure $marker] $orig $_clone -name
318    }
319    # Element component
320    foreach elem [$orig element names] {
321        set oper [$orig element type $elem]
322        $_clone $oper create $elem
323        CopyOptions [list $oper configure $elem] $orig $_clone -data
324        if { [$_clone $oper cget $elem -hide] } {
325            $_clone $oper configure $elem -label ""
326        }
327    }
328    # Fix element display list
329    $_clone element show [$orig element show]
330    # Legend component
331    CopyOptions {legend configure} $orig $_clone
332    # Postscript component
333    CopyOptions {postscript configure} $orig $_clone
334    # Crosshairs component
335    CopyOptions {crosshairs configure} $orig $_clone
336    # Grid component
337    CopyOptions {grid configure} $orig $_clone
338
339    # Create markers representing lines at zero for the x and y axis.
340    $_clone marker create line -name x-zero \
341        -coords "0 -Inf 0 Inf" -dashes 1 -hide yes
342    $_clone marker create line -name y-zero \
343        -coords "-Inf 0 Inf 0" -dashes 1 -hide yes
344}
345
346itcl::body Rappture::XyPrint::InitClone {} {
347   
348    $_clone configure -width 3.4i -height 3.4i -background white \
349        -borderwidth 0 \
350        -leftmargin 0 \
351        -rightmargin 0 \
352        -topmargin 0 \
353        -bottommargin 0
354   
355    # Kill the title and create a border around the plot
356    $_clone configure \
357        -title "" \
358        -plotborderwidth 1 -plotrelief solid  \
359        -plotbackground white -plotpadx 0 -plotpady 0
360
361    set _settings($this-layout-width) [Pixels2Inches [$_clone cget -width]]
362    set _settings($this-layout-height) [Pixels2Inches [$_clone cget -height]]
363
364    set _settings($this-legend-fontfamily) helvetica
365    set _settings($this-legend-fontsize)   10
366    set _settings($this-legend-fontweight) normal
367    set _settings($this-legend-fontslant)  roman
368    set font "helvetica 10 normal roman"
369    $_clone legend configure \
370        -position right \
371        -font $font \
372        -hide yes -borderwidth 0 -background white -relief solid \
373        -anchor nw -activeborderwidth 0
374    #
375    set _settings($this-axis-ticks-fontfamily) helvetica
376    set _settings($this-axis-ticks-fontsize)   10
377    set _settings($this-axis-ticks-fontweight) normal
378    set _settings($this-axis-ticks-fontslant)  roman
379    set _settings($this-axis-title-fontfamily) helvetica
380    set _settings($this-axis-title-fontsize)   10
381    set _settings($this-axis-title-fontweight) normal
382    set _settings($this-axis-title-fontslant)  roman
383    foreach axis [$_clone axis names] {
384        if { $axis == "z" } {
385            continue
386        }
387        if { [$_clone axis cget $axis -hide] } {
388            continue
389        }
390        set _settings($this-$axis-ticks-fontfamily) helvetica
391        set _settings($this-$axis-ticks-fontsize)   10
392        set _settings($this-$axis-ticks-fontweight) normal
393        set _settings($this-$axis-ticks-fontslant)  roman
394        set _settings($this-$axis-title-fontfamily) helvetica
395        set _settings($this-$axis-title-fontsize)   10
396        set _settings($this-$axis-title-fontweight) normal
397        set _settings($this-$axis-title-fontslant)  roman
398        set tickfont "helvetica 10 normal roman"
399        set titlefont "helvetica 10 normal roman"
400        $_clone axis configure $axis -ticklength 5  \
401            -majorticks {} -minorticks {}
402        $_clone axis configure $axis \
403            -tickfont $tickfont \
404            -titlefont $titlefont
405    }
406    set count 0
407    foreach elem [$_clone element names] {
408        if { [$_clone element type $elem] == "bar" } {
409            continue
410        }
411        incr count
412        if { [$_clone element cget $elem -linewidth] > 1 } {
413            $_clone element configure $elem -linewidth 1 -pixels 3
414        }
415    }
416    if { $count == 0 } {
417        # There are no "line" elements in the graph.
418        # Remove the symbol and dashes controls.
419        set page $itk_component(legend_page)
420        # May have already been forgotten.
421        catch {
422            blt::table forget $page.symbol_l
423            blt::table forget $page.symbol
424            blt::table forget $page.dashes_l
425            blt::table forget $page.dashes
426        }
427    }
428}
429
430itcl::body Rappture::XyPrint::SetOption { opt } {
431    set new $_settings($this$opt)
432    set old [$_clone cget $opt]
433    set code [catch [list $_clone configure $opt $new] err]
434    if { $code != 0 } {
435        bell
436        global errorInfo
437        puts stderr "$err: $errorInfo"
438        set _settings($this$opt) $old
439        $_clone configure $opt $old
440    }
441}
442
443itcl::body Rappture::XyPrint::SetComponentOption { comp opt } {
444    set new $_settings($this-$comp$opt)
445    set old [$_clone $comp cget $opt]
446    set code [catch [list $_clone $comp configure $opt $new] err]
447    if { $code != 0 } {
448        bell
449        global errorInfo
450        puts stderr "$err: $errorInfo"
451        set _settings($this-$comp$opt) $old
452        $_clone $comp configure $opt $old
453    }
454}
455
456itcl::body Rappture::XyPrint::SetNamedComponentOption { comp name opt } {
457    set new $_settings($this-$comp$opt)
458    set old [$_clone $comp cget $name $opt]
459    set code [catch [list $_clone $comp configure $name $opt $new] err]
460    if { $code != 0 } {
461        bell
462        global errorInfo
463        puts stderr "$err: $errorInfo"
464        set _settings($this-$comp$opt) $old
465        $_clone $comp configure $name $opt $old
466    }
467}
468
469itcl::body Rappture::XyPrint::RegeneratePreview {} {
470    update
471    set img [image create photo]
472    set w [Inches2Pixels $_settings($this-layout-width) 3.4]
473    set h [Inches2Pixels $_settings($this-layout-height) 3.4]
474    $_clone snap $img -width $w -height $h
475
476    set pixelsPerInch [winfo pixels . 1i]
477    set cw [winfo width $itk_component(preview)]
478    set ch [winfo height $itk_component(preview)]
479    set rw [expr 2.5*$pixelsPerInch]
480    set rh [expr 2.5*$pixelsPerInch]
481    set maxwidth $rw
482    set maxheight $rh
483    if { $maxwidth > $cw } {
484        set maxwidth $cw
485    }
486    if { $maxheight > $ch } {
487        set maxheight $ch
488    }
489    set sx [expr double($maxwidth)/$w]
490    set sy [expr double($maxheight)/$h]
491    set s [expr min($sx,$sy)]
492
493    set pw [expr int(round($s * $w))]
494    set ph [expr int(round($s * $h))]
495    $_preview configure -width $pw -height $ph
496    blt::winop resample $img $_preview box
497    image delete $img
498}
499
500itcl::body Rappture::XyPrint::Pixels2Inches { pixels } {
501    if { [llength $pixels] == 2 } {
502        set pixels [lindex $pixels 0]
503    }
504    set pixelsPerInch [winfo pixels . 1i]
505    set inches [expr { double($pixels) / $pixelsPerInch }]
506    return [format %.3g ${inches}]
507}
508
509itcl::body Rappture::XyPrint::Inches2Pixels { inches {defValue ""}} {
510    set n [scan $inches %g dummy]
511    if { $n != 1  && $defValue != "" } {
512        set inches $defValue
513    }
514    return  [winfo pixels . ${inches}i]
515}
516
517itcl::body Rappture::XyPrint::Color2RGB { color } {
518    foreach { r g b } [winfo rgb $_clone $color] {
519        set r [expr round($r / 257.0)]
520        set g [expr round($g / 257.0)]
521        set b [expr round($b / 257.0)]
522    }
523    return [format "\#%02x%02x%02x" $r $g $b]
524}
525
526itcl::body Rappture::XyPrint::GetAxisType { axis } {
527    foreach type { x y x2 y2 } {
528        set axes [$_clone ${type}axis use]
529        if { [lsearch $axes $axis] >= 0 } {
530            return [string range $type 0 0]
531        }
532    }
533    return ""
534}
535
536itcl::body Rappture::XyPrint::GetAxis {} {
537    set axis [$itk_component(axis_combo) current]
538    foreach option { -min -max -loose -title -stepsize -subdivisions } {
539        set _settings($this-axis$option) [$_clone axis cget $axis $option]
540    }
541    foreach attr { fontfamily fontsize fontweight fontslant } {
542        set specific $this-$axis-ticks
543        set general $this-axis-ticks
544        set _settings(${general}-${attr}) $_settings(${specific}-${attr})
545        set specific $this-$axis-title
546        set general $this-axis-title
547        set _settings(${general}-${attr}) $_settings(${specific}-${attr})
548    }
549    set type [GetAxisType $axis]
550    if { [$_clone grid cget -map${type}] == $axis } {
551        set _settings($this-axis-grid) 1
552    }  else {
553        set _settings($this-axis-grid) 0
554    }
555    set _settings($this-axis-plotpad${type}) \
556        [Pixels2Inches [$_clone cget -plotpad${type}]]
557    set _settings($this-axis-zero) [$_clone marker cget ${type}-zero -hide]
558}
559
560itcl::body Rappture::XyPrint::GetElement { args } {
561    set index 1
562    array unset _settings $this-element-*
563    foreach elem [$_clone element show] {
564        set _settings($this-element-$index) $elem
565        incr index
566    }
567    set index [$itk_component(element_slider) get]
568    set elem $_settings($this-element-$index)
569    set _settings($this-element-label) [$_clone element cget $elem -label]
570    set _settings($this-element-color) [$_clone element cget $elem -color]
571    if { [$_clone element type $elem] != "bar" } {
572        set _settings($this-element-symbol) [$_clone element cget $elem -symbol]
573        set _settings($this-element-dashes) [$_clone element cget $elem -dashes]
574    }
575    set page $itk_component(legend_page)
576    set color [$page.color label $_settings($this-element-color)]
577    if { $color == "" } {
578        set color [Color2RGB $_settings($this-element-color)]
579        $page.color choices insert end $color $color
580        $page.color value $color
581    } else {
582        $page.color value [$page.color label $_settings($this-element-color)]
583    }
584    if { [$_clone element type $elem] != "bar" } {
585        $page.symbol value [$page.symbol label $_settings($this-element-symbol)]
586        $page.dashes value [$page.dashes label $_settings($this-element-dashes)]
587    }
588    #FixElement
589}
590
591itcl::body Rappture::XyPrint::BuildLayoutTab {} {
592    itk_component add layout_page {
593        frame $itk_component(tabs).layout_page
594    }
595    set page $itk_component(layout_page)
596    $itk_component(tabs) insert end "layout" \
597        -text "Layout" -padx 2 -pady 2 -window $page -fill both
598
599    label $page.width_l -text "width" 
600    entry $page.width -width 6 \
601        -textvariable [itcl::scope _settings($this-layout-width)]
602    bind  $page.width <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
603    Rappture::Tooltip::for $page.width \
604        "Set the width (inches) of the output image. Must be a positive number."
605
606    label $page.height_l -text "height"
607    entry $page.height -width 6 \
608        -textvariable [itcl::scope _settings($this-layout-height)]
609    bind  $page.height <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
610    Rappture::Tooltip::for $page.height \
611        "Set the height (inches) of the output image. Must be a positive number"
612
613    label $page.margin_l -text "Margins" 
614
615    label $page.left_l -text "left"
616    entry $page.left -width 6 \
617        -textvariable [itcl::scope _settings($this-layout-leftmargin)]
618    bind  $page.left <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
619    Rappture::Tooltip::for $page.left \
620        "Set the size (inches) of the left margin. If zero, the size is automatically determined."
621
622    label $page.right_l -text "right"
623    entry $page.right -width 6 \
624        -textvariable [itcl::scope _settings($this-layout-rightmargin)]
625    bind  $page.right <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
626    Rappture::Tooltip::for $page.right \
627        "Set the size (inches) of the right margin. If zero, the size is automatically determined."
628
629
630    label $page.top_l -text "top"
631    entry $page.top -width 6 \
632        -textvariable [itcl::scope _settings($this-layout-topmargin)]
633    bind  $page.top <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
634    Rappture::Tooltip::for $page.top \
635        "Set the size (inches) of the top margin. If zero, the size is automatically determined."
636
637    label $page.bottom_l -text "bottom"
638    entry $page.bottom -width 6 \
639        -textvariable [itcl::scope _settings($this-layout-bottommargin)]
640    bind  $page.bottom <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
641    Rappture::Tooltip::for $page.bottom \
642        "Set the size (inches) of the bottom margin. If zero, the size is automatically determined."
643
644
645    label $page.map -image [Rappture::icon graphmargins]
646    blt::table $page \
647        0,0 $page.width_l -anchor e \
648        0,1 $page.width -fill x  \
649        1,0 $page.height_l -anchor e \
650        1,1 $page.height -fill x \
651        3,0 $page.left_l -anchor e \
652        3,1 $page.left -fill x  \
653        4,0 $page.right_l -anchor e \
654        4,1 $page.right -fill x \
655        5,0 $page.top_l -anchor e \
656        5,1 $page.top -fill x \
657        6,0 $page.bottom_l -anchor e \
658        6,1 $page.bottom -fill x  \
659        0,2 $page.map -fill both -rspan 7 -padx 2
660
661    blt::table configure $page c0 r* -resize none
662    blt::table configure $page c1 r2 -resize both
663}
664
665
666itcl::body Rappture::XyPrint::BuildGeneralTab {} {
667    itk_component add graph_page {
668        frame $itk_component(tabs).graph_page
669    }
670    set page $itk_component(graph_page)
671    $itk_component(tabs) insert end "graph" \
672        -text "General" -padx 2 -pady 2 -window $page -fill both
673
674    label $page.format_l -text "format"
675    Rappture::Combobox $page.format -width 30 -editable no
676    $page.format choices insert end \
677        "pdf" "PDF Portable Document Format" \
678        "ps"  "PS PostScript Format"  \
679        "eps" "EPS Encapsulated PostScript"  \
680        "jpg" "JPEG Joint Photographic Experts Group Format" \
681        "png" "PNG Portable Network Graphics Format"         
682
683    bind $page.format <<Value>> [itcl::code $this ApplyGeneralSettings]
684    Rappture::Tooltip::for $page.format \
685        "Set the format of the image."
686
687    label $page.style_l -text "style"
688    Rappture::Combobox $page.style -width 20 -editable no
689    $page.style choices insert end \
690        "ieee" "IEEE Journal"  \
691        "gekco" "G Klimeck" 
692    bind $page.style <<Value>> [itcl::code $this ApplyGeneralSettings]
693    Rappture::Tooltip::for $page.format \
694        "Set the style template."
695
696    checkbutton $page.remember -text "remember settings" \
697        -variable [itcl::scope _settings($this-general-remember)] 
698    Rappture::Tooltip::for $page.remember \
699        "Remember the settings. They will be override the default settings."
700
701    button $page.revert -text "revert" -image [Rappture::icon revert] \
702        -compound left -padx 3 -pady 1 -overrelief raised -relief flat \
703        -command [itcl::code $this ResetSettings]
704    Rappture::Tooltip::for $page.revert \
705        "Revert to the default settings."
706
707    blt::table $page \
708        2,0 $page.format_l -anchor e \
709        2,1 $page.format -fill x -cspan 2 \
710        3,0 $page.style_l -anchor e \
711        3,1 $page.style -fill x -cspan 2 \
712        5,0 $page.remember -cspan 2 -anchor w \
713        5,2 $page.revert -anchor e
714    blt::table configure $page r* -resize none  -pady { 0 2 }
715    blt::table configure $page r4 -resize both
716
717}
718   
719itcl::body Rappture::XyPrint::ApplyLegendSettings {} {
720    set page $itk_component(legend_page)
721    set _settings($this-legend-anchor)    [$page.anchor current]
722    if { $_clone != "" } {
723        lappend font $_settings($this-legend-fontfamily)
724        lappend font $_settings($this-legend-fontsize)
725        lappend font $_settings($this-legend-fontweight)
726        lappend font $_settings($this-legend-fontslant)
727        foreach option { -hide -position -anchor -borderwidth } {
728            SetComponentOption legend $option
729        }
730        $_clone legend configure -font fixed -font $font
731    }
732    ApplyElementSettings
733}
734
735itcl::body Rappture::XyPrint::BuildLegendTab {} {
736    itk_component add legend_page {
737        frame $itk_component(tabs).legend_page
738    }
739    set page $itk_component(legend_page)
740    $itk_component(tabs) insert end "legend" \
741        -text "Legend" -padx 2 -pady 2 -window $page -fill both
742
743    checkbutton $page.show -text "show legend" \
744        -offvalue 1 -onvalue 0 \
745        -variable [itcl::scope _settings($this-legend-hide)]  \
746        -command [itcl::code $this ApplyLegendSettings]
747    Rappture::Tooltip::for $page.show \
748        "Display the legend."
749
750    label $page.position_l -text "position"
751    Rappture::Combobox $page.position -width 15 -editable no
752    $page.position choices insert end \
753        "leftmargin" "left margin"  \
754        "rightmargin" "right margin"  \
755        "bottommargin" "bottom margin"  \
756        "topmargin" "top margin"  \
757        "plotarea" "inside plot"
758    bind $page.position <<Value>> [itcl::code $this ApplyLegendSettings]
759    Rappture::Tooltip::for $page.position \
760        "Set the position of the legend.  This option and the anchor determine the legend's location."
761
762    Rappture::Combobox $page.anchor -width 10 -editable no
763    $page.anchor choices insert end \
764        "nw" "northwest"  \
765        "n" "north"  \
766        "ne" "northeast"  \
767        "sw" "southwest"  \
768        "s" "south"  \
769        "se" "southeast"  \
770        "c" "center"  \
771        "e" "east"  \
772        "w" "west" 
773    bind $page.anchor <<Value>> [itcl::code $this ApplyLegendSettings]
774    Rappture::Tooltip::for $page.anchor \
775        "Set the anchor of the legend.  This option and the anchor determine the legend's location."
776
777    checkbutton $page.border -text "border" \
778        -variable [itcl::scope _settings($this-legend-borderwidth)] \
779        -onvalue 1 -offvalue 0 \
780        -command [itcl::code $this ApplyLegendSettings]
781    Rappture::Tooltip::for $page.border \
782        "Display a solid border around the legend."
783
784    label $page.slider_l -text "legend\nentry"  -justify right
785    itk_component add element_slider {
786        ::scale $page.slider -from 1 -to 1 \
787            -orient horizontal -width 12 \
788            -command [itcl::code $this GetElement]
789    }
790    Rappture::Tooltip::for $page.slider \
791        "Select the current entry."
792
793    label $page.label_l -text "label"
794    entry $page.label \
795        -background white \
796        -textvariable [itcl::scope _settings($this-element-label)]
797    bind  $page.label <KeyPress-Return> [itcl::code $this ApplyElementSettings]
798    Rappture::Tooltip::for $page.label \
799        "Set the label of the current entry in the legend."
800
801    label $page.color_l -text "color "
802    Rappture::Combobox $page.color -width 15 -editable no
803    $page.color choices insert end \
804        "#000000" "black" \
805        "#ffffff" "white" \
806        "#0000cd" "blue" \
807        "#cd0000" "red" \
808        "#00cd00" "green" \
809        "#3a5fcd" "royal blue" \
810        "#cdcd00" "yellow" \
811        "#cd1076" "deep pink" \
812        "#009acd" "deep sky blue" \
813        "#00c5cd" "torquise" \
814        "#a2b5cd" "light steel blue" \
815        "#7ac5cd" "cadet blue" \
816        "#66cdaa" "aquamarine" \
817        "#a2cd5a" "dark olive green" \
818        "#cd9b9b" "rosy brown" \
819        "#0000ff" "blue1" \
820        "#ff0000" "red1" \
821        "#00ff00" "green1"
822    bind $page.color <<Value>> [itcl::code $this ApplyElementSettings]
823    Rappture::Tooltip::for $page.color \
824        "Set the color of the current entry."
825
826    label $page.dashes_l -text "line style"
827    Rappture::Combobox $page.dashes -width 15 -editable no
828    $page.dashes choices insert end \
829        "" "solid"  \
830        "1" "dot" \
831        "5 2" "dash" \
832        "2 4 2" "dashdot" \
833        "2 4 2 2" "dashdotdot"
834    bind $page.dashes <<Value>> [itcl::code $this ApplyElementSettings]
835    Rappture::Tooltip::for $page.dashes \
836        "Set the line style of the current entry."
837
838    label $page.symbol_l -text "symbol"
839    Rappture::Combobox $page.symbol -editable no
840    $page.symbol choices insert end \
841        "none" "none"  \
842        "square" "square" \
843        "circle" "circle" \
844        "diamond" "diamond" \
845        "plus" "plus" \
846        "cross" "cross" \
847        "splus" "skinny plus"  \
848        "scross" "skinny cross" \
849        "triangle" "triangle"
850    bind $page.symbol <<Value>> [itcl::code $this ApplyElementSettings]
851    Rappture::Tooltip::for $page.symbol \
852        "Set the symbol of the current entry. \"none\" display no symbols."
853
854    label $page.font_l -text "font"
855    Rappture::Combobox $page.fontfamily -width 10 -editable no
856    $page.fontfamily choices insert end \
857        "courier" "courier" \
858        "helvetica" "helvetica"  \
859        "new*century*schoolbook"  "new century schoolbook" \
860        "symbol"  "symbol" \
861        "times"  "times"         
862    bind $page.fontfamily <<Value>> [itcl::code $this ApplyLegendSettings]
863    Rappture::Tooltip::for $page.fontfamily \
864        "Set the font of entries in the legend."
865
866    Rappture::Combobox $page.fontsize -width 4 -editable no
867    $page.fontsize choices insert end \
868        "8" "8" \
869        "9" "9" \
870        "10" "10" \
871        "11" "11" \
872        "12" "12" \
873        "14" "14" \
874        "17" "17" \
875        "18" "18" \
876        "20" "20"
877    bind  $page.fontsize <<Value>> [itcl::code $this ApplyLegendSettings]
878    Rappture::Tooltip::for $page.fontsize \
879        "Set the size (points) of the font."
880
881    Rappture::PushButton $page.fontweight \
882        -onimage [Rappture::icon font-bold] \
883        -offimage [Rappture::icon font-bold] \
884        -onvalue "bold" -offvalue "normal" \
885        -command [itcl::code $this ApplyLegendSettings] \
886        -variable [itcl::scope _settings($this-legend-fontweight)]
887    Rappture::Tooltip::for $page.fontweight \
888        "Use the bold version of the font."
889
890    Rappture::PushButton $page.fontslant \
891        -onimage [Rappture::icon font-italic] \
892        -offimage [Rappture::icon font-italic] \
893        -onvalue "italic" -offvalue "roman" \
894        -command [itcl::code $this ApplyLegendSettings] \
895        -variable [itcl::scope _settings($this-legend-fontslant)]
896    Rappture::Tooltip::for $page.fontslant \
897        "Use the italic version of the font."
898
899    blt::table $page \
900        1,0 $page.show -cspan 2 -anchor w \
901        1,2 $page.border -cspan 2 -anchor w \
902        2,0 $page.position_l -anchor e \
903        2,1 $page.position -fill x \
904        2,2 $page.anchor -fill x  -cspan 3 \
905        3,0 $page.font_l -anchor e \
906        3,1 $page.fontfamily -fill x \
907        3,2 $page.fontsize -fill x \
908        3,3 $page.fontweight -anchor e \
909        3,4 $page.fontslant -anchor e \
910        4,0 $page.slider_l -anchor e \
911        4,1 $page.slider -fill x -cspan 5 \
912        5,0 $page.label_l -anchor e \
913        5,1 $page.label -fill x -cspan 5 \
914        6,0 $page.color_l -anchor e \
915        6,1 $page.color -fill x \
916        6,2 $page.symbol_l -anchor e \
917        6,3 $page.symbol -fill both -cspan 3 \
918        7,0 $page.dashes_l -anchor e \
919        7,1 $page.dashes -fill x \
920
921    blt::table configure $page r* -resize none -pady { 0 2 }
922    blt::table configure $page c3 c4 -resize none
923    blt::table configure $page r8 -resize both
924
925}
926
927itcl::body Rappture::XyPrint::BuildAxisTab {} {
928    itk_component add axis_page {
929        frame $itk_component(tabs).axis_page
930    }
931    set page $itk_component(axis_page)
932    $itk_component(tabs) insert end "axis" \
933        -text "Axis" -padx 2 -pady 2 -window $page -fill both
934   
935    label $page.axis_l -text "axis"
936    itk_component add axis_combo {
937        Rappture::Combobox $page.axis -width 20 -editable no
938    }
939    bind $itk_component(axis_combo) <<Value>> [itcl::code $this GetAxis]
940    Rappture::Tooltip::for $page.axis \
941        "Select the current axis."
942
943    label $page.title_l -text "title"
944    entry $page.title \
945        -textvariable [itcl::scope _settings($this-axis-title)]
946    bind  $page.title <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
947    Rappture::Tooltip::for $page.title \
948        "Set the title of the current axis."
949
950    label $page.min_l -text "min"
951    entry $page.min -width 10 \
952        -textvariable [itcl::scope _settings($this-axis-min)]
953    bind  $page.min <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
954    Rappture::Tooltip::for $page.min \
955        "Set the minimum limit for the current axis. If empty, the minimum is automatically determined."
956
957    label $page.max_l -text "max"
958    entry $page.max -width 10 \
959        -textvariable [itcl::scope _settings($this-axis-max)]
960    bind  $page.max <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
961    Rappture::Tooltip::for $page.max \
962        "Set the maximum limit for the current axis. If empty, the maximum is automatically determined."
963
964    label $page.subdivisions_l -text "subdivisions"
965    entry $page.subdivisions \
966        -textvariable [itcl::scope _settings($this-axis-subdivisions)]
967    bind  $page.subdivisions <KeyPress-Return> \
968        [itcl::code $this ApplyAxisSettings]
969    Rappture::Tooltip::for $page.subdivisions \
970        "Set the number of subdivisions (minor ticks) for the current axis."
971
972    label $page.stepsize_l -text "step size"
973    entry $page.stepsize \
974        -textvariable [itcl::scope _settings($this-axis-stepsize)]
975    bind  $page.stepsize <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
976    Rappture::Tooltip::for $page.stepsize \
977        "Set the interval between major ticks for the current axis. If zero, the interval is automatically determined."
978
979    checkbutton $page.loose -text "loose limits" \
980        -onvalue "always" -offvalue "0" \
981        -variable [itcl::scope _settings($this-axis-loose)] \
982        -command [itcl::code $this ApplyAxisSettings]
983    Rappture::Tooltip::for $page.loose \
984        "Set major ticks outside of the limits for the current axis."
985
986    label $page.plotpad_l -text "pad" 
987    entry $page.plotpad -width 6 \
988        -textvariable [itcl::scope _settings($this-axis-plotpad)]
989    bind  $page.plotpad <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
990    Rappture::Tooltip::for $page.plotpad \
991        "Set padding (points) between the current axis and the plot."
992
993    checkbutton $page.grid -text "show grid lines" \
994        -variable [itcl::scope _settings($this-axis-grid)] \
995        -command [itcl::code $this ApplyAxisSettings]
996    Rappture::Tooltip::for $page.grid \
997        "Display grid lines for the current axis."
998
999    checkbutton $page.zero -text "mark zero" \
1000        -offvalue 1 -onvalue 0 \
1001        -variable [itcl::scope _settings($this-axis-zero)] \
1002        -command [itcl::code $this ApplyAxisSettings]
1003    Rappture::Tooltip::for $page.zero \
1004        "Display a line at zero for the current axis."
1005
1006    label $page.tickfont_l -text "tick font"
1007    Rappture::Combobox $page.tickfontfamily -width 10 -editable no
1008    $page.tickfontfamily choices insert end \
1009        "courier" "courier" \
1010        "helvetica" "helvetica"  \
1011        "new*century*schoolbook"  "new century schoolbook" \
1012        "symbol"  "symbol" \
1013        "times"  "times"         
1014    bind $page.tickfontfamily <<Value>> [itcl::code $this ApplyAxisSettings]
1015    Rappture::Tooltip::for $page.tickfontfamily \
1016        "Set the font of the ticks for the current axis."
1017
1018    Rappture::Combobox $page.tickfontsize -width 4 -editable no
1019    $page.tickfontsize choices insert end \
1020        "8" "8" \
1021        "9" "9" \
1022        "10" "10" \
1023        "11" "11" \
1024        "12" "12" \
1025        "14" "14" \
1026        "17" "17" \
1027        "18" "18" \
1028        "20" "20"
1029    bind $page.tickfontsize <<Value>> [itcl::code $this ApplyAxisSettings]
1030    Rappture::Tooltip::for $page.tickfontsize \
1031        "Set the size (points) of the tick font."
1032
1033    Rappture::PushButton $page.tickfontweight \
1034        -onimage [Rappture::icon font-bold] \
1035        -offimage [Rappture::icon font-bold] \
1036        -onvalue "bold" -offvalue "normal" \
1037        -command [itcl::code $this ApplyAxisSettings] \
1038        -variable [itcl::scope _settings($this-axis-ticks-fontweight)]
1039    Rappture::Tooltip::for $page.tickfontweight \
1040        "Use the bold version of the tick font."
1041
1042    Rappture::PushButton $page.tickfontslant \
1043        -onimage [Rappture::icon font-italic] \
1044        -offimage [Rappture::icon font-italic] \
1045        -onvalue "italic" -offvalue "roman" \
1046        -command [itcl::code $this ApplyAxisSettings] \
1047        -variable [itcl::scope _settings($this-axis-ticks-fontslant)]
1048    Rappture::Tooltip::for $page.tickfontslant \
1049        "Use the italic version of the tick font."
1050
1051    label $page.titlefont_l -text "title font"
1052    Rappture::Combobox $page.titlefontfamily -width 10 -editable no
1053    $page.titlefontfamily choices insert end \
1054        "courier" "courier" \
1055        "helvetica" "helvetica"  \
1056        "new*century*schoolbook"  "new century schoolbook" \
1057        "symbol"  "symbol" \
1058        "times"  "times"         
1059    bind $page.titlefontfamily <<Value>> [itcl::code $this ApplyAxisSettings]
1060    Rappture::Tooltip::for $page.titlefontfamily \
1061        "Set the font of the title for the current axis."
1062
1063    Rappture::Combobox $page.titlefontsize -width 4 -editable no
1064    $page.titlefontsize choices insert end \
1065        "8" "8" \
1066        "9" "9" \
1067        "10" "10" \
1068        "11" "11" \
1069        "12" "12" \
1070        "14" "14" \
1071        "17" "17" \
1072        "18" "18" \
1073        "20" "20"
1074    bind $page.titlefontsize <<Value>> [itcl::code $this ApplyAxisSettings]
1075    Rappture::Tooltip::for $page.titlefontsize \
1076        "Set the size (point) of the title font."
1077
1078    Rappture::PushButton $page.titlefontweight \
1079        -onimage [Rappture::icon font-bold] \
1080        -offimage [Rappture::icon font-bold] \
1081        -onvalue "bold" -offvalue "normal" \
1082        -command [itcl::code $this ApplyAxisSettings] \
1083        -variable [itcl::scope _settings($this-axis-title-fontweight)]
1084    Rappture::Tooltip::for $page.titlefontweight \
1085        "Use the bold version of the title font."
1086
1087    Rappture::PushButton $page.titlefontslant \
1088        -onimage [Rappture::icon font-italic] \
1089        -offimage [Rappture::icon font-italic] \
1090        -onvalue "italic" -offvalue "roman" \
1091        -command [itcl::code $this ApplyAxisSettings] \
1092        -variable [itcl::scope _settings($this-axis-title-fontslant)]
1093    Rappture::Tooltip::for $page.titlefontslant \
1094        "Use the italic version of the title font."
1095
1096    blt::table $page \
1097        1,1 $page.axis_l -anchor e  -pady 6 \
1098        1,2 $page.axis -fill x -cspan 6 \
1099        2,1 $page.title_l -anchor e \
1100        2,2 $page.title -fill x -cspan 5 \
1101        3,1 $page.min_l -anchor e \
1102        3,2 $page.min -fill x \
1103        3,3 $page.stepsize_l -anchor e \
1104        3,4 $page.stepsize -fill both -cspan 3 \
1105        4,1 $page.max_l -anchor e \
1106        4,2 $page.max -fill both \
1107        4,3 $page.subdivisions_l -anchor e \
1108        4,4 $page.subdivisions -fill both -cspan 3  \
1109        5,1 $page.titlefont_l -anchor e \
1110        5,2 $page.titlefontfamily -fill x -cspan 2 \
1111        5,4 $page.titlefontsize -fill x \
1112        5,5 $page.titlefontweight -anchor e \
1113        5,6 $page.titlefontslant -anchor e \
1114        6,1 $page.tickfont_l -anchor e \
1115        6,2 $page.tickfontfamily -fill x -cspan 2 \
1116        6,4 $page.tickfontsize -fill x \
1117        6,5 $page.tickfontweight -anchor e \
1118        6,6 $page.tickfontslant -anchor e \
1119        7,1 $page.loose -cspan 2 -anchor w \
1120        7,3 $page.grid -anchor w -cspan 2 \
1121        8,1 $page.zero -cspan 2 -anchor w \
1122        8,3 $page.plotpad_l -anchor e \
1123        8,4 $page.plotpad -fill both -cspan 3
1124
1125    blt::table configure $page  c0 c4 c5 c6 c7 c8 -resize none
1126}
1127
1128itcl::body Rappture::XyPrint::ApplyGeneralSettings {} {
1129    RegeneratePreview
1130}
1131
1132itcl::body Rappture::XyPrint::ApplyLegendSettings {} {
1133    set page $itk_component(legend_page)
1134    set _settings($this-legend-position)  [$page.position current]
1135    set _settings($this-legend-anchor)    [$page.anchor current]
1136   
1137    foreach option { -hide -position -anchor -borderwidth } {
1138        SetComponentOption legend $option
1139    }
1140    set _settings($this-legend-fontfamily)  [$page.fontfamily value]
1141    set _settings($this-legend-fontsize)    [$page.fontsize value]
1142    lappend font $_settings($this-legend-fontfamily)
1143    lappend font $_settings($this-legend-fontsize)
1144    lappend font $_settings($this-legend-fontweight)
1145    lappend font $_settings($this-legend-fontslant)
1146    $_clone legend configure -font fixed -font $font
1147    ApplyElementSettings
1148}
1149
1150itcl::body Rappture::XyPrint::ApplyAxisSettings {} {
1151    set axis [$itk_component(axis_combo) current]
1152    set type [GetAxisType $axis]
1153    set page $itk_component(axis_page)
1154    if { $_settings($this-axis-grid) } {
1155        $_clone grid configure -hide no -map${type} ${axis}
1156    } else {
1157        $_clone grid configure -hide no -map${type} ""
1158    }
1159    $_clone configure -plotpad${type} $_settings($this-axis-plotpad)
1160    foreach option { -min -max -loose -title -stepsize -subdivisions } {
1161        SetNamedComponentOption axis $axis $option
1162    }
1163    set _settings($this-axis-ticks-fontfamily)  [$page.tickfontfamily value]
1164    set _settings($this-axis-ticks-fontsize)    [$page.tickfontsize value]
1165    set _settings($this-axis-title-fontfamily)  [$page.titlefontfamily value]
1166    set _settings($this-axis-title-fontsize)    [$page.titlefontsize value]
1167
1168    set tickfont {}
1169    set titlefont {}
1170
1171    foreach attr { fontfamily fontsize fontweight fontslant } {
1172        set specific $this-$axis-ticks
1173        set general  $this-axis-ticks
1174        set _settings(${specific}-${attr}) $_settings(${general}-${attr})
1175        lappend tickfont $_settings(${general}-${attr})
1176        set specific $this-$axis-title
1177        set general  $this-axis-title
1178        set _settings(${specific}-${attr}) $_settings(${general}-${attr})
1179        lappend titlefont $_settings(${general}-${attr})
1180    }
1181    $_clone axis configure $axis -tickfont $tickfont -titlefont $titlefont
1182    $_clone marker configure ${type}-zero -hide $_settings($this-axis-zero)
1183    GetAxis
1184    RegeneratePreview
1185}
1186
1187itcl::body Rappture::XyPrint::ApplyElementSettings {} {
1188    set index [$itk_component(element_slider) get]
1189    set page $itk_component(legend_page)
1190    set _settings($this-element-color)  [$page.color current]
1191    if { $_clone != "" } {
1192        set elem $_settings($this-element-$index)
1193        if { [$_clone element type $elem] != "bar" } {
1194            set _settings($this-element-symbol) [$page.symbol current]
1195            set _settings($this-element-dashes) [$page.dashes current]
1196            foreach option { -symbol -dashes } {
1197                SetNamedComponentOption element $elem $option
1198            }
1199        }
1200        foreach option { -color -label } {
1201            SetNamedComponentOption element $elem $option
1202        }
1203        RegeneratePreview
1204    }
1205}
1206
1207itcl::body Rappture::XyPrint::SetLayoutOption { opt } {
1208    set new [Inches2Pixels $_settings($this-layout$opt)]
1209    $_clone configure $opt $new
1210}
1211
1212itcl::body Rappture::XyPrint::ApplyLayoutSettings {} {
1213    foreach opt { -width -height -leftmargin -rightmargin -topmargin
1214        -bottommargin } {
1215        set old [$_clone cget $opt]
1216        set code [catch { SetLayoutOption $opt } err]
1217        if { $code != 0 } {
1218            bell
1219            global errorInfo
1220            puts stderr "$err: $errorInfo"
1221            set _settings($this-layout$opt) [Pixels2Inches $old]
1222            $_clone configure $opt [Pixels2Inches $old]
1223        }
1224    }
1225    RegeneratePreview
1226}
1227
1228
1229itcl::body Rappture::XyPrint::InitializeSettings {} {
1230    # General settings
1231
1232    # Always set to "ps" "ieee"
1233    set _settings($this-general-format) ps
1234    set _settings($this-general-style) ieee
1235    set _settings($this-general-remember) 0
1236    set page $itk_component(graph_page)
1237    $page.format value [$page.format label $_settings($this-general-format)]
1238    $page.style value [$page.style label $_settings($this-general-style)]
1239
1240    # Layout settings
1241    set _settings($this-layout-width) [Pixels2Inches [$_clone cget -width]]
1242    set _settings($this-layout-height) [Pixels2Inches [$_clone cget -height]]
1243    set _settings($this-layout-leftmargin) \
1244        [Pixels2Inches [$_clone cget -leftmargin]]
1245    set _settings($this-layout-rightmargin) \
1246        [Pixels2Inches [$_clone cget -rightmargin]]
1247    set _settings($this-layout-topmargin) \
1248        [Pixels2Inches [$_clone cget -topmargin]]
1249    set _settings($this-layout-bottommargin) \
1250        [Pixels2Inches [$_clone cget -bottommargin]]
1251
1252    # Legend settings
1253    set page $itk_component(legend_page)
1254
1255    set names [$_clone element show]
1256    $itk_component(element_slider) configure -from 1 -to [llength $names]
1257    set state [expr  { ([llength $names] < 2) ? "disabled" : "normal" } ]
1258    $page.slider configure -state $state
1259    $page.slider_l configure -state $state
1260    # Always initialize the slider to the first entry in the element list.
1261    $itk_component(element_slider) set 1
1262    # Always set the borderwidth to be not displayed
1263    set _settings($this-legend-borderwidth) 0
1264
1265    $page.fontfamily value $_settings($this-legend-fontfamily)
1266    $page.fontsize value $_settings($this-legend-fontsize)
1267    if { $_settings($this-legend-fontweight) == "bold" } {
1268        set _settings($this-legend-font-bold) 1
1269    }
1270    set _settings($this-legend-hide) [$_clone legend cget -hide]
1271    set _settings($this-legend-position) [$_clone legend cget -position]
1272    set _settings($this-legend-anchor) [$_clone legend cget -anchor]
1273    $page.position value \
1274        [$page.position label $_settings($this-legend-position)]
1275    $page.anchor value [$page.anchor label $_settings($this-legend-anchor)]
1276    GetElement
1277
1278    # Axis settings
1279    set page $itk_component(axis_page)
1280    set names [lsort [$_clone axis names]]
1281    $itk_component(axis_combo) choices delete 0 end
1282    foreach axis $names {
1283        if { $axis == "z" } {
1284            continue
1285        }
1286        if { ![$_clone axis cget $axis -hide] } {
1287            $itk_component(axis_combo) choices insert end $axis $axis
1288        }
1289        lappend axisnames $axis
1290    }
1291    set axis [lindex $names 0]
1292
1293    $page.titlefontfamily value $_settings($this-$axis-title-fontfamily)
1294    $page.titlefontsize value   $_settings($this-$axis-title-fontsize)
1295    $page.tickfontfamily value  $_settings($this-$axis-ticks-fontfamily)
1296    $page.tickfontsize value    $_settings($this-$axis-ticks-fontsize)
1297
1298    # Always hide the zero line.
1299    set _settings($this-axis-zero) 1
1300    set _settings($this-axis-plotpad) [Pixels2Inches [$_clone cget -plotpadx]]
1301    # Pick the first axis to initially display
1302    set axis [lindex $axisnames 0]
1303    $itk_component(axis_combo) value $axis
1304    GetAxis
1305    RegeneratePreview
1306}
1307
1308
1309itcl::body Rappture::XyPrint::restore { toolName plotName data } {
1310    set key [list $toolName $plotName]
1311    set _savedSettings($key) $data
1312}
1313
1314itcl::body Rappture::XyPrint::RestoreSettings { toolName plotName } {
1315    if { ![file readable $_settingsFile] } {
1316        return;                         # No file or not readable
1317    }
1318    if { [file exists $_oldSettingsFile] } {
1319        file delete $_oldSettingsFile
1320    }
1321    # Read the file by sourcing it into a safe interpreter The only commands
1322    # executed will be "xyprint" which will simply add the data into an array
1323    # _savedSettings.
1324    set parser [interp create -safe]
1325    $parser alias xyprint [itcl::code $this restore]
1326    set f [open $_settingsFile "r"]
1327    set code [read $f]
1328    close $f
1329    $parser eval $code
1330   
1331    # Now see if there's an entry for this tool/plot combination.  The data
1332    # associated with the variable is itself code to update the graph (clone).
1333    set key [list $toolName $plotName]
1334    if { [info exists _savedSettings($key)] }  {
1335        $parser alias "preview" $_clone
1336        $parser eval $_savedSettings($key)
1337    }
1338    # Restore settings to this instance
1339    foreach {name value} [$parser eval "array get settings"] {
1340        set _settings($this-$name) $value
1341    }
1342    interp delete $parser
1343}
1344
1345itcl::body Rappture::XyPrint::ResetSettings {} {
1346    set graph $_graph
1347    DestroySettings
1348    set _graph $graph
1349    CloneGraph $graph
1350    InitClone
1351    InitializeSettings
1352}
1353
1354itcl::body Rappture::XyPrint::SaveSettings { toolName plotName } {
1355    if { !$_settings($this-general-remember) } {
1356        return
1357    }
1358    if { [catch { open $_settingsFile "w" 0600 } f ] != 0 } {
1359        puts stderr "$_settingsFile isn't writable: $f"
1360        bell
1361        return
1362    }
1363    set key [list $toolName $plotName]
1364    set _savedSettings($key) [CreateSettings $toolName $plotName]
1365    # Write the settings out
1366    foreach key [lsort [array names _savedSettings]] {
1367        set tool [lindex $key 0]
1368        set plot [lindex $key 1]
1369        puts $f "xyprint \"$tool\" \"$plot\" \{"
1370        set settings [string trim $_savedSettings($key) \n]
1371        puts $f "$settings"
1372        puts $f "\}\n"
1373    }
1374    close $f
1375}
1376
1377itcl::body Rappture::XyPrint::CreateSettings { toolName plotName } {
1378    # Create stanza associated with tool and plot title.
1379    # General settings
1380    set length [string length "${this}-"]
1381    append out "    array set settings {\n"
1382    foreach item [array names _settings ${this}-*] {
1383        set field [string range $item $length end]
1384        if { [regexp {^element-[0-9]+$} $field] } {
1385            continue
1386        }
1387        set value $_settings($item)
1388        append out "        [list $field] [list $value]\n"
1389    }
1390    append out "    }\n"
1391    # Legend font
1392    lappend legendfont $_settings($this-legend-fontfamily)
1393    lappend legendfont $_settings($this-legend-fontsize)
1394    lappend legendfont $_settings($this-legend-fontweight)
1395    lappend legendfont $_settings($this-legend-fontslant)
1396    # Axis tick font
1397    lappend axistickfont $_settings($this-axis-ticks-fontfamily)
1398    lappend axistickfont $_settings($this-axis-ticks-fontsize)
1399    lappend axistickfont $_settings($this-axis-ticks-fontweight)
1400    lappend axistickfont $_settings($this-axis-ticks-fontslant)
1401    # Axis title font
1402    lappend axistitlefont $_settings($this-axis-title-fontfamily)
1403    lappend axistitlefont $_settings($this-axis-title-fontsize)
1404    lappend axistitlefont $_settings($this-axis-title-fontweight)
1405    lappend axistitlefont $_settings($this-axis-title-fontslant)
1406    append out "\n"
1407
1408    # Layout settings
1409    append out "    preview configure"
1410    foreach opt { -width -height -leftmargin -rightmargin -topmargin
1411        -bottommargin -plotpadx -plotpady } {
1412        set value [list [$_clone cget $opt]]
1413        append out " $opt $value"
1414    }
1415    append out "\n"
1416
1417    # Legend settings
1418    append out "    preview legend configure"
1419    foreach opt { -position -anchor -borderwidth -hide } {
1420        set value [list [$_clone legend cget $opt]]
1421        append out " $opt $value"
1422    }
1423    append out " -font \"$legendfont\"\n"
1424
1425    # Element settings
1426    foreach elem [$_clone element show] {
1427        set label [$_clone element cget $elem -label]
1428        if { $label == "" } {
1429            continue
1430        }
1431        append out "    if \{ \[preview element exists \"$label\"\] \} \{\n"
1432        append out "        preview element configure \"$label\""
1433        if { [$_clone element type $elem] != "bar" } {
1434            set options { -symbol -color -dashes -label }
1435        } else {
1436            set options { -color -label }
1437        }
1438        foreach opt $options {
1439            set value [list [$_clone element cget $elem $opt]]
1440            append out " $opt $value"
1441        }
1442        append out "    \}\n"
1443    }
1444   
1445    # Axis settings
1446    foreach axis [$_clone axis names] {
1447        if { [$_clone axis cget $axis -hide] } {
1448            continue
1449        }
1450        append out "    if \{ \[preview axis names \"$axis\"\] == \"$axis\" \} \{\n"
1451        append out "        preview axis configure \"$axis\""
1452        foreach opt { -hide -min -max -loose -title -stepsize -subdivisions } {
1453            set value [list [$_clone axis cget $axis $opt]]
1454            append out " $opt $value"
1455        }
1456        append out " -tickfont \"$axistickfont\""
1457        append out " -titlefont \"$axistitlefont\"\n"
1458        set hide [$_clone marker cget ${axis}-zero -hide]
1459        append out "        preview marker configure \"${axis}-zero\" -hide $hide\n"
1460        append out "    \}\n"
1461    }   
1462
1463    append out "    preview grid configure"
1464    append out " -hide \"[$_clone grid cget -hide]\""
1465    append out " -mapx \"[$_clone grid cget -mapx]\""
1466    append out " -mapy \"[$_clone grid cget -mapy]\""
1467    return $out
1468}
Note: See TracBrowser for help on using the repository browser.