Package pyjamas :: Package ui
[hide private]
[frames] | no frames]

Source Code for Package pyjamas.ui

1 # Copyright 2006 James Tauber and contributors 2 # Copyright 2009 Luke Kenneth Casson Leighton 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 -class HasHorizontalAlignment:
17 ALIGN_LEFT = "left" 18 ALIGN_CENTER = "center" 19 ALIGN_RIGHT = "right"
20
21 -class HasVerticalAlignment:
22 ALIGN_TOP = "top" 23 ALIGN_MIDDLE = "middle" 24 ALIGN_BOTTOM = "bottom"
25
26 -class HasAlignment:
27 ALIGN_BOTTOM = "bottom" 28 ALIGN_MIDDLE = "middle" 29 ALIGN_TOP = "top" 30 ALIGN_CENTER = "center" 31 ALIGN_LEFT = "left" 32 ALIGN_RIGHT = "right"
33 34 PROP_NAME = 0 35 PROP_DESC = 1 36 PROP_FNAM = 2 37 PROP_TYPE = 3 38 39 ELPROP_NAME = 0 40 ELPROP_DESC = 1 41 ELPROP_FNAM = 2 42 ELPROP_TYPE = 3 43 ELPROP_DFLT = 4
44 45 -def get_list_columns(props, cols):
46 res = [] 47 for p in props: 48 r = () 49 for idx in cols: 50 r.append(p[idx]) 51 res.append(r) 52 return res
53
54 -def get_prop_widget_function_names(props):
55 return get_list_columns(props, (PROP_FNAM,))
56
57 -class Applier(object):
58 59 _props = [] 60 _elem_props = [] 61
62 - def __init__(self, **kwargs):
63 """ use this to apply properties as a dictionary, e.g.:: 64 65 x = klass(..., StyleName='class-name') 66 67 will do:: 68 69 x = klass(...) 70 x.setStyleName('class-name') 71 72 and:: 73 74 x = klass(..., Size=("100%", "20px"), Visible=False) 75 76 will do:: 77 78 x = klass(...) 79 x.setSize("100%", "20px") 80 x.setVisible(False) 81 """ 82 83 self.applyValues(**kwargs)
84
85 - def applyValues(self, **kwargs):
86 87 for (prop, args) in kwargs.items(): 88 fn = getattr(self, "set%s" % prop, None) 89 if not fn: 90 raise Exception("setter function for %s does not exist in %s" % (prop, self.__class__.__name__)) 91 args = kwargs[prop] 92 if isinstance(args, tuple): 93 fn(*args) 94 else: 95 fn(args)
96
97 - def retrieveValues(self, *args):
98 """ use this function to obtain a dictionary of properties, as 99 stored in getXXX functions. 100 """ 101 102 res = {} 103 for prop in args: 104 fn = getattr(self, "get%s" % prop, None) 105 if not fn: 106 raise Exception("getter function for %s does not exist" % prop) 107 res[prop] = fn() 108 109 return res
110 111 @classmethod
112 - def _getProps(self):
113 return self._props
114 115 @classmethod
116 - def _getElementProps(self):
117 return self._elem_props
118
119 - def setDefaults(self, defaults):
120 divs = self.retrieveValues(wnames) 121 for p in get_prop_widget_function_names(self._getProps()): 122 defaults[p[PROP_NAME]] = divs[p[PROP_FNAM]]
123
124 - def updateInstance(self, app_context):
125 args = {} 126 for p in self._getProps(): 127 val = app_context.getAppProperty(p[0]) 128 convert_to_type = p[PROP_TYPE] 129 if convert_to_type: 130 val = convert_to_type(val) if val else None 131 args[p[PROP_FNAM]] = val 132 133 self.applyValues(**args)
134
135 - def setElementProperties(self, context, elemProps):
136 args = {} 137 for p in self._getElementProps(): 138 if elemProps.has_key(p): 139 val = elemProps[p] 140 convert_to_type = p[ELPROP_TYPE] 141 if convert_to_type: 142 val = convert_to_type(val) if val else None 143 else: 144 val = p[ELPROP_DFLT] 145 if val is None: 146 continue 147 args[p[ELPROP_FNAM]] = (context, val,) 148 149 self.applyValues(**args)
150