2c537efa61bbc4660012c2e557cdd481678be4b6
[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             op = row.operator("wm.keyitem_restore", text="", icon='BACK')
168             op.item_id = kmi.id
169         else:
170             op = row.operator("wm.keyitem_remove", text="", icon='X')
171             op.item_id = kmi.id
172
173         # Expanded, additional event settings
174         if kmi.show_expanded:
175             box = col.box()
176
177             if map_type not in {'TEXTINPUT', 'TIMER'}:
178                 split = box.split(percentage=0.4)
179                 sub = split.row()
180
181                 if km.is_modal:
182                     sub.prop(kmi, "propvalue", text="")
183                 else:
184                     # One day...
185                     # sub.prop_search(kmi, "idname", bpy.context.window_manager, "operators_all", text="")
186                     sub.prop(kmi, "idname", text="")
187
188                 sub = split.column()
189                 subrow = sub.row(align=True)
190
191                 if map_type in {'KEYBOARD', 'NDOF'}:
192                     subrow.prop(kmi, "type", text="", event=True)
193                     subrow.prop(kmi, "value", text="")
194                 elif map_type == 'MOUSE':
195                     subrow.prop(kmi, "type", text="")
196                     subrow.prop(kmi, "value", text="")
197
198                 subrow = sub.row()
199                 subrow.scale_x = 0.75
200                 subrow.prop(kmi, "any")
201                 subrow.prop(kmi, "shift")
202                 subrow.prop(kmi, "ctrl")
203                 subrow.prop(kmi, "alt")
204                 subrow.prop(kmi, "oskey", text="Cmd")
205                 subrow.prop(kmi, "key_modifier", text="", event=True)
206
207             # Operator properties
208             box.template_keymap_item_properties(kmi)
209
210             # Modal key maps attached to this operator
211             if not km.is_modal:
212                 kmm = kc.keymaps.find_modal(kmi.idname)
213                 if kmm:
214                     self.draw_km(display_keymaps, kc, kmm, None, layout, level + 1)
215                     layout.context_pointer_set("keymap", km)
216
217     def draw_filtered(self, display_keymaps, filter_text, layout):
218         for km, kc in display_keymaps:
219             km = km.active()
220             layout.context_pointer_set("keymap", km)
221
222             filtered_items = [kmi for kmi in km.keymap_items if filter_text in kmi.name.lower()]
223
224             if len(filtered_items) != 0:
225                 col = layout.column()
226
227                 row = col.row()
228                 row.label(text=km.name, icon="DOT")
229
230                 row.label()
231                 row.label()
232
233                 if km.is_user_modified:
234                     row.operator("wm.keymap_restore", text="Restore")
235                 else:
236                     row.label()
237
238                 for kmi in filtered_items:
239                     self.draw_kmi(display_keymaps, kc, km, kmi, col, 1)
240
241                 # "Add New" at end of keymap item list
242                 col = self.indented_layout(layout, 1)
243                 subcol = col.split(percentage=0.2).column()
244                 subcol.operator("wm.keyitem_add", text="Add New", icon='ZOOMIN')
245
246     def draw_hierarchy(self, display_keymaps, layout):
247         from bpy_extras import keyconfig_utils
248         for entry in keyconfig_utils.KM_HIERARCHY:
249             self.draw_entry(display_keymaps, entry, layout)
250
251     def draw_keymaps(self, context, layout):
252         from bpy_extras import keyconfig_utils
253
254         wm = context.window_manager
255         kc = wm.keyconfigs.user
256
257         col = layout.column()
258         sub = col.column()
259
260         subsplit = sub.split()
261         subcol = subsplit.column()
262
263         row = subcol.row(align=True)
264
265         #~ row.prop_search(wm.keyconfigs, "active", wm, "keyconfigs", text="Key Config:")
266         text = bpy.path.display_name(context.window_manager.keyconfigs.active.name)
267         if not text:
268             text = "Blender (default)"
269         row.menu("USERPREF_MT_keyconfigs", text=text)
270         row.operator("wm.keyconfig_preset_add", text="", icon="ZOOMIN")
271         row.operator("wm.keyconfig_preset_add", text="", icon="ZOOMOUT").remove_active = True
272
273         #~ layout.context_pointer_set("keyconfig", wm.keyconfigs.active)
274         #~ row.operator("wm.keyconfig_remove", text="", icon='X')
275
276         row.prop(context.space_data, "filter_text", icon="VIEWZOOM")
277
278         col.separator()
279
280         display_keymaps = keyconfig_utils.keyconfig_merge(kc, kc)
281         if context.space_data.filter_text != "":
282             filter_text = context.space_data.filter_text.lower()
283             self.draw_filtered(display_keymaps, filter_text, col)
284         else:
285             self.draw_hierarchy(display_keymaps, col)
286
287
288 if __name__ == "__main__":  # only for live edit.
289     bpy.utils.register_module(__name__)