comparison clients/editor/scripts/gui/action.py @ 255:51cc05d862f2

Merged editor_rewrite branch to trunk. This contains changes that may break compatibility against existing clients. For a list of changes that may affect your client, see: http://wiki.fifengine.de/Changes_to_pychan_and_FIFE_in_editor_rewrite_branch
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 08 Jun 2009 16:00:02 +0000
parents
children 8b125ec749d7
comparison
equal deleted inserted replaced
254:10b5f7f36dd4 255:51cc05d862f2
1 from scripts.events.signal import Signal
2 import pychan.internal
3
4 changed = Signal(providing_args=[])
5 toggled = Signal(providing_args=["toggled"])
6 activated = Signal(providing_args=[])
7 #triggered = Signal(providing_args=["action"])
8
9 class Action:
10 def __init__(self, text="", icon="", separator=False, checkable=False, checked=False):
11 self._separator = separator
12 self._text = text
13 self._icon = icon
14 self._shortcut = ""
15 self._helptext = ""
16 self._enabled = True
17 self._checked = checked
18 self._checkable = checkable
19
20 def __str__(self):
21 return "%s(name='%s')" % (self.__class__.__name__,self.text)
22
23 def __repr__(self):
24 return "<%s(name='%s') at %x>" % (self.__class__.__name__,self.text,id(self))
25
26
27 def activate(self):
28 if self.isCheckable():
29 self.setChecked(not self.isChecked())
30 activated.send(sender=self)
31
32 def _changed(self):
33 changed.send(sender=self)
34
35 def setSeparator(self, separator):
36 self._separator = separator
37 self._changed()
38 def isSeparator(self): return self._separator
39
40 def _setText(self, text):
41 self._text = text
42 self._changed(self)
43 def _getText(self): return self._text
44 text = property(_getText, _setText)
45
46 def _setIcon(self, icon):
47 self._icon = icon
48 self._changed()
49 def _getIcon(self): return self._icon
50 icon = property(_getIcon, _setIcon)
51
52 def _setShortcut(self, keysequence):
53 self._shortcut = keysequence
54 self._changed()
55 def _getShortcut(self): return self._shortcut
56 shortcut = property(_getShortcut, _setShortcut)
57
58 def _setHelpText(self, helptext):
59 self._helptext = helptext
60 self._changed()
61 def _getHelpText(self): return self._helptext
62 helptext = property(_getHelpText, _setHelpText)
63
64 def setEnabled(self, enabled):
65 self._enabled = enabled
66 self._changed()
67
68 def isEnabled(self):
69 return self._enabled
70
71 def setChecked(self, checked):
72 self._checked = checked
73 self._changed()
74 toggled.send(sender=self, toggled=checked)
75
76 def isChecked(self):
77 return self._checked
78
79 def setCheckable(self, checkable):
80 self._checkable = checkable
81 if self._checkable is False and self._checked is True:
82 self.checked = False
83
84 self._changed()
85
86 def isCheckable(self):
87 return self._checkable
88
89 class ActionGroup:
90 def __init__(self, exclusive=False, name="actiongroup"):
91 self._exclusive = exclusive
92 self._enabled = True
93 self._actions = []
94 self.name = name
95
96 def __str__(self):
97 return "%s(name='%s')" % (self.__class__.__name__,self.name)
98
99 def __repr__(self):
100 return "<%s(name='%s') at %x>" % (self.__class__.__name__,self.name,id(self))
101
102
103 def setEnabled(self, enabled):
104 self._enabled = enabled
105 self._changed()
106
107 def isEnabled(self):
108 return self._enabled
109
110 def setExclusive(self, exclusive):
111 self._exclusive = exclusive
112 self._changed()
113
114 def isExclusive(self):
115 return self._exclusive
116
117 def addAction(self, action):
118 if self.hasAction(action):
119 print "Actiongroup already has this action"
120 return
121 self._actions.append(action)
122 toggled.connect(self._actionToggled, sender=action)
123 self._changed()
124
125 def addSeparator(self):
126 separator = Action(separator=True)
127 self.addAction(separator)
128 self._changed()
129
130 def getActions(self):
131 return self._actions
132
133 def removeAction(self, action):
134 self._actions.remove(action)
135 toggled.disconnect(self._actionToggled, sender=action)
136 self._changed()
137
138 def clear(self):
139 for action in self._actions:
140 toggled.disconnect(self._actionToggled, sender=action)
141 self._actions = []
142 self._changed()
143
144 def hasAction(self, action):
145 for a in self._actions:
146 if a == action:
147 return True
148 return False
149
150 def _actionToggled(self, sender):
151 if sender.isChecked() is False or self._exclusive is False:
152 return
153
154 for a in self._actions:
155 if a != sender and a.isChecked():
156 a.setChecked(False)
157
158 def getChecked(self):
159 for a in self._actions:
160 if a.isChecked():
161 return a
162
163 return None
164
165 def _changed(self):
166 changed.send(sender=self)
167