rename user interface operator properties from 'op' to 'props'
[blender.git] / release / scripts / startup / bl_ui / space_userpref_keymap.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8 compliant>
20 import bpy
21 from bpy.types import Menu, OperatorProperties
22
23
24 class USERPREF_MT_keyconfigs(Menu):
25     bl_label = "KeyPresets"
26     preset_subdir = "keyconfig"
27     preset_operator = "wm.keyconfig_activate"
28
29     def draw(self, context):
30         props = self.layout.operator("wm.context_set_value", text="Blender (default)")
31         props.data_path = "window_manager.keyconfigs.active"
32         props.value = "context.window_manager.keyconfigs.default"
33
34         # now draw the presets
35         Menu.draw_preset(self, context)
36
37
38 class InputKeyMapPanel:
39     bl_space_type = 'USER_PREFERENCES'
40     bl_label = "Input"
41     bl_region_type = 'WINDOW'
42     bl_options = {'HIDE_HEADER'}
43
44     def draw_entry(self, display_keymaps, entry, col, level=0):
45         idname, spaceid, regionid, children = entry
46
47         for km, kc in display_keymaps:
48             if km.name == idname and km.space_type == spaceid and km.region_type == regionid:
49                 self.draw_km(display_keymaps, kc, km, children, col, level)
50
51         '''
52         km = kc.keymaps.find(idname, space_type=spaceid, region_type=regionid)
53         if not km:
54             kc = defkc
55             km = kc.keymaps.find(idname, space_type=spaceid, region_type=regionid)
56
57         if km:
58             self.draw_km(kc, km, children, col, level)
59         '''
60
61     def indented_layout(self, layout, level):
62         indentpx = 16
63         if level == 0:
64             level = 0.0001   # Tweak so that a percentage of 0 won't split by half
65         indent = level * indentpx / bpy.context.region.width
66
67         split = layout.split(percentage=indent)
68         col = split.column()
69         col = split.column()
70         return col
71
72     def draw_km(self, display_keymaps, kc, km, children, layout, level):
73         km = km.active()
74
75         layout.context_pointer_set("keymap", km)
76
77         col = self.indented_layout(layout, level)
78
79         row = col.row()
80         row.prop(km, "show_expanded_children", text="", emboss=False)
81         row.label(text=km.name)
82
83         row.label()
84         row.label()
85
86         if km.is_modal:
87             row.label(text="", icon='LINKED')
88         if km.is_user_modified:
89             row.operator("wm.keymap_restore", text="Restore")
90         else:
91             row.label()
92
93         if km.show_expanded_children:
94             if children:
95                 # Put the Parent key map's entries in a 'global' sub-category
96                 # equal in hierarchy to the other children categories
97                 subcol = self.indented_layout(col, level + 1)
98                 subrow = subcol.row()
99                 subrow.prop(km, "show_expanded_items", text="", emboss=False)
100                 subrow.label(text="%s " % km.name + "(Global)")
101             else:
102                 km.show_expanded_items = True
103
104             # Key Map items
105             if km.show_expanded_items:
106                 for kmi in km.keymap_items:
107                     self.draw_kmi(display_keymaps, kc, km, kmi, col, level + 1)
108
109                 # "Add New" at end of keymap item list
110                 col = self.indented_layout(col, level + 1)
111                 subcol = col.split(percentage=0.2).column()
112                 subcol.operator("wm.keyitem_add", text="Add New", icon='ZOOMIN')
113
114             col.separator()
115
116             # Child key maps
117             if children:
118                 subcol = col.column()
119                 row = subcol.row()
120
121                 for entry in children:
122                     self.draw_entry(display_keymaps, entry, col, level + 1)
123
124     def draw_kmi(self, display_keymaps, kc, km, kmi, layout, level):
125         map_type = kmi.map_type
126
127         col = self.indented_layout(layout, level)
128
129         if kmi.show_expanded:
130             col = col.column(align=True)
131             box = col.box()
132         else:
133             box = col.column()
134
135         split = box.split(percentage=0.05)
136
137         # header bar
138         row = split.row()
139         row.prop(kmi, "show_expanded", text="", emboss=False)
140
141         row = split.row()
142         row.prop(kmi, "active", text="", emboss=False)
143
144         if km.is_modal:
145             row.prop(kmi, "propvalue", text="")
146         else:
147             row.label(text=kmi.name)
148
149         row = split.row()
150         row.prop(kmi, "map_type", text="")
151         if map_type == 'KEYBOARD':
152             row.prop(kmi, "type", text="", full_event=True)
153         elif map_type == 'MOUSE':
154             row.prop(kmi, "type", text="", full_event=True)
155         elif map_type == 'NDOF':
156             row.prop(kmi, "type", text="", full_event=True)
157         elif map_type == 'TWEAK':
158             subrow = row.row()
159             subrow.prop(kmi, "type", text="")
160             subrow.prop(kmi, "value", text="")
161         elif map_type == 'TIMER':
162             row.prop(kmi, "type", text="")
163         else:
164             row.label()
165
166         if (not kmi.is_user_defined) and kmi.is_user_modified:
167             row.operator("wm.keyitem_restore", text="", icon='BACK').item_id = kmi.id
168         else:
169             row.operator("wm.keyitem_remove", text="", icon='X').item_id = kmi.id
170
171         # Expanded, additional event settings
172         if kmi.show_expanded:
173             box = col.box()
174
175             if map_type not in {'TEXTINPUT', 'TIMER'}:
176                 split = box.split(percentage=0.4)
177                 sub = split.row()
178
179                 if km.is_modal:
180                     sub.prop(kmi, "propvalue", text="")
181                 else:
182                     # One day...
183                     # sub.prop_search(kmi, "idname", bpy.context.window_manager, "operators_all", text="")
184                     sub.prop(kmi, "idname", text="")
185
186                 sub = split.column()
187                 subrow = sub.row(align=True)
188
189                 if map_type in {'KEYBOARD', 'NDOF'}:
190                     subrow.prop(kmi, "type", text="", event=True)
191                     subrow.prop(kmi, "value", text="")
192                 elif map_type == 'MOUSE':
193                     subrow.prop(kmi, "type", text="")
194                     subrow.prop(kmi, "value", text="")
195
196                 subrow = sub.row()
197                 subrow.scale_x = 0.75
198                 subrow.prop(kmi, "any")
199                 subrow.prop(kmi, "shift")
200                 subrow.prop(kmi, "ctrl")
201                 subrow.prop(kmi, "alt")
202                 subrow.prop(kmi, "oskey", text="Cmd")
203                 subrow.prop(kmi, "key_modifier", text="", event=True)
204
205             # Operator properties
206             box.template_keymap_item_properties(kmi)
207
208             # Modal key maps attached to this operator
209             if not km.is_modal:
210                 kmm = kc.keymaps.find_modal(kmi.idname)
211                 if kmm:
212                     self.draw_km(display_keymaps, kc, kmm, None, layout, level + 1)
213                     layout.context_pointer_set("keymap", km)
214
215     def draw_filtered(self, display_keymaps, filter_text, layout):
216         for km, kc in display_keymaps:
217             km = km.active()
218             layout.context_pointer_set("keymap", km)
219
220             filtered_items = [kmi for kmi in km.keymap_items if filter_text in kmi.name.lower()]
221
222             if len(filtered_items) != 0:
223                 col = layout.column()
224
225                 row = col.row()
226                 row.label(text=km.name, icon="DOT")
227
228                 row.label()
229                 row.label()
230
231                 if km.is_user_modified:
232                     row.operator("wm.keymap_restore", text="Restore")
233                 else:
234                     row.label()
235
236                 for kmi in filtered_items:
237                     self.draw_kmi(display_keymaps, kc, km, kmi, col, 1)
238
239                 # "Add New" at end of keymap item list
240                 col = self.indented_layout(layout, 1)
241                 subcol = col.split(percentage=0.2).column()
242                 subcol.operator("wm.keyitem_add", text="Add New", icon='ZOOMIN')
243
244     def draw_hierarchy(self, display_keymaps, layout):
245         from bpy_extras import keyconfig_utils
246         for entry in keyconfig_utils.KM_HIERARCHY:
247             self.draw_entry(display_keymaps, entry, layout)
248
249     def draw_keymaps(self, context, layout):
250         from bpy_extras import keyconfig_utils
251
252         wm = context.window_manager
253         kc = wm.keyconfigs.user
254
255         col = layout.column()
256         sub = col.column()
257
258         subsplit = sub.split()
259         subcol = subsplit.column()
260
261         row = subcol.row(align=True)
262
263         #~ row.prop_search(wm.keyconfigs, "active", wm, "keyconfigs", text="Key Config:")
264         text = bpy.path.display_name(context.window_manager.keyconfigs.active.name)
265         if not text:
266             text = "Blender (default)"
267         row.menu("USERPREF_MT_keyconfigs", text=text)
268         row.operator("wm.keyconfig_preset_add", text="", icon="ZOOMIN")
269         row.operator("wm.keyconfig_preset_add", text="", icon="ZOOMOUT").remove_active = True
270
271         #~ layout.context_pointer_set("keyconfig", wm.keyconfigs.active)
272         #~ row.operator("wm.keyconfig_remove", text="", icon='X')
273
274         row.prop(context.space_data, "filter_text", icon="VIEWZOOM")
275
276         col.separator()
277
278         display_keymaps = keyconfig_utils.keyconfig_merge(kc, kc)
279         if context.space_data.filter_text != "":
280             filter_text = context.space_data.filter_text.lower()
281             self.draw_filtered(display_keymaps, filter_text, col)
282         else:
283             self.draw_hierarchy(display_keymaps, col)
284
285
286 if __name__ == "__main__":  # only for live edit.
287     bpy.utils.register_module(__name__)