source: trunk/gui/scripts/controlOwner.tcl @ 2938

Last change on this file since 2938 was 2938, checked in by gah, 12 years ago

experimental drawing entry enabled

File size: 19.0 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: owner - manages Rappture controls
3#
4#  This object represents an entity managing Rappture controls.
5#  It is typically a Tool, a DeviceEditor, or some other large entity
6#  that manages a Rappture XML tree.  All controlling widgets are
7#  registered with an owner, and the owner propagates notifications
8#  out to clients who have an interest in a particular control.
9# ======================================================================
10#  AUTHOR:  Michael McLennan, Purdue University
11#  Copyright (c) 2004-2005  Purdue Research Foundation
12#
13#  See the file "license.terms" for information on usage and
14#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15# ======================================================================
16package require Itcl
17
18itcl::class Rappture::ControlOwner {
19    constructor {owner} { # defined below }
20
21    public method xml {args}
22
23    public method load {newobj}
24    public method widgetfor {path args}
25    public method valuefor {path args}
26    public method dependenciesfor {path args}
27    public method ownerfor {path {skip ""}}
28    public method changed {path}
29    public method regularize {path}
30    public method notify {option owner args}
31    public method sync {}
32    public method tool {}
33
34    protected method _slave {option args}
35
36    protected variable _owner ""     ;# ControlOwner containing this one
37    protected variable _path ""      ;# paths within are relative to this
38    protected variable _slaves ""    ;# this owner has these slaves
39    protected variable _xmlobj ""    ;# Rappture XML description
40    private variable _path2widget    ;# maps path => widget on this page
41    private variable _owner2paths    ;# for notify: maps owner => interests
42    private variable _type2curpath   ;# maps type(path) => path's current value
43    private variable _callbacks      ;# for notify: maps owner/path => callback
44    private variable _dependencies   ;# maps path => other paths dep on this
45}
46
47# ----------------------------------------------------------------------
48# CONSTRUCTOR
49# ----------------------------------------------------------------------
50itcl::body Rappture::ControlOwner::constructor {owner} {
51    if {"" != $owner} {
52        set parts [split $owner @]
53        set _owner [lindex $parts 0]
54        set _path [lindex $parts 1]
55        $_owner _slave add $this
56    }
57
58    # we are adding this so notes can be used
59    # in coordination with loaders inside the load function
60    array set _type2curpath {
61        drawing components
62        choice current
63        boolean current
64        image current
65        integer current
66        loader current
67        note contents
68        number current
69        periodicelement current
70        string current
71    }
72}
73
74# ----------------------------------------------------------------------
75# USAGE: xml <subcommand> ?<arg> <arg> ...?
76# USAGE: xml object
77#
78# Used by clients to manipulate the underlying XML data for this
79# tool.  The <subcommand> can be any operation supported by a
80# Rappture::library object.  Clients can also request the XML object
81# directly by using the "object" subcommand.
82# ----------------------------------------------------------------------
83itcl::body Rappture::ControlOwner::xml {args} {
84    if {"object" == $args} {
85        return $_xmlobj
86    }
87    return [eval $_xmlobj $args]
88}
89
90# ----------------------------------------------------------------------
91# USAGE: widgetfor <path> ?<widget>?
92#
93# Used by embedded widgets such as a Controls panel to register the
94# various controls associated with this page.  That way, this
95# ControlOwner knows what widgets to look at when syncing itself
96# to the underlying XML data.
97# ----------------------------------------------------------------------
98itcl::body Rappture::ControlOwner::widgetfor {path args} {
99    # if this is a query operation, then look for the path
100    if {[llength $args] == 0} {
101        set owner [ownerfor $path]
102        if {$owner != $this && $owner != ""} {
103            return [$owner widgetfor $path]
104        }
105        if {[info exists _path2widget($path)]} {
106            return $_path2widget($path)
107        }
108        return ""
109    }
110
111    # otherwise, associate the path with the given widget
112    set widget [lindex $args 0]
113    if {"" != $widget} {
114        if {[info exists _path2widget($path)]} {
115            error "$path already associated with widget $_path2widget($path)"
116        }
117        set _path2widget($path) $widget
118    } else {
119        catch {unset _path2widget($path)}
120    }
121}
122
123# ----------------------------------------------------------------------
124# USAGE: valuefor <path> ?<newValue>?
125#
126# Used by embedded widgets such as a Loader to query or set the
127# value of another control.  With no extra args, it returns the
128# value of the widget at the <path> in the XML.  Otherwise, it
129# sets the value of the widget to <newValue>.
130# ----------------------------------------------------------------------
131itcl::body Rappture::ControlOwner::valuefor {path args} {
132    set owner [ownerfor $path]
133
134    # if this is a query operation, then look for the path
135    if {[llength $args] == 0} {
136        if {$owner != $this && $owner != ""} {
137            return [$owner valuefor $path]
138        }
139        if {[info exists _path2widget($path)]} {
140            return [$_path2widget($path) value]
141        }
142        # can't find the path? try removing the prefix for this owner
143        set plen [string length $_path]
144        if {[string equal -length $plen $_path $path]} {
145            set relpath [string range $path [expr {$plen+1}] end]
146            if {[info exists _path2widget($relpath)]} {
147                return [$_path2widget($relpath) value]
148            }
149        }
150        return ""
151    }
152
153    # otherwise, set the value
154    if {$owner != $this && $owner != ""} {
155        return [eval $owner valuefor $path $args]
156    }
157    if {[llength $args] > 1} {
158        error "wrong # args: should be \"valuefor path ?newValue?\""
159    }
160
161    if {[info exists _path2widget($path)]} {
162        $_path2widget($path) value [lindex $args 0]
163    } else {
164        error "bad path \"$path\": should be one of [join [lsort [array names _path2widget]] {, }]"
165    }
166}
167
168# ----------------------------------------------------------------------
169# USAGE: dependenciesfor <path> ?<path>...?
170#
171# Used by embedded widgets such as a Controls panel to register the
172# various controls that are dependent on another one.  If only one
173# path is specified, then this method returns all known dependencies
174# for the specified <path>.  Otherwise, the additional <path>'s are
175# noted as being dependent on the first <path>.
176# ----------------------------------------------------------------------
177itcl::body Rappture::ControlOwner::dependenciesfor {path args} {
178    if {"" != $_owner} {
179        #
180        # Keep all dependencies at the highest level.
181        # That way, a structure can come and go, but the
182        # dependencies remain fixed in the topmost tool.
183        #
184        set plen [string length $_path]
185        if {"" != $_path && ![string equal -length $plen $_path $path]} {
186            set path $_path.$path
187        }
188        return [eval $_owner dependenciesfor $path $args]
189    }
190
191    # if this is a query operation, then look for the path
192    if {[llength $args] == 0} {
193        if {[info exists _dependencies($path)]} {
194            return $_dependencies($path)
195        }
196        return ""
197    }
198
199    # add new dependencies
200    if {![info exists _dependencies($path)]} {
201        set _dependencies($path) ""
202    }
203    foreach dpath $args {
204        set i [lsearch -exact $_dependencies($path) $dpath]
205        if {$i < 0} {
206            lappend _dependencies($path) $dpath
207        }
208    }
209}
210
211# ----------------------------------------------------------------------
212# USAGE: ownerfor <path> ?<skip>?
213#
214# Returns the ControlOwner that directly controls the specified <path>.
215# ----------------------------------------------------------------------
216itcl::body Rappture::ControlOwner::ownerfor {path {skip ""}} {
217    if {[info exists _path2widget($path)]} {
218        return $this
219    }
220
221    # can't find the path? try removing the prefix for this owner
222    set plen [string length $_path]
223    if {[string equal -length $plen $_path $path]} {
224        set relpath [string range $path [expr {$plen+1}] end]
225        if {[info exists _path2widget($relpath)]} {
226            return $this
227        }
228    }
229
230    # couldn't find this path?  then check all subordinates
231    foreach slave $_slaves {
232        if {$slave == $skip} {
233            continue  ;# skip this slave if it's already been searched
234        }
235        set rval [$slave ownerfor $path $this]
236        if {"" != $rval} {
237            return $rval
238        }
239    }
240
241    # check the owner as a last resort
242    if {"" != $_owner && $_owner != $skip} {
243        set rval [$_owner ownerfor $path $this]
244        if {"" != $rval} {
245            return $rval
246        }
247    }
248
249    return ""
250}
251
252# ----------------------------------------------------------------------
253# USAGE: load <xmlobj>
254#
255# Loads the contents of a Rappture <xmlobj> into the controls
256# associated with this tool.
257# ----------------------------------------------------------------------
258itcl::body Rappture::ControlOwner::load {newobj} {
259    if {![Rappture::library isvalid $newobj]} {
260        error "\"$newobj\" is not a Rappture::library"
261    }
262
263    foreach path [array names _path2widget] {
264        # the following elements do not accept "current" tags, skip them
265        set type [[tool] xml element -as type $path]
266        if {[lsearch {group separator control} $type] >= 0} {
267            continue
268        }
269
270        set type [[tool] xml element -as type $path]
271 puts stderr type=$type
272        if {[info exists _type2curpath($type)]} {
273            set currentpath $path.$_type2curpath($type)
274        } else {
275            # default incase i forgot an input type in _type2curpath
276            set currentpath $path.current
277        }
278
279        # copy new value to the XML tree
280        # also copy to the widget associated with the tree
281        #
282        # we only copy the values if they existed in newobj
283        # so we don't overwrite values that were set in previous loads.
284        # this is needed for when the users specify copy.from and copy.to
285        # in a loader. in this case, _path2widget holds a list of all
286        # widgets. if there are two loaders loading two different widgets,
287        # and each loader uses the copy from/to functionality,
288        # the second load could wipe out the values set in the first load
289        # because on the second load, the copied paths from the first load no
290        # longer exist in newobj and blanks are copied to the paths
291        # in [tool] xml set by the first loader. the solution is to check
292        # newobj and see if the path exists. if the path exists, then we copy
293        # it over to [tool] xml, otherwise we ignore it.
294        if {"" != [$newobj element -as type $currentpath]} {
295            [tool] xml copy $currentpath from $newobj $currentpath
296            set val [$newobj get $currentpath]
297            if {[string length $val] > 0
298                  || [llength [$newobj children $currentpath]] == 0} {
299                $_path2widget($path) value $val
300            } else {
301                set obj [$newobj element -as object $currentpath]
302                $_path2widget($path) value $obj
303            }
304        }
305    }
306}
307
308# ----------------------------------------------------------------------
309# USAGE: changed <path>
310#
311# Invoked automatically by the various widgets associated with this
312# tool whenever their value changes.  Sends notifications to any
313# client that has registered an interest via "notify add".
314# ----------------------------------------------------------------------
315itcl::body Rappture::ControlOwner::changed {path} {
316    if {"" != $_owner} {
317        set plen [string length $_path]
318        if {"" != $_path && ![string equal -length $plen $_path $path]} {
319            set path $_path.$path
320        }
321        $_owner changed $path
322    } else {
323        # send out any callback notifications
324        foreach owner [array names _owner2paths] {
325            foreach pattern $_owner2paths($owner) {
326                if {[string match $pattern $path]} {
327                    uplevel #0 $_callbacks($owner/$pattern)
328                    break
329                }
330            }
331        }
332
333        # find the control panel for each dependency, and tell it
334        # to update its layout.
335        foreach cpath [dependenciesfor $path] {
336            set wv [widgetfor $cpath]
337            while {"" != $wv} {
338                set wv [winfo parent $wv]
339                if {[winfo class $wv] == "Controls"} {
340                    $wv refresh
341                    break
342                }
343            }
344        }
345    }
346}
347
348# ----------------------------------------------------------------------
349# USAGE: regularize <path>
350#
351# Clients use this to get a full, regularized path for the specified
352# <path>, which may be relative to the current owner.
353# ----------------------------------------------------------------------
354itcl::body Rappture::ControlOwner::regularize {path} {
355    set owner [ownerfor $path]
356    if {$owner != $this && $owner != ""} {
357        return [$owner regularize $path]
358    }
359    set rpath ""
360    if {"" != $_xmlobj} {
361        set rpath [$_xmlobj element -as path $path]
362
363        # can't find the path? try removing the prefix for this owner
364        if {"" == $rpath} {
365            set plen [string length $_path]
366            if {[string equal -length $plen $_path $path]} {
367                set relpath [string range $path [expr {$plen+2}] end]
368                set rpath [$_xmlobj element -as path $relpath]
369            }
370        }
371
372        if {"" != $rpath && "" != $_path} {
373            # return a full name for the path
374            set rpath "$_path.$rpath"
375        }
376    }
377    return $rpath
378}
379
380# ----------------------------------------------------------------------
381# USAGE: notify add <owner> <path> <callback>
382# USAGE: notify info ?<owner>? ?<path>?
383# USAGE: notify remove <owner> ?<path> ...?
384#
385# Clients use this to request notifications about changes to a
386# particular <path> for a control under this tool.  Whenever the
387# value associated with <path> changes, the client identified by
388# <owner> is sent a message by invoking its <callback> routine.
389#
390# Notifications can be silenced by calling the "notify remove"
391# function.
392# ----------------------------------------------------------------------
393itcl::body Rappture::ControlOwner::notify {option args} {
394    switch -- $option {
395        add {
396            if {[llength $args] != 3} {
397                error "wrong # args: should be \"notify add owner path callback\""
398            }
399            set owner [lindex $args 0]
400            set path [lindex $args 1]
401            set cb [lindex $args 2]
402
403            if {[info exists _owner2paths($owner)]} {
404                set plist $_owner2paths($owner)
405            } else {
406                set plist ""
407            }
408
409            set i [lsearch -exact $plist $path]
410            if {$i < 0} { lappend _owner2paths($owner) $path }
411            set _callbacks($owner/$path) $cb
412        }
413        info {
414            if {[llength $args] == 0} {
415                # no args? then return all owners
416                return [array names _owner2paths]
417            } else {
418                set owner [lindex $args 0]
419                if {[info exists _owner2paths($owner)]} {
420                    set plist $_owner2paths($owner)
421                } else {
422                    set plist ""
423                }
424                if {[llength $args] == 1} {
425                    # 1 arg? then return paths for this owner
426                    return $plist
427                } elseif {[llength $args] == 2} {
428                    # 2 args? then return callback for this path
429                    set path [lindex $args 1]
430                    if {[info exists _callbacks($owner/$path)]} {
431                        return $_callbacks($owner/$path)
432                    }
433                    return ""
434                } else {
435                    error "wrong # args: should be \"notify info ?owner? ?path?\""
436                }
437            }
438        }
439        remove {
440            if {[llength $args] < 1} {
441                error "wrong # args: should be \"notify remove owner ?path ...?\""
442            }
443            set owner [lindex $args 0]
444
445            if {[llength $args] == 1} {
446                # no args? then delete all paths for this owner
447                if {[info exists _owner2paths($owner)]} {
448                    set plist $_owner2paths($owner)
449                } else {
450                    set plist ""
451                }
452            } else {
453                set plist [lrange $args 1 end]
454            }
455
456            # forget about the callback for each path
457            foreach path $plist {
458                catch {unset _callbacks($owner/$path)}
459
460                if {[info exists _owner2paths($owner)]} {
461                    set i [lsearch -exact $_owner2paths($owner) $path]
462                    if {$i >= 0} {
463                        set _owner2paths($owner) \
464                            [lreplace $_owner2paths($owner) $i $i]
465                    }
466                }
467            }
468        }
469    }
470}
471
472# ----------------------------------------------------------------------
473# USAGE: sync
474#
475# Used by descendents such as a Controls panel to register the
476# various controls associated with this page.  That way, this
477# ControlOwner knows what widgets to look at when syncing itself
478# to the underlying XML data.
479# ----------------------------------------------------------------------
480itcl::body Rappture::ControlOwner::sync {} {
481    # sync all of the widgets under control of this owner
482    if {"" != $_xmlobj} {
483        foreach path [lsort [array names _path2widget]] {
484            set type [$_xmlobj element -as type $path]
485            if {[lsearch {group separator control note} $type] >= 0} {
486                continue
487            }
488            $_xmlobj put $path.current [$_path2widget($path) value]
489        }
490    }
491
492    # sync all subordinate slaves as well
493    foreach slave $_slaves {
494        $slave sync
495    }
496}
497
498# ----------------------------------------------------------------------
499# USAGE: tool
500#
501# Clients use this to figure out which tool is associated with
502# this object.  If there is no parent ControlOwner, then this
503# must be the top-level tool.
504# ----------------------------------------------------------------------
505itcl::body Rappture::ControlOwner::tool {} {
506    if {"" != $_owner} {
507        return [$_owner tool]
508    }
509    return $this
510}
511
512# ----------------------------------------------------------------------
513# USAGE: _slave add <newobj>...
514#
515# Used internally to register the parent-child relationship whenever
516# one ControlOwner is registered to another.  When the parent syncs,
517# it causes all of its children to sync.  When a name is being
518# resolved, it is resolved locally first, then up to the parent for
519# further resolution.
520# ----------------------------------------------------------------------
521itcl::body Rappture::ControlOwner::_slave {option args} {
522    switch -- $option {
523        add {
524            eval lappend _slaves $args
525        }
526        default {
527            error "bad option \"$option\": should be add"
528        }
529    }
530}
Note: See TracBrowser for help on using the repository browser.