source: trunk/gui/scripts/deviceEditor.tcl @ 472

Last change on this file since 472 was 472, checked in by mmc, 18 years ago

Oops! Two more problems related to downloads. The DeviceResult? and
DeviceEditor? didn't have proper args to support "download controls".
CNTbands was misbehaving when you downloaded the molecular structure.

File size: 5.8 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: deviceEditor - general-purpose device editor
3#
4#  This widget takes a <structure> description and chooses a specific
5#  editor appropriate for the contents of the structure.  If the
6#  <structure> contains a molecule, then it chooses a MoleculeViewer.
7#  If it contains a 1D device, then it chooses a deviceViewer1D.
8# ======================================================================
9#  AUTHOR:  Michael McLennan, Purdue University
10#  Copyright (c) 2004-2005  Purdue Research Foundation
11#
12#  See the file "license.terms" for information on usage and
13#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14# ======================================================================
15package require Itk
16
17option add *DeviceEditor.autoCleanUp yes widgetDefault
18
19itcl::class Rappture::DeviceEditor {
20    inherit itk::Widget Rappture::ControlOwner
21
22    itk_option define -autocleanup autoCleanUp AutoCleanUp 1
23
24    constructor {owner args} {
25        Rappture::ControlOwner::constructor $owner
26    } { # defined below }
27
28    public method value {args}
29    public method download {option args}
30
31    protected method _redraw {}
32    protected method _type {xmlobj}
33
34    private variable _current ""  ;# active device editor
35}
36                                                                               
37itk::usual DeviceEditor {
38}
39
40# ----------------------------------------------------------------------
41# CONSTRUCTOR
42# ----------------------------------------------------------------------
43itcl::body Rappture::DeviceEditor::constructor {owner args} {
44    itk_component add top {
45        frame $itk_interior.top
46    }
47    pack $itk_component(top) -fill x
48
49    eval itk_initialize $args
50}
51
52
53# ----------------------------------------------------------------------
54# USAGE: value ?-check? ?<newval>?
55#
56# Clients use this to query/set the value for this widget.  With
57# no args, it returns the current value for the widget.  If the
58# <newval> is specified, it sets the value of the widget and
59# sends a <<Value>> event.  If the -check flag is included, the
60# new value is not actually applied, but just checked for correctness.
61# ----------------------------------------------------------------------
62itcl::body Rappture::DeviceEditor::value {args} {
63    set onlycheck 0
64    set i [lsearch -exact $args -check]
65    if {$i >= 0} {
66        set onlycheck 1
67        set args [lreplace $args $i $i]
68    }
69
70    if {[llength $args] == 1} {
71        if {$_xmlobj != ""} {
72            if {$itk_option(-autocleanup)} {
73                # delete any existing object
74                itcl::delete object $_xmlobj
75            }
76            set _xmlobj ""
77        }
78
79        set newval [lindex $args 0]
80        if {$newval != ""} {
81            if {$onlycheck} {
82                return
83            }
84            if {![Rappture::library isvalid $newval]} {
85                error "bad value \"$newval\": should be Rappture::Library"
86            }
87            set _xmlobj $newval
88        }
89        _redraw
90        event generate $itk_component(hull) <<Value>>
91
92    } elseif {[llength $args] == 0} {
93        sync  ;# querying -- must sync controls with the value
94    } else {
95        error "wrong # args: should be \"value ?-check? ?newval?\""
96    }
97    return $_xmlobj
98}
99
100# ----------------------------------------------------------------------
101# USAGE: download coming
102# USAGE: download controls <downloadCommand>
103# USAGE: download now
104#
105# Clients use this method to create a downloadable representation
106# of the plot.  Returns a list of the form {ext string}, where
107# "ext" is the file extension (indicating the type of data) and
108# "string" is the data itself.
109# ----------------------------------------------------------------------
110itcl::body Rappture::DeviceEditor::download {option args} {
111    if {"" != $_current} {
112        return [eval $_current download $option $args]
113    }
114    return ""
115}
116
117# ----------------------------------------------------------------------
118# USAGE: _redraw
119#
120# Used internally to load new device data into the appropriate
121# editor.  If the editor needs to be created, it is created and
122# activated within this widget.  Then, the data is loaded into
123# the editor.
124# ----------------------------------------------------------------------
125itcl::body Rappture::DeviceEditor::_redraw {} {
126    if {$_current != ""} {
127        $_current configure -device ""
128        set _current ""
129    }
130    switch -- [_type $_xmlobj] {
131        molecule {
132            if {![winfo exists $itk_component(hull).mol]} {
133                catch {destroy $itk_component(hull).dev}
134                Rappture::MoleculeViewer $itk_component(hull).mol $this
135                pack $itk_component(hull).mol -expand yes -fill both
136            }
137            $itk_component(hull).mol configure -device $_xmlobj
138
139            set _current $itk_component(hull).mol
140        }
141        device1D {
142            if {![winfo exists $itk_component(hull).dev]} {
143                catch {destroy $itk_component(hull).mol}
144                Rappture::DeviceViewer1D $itk_component(hull).dev $this
145                pack $itk_component(hull).dev -expand yes -fill both
146            }
147            $itk_component(hull).dev configure -device $_xmlobj
148
149            set _current $itk_component(hull).dev
150        }
151    }
152}
153
154# ----------------------------------------------------------------------
155# USAGE: _type <xmlobj>
156#
157# Used internally to determine the type of the device data stored
158# within the <xmlobj>.  Returns a name such as "molecule" or
159# "device1D" indicating the type of editor that should be used to
160# display the data.
161# ----------------------------------------------------------------------
162itcl::body Rappture::DeviceEditor::_type {xmlobj} {
163    if {$xmlobj == ""} {
164        return ""
165    }
166    if {[llength [$xmlobj children -type molecule components]] > 0} {
167        return "molecule"
168    }
169    return "device1D"
170}
Note: See TracBrowser for help on using the repository browser.