comparison engine/extensions/pychan/widgets/widget.py @ 336:16112ef84609

PyChan fixes: * Previous commits removed the ability to map events to unnamed widgets. Fixed.
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 24 Aug 2009 15:34:23 +0000
parents 48c99636453e
children dfd48d49c044
comparison
equal deleted inserted replaced
335:162662bf5c8a 336:16112ef84609
302 if widget.match(**kwargs): 302 if widget.match(**kwargs):
303 children.append(widget) 303 children.append(widget)
304 self.deepApply(_childCollector) 304 self.deepApply(_childCollector)
305 return children 305 return children
306 306
307 def getNamedChildren(self): 307 def getNamedChildren(self, include_unnamed = False):
308 """ 308 """
309 Create a dictionary of child widgets with the keys being 309 Create a dictionary of child widgets with the keys being
310 their name. This will contain only Widgets which have 310 their name. This will contain only Widgets which have
311 a name different from "__unnamed__" (which is the default). 311 a name different from "__unnamed__" (which is the default).
312 312
313 @param include_unnamed Defaults to false. If this is true unnamed widgets are added, too.
314
313 The values are lists of widgets, so not only unique names 315 The values are lists of widgets, so not only unique names
314 are handled correctly. 316 are handled correctly.
315 317
316 Usage:: 318 Usage::
317 children = widget.getNamedChildren() 319 children = widget.getNamedChildren()
318 for widget in children.get("info",[]) 320 for widget in children.get("info",[])
319 print widget.name , " == info" 321 print widget.name , " == info"
320 """ 322 """
321 children = {} 323 children = {}
322 def _childCollector(widget): 324 if include_unnamed:
323 if widget.has_name: 325 def _childCollector(widget):
324 children.setdefault(widget._name,[]).append(widget) 326 children.setdefault(widget._name,[]).append(widget)
327 else:
328 def _childCollector(widget):
329 if widget.has_name:
330 children.setdefault(widget._name,[]).append(widget)
325 self.deepApply(_childCollector) 331 self.deepApply(_childCollector)
326 return children 332 return children
327 333
328 def findChild(self,**kwargs): 334 def findChild(self,**kwargs):
329 """ Find the first contained child widgets by attribute values. 335 """ Find the first contained child widgets by attribute values.
465 "button/mouseEntered" : toggleButtonColorGreen, 471 "button/mouseEntered" : toggleButtonColorGreen,
466 "button/mouseExited" : toggleButtonColorBlue, 472 "button/mouseExited" : toggleButtonColorBlue,
467 }) 473 })
468 474
469 """ 475 """
470 children = self.getNamedChildren() 476 children = self.getNamedChildren(include_unnamed=True)
471 for descr,func in eventMap.items(): 477 for descr,func in eventMap.items():
472 name, event_name, group_name = events.splitEventDescriptor(descr) 478 name, event_name, group_name = events.splitEventDescriptor(descr)
473 #print name, event_name, group_name 479 #print name, event_name, group_name
474 widgets = children.get(name,[]) 480 widgets = children.get(name,[])
475 if widgets: 481 if widgets:
519 'myTextField' : 'Hello World!', 525 'myTextField' : 'Hello World!',
520 'myListBox' : ["1","2","3"] 526 'myListBox' : ["1","2","3"]
521 }) 527 })
522 528
523 """ 529 """
524 children = self.getNamedChildren() 530 children = self.getNamedChildren(include_unnamed=True)
525 for name,data in initialDataMap.items(): 531 for name,data in initialDataMap.items():
526 widgetList = children.get(name,[]) 532 widgetList = children.get(name,[])
527 for widget in widgetList: 533 for widget in widgetList:
528 widget.setInitialData(data) 534 widget.setInitialData(data)
529 535
538 'myTextField' : 'Hello World!', 544 'myTextField' : 'Hello World!',
539 'myListBox' : ["1","2","3"] 545 'myListBox' : ["1","2","3"]
540 }) 546 })
541 547
542 """ 548 """
543 children = self.getNamedChildren() 549 children = self.getNamedChildren(include_unnamed=True)
544 for name,data in dataMap.items(): 550 for name,data in dataMap.items():
545 widgetList = children.get(name,[]) 551 widgetList = children.get(name,[])
546 if len(widgetList) != 1: 552 if len(widgetList) != 1:
547 if get_manager().debug: 553 if get_manager().debug:
548 self.listNamedWidgets() 554 self.listNamedWidgets()
559 Usage:: 565 Usage::
560 data = guiElement.collectDataAsDict(['myTextField','myListBox']) 566 data = guiElement.collectDataAsDict(['myTextField','myListBox'])
561 print "You entered:",data['myTextField']," and selected ",data['myListBox'] 567 print "You entered:",data['myTextField']," and selected ",data['myListBox']
562 568
563 """ 569 """
564 children = self.getNamedChildren() 570 children = self.getNamedChildren(include_unnamed=True)
565 dataMap = {} 571 dataMap = {}
566 for name in widgetNames: 572 for name in widgetNames:
567 widgetList = children.get(name,[]) 573 widgetList = children.get(name,[])
568 if len(widgetList) != 1: 574 if len(widgetList) != 1:
569 if get_manager().debug: 575 if get_manager().debug:
592 print "You entered:",text," and selected item nr",selected 598 print "You entered:",text," and selected item nr",selected
593 # Single elements are handled gracefully, too: 599 # Single elements are handled gracefully, too:
594 test = guiElement.collectData('testElement') 600 test = guiElement.collectData('testElement')
595 601
596 """ 602 """
597 children = self.getNamedChildren() 603 children = self.getNamedChildren(include_unnamed=True)
598 dataList = [] 604 dataList = []
599 for name in widgetNames: 605 for name in widgetNames:
600 widgetList = children.get(name,[]) 606 widgetList = children.get(name,[])
601 if len(widgetList) != 1: 607 if len(widgetList) != 1:
602 if get_manager().debug: 608 if get_manager().debug: