Cleanup: tabs -> spaces
[blender-addons.git] / archipack / archipack_keymaps.py
blob05458b9c6463a6143e30c833bca0342cd394cdf4
1 # -*- coding:utf-8 -*-
3 # ##### BEGIN GPL LICENSE BLOCK #####
5 # This program 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.
10 # This program 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.
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 Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110- 1301, USA.
19 # ##### END GPL LICENSE BLOCK #####
21 # <pep8 compliant>
23 # ----------------------------------------------------------
24 # Author: Stephen Leger (s-leger)
26 # ----------------------------------------------------------
29 class Keymaps:
30 """
31 Expose user defined keymaps as event
32 so in modal operator we are able to
33 identify like
34 if (event == keymap.undo.event):
36 and in feedback panels:
37 keymap.undo.key
38 keymap.undo.name
39 """
40 def __init__(self, context):
41 """
42 Init keymaps properties
43 """
45 # undo event
46 self.undo = self.get_event(context, 'Screen', 'ed.undo')
48 # delete event
49 self.delete = self.get_event(context, 'Object Mode', 'object.delete')
51 """
52 # provide abstration between user and addon
53 # with different select mouse side
54 mouse_right = context.window_manager.keyconfigs.active.preferences.select_mouse
55 if mouse_right == 'LEFT':
56 mouse_left = 'RIGHT'
57 mouse_right_side = 'Left'
58 mouse_left_side = 'Right'
59 else:
60 mouse_left = 'LEFT'
61 mouse_right_side = 'Right'
62 mouse_left_side = 'Left'
64 self.leftmouse = mouse_left + 'MOUSE'
65 self.rightmouse = mouse_right + 'MOUSE'
66 """
68 def check(self, event, against):
69 res = False
70 signature = (event.alt, event.ctrl, event.shift, event.type, event.value)
71 for ev in against:
72 # print ("check %s == %s" % (signature, ev))
73 if ev['event'] == signature:
74 # print("check True")
75 res = True
76 break
77 return res
79 def get_event(self, context, keyconfig, keymap_item):
80 """
81 Return simple keymaps event signature as array of dict
82 NOTE:
83 this won't work for complex keymaps such as select_all
84 using properties to call operator in different manner
85 type: keyboard main type
86 name: event name as defined in user preferences
87 event: simple event signature to compare like :
88 if keymap.check(event, keymap.undo):
89 """
90 evs = [ev for k, ev in context.window_manager.keyconfigs[0].keymaps[keyconfig].keymap_items.items()
91 if k == keymap_item]
92 # ev = context.window_manager.keyconfigs[0].keymaps[keyconfig].keymap_items[keymap_item]
93 res = []
94 for ev in evs:
95 key = ev.type
96 if ev.ctrl:
97 key += '+CTRL'
98 if ev.alt:
99 key += '+ALT'
100 if ev.shift:
101 key += '+SHIFT'
102 res.append({'type': key, 'name': ev.name, 'event': (ev.alt, ev.ctrl, ev.shift, ev.type, ev.value)})
103 return res
105 def dump_keys(self, context, filename="/tmp/keymap.txt"):
107 Utility for developers :
108 Dump all keymaps to a file
109 filename : string a file path to dump keymaps
111 str = ""
112 kms = context.window_manager.keyconfigs
113 for name, km in kms.items():
114 for key in km.keymaps.keys():
115 str += "\n\n#--------------------------------\n{} - {}:\n#--------------------------------\n\n".format(name, key)
116 for sub in km[key].keymap_items.keys():
117 k = km[key].keymap_items[sub]
118 str += "alt:{} ctrl:{} shift:{} type:{} value:{} idname:{} name:{}\n".format(
119 k.alt, k.ctrl, k.shift, k.type, k.value, sub, k.name)
120 file = open(filename, "w")
121 file.write(str)
122 file.close()