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

archipack_keymaps.py « archipack - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 05458b9c6463a6143e30c833bca0342cd394cdf4 (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
# -*- coding:utf-8 -*-

# ##### 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>

# ----------------------------------------------------------
# Author: Stephen Leger (s-leger)
#
# ----------------------------------------------------------


class Keymaps:
    """
        Expose user defined keymaps as event
        so in modal operator we are able to
        identify like
        if (event == keymap.undo.event):

        and in feedback panels:
            keymap.undo.key
            keymap.undo.name
    """
    def __init__(self, context):
        """
            Init keymaps properties
        """

        # undo event
        self.undo = self.get_event(context, 'Screen', 'ed.undo')

        # delete event
        self.delete = self.get_event(context, 'Object Mode', 'object.delete')

        """
        # provide abstration between user and addon
        # with different select mouse side
        mouse_right = context.window_manager.keyconfigs.active.preferences.select_mouse
        if mouse_right == 'LEFT':
            mouse_left = 'RIGHT'
            mouse_right_side = 'Left'
            mouse_left_side = 'Right'
        else:
            mouse_left = 'LEFT'
            mouse_right_side = 'Right'
            mouse_left_side = 'Left'

        self.leftmouse = mouse_left + 'MOUSE'
        self.rightmouse = mouse_right + 'MOUSE'
        """

    def check(self, event, against):
        res = False
        signature = (event.alt, event.ctrl, event.shift, event.type, event.value)
        for ev in against:
            # print ("check %s == %s" % (signature, ev))
            if ev['event'] == signature:
                # print("check True")
                res = True
                break
        return res

    def get_event(self, context, keyconfig, keymap_item):
        """
            Return simple keymaps event signature as array of dict
            NOTE:
                this won't work for complex keymaps such as select_all
                using properties to call operator in different manner
            type: keyboard main type
            name: event name as defined in user preferences
            event: simple event signature to compare  like :
              if keymap.check(event, keymap.undo):
        """
        evs = [ev for k, ev in context.window_manager.keyconfigs[0].keymaps[keyconfig].keymap_items.items()
               if k == keymap_item]
        # ev = context.window_manager.keyconfigs[0].keymaps[keyconfig].keymap_items[keymap_item]
        res = []
        for ev in evs:
            key = ev.type
            if ev.ctrl:
                key += '+CTRL'
            if ev.alt:
                key += '+ALT'
            if ev.shift:
                key += '+SHIFT'
            res.append({'type': key, 'name': ev.name, 'event': (ev.alt, ev.ctrl, ev.shift, ev.type, ev.value)})
        return res

    def dump_keys(self, context, filename="/tmp/keymap.txt"):
        """
            Utility for developers :
            Dump all keymaps to a file
            filename : string a file path to dump keymaps
        """
        str = ""
        kms = context.window_manager.keyconfigs
        for name, km in kms.items():
            for key in km.keymaps.keys():
                str += "\n\n#--------------------------------\n{} - {}:\n#--------------------------------\n\n".format(name, key)
                for sub in km[key].keymap_items.keys():
                    k = km[key].keymap_items[sub]
                    str += "alt:{} ctrl:{} shift:{} type:{} value:{}  idname:{} name:{}\n".format(
                        k.alt, k.ctrl, k.shift, k.type, k.value, sub, k.name)
        file = open(filename, "w")
        file.write(str)
        file.close()