Mercurial > fife-parpg
changeset 163:31718fa356f8
Backported the attrSetCallback from pychan_rework.
author | phoku@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Wed, 15 Oct 2008 09:13:59 +0000 |
parents | c3418554467f |
children | 5b04a7d3ded6 |
files | engine/extensions/pychan/exceptions.py engine/extensions/pychan/tools.py |
diffstat | 2 files changed, 57 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/engine/extensions/pychan/exceptions.py Tue Oct 14 20:40:02 2008 +0000 +++ b/engine/extensions/pychan/exceptions.py Wed Oct 15 09:13:59 2008 +0000 @@ -28,3 +28,9 @@ """ An error that occured during parsing an attribute. """ + +class PrivateFunctionalityError(RuntimeError): + """ + Exception raised if private attributes/functions are used." + """ + pass
--- a/engine/extensions/pychan/tools.py Tue Oct 14 20:40:02 2008 +0000 +++ b/engine/extensions/pychan/tools.py Wed Oct 15 09:13:59 2008 +0000 @@ -1,5 +1,7 @@ # coding: utf-8 +import exceptions + ### Functools ### def applyOnlySuitable(func,**kwargs): @@ -48,6 +50,55 @@ callback(*args,**kwargs) return real_callback +def attrSetCallback(**kwargs): + """ + Generates an event callback that sets attributes on the widget + it is called on. This is especially useful for mouseEntered/Exited + events - to create hover effects. + + It takes a set of keyword arguments. The keys are treated as attribute names, + which are then set to the corresponding value when the callback is called. + Some key names are treated special - see below. + + Usage - Example adapted from demo application:: + eventMap = { + 'creditsLink' : showCreditsCallback, + 'creditsLink/mouseEntered' : attrSetCallback( + text = "Show credits!", + background_color = (255,255,0,255), + do__adaptLayout=True), + 'creditsLink/mouseExited' : attrSetCallback(text = "Credits"), + gui.mapEvents(eventMap) + + Now when the mouse enters the creditsLink (a Label in our case), the following code will be executed:: + #widget is the creditsLink - given to the event callback. + widget.text = "Show credits!" + widget.background_color = (255,255,0,255) + widget.adaptLayout() + + The C{do__adaptLayout} argument causes the method C{adaptLayout} to be called. + In fact any key starting with C{do__} results in such a method call. The order + of execution of such calls is undefined. + + Keys starting with an underscore raise a L{execptions.PrivateFunctionalityError}. + """ + do_calls = [] + + for name in kwargs.keys(): + if name.startswith("_"): + raise exceptions.PrivateFunctionalityError(name) + if name.startswith("do__"): + do_calls.append(name[4:]) + del kwargs[name] + + def callback(widget=None): + for name,value in kwargs.items(): + setattr(widget,name,value) + for method_name in do_calls: + method = getattr(widget,method_name) + method() + return callback + def this_is_deprecated(func,message=None): if message is None: message = repr(func)