fix for uniformity convention with gtk signal name strings
[debian/gnuradio] / grc / gui / Actions.py
1 """
2 Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 """
19
20 import pygtk
21 pygtk.require('2.0')
22 import gtk
23
24 NO_MODS_MASK = 0
25
26 ########################################################################
27 # Actions API
28 ########################################################################
29 _actions_keypress_dict = dict()
30 _keymap = gtk.gdk.keymap_get_default()
31 _used_mods_mask = NO_MODS_MASK
32 def get_action_from_key_press(event):
33         """
34         Get the action associated with the key press event.
35         Both the key value and the mask must have a match.
36         @param event a gtk key press event
37         @throws a key error when no action matches
38         @return the action object
39         """
40         _used_mods_mask = reduce(lambda x, y: x | y, [mod_mask for keyval, mod_mask in _actions_keypress_dict], NO_MODS_MASK)
41         #extract the key value and the consumed modifiers
42         keyval, egroup, level, consumed = _keymap.translate_keyboard_state(
43                 event.hardware_keycode, event.state, event.group)
44         #get the modifier mask and ignore irrelevant modifiers
45         mod_mask = event.state & ~consumed & _used_mods_mask
46         try: return _actions_keypress_dict[(keyval, mod_mask)]
47         except KeyError: raise KeyError, 'Keypress: "%s, %s" does not have an associated action'%(gtk.gdk.keyval_name(keyval), mod_mask)
48
49 _all_actions_list = list()
50 def get_all_actions(): return _all_actions_list
51
52 _accel_group = gtk.AccelGroup()
53 def get_accel_group(): return _accel_group
54
55 class Action(gtk.Action):
56         """
57         A custom Action class based on gtk.Action.
58         Pass additional arguments such as keypresses.
59         Register actions and keypresses with this module.
60         """
61
62         def __init__(self, keypresses=(), name=None, label=None, tooltip=None, stock_id=None):
63                 """
64                 Create a new Action instance.
65                 @param key_presses a tuple of (keyval1, mod_mask1, keyval2, mod_mask2, ...)
66                 @param the regular gtk.Action parameters (defaults to None)
67                 """
68                 if name is None: name = label
69                 gtk.Action.__init__(self,
70                         name=name, label=label,
71                         tooltip=tooltip, stock_id=stock_id,
72                 )
73                 #register this action
74                 _all_actions_list.append(self)
75                 for i in range(len(keypresses)/2):
76                         keyval, mod_mask = keypresses[i*2:(i+1)*2]
77                         #register this keypress
78                         assert not _actions_keypress_dict.has_key((keyval, mod_mask))
79                         _actions_keypress_dict[(keyval, mod_mask)] = self
80                         #set the accelerator group, and accelerator path
81                         #register the key name and mod mask with the accelerator path
82                         if label is None: continue #dont register accel
83                         accel_path = '<main>/'+self.get_name()
84                         self.set_accel_group(get_accel_group())
85                         self.set_accel_path(accel_path)
86                         gtk.accel_map_add_entry(accel_path, keyval, mod_mask)
87
88         def __str__(self):
89                 """
90                 The string representation should be the name of the action id.
91                 Try to find the action id for this action by searching this module.
92                 """
93                 try:
94                         import Actions
95                         return filter(lambda attr: getattr(Actions, attr) == self, dir(Actions))[0]
96                 except: return self.get_name()
97
98         def __repr__(self): return str(self)
99
100         def __call__(self):
101                 """
102                 Emit the activate signal when called with ().
103                 """
104                 self.emit('activate')
105
106 ########################################################################
107 # Actions
108 ########################################################################
109 PAGE_CHANGE = Action()
110 FLOW_GRAPH_NEW = Action(
111         label='_New',
112         tooltip='Create a new flow graph',
113         stock_id=gtk.STOCK_NEW,
114         keypresses=(gtk.keysyms.n, gtk.gdk.CONTROL_MASK),
115 )
116 FLOW_GRAPH_OPEN = Action(
117         label='_Open',
118         tooltip='Open an existing flow graph',
119         stock_id=gtk.STOCK_OPEN,
120         keypresses=(gtk.keysyms.o, gtk.gdk.CONTROL_MASK),
121 )
122 FLOW_GRAPH_SAVE = Action(
123         label='_Save',
124         tooltip='Save the current flow graph',
125         stock_id=gtk.STOCK_SAVE,
126         keypresses=(gtk.keysyms.s, gtk.gdk.CONTROL_MASK),
127 )
128 FLOW_GRAPH_SAVE_AS = Action(
129         label='Save _As',
130         tooltip='Save the current flow graph as...',
131         stock_id=gtk.STOCK_SAVE_AS,
132         keypresses=(gtk.keysyms.s, gtk.gdk.CONTROL_MASK | gtk.gdk.SHIFT_MASK),
133 )
134 FLOW_GRAPH_CLOSE = Action(
135         label='_Close',
136         tooltip='Close the current flow graph',
137         stock_id=gtk.STOCK_CLOSE,
138         keypresses=(gtk.keysyms.w, gtk.gdk.CONTROL_MASK),
139 )
140 APPLICATION_INITIALIZE = Action()
141 APPLICATION_QUIT = Action(
142         label='_Quit',
143         tooltip='Quit program',
144         stock_id=gtk.STOCK_QUIT,
145         keypresses=(gtk.keysyms.q, gtk.gdk.CONTROL_MASK),
146 )
147 FLOW_GRAPH_UNDO = Action(
148         label='_Undo',
149         tooltip='Undo a change to the flow graph',
150         stock_id=gtk.STOCK_UNDO,
151         keypresses=(gtk.keysyms.z, gtk.gdk.CONTROL_MASK),
152 )
153 FLOW_GRAPH_REDO = Action(
154         label='_Redo',
155         tooltip='Redo a change to the flow graph',
156         stock_id=gtk.STOCK_REDO,
157         keypresses=(gtk.keysyms.y, gtk.gdk.CONTROL_MASK),
158 )
159 NOTHING_SELECT = Action()
160 ELEMENT_SELECT = Action()
161 ELEMENT_CREATE = Action()
162 ELEMENT_DELETE = Action(
163         label='_Delete',
164         tooltip='Delete the selected blocks',
165         stock_id=gtk.STOCK_DELETE,
166         keypresses=(gtk.keysyms.Delete, NO_MODS_MASK),
167 )
168 BLOCK_MOVE = Action()
169 BLOCK_ROTATE_CCW = Action(
170         label='Rotate Counterclockwise',
171         tooltip='Rotate the selected blocks 90 degrees to the left',
172         stock_id=gtk.STOCK_GO_BACK,
173         keypresses=(gtk.keysyms.Left, NO_MODS_MASK),
174 )
175 BLOCK_ROTATE_CW = Action(
176         label='Rotate Clockwise',
177         tooltip='Rotate the selected blocks 90 degrees to the right',
178         stock_id=gtk.STOCK_GO_FORWARD,
179         keypresses=(gtk.keysyms.Right, NO_MODS_MASK),
180 )
181 BLOCK_PARAM_MODIFY = Action(
182         label='_Properties',
183         tooltip='Modify params for the selected block',
184         stock_id=gtk.STOCK_PROPERTIES,
185         keypresses=(gtk.keysyms.Return, NO_MODS_MASK),
186 )
187 BLOCK_ENABLE = Action(
188         label='E_nable',
189         tooltip='Enable the selected blocks',
190         stock_id=gtk.STOCK_CONNECT,
191         keypresses=(gtk.keysyms.e, NO_MODS_MASK),
192 )
193 BLOCK_DISABLE = Action(
194         label='D_isable',
195         tooltip='Disable the selected blocks',
196         stock_id=gtk.STOCK_DISCONNECT,
197         keypresses=(gtk.keysyms.d, NO_MODS_MASK),
198 )
199 BLOCK_CUT = Action(
200         label='Cu_t',
201         tooltip='Cut',
202         stock_id=gtk.STOCK_CUT,
203         keypresses=(gtk.keysyms.x, gtk.gdk.CONTROL_MASK),
204 )
205 BLOCK_COPY = Action(
206         label='_Copy',
207         tooltip='Copy',
208         stock_id=gtk.STOCK_COPY,
209         keypresses=(gtk.keysyms.c, gtk.gdk.CONTROL_MASK),
210 )
211 BLOCK_PASTE = Action(
212         label='_Paste',
213         tooltip='Paste',
214         stock_id=gtk.STOCK_PASTE,
215         keypresses=(gtk.keysyms.v, gtk.gdk.CONTROL_MASK),
216 )
217 ABOUT_WINDOW_DISPLAY = Action(
218         label='_About',
219         tooltip='About this program',
220         stock_id=gtk.STOCK_ABOUT,
221 )
222 HELP_WINDOW_DISPLAY = Action(
223         label='_Help',
224         tooltip='Usage Tips',
225         stock_id=gtk.STOCK_HELP,
226         keypresses=(gtk.keysyms.F1, NO_MODS_MASK),
227 )
228 TYPES_WINDOW_DISPLAY = Action(
229         label='_Types',
230         tooltip='Types Color Mapping',
231         stock_id=gtk.STOCK_DIALOG_INFO,
232 )
233 FLOW_GRAPH_GEN = Action(
234         label='_Generate',
235         tooltip='Generate the flow graph',
236         stock_id=gtk.STOCK_CONVERT,
237         keypresses=(gtk.keysyms.F5, NO_MODS_MASK),
238 )
239 FLOW_GRAPH_EXEC = Action(
240         label='_Execute',
241         tooltip='Execute the flow graph',
242         stock_id=gtk.STOCK_EXECUTE,
243         keypresses=(gtk.keysyms.F6, NO_MODS_MASK),
244 )
245 FLOW_GRAPH_KILL = Action(
246         label='_Kill',
247         tooltip='Kill the flow graph',
248         stock_id=gtk.STOCK_STOP,
249         keypresses=(gtk.keysyms.F7, NO_MODS_MASK),
250 )
251 FLOW_GRAPH_SCREEN_CAPTURE = Action(
252         label='S_creen Capture',
253         tooltip='Create a screen capture of the flow graph',
254         stock_id=gtk.STOCK_PRINT,
255         keypresses=(gtk.keysyms.Print, NO_MODS_MASK),
256 )
257 PORT_CONTROLLER_DEC = Action(
258         keypresses=(gtk.keysyms.minus, NO_MODS_MASK, gtk.keysyms.KP_Subtract, NO_MODS_MASK),
259 )
260 PORT_CONTROLLER_INC = Action(
261         keypresses=(gtk.keysyms.plus, NO_MODS_MASK, gtk.keysyms.KP_Add, NO_MODS_MASK),
262 )
263 BLOCK_INC_TYPE = Action(
264         keypresses=(gtk.keysyms.Down, NO_MODS_MASK),
265 )
266 BLOCK_DEC_TYPE = Action(
267         keypresses=(gtk.keysyms.Up, NO_MODS_MASK),
268 )