__repr__ dissocié de __str__
[minwii.git] / src / pgu / gui / form.py
1 """
2 """
3 import widget
4
5 class Form(widget.Widget):
6 """A form that automatically will contain all named widgets.
7
8 <p>After a form is created, all named widget that are subsequently created are added
9 to that form. You may use dict style access to access named widgets.</p>
10
11 <pre>Form()</pre>
12
13 <strong>Example</strong>
14 <code>
15 f = gui.Form()
16
17 w = gui.Input("Phil",name="firstname")
18 w = gui.Input("Hassey",name="lastname")
19
20 print f.results()
21 print ''
22 print f.items()
23 print ''
24 print f['firstname'].value
25 print f['lastname'].value
26 </code>
27 """
28
29 def __init__(self):
30 widget.Widget.__init__(self,decorate=False)
31 self._elist = []
32 self._emap = {}
33 self._dirty = 0
34 Form.form = self
35
36 def add(self,e,name=None,value=None):
37 if name != None: e.name = name
38 if value != None: e.value = value
39 self._elist.append(e)
40 self._dirty = 1
41
42 def _clean(self):
43 for e in self._elist[:]:
44 if not hasattr(e,'name') or e.name == None:
45 self._elist.remove(e)
46 self._emap = {}
47 for e in self._elist:
48 self._emap[e.name] = e
49 self._dirty = 0
50
51 def __getitem__(self,k):
52 if self._dirty: self._clean()
53 return self._emap[k]
54
55 def __contains__(self,k):
56 if self._dirty: self._clean()
57 if k in self._emap: return True
58 return False
59
60 def results(self):
61 """Return a dict of name => values.
62
63 <pre>Form.results(): return dict</pre>
64 """
65 if self._dirty: self._clean()
66 r = {}
67 for e in self._elist:
68 r[e.name] = e.value
69 return r
70
71 def items(self):
72 """Return a list of name, value keys.
73
74 <pre>Form.items(): return list</pre>
75 """
76 return self.results().items()
77
78 #def start(self):
79 # Object.start(self,-1)