Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rna_keymap_ui.py « modules « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2ca7a7997a5cc37d406f837cb763632666ee86f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

# <pep8 compliant>

__all__ = (
    "draw_entry",
    "draw_km",
    "draw_kmi",
    "draw_filtered",
    "draw_hierarchy",
    "draw_keymaps",
    )


import bpy
from bpy.app.translations import pgettext_iface as iface_
from bpy.app.translations import contexts as i18n_contexts


def _indented_layout(layout, level):
    indentpx = 16
    if level == 0:
        level = 0.0001   # Tweak so that a percentage of 0 won't split by half
    indent = level * indentpx / bpy.context.region.width

    split = layout.split(percentage=indent)
    col = split.column()
    col = split.column()
    return col


def draw_entry(display_keymaps, entry, col, level=0):
    idname, spaceid, regionid, children = entry

    for km, kc in display_keymaps:
        if km.name == idname and km.space_type == spaceid and km.region_type == regionid:
            draw_km(display_keymaps, kc, km, children, col, level)

    '''
    km = kc.keymaps.find(idname, space_type=spaceid, region_type=regionid)
    if not km:
        kc = defkc
        km = kc.keymaps.find(idname, space_type=spaceid, region_type=regionid)

    if km:
        draw_km(kc, km, children, col, level)
    '''


def draw_km(display_keymaps, kc, km, children, layout, level):
    km = km.active()

    layout.context_pointer_set("keymap", km)

    col = _indented_layout(layout, level)

    row = col.row(align=True)
    row.prop(km, "show_expanded_children", text="", emboss=False)
    row.label(text=km.name, text_ctxt=i18n_contexts.id_windowmanager)

    if km.is_user_modified or km.is_modal:
        subrow = row.row()
        subrow.alignment = 'RIGHT'

        if km.is_user_modified:
            subrow.operator("wm.keymap_restore", text="Restore")
        if km.is_modal:
            subrow.label(text="", icon='LINKED')
        del subrow

    if km.show_expanded_children:
        if children:
            # Put the Parent key map's entries in a 'global' sub-category
            # equal in hierarchy to the other children categories
            subcol = _indented_layout(col, level + 1)
            subrow = subcol.row(align=True)
            subrow.prop(km, "show_expanded_items", text="", emboss=False)
            subrow.label(text=iface_("%s (Global)") % km.name, translate=False)
        else:
            km.show_expanded_items = True

        # Key Map items
        if km.show_expanded_items:
            kmi_level = level + 3 if children else level + 1
            for kmi in km.keymap_items:
                draw_kmi(display_keymaps, kc, km, kmi, col, kmi_level)

            # "Add New" at end of keymap item list
            subcol = _indented_layout(col, kmi_level)
            subcol = subcol.split(percentage=0.2).column()
            subcol.operator("wm.keyitem_add", text="Add New", text_ctxt=i18n_contexts.id_windowmanager,
                            icon='ZOOMIN')

            col.separator()

        # Child key maps
        if children:
            for entry in children:
                draw_entry(display_keymaps, entry, col, level + 1)

        col.separator()


