531888ac1aa14e1b6a206b33701f91387cbf5772
[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 ######################################################################################################
25 # Action Names
26 ######################################################################################################
27 APPLICATION_INITIALIZE = 'app init'
28 APPLICATION_QUIT = 'app quit'
29 PARAM_MODIFY = 'param modify'
30 BLOCK_MOVE = 'block move'
31 BLOCK_ROTATE_CCW = 'block rotate ccw'
32 BLOCK_ROTATE_CW = 'block rotate cw'
33 BLOCK_PARAM_MODIFY = 'block param modify'
34 BLOCK_INC_TYPE = 'block increment type'
35 BLOCK_DEC_TYPE = 'block decrement type'
36 BLOCK_ENABLE = 'block enable'
37 BLOCK_DISABLE = 'block disable'
38 BLOCK_CUT = 'block cut'
39 BLOCK_COPY = 'block copy'
40 BLOCK_PASTE = 'block paste'
41 PORT_CONTROLLER_INC = 'port controller increment'
42 PORT_CONTROLLER_DEC = 'port controller decrement'
43 ELEMENT_CREATE = 'element create'
44 ELEMENT_DELETE = 'element delete'
45 ELEMENT_SELECT = 'element select'
46 NOTHING_SELECT = 'nothing select'
47 FLOW_GRAPH_OPEN = 'flow graph open'
48 FLOW_GRAPH_UNDO = 'flow graph undo'
49 FLOW_GRAPH_REDO = 'flow graph redo'
50 FLOW_GRAPH_SAVE = 'flow graph save'
51 FLOW_GRAPH_SAVE_AS = 'flow graph save as'
52 FLOW_GRAPH_CLOSE = 'flow graph close'
53 FLOW_GRAPH_NEW = 'flow graph new'
54 FLOW_GRAPH_GEN = 'flow graph gen'
55 FLOW_GRAPH_EXEC = 'flow graph exec'
56 FLOW_GRAPH_KILL = 'flow graph kill'
57 FLOW_GRAPH_SCREEN_CAPTURE = 'flow graph screen capture'
58 ABOUT_WINDOW_DISPLAY = 'about window display'
59 HELP_WINDOW_DISPLAY = 'help window display'
60 TYPES_WINDOW_DISPLAY = 'types window display'
61
62 ######################################################################################################
63 # Action Key Map
64 ######################################################################################################
65 NO_MODS_MASK = 0
66 _actions_key_list = (
67         #action name, key value, mod mask
68         (FLOW_GRAPH_NEW,            gtk.keysyms.n,           gtk.gdk.CONTROL_MASK),
69         (FLOW_GRAPH_OPEN,           gtk.keysyms.o,           gtk.gdk.CONTROL_MASK),
70         (FLOW_GRAPH_SAVE,           gtk.keysyms.s,           gtk.gdk.CONTROL_MASK),
71         (FLOW_GRAPH_SAVE_AS,        gtk.keysyms.s,           gtk.gdk.CONTROL_MASK | gtk.gdk.SHIFT_MASK),
72         (FLOW_GRAPH_CLOSE,          gtk.keysyms.w,           gtk.gdk.CONTROL_MASK),
73         (APPLICATION_QUIT,          gtk.keysyms.q,           gtk.gdk.CONTROL_MASK),
74         (FLOW_GRAPH_UNDO,           gtk.keysyms.z,           gtk.gdk.CONTROL_MASK),
75         (FLOW_GRAPH_REDO,           gtk.keysyms.y,           gtk.gdk.CONTROL_MASK),
76         (ELEMENT_DELETE,            gtk.keysyms.Delete,      NO_MODS_MASK),
77         (BLOCK_ROTATE_CCW,          gtk.keysyms.Left,        NO_MODS_MASK),
78         (BLOCK_ROTATE_CW,           gtk.keysyms.Right,       NO_MODS_MASK),
79         (BLOCK_DEC_TYPE,            gtk.keysyms.Up,          NO_MODS_MASK),
80         (BLOCK_INC_TYPE,            gtk.keysyms.Down,        NO_MODS_MASK),
81         (BLOCK_PARAM_MODIFY,        gtk.keysyms.Return,      NO_MODS_MASK),
82         (BLOCK_ENABLE,              gtk.keysyms.e,           NO_MODS_MASK),
83         (BLOCK_DISABLE,             gtk.keysyms.d,           NO_MODS_MASK),
84         (BLOCK_CUT,                 gtk.keysyms.x,           gtk.gdk.CONTROL_MASK),
85         (BLOCK_COPY,                gtk.keysyms.c,           gtk.gdk.CONTROL_MASK),
86         (BLOCK_PASTE,               gtk.keysyms.v,           gtk.gdk.CONTROL_MASK),
87         (FLOW_GRAPH_GEN,            gtk.keysyms.F5,          NO_MODS_MASK),
88         (FLOW_GRAPH_EXEC,           gtk.keysyms.F6,          NO_MODS_MASK),
89         (FLOW_GRAPH_KILL,           gtk.keysyms.F7,          NO_MODS_MASK),
90         (FLOW_GRAPH_SCREEN_CAPTURE, gtk.keysyms.Print,       NO_MODS_MASK),
91         (HELP_WINDOW_DISPLAY,       gtk.keysyms.F1,          NO_MODS_MASK),
92         #the following have no associated gtk.Action
93         (PORT_CONTROLLER_INC,       gtk.keysyms.equal,       NO_MODS_MASK),
94         (PORT_CONTROLLER_INC,       gtk.keysyms.plus,        NO_MODS_MASK),
95         (PORT_CONTROLLER_INC,       gtk.keysyms.KP_Add,      NO_MODS_MASK),
96         (PORT_CONTROLLER_DEC,       gtk.keysyms.minus,       NO_MODS_MASK),
97         (PORT_CONTROLLER_DEC,       gtk.keysyms.KP_Subtract, NO_MODS_MASK),
98 )
99
100 _actions_key_dict = dict(((keyval, mod_mask), action_name) for action_name, keyval, mod_mask in _actions_key_list)
101 _used_mods_mask = reduce(lambda x, y: x | y, [mod_mask for action_name, keyval, mod_mask in _actions_key_list], NO_MODS_MASK)
102 _keymap = gtk.gdk.keymap_get_default()
103 def get_action_name_from_key_press(event):
104         """
105         Get the action name associated with the key press event.
106         Both the key value and the mask must have a match.
107         @param event a gtk key press event
108         @return the action name or blank string
109         """
110         #extract the key value and the consumed modifiers
111         keyval, egroup, level, consumed = _keymap.translate_keyboard_state(
112                 event.hardware_keycode, event.state, event.group)
113         #get the modifier mask and ignore irrelevant modifiers
114         mod_mask = event.state & ~consumed & _used_mods_mask
115         try: return _actions_key_dict[(keyval, mod_mask)]
116         except KeyError: raise KeyError, 'Keypress: "%s, %s" does not have an associated action'%(gtk.gdk.keyval_name(keyval), mod_mask)
117
118 ######################################################################################################
119 # Actions
120 ######################################################################################################
121 _actions_list = (
122         gtk.Action(FLOW_GRAPH_NEW, '_New', 'Create a new flow graph', gtk.STOCK_NEW),
123         gtk.Action(FLOW_GRAPH_OPEN, '_Open', 'Open an existing flow graph', gtk.STOCK_OPEN),
124         gtk.Action(FLOW_GRAPH_SAVE, '_Save', 'Save the current flow graph', gtk.STOCK_SAVE),
125         gtk.Action(FLOW_GRAPH_SAVE_AS, 'Save _As', 'Save the current flow graph as...', gtk.STOCK_SAVE_AS),
126         gtk.Action(FLOW_GRAPH_CLOSE, '_Close', 'Close the current flow graph', gtk.STOCK_CLOSE),
127         gtk.Action(APPLICATION_QUIT, '_Quit', 'Quit program', gtk.STOCK_QUIT),
128         gtk.Action(FLOW_GRAPH_UNDO, '_Undo', 'Undo a change to the flow graph', gtk.STOCK_UNDO),
129         gtk.Action(FLOW_GRAPH_REDO, '_Redo', 'Redo a change to the flow graph', gtk.STOCK_REDO),
130         gtk.Action(ELEMENT_DELETE, '_Delete', 'Delete the selected blocks', gtk.STOCK_DELETE),
131         gtk.Action(BLOCK_ROTATE_CCW, 'Rotate Counterclockwise', 'Rotate the selected blocks 90 degrees to the left', gtk.STOCK_GO_BACK),
132         gtk.Action(BLOCK_ROTATE_CW, 'Rotate Clockwise', 'Rotate the selected blocks 90 degrees to the right', gtk.STOCK_GO_FORWARD),
133         gtk.Action(BLOCK_PARAM_MODIFY, '_Properties', 'Modify params for the selected block', gtk.STOCK_PROPERTIES),
134         gtk.Action(BLOCK_ENABLE, 'E_nable', 'Enable the selected blocks', gtk.STOCK_CONNECT),
135         gtk.Action(BLOCK_DISABLE, 'D_isable', 'Disable the selected blocks', gtk.STOCK_DISCONNECT),
136         gtk.Action(BLOCK_CUT, 'Cu_t', 'Cut', gtk.STOCK_CUT),
137         gtk.Action(BLOCK_COPY, '_Copy', 'Copy', gtk.STOCK_COPY),
138         gtk.Action(BLOCK_PASTE, '_Paste', 'Paste', gtk.STOCK_PASTE),
139         gtk.Action(ABOUT_WINDOW_DISPLAY, '_About', 'About this program', gtk.STOCK_ABOUT),
140         gtk.Action(HELP_WINDOW_DISPLAY, '_Help', 'Usage Tips', gtk.STOCK_HELP),
141         gtk.Action(TYPES_WINDOW_DISPLAY, '_Types', 'Types Color Mapping', gtk.STOCK_DIALOG_INFO),
142         gtk.Action(FLOW_GRAPH_GEN, '_Generate', 'Generate the flow graph', gtk.STOCK_CONVERT),
143         gtk.Action(FLOW_GRAPH_EXEC, '_Execute', 'Execute the flow graph', gtk.STOCK_EXECUTE),
144         gtk.Action(FLOW_GRAPH_KILL, '_Kill', 'Kill the flow graph', gtk.STOCK_STOP),
145         gtk.Action(FLOW_GRAPH_SCREEN_CAPTURE, 'S_creen Capture', 'Create a screen capture of the flow graph', gtk.STOCK_PRINT),
146 )
147 def get_all_actions(): return _actions_list
148
149 _actions_dict = dict((action.get_name(), action) for action in get_all_actions())
150 def get_action_from_name(action_name):
151         """
152         Retrieve the action from the action list.
153         Search the list and find an action with said name.
154         @param action_name the action name(string)
155         @throw KeyError bad action name
156         @return a gtk action object
157         """
158         try: return _actions_dict[action_name]
159         except KeyError: raise KeyError, 'Action Name: "%s" does not exist'%action_name
160
161 ######################################################################################################
162 # Accelerators
163 ######################################################################################################
164 _accel_group = gtk.AccelGroup()
165 def get_accel_group(): return _accel_group
166
167 #set the accelerator group, and accelerator path
168 #register the key name and mod mask with the accelerator path
169 for action_name, keyval, mod_mask in _actions_key_list:
170         try:
171                 accel_path = '<main>/'+action_name
172                 get_action_from_name(action_name).set_accel_group(get_accel_group())
173                 get_action_from_name(action_name).set_accel_path(accel_path)
174                 gtk.accel_map_add_entry(accel_path, keyval, mod_mask)
175         except KeyError: pass #no action was created for this action name