| 1 | n/a | # Module 'panel' |
|---|
| 2 | n/a | # |
|---|
| 3 | n/a | # Support for the Panel library. |
|---|
| 4 | n/a | # Uses built-in module 'pnl'. |
|---|
| 5 | n/a | # Applications should use 'panel.function' instead of 'pnl.function'; |
|---|
| 6 | n/a | # most 'pnl' functions are transparently exported by 'panel', |
|---|
| 7 | n/a | # but dopanel() is overridden and you have to use this version |
|---|
| 8 | n/a | # if you want to use callbacks. |
|---|
| 9 | n/a | from warnings import warnpy3k |
|---|
| 10 | n/a | warnpy3k("the panel module has been removed in Python 3.0", stacklevel=2) |
|---|
| 11 | n/a | del warnpy3k |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | import pnl |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | debug = 0 |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | # Test if an object is a list. |
|---|
| 21 | n/a | # |
|---|
| 22 | n/a | def is_list(x): |
|---|
| 23 | n/a | return type(x) == type([]) |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | # Reverse a list. |
|---|
| 27 | n/a | # |
|---|
| 28 | n/a | def reverse(list): |
|---|
| 29 | n/a | res = [] |
|---|
| 30 | n/a | for item in list: |
|---|
| 31 | n/a | res.insert(0, item) |
|---|
| 32 | n/a | return res |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | # Get an attribute of a list, which may itself be another list. |
|---|
| 36 | n/a | # Don't use 'prop' for name. |
|---|
| 37 | n/a | # |
|---|
| 38 | n/a | def getattrlist(list, name): |
|---|
| 39 | n/a | for item in list: |
|---|
| 40 | n/a | if item and is_list(item) and item[0] == name: |
|---|
| 41 | n/a | return item[1:] |
|---|
| 42 | n/a | return [] |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | # Get a property of a list, which may itself be another list. |
|---|
| 46 | n/a | # |
|---|
| 47 | n/a | def getproplist(list, name): |
|---|
| 48 | n/a | for item in list: |
|---|
| 49 | n/a | if item and is_list(item) and item[0] == 'prop': |
|---|
| 50 | n/a | if len(item) > 1 and item[1] == name: |
|---|
| 51 | n/a | return item[2:] |
|---|
| 52 | n/a | return [] |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | # Test if an actuator description contains the property 'end-of-group' |
|---|
| 56 | n/a | # |
|---|
| 57 | n/a | def is_endgroup(list): |
|---|
| 58 | n/a | x = getproplist(list, 'end-of-group') |
|---|
| 59 | n/a | return (x and x[0] == '#t') |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | # Neatly display an actuator definition given as S-expression |
|---|
| 63 | n/a | # the prefix string is printed before each line. |
|---|
| 64 | n/a | # |
|---|
| 65 | n/a | def show_actuator(prefix, a): |
|---|
| 66 | n/a | for item in a: |
|---|
| 67 | n/a | if not is_list(item): |
|---|
| 68 | n/a | print prefix, item |
|---|
| 69 | n/a | elif item and item[0] == 'al': |
|---|
| 70 | n/a | print prefix, 'Subactuator list:' |
|---|
| 71 | n/a | for a in item[1:]: |
|---|
| 72 | n/a | show_actuator(prefix + ' ', a) |
|---|
| 73 | n/a | elif len(item) == 2: |
|---|
| 74 | n/a | print prefix, item[0], '=>', item[1] |
|---|
| 75 | n/a | elif len(item) == 3 and item[0] == 'prop': |
|---|
| 76 | n/a | print prefix, 'Prop', item[1], '=>', |
|---|
| 77 | n/a | print item[2] |
|---|
| 78 | n/a | else: |
|---|
| 79 | n/a | print prefix, '?', item |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | # Neatly display a panel. |
|---|
| 83 | n/a | # |
|---|
| 84 | n/a | def show_panel(prefix, p): |
|---|
| 85 | n/a | for item in p: |
|---|
| 86 | n/a | if not is_list(item): |
|---|
| 87 | n/a | print prefix, item |
|---|
| 88 | n/a | elif item and item[0] == 'al': |
|---|
| 89 | n/a | print prefix, 'Actuator list:' |
|---|
| 90 | n/a | for a in item[1:]: |
|---|
| 91 | n/a | show_actuator(prefix + ' ', a) |
|---|
| 92 | n/a | elif len(item) == 2: |
|---|
| 93 | n/a | print prefix, item[0], '=>', item[1] |
|---|
| 94 | n/a | elif len(item) == 3 and item[0] == 'prop': |
|---|
| 95 | n/a | print prefix, 'Prop', item[1], '=>', |
|---|
| 96 | n/a | print item[2] |
|---|
| 97 | n/a | else: |
|---|
| 98 | n/a | print prefix, '?', item |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | # Exception raised by build_actuator or build_panel. |
|---|
| 102 | n/a | # |
|---|
| 103 | n/a | panel_error = 'panel error' |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | # Dummy callback used to initialize the callbacks. |
|---|
| 107 | n/a | # |
|---|
| 108 | n/a | def dummy_callback(arg): |
|---|
| 109 | n/a | pass |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | # Assign attributes to members of the target. |
|---|
| 113 | n/a | # Attribute names in exclist are ignored. |
|---|
| 114 | n/a | # The member name is the attribute name prefixed with the prefix. |
|---|
| 115 | n/a | # |
|---|
| 116 | n/a | def assign_members(target, attrlist, exclist, prefix): |
|---|
| 117 | n/a | for item in attrlist: |
|---|
| 118 | n/a | if is_list(item) and len(item) == 2 and item[0] not in exclist: |
|---|
| 119 | n/a | name, value = item[0], item[1] |
|---|
| 120 | n/a | ok = 1 |
|---|
| 121 | n/a | if value[0] in '-0123456789': |
|---|
| 122 | n/a | value = eval(value) |
|---|
| 123 | n/a | elif value[0] == '"': |
|---|
| 124 | n/a | value = value[1:-1] |
|---|
| 125 | n/a | elif value == 'move-then-resize': |
|---|
| 126 | n/a | # Strange default set by Panel Editor... |
|---|
| 127 | n/a | ok = 0 |
|---|
| 128 | n/a | else: |
|---|
| 129 | n/a | print 'unknown value', value, 'for', name |
|---|
| 130 | n/a | ok = 0 |
|---|
| 131 | n/a | if ok: |
|---|
| 132 | n/a | lhs = 'target.' + prefix + name |
|---|
| 133 | n/a | stmt = lhs + '=' + repr(value) |
|---|
| 134 | n/a | if debug: print 'exec', stmt |
|---|
| 135 | n/a | try: |
|---|
| 136 | n/a | exec stmt + '\n' |
|---|
| 137 | n/a | except KeyboardInterrupt: # Don't catch this! |
|---|
| 138 | n/a | raise KeyboardInterrupt |
|---|
| 139 | n/a | except: |
|---|
| 140 | n/a | print 'assign failed:', stmt |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | # Build a real actuator from an actuator description. |
|---|
| 144 | n/a | # Return a pair (actuator, name). |
|---|
| 145 | n/a | # |
|---|
| 146 | n/a | def build_actuator(descr): |
|---|
| 147 | n/a | namelist = getattrlist(descr, 'name') |
|---|
| 148 | n/a | if namelist: |
|---|
| 149 | n/a | # Assume it is a string |
|---|
| 150 | n/a | actuatorname = namelist[0][1:-1] |
|---|
| 151 | n/a | else: |
|---|
| 152 | n/a | actuatorname = '' |
|---|
| 153 | n/a | type = descr[0] |
|---|
| 154 | n/a | if type[:4] == 'pnl_': type = type[4:] |
|---|
| 155 | n/a | act = pnl.mkact(type) |
|---|
| 156 | n/a | act.downfunc = act.activefunc = act.upfunc = dummy_callback |
|---|
| 157 | n/a | # |
|---|
| 158 | n/a | assign_members(act, descr[1:], ['al', 'data', 'name'], '') |
|---|
| 159 | n/a | # |
|---|
| 160 | n/a | # Treat actuator-specific data |
|---|
| 161 | n/a | # |
|---|
| 162 | n/a | datalist = getattrlist(descr, 'data') |
|---|
| 163 | n/a | prefix = '' |
|---|
| 164 | n/a | if type[-4:] == 'puck': |
|---|
| 165 | n/a | prefix = 'puck_' |
|---|
| 166 | n/a | elif type == 'mouse': |
|---|
| 167 | n/a | prefix = 'mouse_' |
|---|
| 168 | n/a | assign_members(act, datalist, [], prefix) |
|---|
| 169 | n/a | # |
|---|
| 170 | n/a | return act, actuatorname |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | |
|---|
| 173 | n/a | # Build all sub-actuators and add them to the super-actuator. |
|---|
| 174 | n/a | # The super-actuator must already have been added to the panel. |
|---|
| 175 | n/a | # Sub-actuators with defined names are added as members to the panel |
|---|
| 176 | n/a | # so they can be referenced as p.name. |
|---|
| 177 | n/a | # |
|---|
| 178 | n/a | # Note: I have no idea how panel.endgroup() works when applied |
|---|
| 179 | n/a | # to a sub-actuator. |
|---|
| 180 | n/a | # |
|---|
| 181 | n/a | def build_subactuators(panel, super_act, al): |
|---|
| 182 | n/a | # |
|---|
| 183 | n/a | # This is nearly the same loop as below in build_panel(), |
|---|
| 184 | n/a | # except a call is made to addsubact() instead of addact(). |
|---|
| 185 | n/a | # |
|---|
| 186 | n/a | for a in al: |
|---|
| 187 | n/a | act, name = build_actuator(a) |
|---|
| 188 | n/a | act.addsubact(super_act) |
|---|
| 189 | n/a | if name: |
|---|
| 190 | n/a | stmt = 'panel.' + name + ' = act' |
|---|
| 191 | n/a | if debug: print 'exec', stmt |
|---|
| 192 | n/a | exec stmt + '\n' |
|---|
| 193 | n/a | if is_endgroup(a): |
|---|
| 194 | n/a | panel.endgroup() |
|---|
| 195 | n/a | sub_al = getattrlist(a, 'al') |
|---|
| 196 | n/a | if sub_al: |
|---|
| 197 | n/a | build_subactuators(panel, act, sub_al) |
|---|
| 198 | n/a | # |
|---|
| 199 | n/a | # Fix the actuator to which whe just added subactuators. |
|---|
| 200 | n/a | # This can't hurt (I hope) and is needed for the scroll actuator. |
|---|
| 201 | n/a | # |
|---|
| 202 | n/a | super_act.fixact() |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | |
|---|
| 205 | n/a | # Build a real panel from a panel definition. |
|---|
| 206 | n/a | # Return a panel object p, where for each named actuator a, p.name is a |
|---|
| 207 | n/a | # reference to a. |
|---|
| 208 | n/a | # |
|---|
| 209 | n/a | def build_panel(descr): |
|---|
| 210 | n/a | # |
|---|
| 211 | n/a | # Sanity check |
|---|
| 212 | n/a | # |
|---|
| 213 | n/a | if (not descr) or descr[0] != 'panel': |
|---|
| 214 | n/a | raise panel_error, 'panel description must start with "panel"' |
|---|
| 215 | n/a | # |
|---|
| 216 | n/a | if debug: show_panel('', descr) |
|---|
| 217 | n/a | # |
|---|
| 218 | n/a | # Create an empty panel |
|---|
| 219 | n/a | # |
|---|
| 220 | n/a | panel = pnl.mkpanel() |
|---|
| 221 | n/a | # |
|---|
| 222 | n/a | # Assign panel attributes |
|---|
| 223 | n/a | # |
|---|
| 224 | n/a | assign_members(panel, descr[1:], ['al'], '') |
|---|
| 225 | n/a | # |
|---|
| 226 | n/a | # Look for actuator list |
|---|
| 227 | n/a | # |
|---|
| 228 | n/a | al = getattrlist(descr, 'al') |
|---|
| 229 | n/a | # |
|---|
| 230 | n/a | # The order in which actuators are created is important |
|---|
| 231 | n/a | # because of the endgroup() operator. |
|---|
| 232 | n/a | # Unfortunately the Panel Editor outputs the actuator list |
|---|
| 233 | n/a | # in reverse order, so we reverse it here. |
|---|
| 234 | n/a | # |
|---|
| 235 | n/a | al = reverse(al) |
|---|
| 236 | n/a | # |
|---|
| 237 | n/a | for a in al: |
|---|
| 238 | n/a | act, name = build_actuator(a) |
|---|
| 239 | n/a | act.addact(panel) |
|---|
| 240 | n/a | if name: |
|---|
| 241 | n/a | stmt = 'panel.' + name + ' = act' |
|---|
| 242 | n/a | exec stmt + '\n' |
|---|
| 243 | n/a | if is_endgroup(a): |
|---|
| 244 | n/a | panel.endgroup() |
|---|
| 245 | n/a | sub_al = getattrlist(a, 'al') |
|---|
| 246 | n/a | if sub_al: |
|---|
| 247 | n/a | build_subactuators(panel, act, sub_al) |
|---|
| 248 | n/a | # |
|---|
| 249 | n/a | return panel |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | # Wrapper around pnl.dopanel() which calls call-back functions. |
|---|
| 253 | n/a | # |
|---|
| 254 | n/a | def my_dopanel(): |
|---|
| 255 | n/a | # Extract only the first 4 elements to allow for future expansion |
|---|
| 256 | n/a | a, down, active, up = pnl.dopanel()[:4] |
|---|
| 257 | n/a | if down: |
|---|
| 258 | n/a | down.downfunc(down) |
|---|
| 259 | n/a | if active: |
|---|
| 260 | n/a | active.activefunc(active) |
|---|
| 261 | n/a | if up: |
|---|
| 262 | n/a | up.upfunc(up) |
|---|
| 263 | n/a | return a |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | # Create one or more panels from a description file (S-expressions) |
|---|
| 267 | n/a | # generated by the Panel Editor. |
|---|
| 268 | n/a | # |
|---|
| 269 | n/a | def defpanellist(file): |
|---|
| 270 | n/a | import panelparser |
|---|
| 271 | n/a | descrlist = panelparser.parse_file(open(file, 'r')) |
|---|
| 272 | n/a | panellist = [] |
|---|
| 273 | n/a | for descr in descrlist: |
|---|
| 274 | n/a | panellist.append(build_panel(descr)) |
|---|
| 275 | n/a | return panellist |
|---|
| 276 | n/a | |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | # Import everything from built-in method pnl, so the user can always |
|---|
| 279 | n/a | # use panel.foo() instead of pnl.foo(). |
|---|
| 280 | n/a | # This gives *no* performance penalty once this module is imported. |
|---|
| 281 | n/a | # |
|---|
| 282 | n/a | from pnl import * # for export |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | dopanel = my_dopanel # override pnl.dopanel |
|---|