def draw_kmi(display_keymaps, kc, km, kmi, layout, level):
    map_type = kmi.map_type

    col = _indented_layout(layout, level)

    if kmi.show_expanded:
        col = col.column(align=True)
        box = col.box()
    else:
        box = col.column()

    split = box.split(percentage=0.01)

    # header bar
    row = split.row()
    row.prop(kmi, "show_expanded", text="", emboss=False)

    row = split.row()
    row.prop(kmi, "active", text="", emboss=False)

    if km.is_modal:
        row.prop(kmi, "propvalue", text="")
    else:
        row.label(text=kmi.name)

    row = split.row()
    row.prop(kmi, "map_type", text="")
    if map_type == 'KEYBOARD':
        row.prop(kmi, "type", text="", full_event=True)
    elif map_type == 'MOUSE':
        row.prop(kmi, "type", text="", full_event=True)
    elif map_type == 'NDOF':
        row.prop(kmi, "type", text="", full_event=True)
    elif map_type == 'TWEAK':
        subrow = row.row()
        subrow.prop(kmi, "type", text="")
        subrow.prop(kmi, "value", text="")
    elif map_type == 'TIMER':
        row.prop(kmi, "type", text="")
    else:
        row.label()

    if (not kmi.is_user_defined) and kmi.is_user_modified:
        row.operator("wm.keyitem_restore", text="", icon='BACK').item_id = kmi.id
    else:
        row.operator("wm.keyitem_remove", text="", icon='X').item_id = kmi.id

    # Expanded, additional event settings
    if kmi.show_expanded:
        box = col.box()

        split = box.split(percentage=0.4)
        sub = split.row()

        if km.is_modal:
            sub.prop(kmi, "propvalue", text="")
        else:
            # One day...
            #~ sub.prop_search(kmi, "idname", bpy.context.window_manager, "operators_all", text="")
            sub.prop(kmi, "idname", text="")

        if map_type not in {'TEXTINPUT', 'TIMER'}:
            sub = split.column()
            subrow = sub.row(align=True)

            if map_type == 'KEYBOARD':
                subrow.prop(kmi, "type", text="", event=True)
                subrow.prop(kmi, "value", text="")
            elif map_type in {'MOUSE', 'NDOF'}:
                subrow.prop(kmi, "type", text="")
                subrow.prop(kmi, "value", text="")

            subrow = sub.row()
            subrow.scale_x = 0.75
            subrow.prop(kmi, "any")
            subrow.prop(kmi, "shift")
            subrow.prop(kmi, "ctrl")
            subrow.prop(kmi, "alt")
            subrow.prop(kmi, "oskey", text="Cmd")
            subrow.prop(kmi, "key_modifier", text="", event=True)

        # Operator properties
        box.template_keymap_item_properties(kmi)

        # Modal key maps attached to this operator
        if not km.is_modal:
            kmm = kc.keymaps.find_modal(kmi.idname)
            if kmm:
                draw_km(display_keymaps, kc, kmm, None, layout, level + 1)
                layout.context_pointer_set("keymap", km)

_EVENT_TYPES = set()
_EVENT_TYPE_MAP = {}
_EVENT_TYPE_MAP_EXTRA = {}


