Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / pgu / gui / form.py
1 """
2 """
3 from . import widget
4
5 class Form(widget.Widget):
6     """A form that automatically will contain all named widgets.
7     
8     After a form is created, all named widget that are subsequently created are 
9     added to that form.  You may use dict style access to access named widgets.
10     
11     Example:
12
13         f = gui.Form()
14         
15         w = gui.Input("Phil",name="firstname")
16         w = gui.Input("Hassey",name="lastname")
17         
18         print(f.results())
19         print('')
20         print(f.items())
21         print('')
22         print(f['firstname'].value)
23         print(f['lastname'].value)
24
25     """
26
27     # The current form instance
28     form = None
29     # The list of PGU widgets that are tracked by this form
30     _elist = None
31     # A mapping of PGU widgets tracked by this form (name -> instance)
32     _emap = None
33     # The dirty flag is set when a new widget is added to the form
34     _dirty = 0
35     
36     def __init__(self):
37         widget.Widget.__init__(self,decorate=False)
38         self._elist = []
39         self._emap = {}
40         self._dirty = 0
41         # Register this form as the one used by new widgets
42         Form.form = self
43     
44     def add(self,e,name=None,value=None):
45         """Adds a PGU widget to this form"""
46         if name != None: e.name = name
47         if value != None: e.value = value
48         self._elist.append(e)
49         self._dirty = 1
50     
51     def _clean(self):
52         # Remove elements from our list if they no longer have an assigned name
53         for e in self._elist[:]:
54             if not hasattr(e,'name') or e.name == None:
55                 self._elist.remove(e)
56         # Update the name-to-widget mapping
57         self._emap = {}
58         for e in self._elist:
59             self._emap[e.name] = e
60         self._dirty = 0
61     
62     def __getitem__(self,k):
63         """Returns the widget instance given the name of the widget"""
64         if self._dirty: self._clean()
65         return self._emap[k]
66     
67     def __contains__(self,k):
68         """Returns true if this form contains the named widget"""
69         if self._dirty: self._clean()
70         if k in self._emap: return True
71         return False
72     
73     def results(self):
74         """Return a dict of name, widget-value pairs."""
75         if self._dirty: self._clean()
76         r = {}
77         for e in self._elist:
78             # Make sure the widget has a 'value' (eg tables do not)
79             if (hasattr(e, "value")):
80                 r[e.name] = e.value
81             else:
82                 r[e.name] = None
83         return r
84     
85     def items(self):
86         """Return a list of name, widget pairs."""
87         return self.results().items()
88     
89

UCC git Repository :: git.ucc.asn.au