annotate engine/extensions/pychan/tools.py @ 343:8e71629c4c43

Several changes to layer, camera and map dialogs: * Renamed CameraEditor and LayerEditor to CameraDialog and LayerDialog * Moved CameraDialog and LayerDialog into their own modules in scripts.gui so other components can use them. * Prettier camera dialog * Added LayerDialog to map wizard
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 25 Aug 2009 01:21:00 +0000
parents 48c99636453e
children 7ea98190a75e
rev   line source
331
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
1 #!/usr/bin/env python
330
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
2 # -*- coding: utf-8 -*-
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
3
331
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
4 # ####################################################################
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
5 # Copyright (C) 2005-2009 by the FIFE team
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
6 # http://www.fifengine.de
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
7 # This file is part of FIFE.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
8 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
9 # FIFE is free software; you can redistribute it and/or
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
10 # modify it under the terms of the GNU Lesser General Public
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
11 # License as published by the Free Software Foundation; either
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
12 # version 2.1 of the License, or (at your option) any later version.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
13 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
14 # This library is distributed in the hope that it will be useful,
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
17 # Lesser General Public License for more details.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
18 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
19 # You should have received a copy of the GNU Lesser General Public
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
20 # License along with this library; if not, write to the
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
21 # Free Software Foundation, Inc.,
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
23 # ####################################################################
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 330
diff changeset
24
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
25 """
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
26 Functional utilities designed for pychan use cases.
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
27 """
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
28
163
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
29 import exceptions
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
30
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
31 ### Functools ###
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
32
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
33 def applyOnlySuitable(func,*args,**kwargs):
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
34 """
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
35 This nifty little function takes another function and applies it to a dictionary of
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
36 keyword arguments. If the supplied function does not expect one or more of the
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
37 keyword arguments, these are silently discarded. The result of the application is returned.
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
38 This is useful to pass information to callbacks without enforcing a particular signature.
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
39 """
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
40 if hasattr(func,'im_func'):
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
41 code = func.im_func.func_code
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
42 varnames = code.co_varnames[1:code.co_argcount]#ditch bound instance
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
43 else:
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
44 code = func.func_code
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
45 varnames = code.co_varnames[0:code.co_argcount]
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
46
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
47 #http://docs.python.org/lib/inspect-types.html
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
48 if code.co_flags & 8:
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
49 return func(*args,**kwargs)
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
50 for name,value in kwargs.items():
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
51 if name not in varnames:
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
52 del kwargs[name]
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
53 return func(*args,**kwargs)
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
54
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
55 def callbackWithArguments(callback,*args,**kwargs):
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
56 """
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
57 Curries a function with extra arguments to
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
58 create a suitable callback.
129
9a1529f9625e * Indentation patch by GreyGhost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 0
diff changeset
59
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
60 If you don't know what this means, don't worry.
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
61 It is designed for the case where you need
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
62 different buttons to execute basically the same code
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
63 with different argumnets.
129
9a1529f9625e * Indentation patch by GreyGhost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 0
diff changeset
64
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
65 Usage::
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
66 # The target callback
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
67 def printStuff(text):
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
68 print text
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
69 # Mapping the events
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
70 gui.mapEvents({
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
71 'buttonHello' : callbackWithArguments(printStuff,"Hello"),
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
72 'buttonBye' : callbackWithArguments(printStuff,"Adieu")
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
73 })
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
74 """
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
75 def real_callback():
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
76 callback(*args,**kwargs)
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
77 return real_callback
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
78
163
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
79 def attrSetCallback(**kwargs):
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
80 """
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
81 Generates an event callback that sets attributes on the widget
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
82 it is called on. This is especially useful for mouseEntered/Exited
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
83 events - to create hover effects.
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
84
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
85 It takes a set of keyword arguments. The keys are treated as attribute names,
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
86 which are then set to the corresponding value when the callback is called.
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
87 Some key names are treated special - see below.
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
88
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
89 Usage - Example adapted from demo application::
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
90 eventMap = {
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
91 'creditsLink' : showCreditsCallback,
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
92 'creditsLink/mouseEntered' : attrSetCallback(
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
93 text = "Show credits!",
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
94 background_color = (255,255,0,255),
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
95 do__adaptLayout=True),
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
96 'creditsLink/mouseExited' : attrSetCallback(text = "Credits"),
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
97 gui.mapEvents(eventMap)
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
98
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
99 Now when the mouse enters the creditsLink (a Label in our case), the following code will be executed::
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
100 #widget is the creditsLink - given to the event callback.
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
101 widget.text = "Show credits!"
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
102 widget.background_color = (255,255,0,255)
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
103 widget.adaptLayout()
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
104
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
105 The C{do__adaptLayout} argument causes the method C{adaptLayout} to be called.
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
106 In fact any key starting with C{do__} results in such a method call. The order
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
107 of execution of such calls is undefined.
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
108
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
109 Keys starting with an underscore raise a L{execptions.PrivateFunctionalityError}.
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
110 """
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
111 do_calls = []
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
112
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
113 for name in kwargs.keys():
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
114 if name.startswith("_"):
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
115 raise exceptions.PrivateFunctionalityError(name)
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
116 if name.startswith("do__"):
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
117 do_calls.append(name[4:])
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
118 del kwargs[name]
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
119
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
120 def attrSet_callback(widget=None):
163
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
121 for name,value in kwargs.items():
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
122 setattr(widget,name,value)
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
123 for method_name in do_calls:
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
124 method = getattr(widget,method_name)
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
125 method()
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
126 return attrSet_callback
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
127
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
128 def chainCallbacks(*args):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
129 """
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
130 Chains callbacks to be called one after the other.
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
131
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
132 Example Usage::
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
133 def print_event(event=0):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
134 print event
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
135 def print_widget(widget=0):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
136 print widget
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
137 callback = tools.chainCallbacks(doSomethingUseful, print_event, print_widget)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
138 guiElement.capture(callback)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
139 """
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
140 callbacks = args
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
141 def chain_callback(event=0,widget=0):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
142 for callback in callbacks:
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
143 applyOnlySuitable(callback, event=event, widget=widget)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 163
diff changeset
144 return chain_callback
163
31718fa356f8 Backported the attrSetCallback from pychan_rework.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
145
330
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
146 def repeatALot(n = 1000):
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
147 """
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
148 Internal decorator used to profile some pychan functions.
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
149 Only use with functions without side-effect.
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
150
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
151 Usage::
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
152 @repeatALot(n=10000)
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
153 def findChild(self,**kwargs):
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
154 ...
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
155 """
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
156 def wrap_f(f):
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
157 def new_f(*args,**kwargs):
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
158 for i in xrange(n):
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
159 f(*args,**kwargs)
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
160 return f(*args,**kwargs)
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
161 return new_f
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
162 return wrap_f
33dd55160a9d * Added a new method Widget.getNamedChildren
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
163
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
164 def this_is_deprecated(func,message=None):
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
165 if message is None:
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
166 message = repr(func)
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
167 def wrapped_func(*args,**kwargs):
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
168 print "PyChan: You are using the DEPRECATED functionality: %s" % message
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
169 return func(*args,**kwargs)
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
170 return wrapped_func