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

Last change on this file since 1788 was 1788, checked in by gah, 14 years ago
File size: 46.4 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    private common _settings
77    private common _fonts
78    private common _wait
79}
80
81# ----------------------------------------------------------------------
82# CONSTRUCTOR
83# ----------------------------------------------------------------------
84itcl::body Rappture::XyPrint::constructor { args } {
85    Rappture::dispatcher _dispatcher
86    $_dispatcher register !rebuild
87    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
88   
89    set w [winfo pixels . 2.5i]
90    set h [winfo pixels . 2i]
91    set _preview [image create photo -width $w -height $h]
92    itk_component add tabs {
93        blt::tabset $itk_interior.tabs \
94            -highlightthickness 0 -tearoff 0 -side top \
95            -bd 0 -gap 0 -tabborderwidth 1 \
96            -outerpad 1 -background grey
97    } {
98        keep -cursor
99        ignore -highlightthickness -borderwidth -background
100    }
101    itk_component add preview {
102        label $itk_interior.preview \
103            -highlightthickness 0 -bd 0 -image $_preview -width 2.5i \
104                -height 2.25i -background grey -padx 10 -pady 10
105    } {
106        ignore -background
107    }
108    itk_component add ok {
109        button $itk_interior.ok -text "Save" \
110            -highlightthickness 0 -pady 2 -padx 0 \
111            -command [itcl::code $this SetWaitVariable 1] \
112            -compound left \
113            -image [Rappture::icon download]
114    }
115    itk_component add cancel {
116        button $itk_interior.cancel -text "Cancel" \
117            -highlightthickness 0 -pady 2 -padx 0 \
118            -command [itcl::code $this SetWaitVariable 0] \
119            -compound left \
120            -image [Rappture::icon cancel]
121    }
122    blt::table $itk_interior \
123        0,0 $itk_component(preview) -cspan 2 -fill both \
124        1,0 $itk_component(tabs) -fill both -cspan 2 \
125        2,1 $itk_component(cancel) -padx 2 -pady 2 -width .9i -fill y \
126        2,0 $itk_component(ok) -padx 2 -pady 2 -width .9i -fill y
127    blt::table configure $itk_interior r1 -resize none
128    blt::table configure $itk_interior r1 -resize both
129
130    BuildGeneralTab
131    BuildAxisTab
132    BuildLegendTab
133    BuildLayoutTab
134    eval itk_initialize $args
135}
136
137# ----------------------------------------------------------------------
138# DESTRUCTOR
139# ----------------------------------------------------------------------
140itcl::body Rappture::XyPrint::destructor {} {
141    destroy $_clone
142    image delete $_preview
143    array unset _settings $this-*
144}
145
146itcl::body Rappture::XyPrint::DestroySettings {} {
147    destroy $_clone
148    set _clone ""
149    set _graph ""
150    foreach font [array names _fonts] {
151        font delete $font
152    }
153    array unset _fonts
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 { [$orig axis cget $axis -hide] } {
294            continue
295        }
296        if { [$_clone axis name $axis] == "" } {
297            $_clone axis create $axis
298        }
299        CopyOptions [list axis configure $axis] $orig $_clone
300    }
301    foreach axis { x y x2 y2 } {
302        $_clone ${axis}axis use [$orig ${axis}axis use]
303    }
304    # Pen component
305    foreach pen [$orig pen names] {
306        if { [$_clone pen name $pen] == "" } {
307            $_clone pen create $pen
308        }
309        CopyOptions [list pen configure $pen] $orig $_clone
310    }
311    # Marker component
312    foreach marker [$orig marker names] {
313        $_clone marker create [$orig marker type $marker] -name $marker
314        CopyOptions [list marker configure $marker] $orig $_clone -name
315    }
316    # Element component
317    foreach elem [$orig element names] {
318        $_clone element create $elem
319        CopyOptions [list element configure $elem] $orig $_clone -data
320        if { [$_clone element cget $elem -hide] } {
321            $_clone element configure $elem -label ""
322        }
323    }
324    # Fix element display list
325    $_clone element show [$orig element show]
326    # Legend component
327    CopyOptions {legend configure} $orig $_clone
328    # Postscript component
329    CopyOptions {postscript configure} $orig $_clone
330    # Crosshairs component
331    CopyOptions {crosshairs configure} $orig $_clone
332    # Grid component
333    CopyOptions {grid configure} $orig $_clone
334
335    # Create markers representing lines at zero for the x and y axis.
336    $_clone marker create line -name x-zero \
337        -coords "0 -Inf 0 Inf" -dashes 1 -hide yes
338    $_clone marker create line -name y-zero \
339        -coords "-Inf 0 Inf 0" -dashes 1 -hide yes
340}
341
342itcl::body Rappture::XyPrint::InitClone {} {
343   
344    $_clone configure -width 3.4i -height 3.4i -background white \
345        -borderwidth 0 \
346        -leftmargin 0 \
347        -rightmargin 0 \
348        -topmargin 0 \
349        -bottommargin 0
350   
351    # Kill the title and create a border around the plot
352    $_clone configure \
353        -title "" \
354        -plotborderwidth 1 -plotrelief solid  \
355        -plotbackground white -plotpadx 0 -plotpady 0
356    #
357    set _settings($this-layout-width) [Pixels2Inches [$_clone cget -width]]
358    set _settings($this-layout-height) [Pixels2Inches [$_clone cget -height]]
359
360    set _fonts(legend) [font create legend \
361                            -family helvetica -size 10 -weight normal]
362    update
363    $_clone legend configure \
364        -position right \
365        -font $_fonts(legend) \
366        -hide yes -borderwidth 0 -background white -relief solid \
367        -anchor nw -activeborderwidth 0
368    #
369    foreach axis [$_clone axis names] {
370        if { [$_clone axis cget $axis -hide] } {
371            continue
372        }
373        set _fonts($axis-ticks) [font create $axis-ticks \
374                                     -family helvetica -size 10 \
375                                     -weight normal -slant roman]
376        set _fonts($axis-title) [font create $axis-title \
377                                     -family helvetica -size 10 \
378                                     -weight normal -slant roman]
379        update
380        $_clone axis configure $axis -ticklength 5  \
381            -majorticks {} -minorticks {}
382        $_clone axis configure $axis \
383            -tickfont $_fonts($axis-ticks) \
384            -titlefont $_fonts($axis-title)
385    }
386    foreach elem [$_clone element names] {
387        if { [$_clone element cget $elem -linewidth] > 1 } {
388            $_clone element configure $elem -linewidth 1 -pixels 3
389        }
390    }
391}
392
393itcl::body Rappture::XyPrint::SetOption { opt } {
394    set new $_settings($this-graph$opt)
395    set old [$_clone cget $opt]
396    set code [catch [list $_clone configure $opt $new] err]
397    if { $code != 0 } {
398        bell
399        global errorInfo
400        puts stderr "$err: $errorInfo"
401        set _settings($this-graph$opt) $old
402        $_clone configure $opt $old
403    }
404}
405
406itcl::body Rappture::XyPrint::SetComponentOption { comp opt } {
407    set new $_settings($this-$comp$opt)
408    set old [$_clone $comp cget $opt]
409    set code [catch [list $_clone $comp configure $opt $new] err]
410    if { $code != 0 } {
411        bell
412        global errorInfo
413        puts stderr "$err: $errorInfo"
414        set _settings($this-$comp$opt) $old
415        $_clone $comp configure $opt $old
416    }
417}
418
419itcl::body Rappture::XyPrint::SetNamedComponentOption { comp name opt } {
420    set new $_settings($this-$comp$opt)
421    set old [$_clone $comp cget $name $opt]
422    set code [catch [list $_clone $comp configure $name $opt $new] err]
423    if { $code != 0 } {
424        bell
425        global errorInfo
426        puts stderr "$err: $errorInfo"
427        set _settings($this-$comp$opt) $old
428        $_clone $comp configure $name $opt $old
429    }
430}
431
432itcl::body Rappture::XyPrint::RegeneratePreview {} {
433    update
434    set img [image create photo]
435    set w [Inches2Pixels $_settings($this-layout-width) 3.4]
436    set h [Inches2Pixels $_settings($this-layout-height) 3.4]
437    set pixelsPerInch [winfo pixels . 1i]
438    set sx [expr 2.5*$pixelsPerInch/$w]
439    set sy [expr 2.0*$pixelsPerInch/$h]
440    set s [expr min($sx,$sy)]
441    $_clone snap $img -width $w -height $h
442
443    if 0 {
444    if { ![winfo exists .labeltest] } {
445        toplevel .labeltest -bg red
446        label .labeltest.label -image $img
447        pack .labeltest.label -fill both
448    }
449    }
450    set pw [expr int(round($s * $w))]
451    set ph [expr int(round($s * $h))]
452    $_preview configure -width $pw -height $ph
453    #.labeltest.label configure -image $img
454    blt::winop resample $img $_preview box
455    image delete $img
456}
457
458itcl::body Rappture::XyPrint::Pixels2Inches { pixels } {
459    if { [llength $pixels] == 2 } {
460        set pixels [lindex $pixels 0]
461    }
462    set pixelsPerInch [winfo pixels . 1i]
463    set inches [expr { double($pixels) / $pixelsPerInch }]
464    return [format %.3g ${inches}]
465}
466
467itcl::body Rappture::XyPrint::Inches2Pixels { inches {defValue ""}} {
468    set n [scan $inches %g dummy]
469    if { $n != 1  && $defValue != "" } {
470        set inches $defValue
471    }
472    return  [winfo pixels . ${inches}i]
473}
474
475itcl::body Rappture::XyPrint::Color2RGB { color } {
476    foreach { r g b } [winfo rgb $_clone $color] {
477        set r [expr round($r / 257.0)]
478        set g [expr round($g / 257.0)]
479        set b [expr round($b / 257.0)]
480    }
481    return [format "\#%02x%02x%02x" $r $g $b]
482}
483
484itcl::body Rappture::XyPrint::GetAxisType { axis } {
485    foreach type { x y x2 y2 } {
486        set axes [$_clone ${type}axis use]
487        if { [lsearch $axes $axis] >= 0 } {
488            return [string range $type 0 0]
489        }
490    }
491    return ""
492}
493
494itcl::body Rappture::XyPrint::GetAxis {} {
495    set axis [$itk_component(axis_combo) current]
496    foreach option { -min -max -loose -title -stepsize -subdivisions } {
497        set _settings($this-axis$option) [$_clone axis cget $axis $option]
498    }
499    set type [GetAxisType $axis]
500    if { [$_clone grid cget -map${type}] == $axis } {
501        set _settings($this-axis-grid) 1
502    }  else {
503        set _settings($this-axis-grid) 0
504    }
505    set _settings($this-axis-plotpad${type}) \
506        [Pixels2Inches [$_clone cget -plotpad${type}]]
507    set _settings($this-axis-zero) [$_clone marker cget ${type}-zero -hide]
508}
509
510itcl::body Rappture::XyPrint::GetElement { args } {
511    set index 1
512    array unset _settings $this-element-*
513    foreach elem [$_clone element show] {
514        set _settings($this-element-$index) $elem
515        incr index
516    }
517    set index [$itk_component(element_slider) get]
518    set elem $_settings($this-element-$index)
519    set _settings($this-element-symbol) [$_clone element cget $elem -symbol]
520    set _settings($this-element-color) [$_clone element cget $elem -color]
521    set _settings($this-element-dashes) [$_clone element cget $elem -dashes]
522    set _settings($this-element-label) [$_clone element cget $elem -label]
523    set page $itk_component(legend_page)
524    set color [$page.color label $_settings($this-element-color)]
525    if { $color == "" } {
526        set color [Color2RGB $_settings($this-element-color)]
527        $page.color choices insert end $color $color
528        $page.color value $color
529    } else {
530        $page.color value [$page.color label $_settings($this-element-color)]
531    }
532    $page.symbol value [$page.symbol label $_settings($this-element-symbol)]
533    $page.dashes value [$page.dashes label $_settings($this-element-dashes)]
534    #FixElement
535}
536
537itcl::body Rappture::XyPrint::BuildLayoutTab {} {
538    itk_component add layout_page {
539        frame $itk_component(tabs).layout_page
540    }
541    set page $itk_component(layout_page)
542    $itk_component(tabs) insert end "layout" \
543        -text "Layout" -padx 2 -pady 2 -window $page -fill both
544
545    label $page.width_l -text "width" 
546    entry $page.width -width 6 \
547        -textvariable [itcl::scope _settings($this-layout-width)]
548    bind  $page.width <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
549    Rappture::Tooltip::for $page.width \
550        "Set the width (inches) of the output image. Must be a positive number."
551
552    label $page.height_l -text "height"
553    entry $page.height -width 6 \
554        -textvariable [itcl::scope _settings($this-layout-height)]
555    bind  $page.height <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
556    Rappture::Tooltip::for $page.height \
557        "Set the height (inches) of the output image. Must be a positive number"
558
559    label $page.margin_l -text "Margins" 
560
561    label $page.left_l -text "left"
562    entry $page.left -width 6 \
563        -textvariable [itcl::scope _settings($this-layout-leftmargin)]
564    bind  $page.left <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
565    Rappture::Tooltip::for $page.left \
566        "Set the size (inches) of the left margin. If zero, the size is automatically determined."
567
568    label $page.right_l -text "right"
569    entry $page.right -width 6 \
570        -textvariable [itcl::scope _settings($this-layout-rightmargin)]
571    bind  $page.right <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
572    Rappture::Tooltip::for $page.right \
573        "Set the size (inches) of the right margin. If zero, the size is automatically determined."
574
575
576    label $page.top_l -text "top"
577    entry $page.top -width 6 \
578        -textvariable [itcl::scope _settings($this-layout-topmargin)]
579    bind  $page.top <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
580    Rappture::Tooltip::for $page.top \
581        "Set the size (inches) of the top margin. If zero, the size is automatically determined."
582
583    label $page.bottom_l -text "bottom"
584    entry $page.bottom -width 6 \
585        -textvariable [itcl::scope _settings($this-layout-bottommargin)]
586    bind  $page.bottom <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
587    Rappture::Tooltip::for $page.bottom \
588        "Set the size (inches) of the bottom margin. If zero, the size is automatically determined."
589
590
591    label $page.map -image [Rappture::icon graphmargins]
592    blt::table $page \
593        0,0 $page.width_l -anchor e \
594        0,1 $page.width -fill x  \
595        1,0 $page.height_l -anchor e \
596        1,1 $page.height -fill x \
597        3,0 $page.left_l -anchor e \
598        3,1 $page.left -fill x  \
599        4,0 $page.right_l -anchor e \
600        4,1 $page.right -fill x \
601        5,0 $page.top_l -anchor e \
602        5,1 $page.top -fill x \
603        6,0 $page.bottom_l -anchor e \
604        6,1 $page.bottom -fill x  \
605        0,2 $page.map -fill both -rspan 7 -padx 2
606
607    blt::table configure $page c0 r* -resize none
608    blt::table configure $page c1 r2 -resize both
609}
610
611
612itcl::body Rappture::XyPrint::BuildGeneralTab {} {
613    itk_component add graph_page {
614        frame $itk_component(tabs).graph_page
615    }
616    set page $itk_component(graph_page)
617    $itk_component(tabs) insert end "graph" \
618        -text "General" -padx 2 -pady 2 -window $page -fill both
619
620    label $page.format_l -text "format"
621    Rappture::Combobox $page.format -width 30 -editable no
622    $page.format choices insert end \
623        "pdf" "PDF Portable Document Format" \
624        "ps"  "PS PostScript Format"  \
625        "eps" "EPS Encapsulated PostScript"  \
626        "jpg" "JPEG Joint Photographic Experts Group Format" \
627        "png" "PNG Portable Network Graphics Format"         
628
629    bind $page.format <<Value>> [itcl::code $this ApplyGeneralSettings]
630    Rappture::Tooltip::for $page.format \
631        "Set the format of the image."
632
633    label $page.style_l -text "style"
634    Rappture::Combobox $page.style -width 20 -editable no
635    $page.style choices insert end \
636        "ieee" "IEEE Journal"  \
637        "gekco" "G Klimeck" 
638    bind $page.style <<Value>> [itcl::code $this ApplyGeneralSettings]
639    Rappture::Tooltip::for $page.format \
640        "Set the style template."
641
642    checkbutton $page.remember -text "remember settings" \
643        -variable [itcl::scope _settings($this-general-remember)] 
644    Rappture::Tooltip::for $page.remember \
645        "Remember the settings. They will be override the default settings."
646
647    button $page.revert -text "revert" -image [Rappture::icon revert] \
648        -compound left -padx 3 -pady 1 -overrelief raised -relief flat \
649        -command [itcl::code $this ResetSettings]
650    Rappture::Tooltip::for $page.revert \
651        "Revert to the default settings."
652
653    blt::table $page \
654        2,0 $page.format_l -anchor e \
655        2,1 $page.format -fill x -cspan 2 \
656        3,0 $page.style_l -anchor e \
657        3,1 $page.style -fill x -cspan 2 \
658        5,0 $page.remember -cspan 2 -anchor w \
659        5,2 $page.revert -anchor e
660    blt::table configure $page r* -resize none  -pady { 0 2 }
661    blt::table configure $page r4 -resize both
662
663}
664   
665itcl::body Rappture::XyPrint::ApplyLegendSettings {} {
666    set page $itk_component(legend_page)
667    set _settings($this-legend-position)  [$page.position current]
668    set _settings($this-legend-anchor)    [$page.anchor current]
669    foreach option { -hide -position -anchor -borderwidth } {
670        SetComponentOption legend $option
671    }
672    $_clone legend configure -font legend
673    ApplyElementSettings
674}
675
676itcl::body Rappture::XyPrint::BuildLegendTab {} {
677    itk_component add legend_page {
678        frame $itk_component(tabs).legend_page
679    }
680    set page $itk_component(legend_page)
681    $itk_component(tabs) insert end "legend" \
682        -text "Legend" -padx 2 -pady 2 -window $page -fill both
683
684    checkbutton $page.show -text "show legend" \
685        -offvalue 1 -onvalue 0 \
686        -variable [itcl::scope _settings($this-legend-hide)]  \
687        -command [itcl::code $this ApplyLegendSettings]
688    Rappture::Tooltip::for $page.show \
689        "Display the legend."
690
691    label $page.position_l -text "position"
692    Rappture::Combobox $page.position -width 15 -editable no
693    $page.position choices insert end \
694        "leftmargin" "left margin"  \
695        "rightmargin" "right margin"  \
696        "bottommargin" "bottom margin"  \
697        "topmargin" "top margin"  \
698        "plotarea" "inside plot"
699    bind $page.position <<Value>> [itcl::code $this ApplyLegendSettings]
700    Rappture::Tooltip::for $page.position \
701        "Set the position of the legend.  This option and the anchor determine the legend's location."
702
703    Rappture::Combobox $page.anchor -width 10 -editable no
704    $page.anchor choices insert end \
705        "nw" "northwest"  \
706        "n" "north"  \
707        "ne" "northeast"  \
708        "sw" "southwest"  \
709        "s" "south"  \
710        "se" "southeast"  \
711        "c" "center"  \
712        "e" "east"  \
713        "w" "west" 
714    bind $page.anchor <<Value>> [itcl::code $this ApplyLegendSettings]
715    Rappture::Tooltip::for $page.anchor \
716        "Set the anchor of the legend.  This option and the anchor determine the legend's location."
717
718    checkbutton $page.border -text "border" \
719        -variable [itcl::scope _settings($this-legend-borderwidth)] \
720        -onvalue 1 -offvalue 0 \
721        -command [itcl::code $this ApplyLegendSettings]
722    Rappture::Tooltip::for $page.border \
723        "Display a solid border around the legend."
724
725    label $page.slider_l -text "legend\nentry"  -justify right
726    itk_component add element_slider {
727        ::scale $page.slider -from 1 -to 1 \
728            -orient horizontal -width 12 \
729            -command [itcl::code $this GetElement]
730    }
731    Rappture::Tooltip::for $page.slider \
732        "Select the current entry."
733
734    label $page.label_l -text "label"
735    entry $page.label \
736        -background white \
737        -textvariable [itcl::scope _settings($this-element-label)]
738    bind  $page.label <KeyPress-Return> [itcl::code $this ApplyElementSettings]
739    Rappture::Tooltip::for $page.label \
740        "Set the label of the current entry in the legend."
741
742    label $page.color_l -text "color "
743    Rappture::Combobox $page.color -width 15 -editable no
744    $page.color choices insert end \
745        "#000000" "black" \
746        "#ffffff" "white" \
747        "#0000cd" "blue" \
748        "#cd0000" "red" \
749        "#00cd00" "green" \
750        "#3a5fcd" "royal blue" \
751        "#cdcd00" "yellow" \
752        "#cd1076" "deep pink" \
753        "#009acd" "deep sky blue" \
754        "#00c5cd" "torquise" \
755        "#a2b5cd" "light steel blue" \
756        "#7ac5cd" "cadet blue" \
757        "#66cdaa" "aquamarine" \
758        "#a2cd5a" "dark olive green" \
759        "#cd9b9b" "rosy brown" \
760        "#0000ff" "blue1" \
761        "#ff0000" "red1" \
762        "#00ff00" "green1"
763    bind $page.color <<Value>> [itcl::code $this ApplyElementSettings]
764    Rappture::Tooltip::for $page.color \
765        "Set the color of the current entry."
766
767    label $page.dashes_l -text "line style"
768    Rappture::Combobox $page.dashes -width 15 -editable no
769    $page.dashes choices insert end \
770        "" "solid"  \
771        "1" "dot" \
772        "5 2" "dash" \
773        "2 4 2" "dashdot" \
774        "2 4 2 2" "dashdotdot"
775    bind $page.dashes <<Value>> [itcl::code $this ApplyElementSettings]
776    Rappture::Tooltip::for $page.dashes \
777        "Set the line style of the current entry."
778
779    label $page.symbol_l -text "symbol"
780    Rappture::Combobox $page.symbol -editable no
781    $page.symbol choices insert end \
782        "none" "none"  \
783        "square" "square" \
784        "circle" "circle" \
785        "diamond" "diamond" \
786        "plus" "plus" \
787        "cross" "cross" \
788        "splus" "skinny plus"  \
789        "scross" "skinny cross" \
790        "triangle" "triangle"
791    bind $page.symbol <<Value>> [itcl::code $this ApplyElementSettings]
792    Rappture::Tooltip::for $page.symbol \
793        "Set the symbol of the current entry. \"none\" display no symbols."
794
795    label $page.font_l -text "font"
796    Rappture::Combobox $page.fontfamily -width 10 -editable no
797    $page.fontfamily choices insert end \
798        "courier" "courier" \
799        "helvetica" "helvetica"  \
800        "new*century*schoolbook"  "new century schoolbook" \
801        "symbol"  "symbol" \
802        "times"  "times"         
803    bind $page.fontfamily <<Value>> [itcl::code $this ApplyLegendSettings]
804    Rappture::Tooltip::for $page.fontfamily \
805        "Set the font of entries in the legend."
806
807    Rappture::Combobox $page.fontsize -width 4 -editable no
808    $page.fontsize choices insert end \
809        "8" "8" \
810        "9" "9" \
811        "10" "10" \
812        "11" "11" \
813        "12" "12" \
814        "14" "14" \
815        "17" "17" \
816        "18" "18" \
817        "20" "20"
818    bind  $page.fontsize <<Value>> [itcl::code $this ApplyLegendSettings]
819    Rappture::Tooltip::for $page.fontsize \
820        "Set the size (points) of the font."
821
822    Rappture::PushButton $page.fontweight \
823        -width 18 -height 18 \
824        -onimage [Rappture::icon font-bold] \
825        -offimage [Rappture::icon font-bold] \
826        -onvalue "bold" -offvalue "normal" \
827        -command [itcl::code $this ApplyLegendSettings] \
828        -variable [itcl::scope _settings($this-legend-font-weight)]
829    Rappture::Tooltip::for $page.fontweight \
830        "Use the bold version of the font."
831
832    Rappture::PushButton $page.fontslant \
833        -width 18 -height 18 \
834        -onimage [Rappture::icon font-italic] \
835        -offimage [Rappture::icon font-italic] \
836        -onvalue "italic" -offvalue "roman" \
837        -command [itcl::code $this ApplyLegendSettings] \
838        -variable [itcl::scope _settings($this-legend-font-slant)]
839    Rappture::Tooltip::for $page.fontslant \
840        "Use the italic version of the font."
841
842    blt::table $page \
843        1,0 $page.show -cspan 2 -anchor w \
844        1,2 $page.border -cspan 2 -anchor w \
845        2,0 $page.position_l -anchor e \
846        2,1 $page.position -fill x \
847        2,2 $page.anchor -fill x  -cspan 3 \
848        3,0 $page.font_l -anchor e \
849        3,1 $page.fontfamily -fill x \
850        3,2 $page.fontsize -fill x \
851        3,3 $page.fontweight -anchor e \
852        3,4 $page.fontslant -anchor e \
853        4,0 $page.slider_l -anchor e \
854        4,1 $page.slider -fill x -cspan 5 \
855        5,0 $page.label_l -anchor e \
856        5,1 $page.label -fill x -cspan 5 \
857        6,0 $page.color_l -anchor e \
858        6,1 $page.color -fill x \
859        6,2 $page.symbol_l -anchor e \
860        6,3 $page.symbol -fill both -cspan 3 \
861        7,0 $page.dashes_l -anchor e \
862        7,1 $page.dashes -fill x \
863
864    blt::table configure $page r* -resize none -pady { 0 2 }
865    blt::table configure $page r8 -resize both
866
867}
868
869itcl::body Rappture::XyPrint::BuildAxisTab {} {
870    itk_component add axis_page {
871        frame $itk_component(tabs).axis_page
872    }
873    set page $itk_component(axis_page)
874    $itk_component(tabs) insert end "axis" \
875        -text "Axis" -padx 2 -pady 2 -window $page -fill both
876   
877    label $page.axis_l -text "axis"
878    itk_component add axis_combo {
879        Rappture::Combobox $page.axis -width 20 -editable no
880    }
881    bind $itk_component(axis_combo) <<Value>> [itcl::code $this GetAxis]
882    Rappture::Tooltip::for $page.axis \
883        "Select the current axis."
884
885    label $page.title_l -text "title"
886    entry $page.title \
887        -textvariable [itcl::scope _settings($this-axis-title)]
888    bind  $page.title <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
889    Rappture::Tooltip::for $page.title \
890        "Set the title of the current axis."
891
892    label $page.min_l -text "min"
893    entry $page.min -width 10 \
894        -textvariable [itcl::scope _settings($this-axis-min)]
895    bind  $page.min <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
896    Rappture::Tooltip::for $page.min \
897        "Set the minimum limit for the current axis. If empty, the minimum is automatically determined."
898
899    label $page.max_l -text "max"
900    entry $page.max -width 10 \
901        -textvariable [itcl::scope _settings($this-axis-max)]
902    bind  $page.max <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
903    Rappture::Tooltip::for $page.max \
904        "Set the maximum limit for the current axis. If empty, the maximum is automatically determined."
905
906    label $page.subdivisions_l -text "subdivisions"
907    entry $page.subdivisions \
908        -textvariable [itcl::scope _settings($this-axis-subdivisions)]
909    bind  $page.subdivisions <KeyPress-Return> \
910        [itcl::code $this ApplyAxisSettings]
911    Rappture::Tooltip::for $page.subdivisions \
912        "Set the number of subdivisions (minor ticks) for the current axis."
913
914    label $page.stepsize_l -text "step size"
915    entry $page.stepsize \
916        -textvariable [itcl::scope _settings($this-axis-stepsize)]
917    bind  $page.stepsize <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
918    Rappture::Tooltip::for $page.stepsize \
919        "Set the interval between major ticks for the current axis. If zero, the interval is automatically determined."
920
921    checkbutton $page.loose -text "loose limits" \
922        -onvalue "always" -offvalue "0" \
923        -variable [itcl::scope _settings($this-axis-loose)] \
924        -command [itcl::code $this ApplyAxisSettings]
925    Rappture::Tooltip::for $page.loose \
926        "Set major ticks outside of the limits for the current axis."
927
928    label $page.plotpad_l -text "pad" 
929    entry $page.plotpad -width 6 \
930        -textvariable [itcl::scope _settings($this-axis-plotpad)]
931    bind  $page.plotpad <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
932    Rappture::Tooltip::for $page.plotpad \
933        "Set padding (points) between the current axis and the plot."
934
935    checkbutton $page.grid -text "show grid lines" \
936        -variable [itcl::scope _settings($this-axis-grid)] \
937        -command [itcl::code $this ApplyAxisSettings]
938    Rappture::Tooltip::for $page.grid \
939        "Display grid lines for the current axis."
940
941    checkbutton $page.zero -text "mark zero" \
942        -offvalue 1 -onvalue 0 \
943        -variable [itcl::scope _settings($this-axis-zero)] \
944        -command [itcl::code $this ApplyAxisSettings]
945    Rappture::Tooltip::for $page.zero \
946        "Display a line at zero for the current axis."
947
948    label $page.tickfont_l -text "tick font"
949    Rappture::Combobox $page.tickfontfamily -width 10 -editable no
950    $page.tickfontfamily choices insert end \
951        "courier" "courier" \
952        "helvetica" "helvetica"  \
953        "new*century*schoolbook"  "new century schoolbook" \
954        "symbol"  "symbol" \
955        "times"  "times"         
956    bind $page.tickfontfamily <<Value>> [itcl::code $this ApplyAxisSettings]
957    Rappture::Tooltip::for $page.tickfontfamily \
958        "Set the font of the ticks for the current axis."
959
960    Rappture::Combobox $page.tickfontsize -width 4 -editable no
961    $page.tickfontsize choices insert end \
962        "8" "8" \
963        "9" "9" \
964        "10" "10" \
965        "11" "11" \
966        "12" "12" \
967        "14" "14" \
968        "17" "17" \
969        "18" "18" \
970        "20" "20"
971    bind $page.tickfontsize <<Value>> [itcl::code $this ApplyAxisSettings]
972    Rappture::Tooltip::for $page.tickfontsize \
973        "Set the size (points) of the tick font."
974
975    Rappture::PushButton $page.tickfontweight \
976        -width 18 -height 18 \
977        -onimage [Rappture::icon font-bold] \
978        -offimage [Rappture::icon font-bold] \
979        -onvalue "bold" -offvalue "normal" \
980        -command [itcl::code $this ApplyAxisSettings] \
981        -variable [itcl::scope _settings($this-axis-tickfont-weight)]
982    Rappture::Tooltip::for $page.tickfontweight \
983        "Use the bold version of the tick font."
984
985    Rappture::PushButton $page.tickfontslant \
986        -width 18 -height 18 \
987        -onimage [Rappture::icon font-italic] \
988        -offimage [Rappture::icon font-italic] \
989        -onvalue "italic" -offvalue "roman" \
990        -command [itcl::code $this ApplyAxisSettings] \
991        -variable [itcl::scope _settings($this-axis-tickfont-slant)]
992    Rappture::Tooltip::for $page.tickfontslant \
993        "Use the italic version of the tick font."
994
995    label $page.titlefont_l -text "title font"
996    Rappture::Combobox $page.titlefontfamily -width 10 -editable no
997    $page.titlefontfamily choices insert end \
998        "courier" "courier" \
999        "helvetica" "helvetica"  \
1000        "new*century*schoolbook"  "new century schoolbook" \
1001        "symbol"  "symbol" \
1002        "times"  "times"         
1003    bind $page.titlefontfamily <<Value>> [itcl::code $this ApplyAxisSettings]
1004    Rappture::Tooltip::for $page.titlefontfamily \
1005        "Set the font of the title for the current axis."
1006
1007    Rappture::Combobox $page.titlefontsize -width 4 -editable no
1008    $page.titlefontsize choices insert end \
1009        "8" "8" \
1010        "9" "9" \
1011        "10" "10" \
1012        "11" "11" \
1013        "12" "12" \
1014        "14" "14" \
1015        "17" "17" \
1016        "18" "18" \
1017        "20" "20"
1018    bind $page.titlefontsize <<Value>> [itcl::code $this ApplyAxisSettings]
1019    Rappture::Tooltip::for $page.titlefontsize \
1020        "Set the size (point) of the title font."
1021
1022    Rappture::PushButton $page.titlefontweight \
1023        -width 18 -height 18 \
1024        -onimage [Rappture::icon font-bold] \
1025        -offimage [Rappture::icon font-bold] \
1026        -onvalue "bold" -offvalue "normal" \
1027        -command [itcl::code $this ApplyAxisSettings] \
1028        -variable [itcl::scope _settings($this-axis-titlefont-weight)]
1029    Rappture::Tooltip::for $page.titlefontweight \
1030        "Use the bold version of the title font."
1031
1032    Rappture::PushButton $page.titlefontslant \
1033        -width 18 -height 18 \
1034        -onimage [Rappture::icon font-italic] \
1035        -offimage [Rappture::icon font-italic] \
1036        -onvalue "italic" -offvalue "roman" \
1037        -command [itcl::code $this ApplyAxisSettings] \
1038        -variable [itcl::scope _settings($this-axis-titlefont-slant)]
1039    Rappture::Tooltip::for $page.titlefontslant \
1040        "Use the italic version of the title font."
1041
1042    blt::table $page \
1043        1,1 $page.axis_l -anchor e  -pady 6 \
1044        1,2 $page.axis -fill x -cspan 6 \
1045        2,1 $page.title_l -anchor e \
1046        2,2 $page.title -fill x -cspan 5 \
1047        3,1 $page.min_l -anchor e \
1048        3,2 $page.min -fill x \
1049        3,3 $page.stepsize_l -anchor e \
1050        3,4 $page.stepsize -fill both -cspan 3 \
1051        4,1 $page.max_l -anchor e \
1052        4,2 $page.max -fill both \
1053        4,3 $page.subdivisions_l -anchor e \
1054        4,4 $page.subdivisions -fill both -cspan 3  \
1055        5,1 $page.titlefont_l -anchor e \
1056        5,2 $page.titlefontfamily -fill x -cspan 2 \
1057        5,4 $page.titlefontsize -fill x \
1058        5,5 $page.titlefontweight -anchor e \
1059        5,6 $page.titlefontslant -anchor e \
1060        6,1 $page.tickfont_l -anchor e \
1061        6,2 $page.tickfontfamily -fill x -cspan 2 \
1062        6,4 $page.tickfontsize -fill x \
1063        6,5 $page.tickfontweight -anchor e \
1064        6,6 $page.tickfontslant -anchor e \
1065        7,1 $page.loose -cspan 2 -anchor w \
1066        7,3 $page.grid -anchor w -cspan 2 \
1067        8,1 $page.zero -cspan 2 -anchor w \
1068        8,3 $page.plotpad_l -anchor e \
1069        8,4 $page.plotpad -fill both -cspan 3
1070
1071    blt::table configure $page  c0 c7 c8 -resize none
1072}
1073
1074itcl::body Rappture::XyPrint::ApplyGeneralSettings {} {
1075    RegeneratePreview
1076}
1077
1078itcl::body Rappture::XyPrint::ApplyLegendSettings {} {
1079    set page $itk_component(legend_page)
1080    set _settings($this-legend-position)  [$page.position current]
1081    set _settings($this-legend-anchor)    [$page.anchor current]
1082
1083    foreach option { -hide -position -anchor -borderwidth } {
1084        SetComponentOption legend $option
1085    }
1086    font configure $_fonts(legend) \
1087        -family [$page.fontfamily current] \
1088        -size [$page.fontsize current] \
1089        -weight $_settings($this-legend-font-weight) \
1090        -slant $_settings($this-legend-font-slant)
1091    $_clone legend configure -font fixed -font $_fonts(legend)
1092    ApplyElementSettings
1093}
1094
1095itcl::body Rappture::XyPrint::ApplyAxisSettings {} {
1096    set axis [$itk_component(axis_combo) current]
1097    set type [GetAxisType $axis]
1098    set page $itk_component(axis_page)
1099    if { $_settings($this-axis-grid) } {
1100        $_clone grid configure -hide no -map${type} ${axis}
1101    } else {
1102        $_clone grid configure -hide no -map${type} ""
1103    }
1104    $_clone configure -plotpad${type} $_settings($this-axis-plotpad)
1105    foreach option { -min -max -loose -title -stepsize -subdivisions } {
1106        SetNamedComponentOption axis $axis $option
1107    }
1108    $_clone marker configure ${type}-zero -hide $_settings($this-axis-zero)
1109    font configure $axis-title \
1110        -family [$page.titlefontfamily current] \
1111        -size   [$page.titlefontsize current] \
1112        -weight $_settings($this-axis-titlefont-weight) \
1113        -slant  $_settings($this-axis-titlefont-slant)
1114    font configure $axis-ticks \
1115        -family [$page.tickfontfamily current] \
1116        -size   [$page.tickfontsize current] \
1117        -weight $_settings($this-axis-tickfont-weight) \
1118        -slant  $_settings($this-axis-tickfont-slant)
1119    $_clone axis configure $axis -tickfont $axis-ticks -titlefont $axis-title
1120    GetAxis
1121    RegeneratePreview
1122}
1123
1124itcl::body Rappture::XyPrint::ApplyElementSettings {} {
1125    set index [$itk_component(element_slider) get]
1126    set elem $_settings($this-element-$index)
1127    set page $itk_component(legend_page)
1128    set _settings($this-element-color)  [$page.color current]
1129    set _settings($this-element-symbol) [$page.symbol current]
1130    set _settings($this-element-dashes) [$page.dashes current]
1131    foreach option { -symbol -color -dashes -label } {
1132        SetNamedComponentOption element $elem $option
1133    }
1134    RegeneratePreview
1135}
1136
1137itcl::body Rappture::XyPrint::SetLayoutOption { opt } {
1138    set new [Inches2Pixels $_settings($this-layout$opt)]
1139    $_clone configure $opt $new
1140}
1141
1142itcl::body Rappture::XyPrint::ApplyLayoutSettings {} {
1143    foreach opt { -width -height -leftmargin -rightmargin -topmargin
1144        -bottommargin } {
1145        set old [$_clone cget $opt]
1146        set code [catch { SetLayoutOption $opt } err]
1147        if { $code != 0 } {
1148            bell
1149            global errorInfo
1150            puts stderr "$err: $errorInfo"
1151            set _settings($this-layout$opt) [Pixels2Inches $old]
1152            $_clone configure $opt [Pixels2Inches $old]
1153        }
1154    }
1155    RegeneratePreview
1156}
1157
1158
1159itcl::body Rappture::XyPrint::InitializeSettings {} {
1160    # General settings
1161
1162    # Always set to "ps" "ieee"
1163    set _settings($this-general-format) ps
1164    set _settings($this-general-style) ieee
1165    set _settings($this-general-remember) 0
1166    set page $itk_component(graph_page)
1167    $page.format value [$page.format label $_settings($this-general-format)]
1168    $page.style value [$page.style label $_settings($this-general-style)]
1169
1170    # Layout settings
1171    set _settings($this-layout-width) [Pixels2Inches [$_clone cget -width]]
1172    set _settings($this-layout-height) [Pixels2Inches [$_clone cget -height]]
1173    set _settings($this-layout-leftmargin) \
1174        [Pixels2Inches [$_clone cget -leftmargin]]
1175    set _settings($this-layout-rightmargin) \
1176        [Pixels2Inches [$_clone cget -rightmargin]]
1177    set _settings($this-layout-topmargin) \
1178        [Pixels2Inches [$_clone cget -topmargin]]
1179    set _settings($this-layout-bottommargin) \
1180        [Pixels2Inches [$_clone cget -bottommargin]]
1181
1182    # Legend settings
1183    set page $itk_component(legend_page)
1184
1185    set names [$_clone element show]
1186    $itk_component(element_slider) configure -from 1 -to [llength $names]
1187    set state [expr  { ([llength $names] < 2) ? "disabled" : "normal" } ]
1188    $page.slider configure -state $state
1189    $page.slider_l configure -state $state
1190    # Always initialize the slider to the first entry in the element list.
1191    $itk_component(element_slider) set 1
1192    # Always set the borderwidth to be not displayed
1193    set _settings($this-legend-borderwidth) 0
1194
1195    array unset info
1196    array set info [font configure legend]
1197    $page.fontfamily value $info(-family)
1198    $page.fontsize value $info(-size)
1199    set _settings($this-legend-font-weight) $info(-weight)
1200    set _settings($this-legend-font-slant) $info(-slant)
1201    if { $info(-weight) == "bold" } {
1202        set _settings($this-legend-font-bold) 1
1203    }
1204    set _settings($this-legend-hide) [$_clone legend cget -hide]
1205    set _settings($this-legend-position) [$_clone legend cget -position]
1206    set _settings($this-legend-anchor) [$_clone legend cget -anchor]
1207    $page.position value \
1208        [$page.position label $_settings($this-legend-position)]
1209    $page.anchor value [$page.anchor label $_settings($this-legend-anchor)]
1210    GetElement
1211
1212    # Axis settings
1213    set page $itk_component(axis_page)
1214    set names [lsort [$_clone axis names]]
1215    $itk_component(axis_combo) choices delete 0 end
1216    foreach axis $names {
1217        if { ![$_clone axis cget $axis -hide] } {
1218            $itk_component(axis_combo) choices insert end $axis $axis
1219        }
1220        lappend axisnames $axis
1221    }
1222    set axis [lindex $names 0]
1223
1224    array set info [font configure $axis-title]
1225    $page.titlefontfamily value $info(-family)
1226    $page.titlefontsize value $info(-size)
1227    set _settings($this-axis-titlefont-weight) $info(-weight)
1228    set _settings($this-axis-titlefont-slant) $info(-slant)
1229
1230    array set info [font configure $axis-ticks]
1231    $page.tickfontfamily value $info(-family)
1232    $page.tickfontsize value $info(-size)
1233    set _settings($this-axis-tickfont-weight) $info(-weight)
1234    set _settings($this-axis-tickfont-slant) $info(-slant)
1235
1236    # Always hide the zero line.
1237    set _settings($this-axis-zero) 1
1238    set _settings($this-axis-plotpad) [Pixels2Inches [$_clone cget -plotpadx]]
1239    # Pick the first axis to initially display
1240    set axis [lindex $axisnames 0]
1241    $itk_component(axis_combo) value $axis
1242    GetAxis
1243}
1244
1245
1246itcl::body Rappture::XyPrint::restore { toolName plotName data } {
1247    set key [list $toolName $plotName]
1248    set _savedSettings($key) $data
1249}
1250
1251itcl::body Rappture::XyPrint::RestoreSettings { toolName plotName } {
1252    if { ![file readable $_settingsFile] } {
1253        return;                         # No file or not readable
1254    }
1255    if { [file exists $_oldSettingsFile] } {
1256        file delete $_oldSettingsFile
1257    }
1258    # Read the file by sourcing it into a safe interpreter The only commands
1259    # executed will be "xyprint" which will simply add the data into an array
1260    # _savedSettings.
1261    set parser [interp create -safe]
1262    $parser alias xyprint [itcl::code $this restore]
1263    $parser alias font font
1264    set f [open $_settingsFile "r"]
1265    set code [read $f]
1266    close $f
1267    $parser eval $code
1268   
1269    # Now see if there's an entry for this tool/plot combination.  The data
1270    # associated with the variable is itself code to update the graph (clone).
1271    set key [list $toolName $plotName]
1272    if { [info exists _savedSettings($key)] }  {
1273        $parser alias "preview" $_clone
1274        $parser eval $_savedSettings($key)
1275    }
1276    foreach {name value} [$parser eval "array get general"] {
1277        set _settings($this-graph-$name) $value
1278    }
1279    interp delete $parser
1280}
1281
1282itcl::body Rappture::XyPrint::ResetSettings {} {
1283    set graph $_graph
1284    DestroySettings
1285    set _graph $graph
1286    CloneGraph $graph
1287    InitClone
1288    InitializeSettings
1289}
1290
1291itcl::body Rappture::XyPrint::SaveSettings { toolName plotName } {
1292    if { !$_settings($this-general-remember) } {
1293        return
1294    }
1295    if { [catch { open $_settingsFile "w" 0600 } f ] != 0 } {
1296        puts stderr "$_settingsFile isn't writable: $f"
1297        bell
1298        return
1299    }
1300    set key [list $toolName $plotName]
1301    set _savedSettings($key) [CreateSettings $toolName $plotName]
1302    # Write the settings out
1303    foreach key [lsort [array names _savedSettings]] {
1304        set tool [lindex $key 0]
1305        set plot [lindex $key 1]
1306        puts $f "xyprint \"$tool\" \"$plot\" \{"
1307        set settings [string trim $_savedSettings($key) \n]
1308        puts $f "$settings"
1309        puts $f "\}\n"
1310    }
1311    close $f
1312}
1313
1314itcl::body Rappture::XyPrint::CreateSettings { toolName plotName } {
1315    # Create stanza associated with tool and plot title.
1316    # General settings
1317    append out "    set general(format) $_settings($this-general-format)\n"
1318    append out "    set general(style) $_settings($this-general-style)\n"
1319
1320    foreach font [array names _fonts] {
1321        append out "    font configure $font"
1322        array unset info
1323        array set info [font configure $font]
1324        foreach opt { -family -size -weight -slant } {
1325            set value [list $info($opt)]
1326            append out " $opt $value"
1327        }
1328        append out "\n"
1329    }
1330    append out "\n"
1331
1332    # Layout settings
1333    append out "    preview configure"
1334    foreach opt { -width -height -leftmargin -rightmargin -topmargin
1335        -bottommargin -plotpadx -plotpady } {
1336        set value [list [$_clone cget $opt]]
1337        append out " $opt $value"
1338    }
1339    append out "\n"
1340
1341    # Legend settings
1342    append out "    preview legend configure"
1343    foreach opt { -position -anchor -borderwidth -hide } {
1344        set value [list [$_clone legend cget $opt]]
1345        append out " $opt $value"
1346    }
1347    append out " -font legend\n"
1348
1349    # Element settings
1350    foreach elem [$_clone element show] {
1351        set label [$_clone element cget $elem -label]
1352        if { $label == "" } {
1353            continue
1354        }
1355        append out "    if \{ \[preview element exists \"$label\"\] \} \{\n"
1356        append out "        preview element configure \"$label\""
1357        foreach opt { -symbol -color -dashes -label } {
1358            set value [list [$_clone element cget $elem $opt]]
1359            append out " $opt $value"
1360        }
1361        append out "    \}\n"
1362    }
1363   
1364    # Axis settings
1365    foreach axis [$_clone axis names] {
1366        if { [$_clone axis cget $axis -hide] } {
1367            continue
1368        }
1369        append out "    if \{ \[preview axis names \"$axis\"\] == \"$axis\" \} \{\n"
1370        append out "        preview axis configure \"$axis\""
1371        foreach opt { -hide -min -max -loose -title -stepsize -subdivisions } {
1372            set value [list [$_clone axis cget $axis $opt]]
1373            append out " $opt $value"
1374        }
1375        append out " -tickfont \"$axis-ticks\""
1376        append out " -titlefont \"$axis-title\"\n"
1377        set hide [$_clone marker cget ${axis}-zero -hide]
1378        append out "        preview marker configure \"${axis}-zero\" -hide $hide\n"
1379        append out "    \}\n"
1380    }   
1381
1382    append out "    preview grid configure"
1383    append out " -hide \"[$_clone grid cget -hide]\""
1384    append out " -mapx \"[$_clone grid cget -mapx]\""
1385    append out " -mapy \"[$_clone grid cget -mapy]\""
1386    return $out
1387}
Note: See TracBrowser for help on using the repository browser.