switch source package format to 3.0 quilt
[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 handle_key_press(event):
33         """
34         Call 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         @return true if handled
38         """
39         _used_mods_mask = reduce(lambda x, y: x | y, [mod_mask for keyval, mod_mask in _actions_keypress_dict], NO_MODS_MASK)
40         #extract the key value and the consumed modifiers
41         keyval, egroup, level, consumed = _keymap.translate_keyboard_state(
42                 event.hardware_keycode, event.state, event.group)
43         #get the modifier mask and ignore irrelevant modifiers
44         mod_mask = event.state & ~consumed & _used_mods_mask
45         #look up the keypress and call the action
46         try: _actions_keypress_dict[(keyval, mod_mask)]()
47         except KeyError: return False #not handled
48         return True #handled here
49
50 _all_actions_list = list()
51 def get_all_actions(): return _all_actions_list
52
53 _accel_group = gtk.AccelGroup()
54 def get_accel_group(): return _accel_group
55
56 class Action(gtk.Action):
57         """
58         A custom Action class based on gtk.Action.
59         Pass additional arguments such as keypresses.
60         Register actions and keypresses with this module.
61         """
62
63         def __init__(self, keypresses=(), name=None, label=None, tooltip=None, stock_id=None):
64                 """
65                 Create a new Action instance.
66                 @param key_presses a tuple of (keyval1, mod_mask1, keyval2, mod_mask2, ...)
67                 @param the regular gtk.Action parameters (defaults to None)
68                 """
69                 if name is None: name = label
70                 gtk.Action.__init__(self,
71                         name=name, label=label,
72                         tooltip=tooltip, stock_id=stock_id,
73                 )
74                 #register this action
75                 _all_actions_list.append(self)
76                 for i in range(len(keypresses)/2):
77                         keyval, mod_mask = keypresses[i*2:(i+1)*2]
78                         #register this keypress
79                         assert not _actions_keypress_dict.has_key((keyval, mod_mask))
80                         _actions_keypress_dict[(keyval, mod_mask)] = self
81                         #set the accelerator group, and accelerator path
82                         #register the key name and mod mask with the accelerator path
83                         if label is None: continue #dont register accel
84                         accel_path = '<main>/'+self.get_name()
85                         self.set_accel_group(get_accel_group())
86                         self.set_accel_path(accel_path)
87                         gtk.accel_map_add_entry(accel_path, keyval, mod_mask)
88
89         def __str__(self):
90                 """
91                 The string representation should be the name of the action id.
92                 Try to find the action id for this action by searching this module.
93                 """
94                 try:
95                         import Actions
96                         return filter(lambda attr: getattr(Actions, attr) == self, dir(Actions))[0]
97                 except: return self.get_name()
98
99         def __repr__(self): return str(self)
100
101         def __call__(self):
102                 """
103                 Emit the activate signal when called with ().
104                 """
105                 self.emit('activate')
106
107 ########################################################################
108 # Actions
109 ########################################################################
110 PAGE_CHANGE = Action()
111 FLOW_GRAPH_NEW = Action(
112         label='_New',
113         tooltip='Create a new flow graph',
114         stock_id=gtk.STOCK_NEW,
115         keypresses=(gtk.keysyms.n, gtk.gdk.CONTROL_MASK),
116 )
117 FLOW_GRAPH_OPEN = Action(
118         label='_Open',
119         tooltip='Open an existing flow graph',
120         stock_id=gtk.STOCK_OPEN,
121         keypresses=(gtk.keysyms.o, gtk.gdk.CONTROL_MASK),
122 )
123 FLOW_GRAPH_SAVE = Action(
124         label='_Save',
125         tooltip='Save the current flow graph',
126         stock_id=gtk.STOCK_SAVE,
127         keypresses=(gtk.keysyms.s, gtk.gdk.CONTROL_MASK),
128 )
129 FLOW_GRAPH_SAVE_AS = Action(
130         label='Save _As',
131         tooltip='Save the current flow graph as...',
132         stock_id=gtk.STOCK_SAVE_AS,
133         keypresses=(gtk.keysyms.s, gtk.gdk.CONTROL_MASK | gtk.gdk.SHIFT_MASK),
134 )
135 FLOW_GRAPH_CLOSE = Action(
136         label='_Close',
137         tooltip='Close the current flow graph',
138         stock_id=gtk.STOCK_CLOSE,
139         keypresses=(gtk.keysyms.w, gtk.gdk.CONTROL_MASK),
140 )
141 APPLICATION_INITIALIZE = Action()
142 APPLICATION_QUIT = Action(
143         label='_Quit',
144         tooltip='Quit program',
145         stock_id=gtk.STOCK_QUIT,
146         keypresses=(gtk.keysyms.q, gtk.gdk.CONTROL_MASK),
147 )
148 FLOW_GRAPH_UNDO = Action(
149         label='_Undo',
150         tooltip='Undo a change to the flow graph',
151         stock_id=gtk.STOCK_UNDO,
152         keypresses=(gtk.keysyms.z, gtk.gdk.CONTROL_MASK),
153 )
154 FLOW_GRAPH_REDO = Action(
155         label='_Redo',
156         tooltip='Redo a change to the flow graph',
157         stock_id=gtk.STOCK_REDO,
158         keypresses=(gtk.keysyms.y, gtk.gdk.CONTROL_MASK),
159 )
160 NOTHING_SELECT = Action()
161 ELEMENT_SELECT = Action()
162 ELEMENT_CREATE = Action()
163 ELEMENT_DELETE = Action(
164         label='_Delete',
165         tooltip='Delete the selected blocks',
166         stock_id=gtk.STOCK_DELETE,
167         keypresses=(gtk.keysyms.Delete, NO_MODS_MASK),
168 )
169 BLOCK_MOVE = Action()
170 BLOCK_ROTATE_CCW = Action(
171         label='Rotate Counterclockwise',
172         tooltip='Rotate the selected blocks 90 degrees to the left',
173         stock_id=gtk.STOCK_GO_BACK,
174         keypresses=(gtk.keysyms.Left, NO_MODS_MASK),
175 )
176 BLOCK_ROTATE_CW = Action(
177         label='Rotate Clockwise',
178         tooltip='Rotate the selected blocks 90 degrees to the right',
179         stock_id=gtk.STOCK_GO_FORWARD,
180         keypresses=(gtk.keysyms.Right, NO_MODS_MASK),
181 )
182 BLOCK_PARAM_MODIFY = Action(
183         label='_Properties',
184         tooltip='Modify params for the selected block',
185         stock_id=gtk.STOCK_PROPERTIES,
186         keypresses=(gtk.keysyms.Return, NO_MODS_MASK),
187 )
188 BLOCK_ENABLE = Action(
189         label='E_nable',
190         tooltip='Enable the selected blocks',
191         stock_id=gtk.STOCK_CONNECT,
192         keypresses=(gtk.keysyms.e, NO_MODS_MASK),
193 )
194 BLOCK_DISABLE = Action(
195         label='D_isable',
196         tooltip='Disable the selected blocks',
197         stock_id=gtk.STOCK_DISCONNECT,
198         keypresses=(gtk.keysyms.d, NO_MODS_MASK),
199 )
200 BLOCK_CUT = Action(
201         label='Cu_t',
202         tooltip='Cut',
203         stock_id=gtk.STOCK_CUT,
204         keypresses=(gtk.keysyms.x, gtk.gdk.CONTROL_MASK),
205 )
206 BLOCK_COPY = Action(
207         label='_Copy',
208         tooltip='Copy',
209         stock_id=gtk.STOCK_COPY,
210         keypresses=(gtk.keysyms.c, gtk.gdk.CONTROL_MASK),
211 )
212 BLOCK_PASTE = Action(
213         label='_Paste',
214         tooltip='Paste',
215         stock_id=gtk.STOCK_PASTE,
216         keypresses=(gtk.keysyms.v, gtk.gdk.CONTROL_MASK),
217 )
218 ERRORS_WINDOW_DISPLAY = Action(
219         label='_Errors',
220         tooltip='View flow graph errors',
221         stock_id=gtk.STOCK_DIALOG_ERROR,
222 )
223 ABOUT_WINDOW_DISPLAY = Action(
224         label='_About',
225         tooltip='About this program',
226         stock_id=gtk.STOCK_ABOUT,
227 )
228 HELP_WINDOW_DISPLAY = Action(
229         label='_Help',
230         tooltip='Usage tips',
231         stock_id=gtk.STOCK_HELP,
232         keypresses=(gtk.keysyms.F1, NO_MODS_MASK),
233 )
234 TYPES_WINDOW_DISPLAY = Action(
235         label='_Types',
236         tooltip='Types color mapping',
237         stock_id=gtk.STOCK_DIALOG_INFO,
238 )
239 FLOW_GRAPH_GEN = Action(
240         label='_Generate',
241         tooltip='Generate the flow graph',
242         stock_id=gtk.STOCK_CONVERT,
243         keypresses=(gtk.keysyms.F5, NO_MODS_MASK),
244 )
245 FLOW_GRAPH_EXEC = Action(
246         label='_Execute',
247         tooltip='Execute the flow graph',
248         stock_id=gtk.STOCK_EXECUTE,
249         keypresses=(gtk.keysyms.F6, NO_MODS_MASK),
250 )
251 FLOW_GRAPH_KILL = Action(
252         label='_Kill',
253         tooltip='Kill the flow graph',
254         stock_id=gtk.STOCK_STOP,
255         keypresses=(gtk.keysyms.F7, NO_MODS_MASK),
256 )
257 FLOW_GRAPH_SCREEN_CAPTURE = Action(
258         label='S_creen Capture',
259         tooltip='Create a screen capture of the flow graph',
260         stock_id=gtk.STOCK_PRINT,
261         keypresses=(gtk.keysyms.Print, NO_MODS_MASK),
262 )
263 PORT_CONTROLLER_DEC = Action(
264         keypresses=(gtk.keysyms.minus, NO_MODS_MASK, gtk.keysyms.KP_Subtract, NO_MODS_MASK),
265 )
266 PORT_CONTROLLER_INC = Action(
267         keypresses=(gtk.keysyms.plus, NO_MODS_MASK, gtk.keysyms.KP_Add, NO_MODS_MASK),
268 )
269 BLOCK_INC_TYPE = Action(
270         keypresses=(gtk.keysyms.Down, NO_MODS_MASK),
271 )
272 BLOCK_DEC_TYPE = Action(
273         keypresses=(gtk.keysyms.Up, NO_MODS_MASK),
274 )