def draw_filtered(display_keymaps, filter_type, filter_text, layout):

    if filter_type == 'NAME':
        def filter_func(kmi):
            return (filter_text in kmi.idname.lower() or
                    filter_text in kmi.name.lower())
    else:
        if not _EVENT_TYPES:
            enum = bpy.types.Event.bl_rna.properties["type"].enum_items
            _EVENT_TYPES.update(enum.keys())
            _EVENT_TYPE_MAP.update({item.name.replace(" ", "_").upper(): key
                                    for key, item in enum.items()})

            del enum
            _EVENT_TYPE_MAP_EXTRA.update({
                "`": 'ACCENT_GRAVE',
                "*": 'NUMPAD_ASTERIX',
                "/": 'NUMPAD_SLASH',
                "RMB": 'RIGHTMOUSE',
                "LMB": 'LEFTMOUSE',
                "MMB": 'MIDDLEMOUSE',
                })
            _EVENT_TYPE_MAP_EXTRA.update({
                "%d" % i: "NUMPAD_%d" % i for i in range(10)
                })
        # done with once off init

        filter_text_split = filter_text.strip()
        filter_text_split = filter_text.split()

        # Modifier {kmi.attribute: name} mapping
        key_mod = {
            "ctrl": "ctrl",
            "alt": "alt",
            "shift": "shift",
            "cmd": "oskey",
            "oskey": "oskey",
            "any": "any",
            }
        # KeyMapItem like dict, use for comparing against
        # attr: {states, ...}
        kmi_test_dict = {}
        # Special handling of 'type' using a list if sets,
        # keymap items must match against all.
        kmi_test_type = []

        # initialize? - so if a if a kmi has a MOD assigned it wont show up.
        #~ for kv in key_mod.values():
        #~    kmi_test_dict[kv] = {False}

        # altname: attr
        for kk, kv in key_mod.items():
            if kk in filter_text_split:
                filter_text_split.remove(kk)
                kmi_test_dict[kv] = {True}

        # whats left should be the event type
        def kmi_type_set_from_string(kmi_type):
            kmi_type = kmi_type.upper()
            kmi_type_set = set()

            if kmi_type in _EVENT_TYPES:
                kmi_type_set.add(kmi_type)

            if not kmi_type_set or len(kmi_type) > 1:
                # replacement table
                for event_type_map in (_EVENT_TYPE_MAP, _EVENT_TYPE_MAP_EXTRA):
                    kmi_type_test = event_type_map.get(kmi_type)
                    if kmi_type_test is not None:
                        kmi_type_set.add(kmi_type_test)
                    else:
                        # print("Unknown Type:", kmi_type)

                        # Partial match
                        for k, v in event_type_map.items():
                            if (kmi_type in k) or (kmi_type in v):
                                kmi_type_set.add(v)
            return kmi_type_set

        for i, kmi_type in enumerate(filter_text_split):
            kmi_type_set = kmi_type_set_from_string(kmi_type)

            if not kmi_type_set:
                return False

            kmi_test_type.append(kmi_type_set)
        # tiny optimization, sort sets so the smallest is first
        # improve chances of failing early
        kmi_test_type.sort(key=lambda kmi_type_set: len(kmi_type_set))

        # main filter func, runs many times
        def filter_func(kmi):
            for kk, ki in kmi_test_dict.items():
                val = getattr(kmi, kk)
                if val not in ki:
                    return False

            # special handling of 'type'
            for ki in kmi_test_type:
                val = kmi.type
                if val == 'NONE' or val not in ki:
                    # exception for 'type'
                    # also inspect 'key_modifier' as a fallback
                    val = kmi.key_modifier
                    if not (val == 'NONE' or val not in ki):
                        continue
                    return False

            return True

    for km, kc in display_keymaps:
        km = km.active()
        layout.context_pointer_set("keymap", km)

        filtered_items = [kmi for kmi in km.keymap_items if filter_func(kmi)]

        if filtered_items:
            col = layout.column()

            row = col.row()
            row.label(text=km.name, icon='DOT')

            row.label()
            row.label()

            if km.is_user_modified:
                row.operator("wm.keymap_restore", text="Restore")
            else:
                row.label()

            for kmi in filtered_items:
                draw_kmi(display_keymaps, kc, km, kmi, col, 1)

            # "Add New" at end of keymap item list
            col = _indented_layout(layout, 1)
            subcol = col.split(percentage=0.2).column()
            subcol.operator("wm.keyitem_add", text="Add New", icon='ZOOMIN')
    return True


def draw_hierarchy(display_keymaps, layout):
    from bpy_extras import keyconfig_utils
    for entry in keyconfig_utils.KM_HIERARCHY:
        draw_entry(display_keymaps, entry, layout)


def draw_keymaps(context, layout):
    from bpy_extras import keyconfig_utils

    wm = context.window_manager
    kc = wm.keyconfigs.user
    spref = context.space_data

    col = layout.column()
    sub = col.column()

    subsplit = sub.split()
    subcol = subsplit.column()

    row = subcol.row(align=True)

    #~ row.prop_search(wm.keyconfigs, "active", wm, "keyconfigs", text="Key Config")
    text = bpy.path.display_name(wm.keyconfigs.active.name)
    if not text:
        text = "Blender (default)"
    row.menu("USERPREF_MT_keyconfigs", text=text)
    row.operator("wm.keyconfig_preset_add", text="", icon='ZOOMIN')
    row.operator("wm.keyconfig_preset_add", text="", icon='ZOOMOUT').remove_active = True

    #~ layout.context_pointer_set("keyconfig", wm.keyconfigs.active)
    #~ row.operator("wm.keyconfig_remove", text="", icon='X')
    row.separator()
    rowsub = row.split(align=True, percentage=0.33)
    # postpone drawing into rowsub, so we can set alert!

    col.separator()
    display_keymaps = keyconfig_utils.keyconfig_merge(kc, kc)
    filter_type = spref.filter_type
    filter_text = spref.filter_text.strip()
    if filter_text:
        filter_text = filter_text.lower()
        ok = draw_filtered(display_keymaps, filter_type, filter_text, col)
    else:
        draw_hierarchy(display_keymaps, col)
        ok = True

    # go back and fill in rowsub
    rowsub.prop(spref, "filter_type", text="")
    rowsubsub = rowsub.row(align=True)
    if not ok:
        rowsubsub.alert = True
    rowsubsub.prop(spref, "filter_text", text="", icon='VIEWZOOM')