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

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