-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeymap_change_dialog.py
More file actions
284 lines (261 loc) · 11.7 KB
/
keymap_change_dialog.py
File metadata and controls
284 lines (261 loc) · 11.7 KB
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
# -*- coding: utf-8 -*
from PySide2.QtWidgets import *
from . import config
class MapTypeWidget(QWidget):
"""
Widget to display KeyMapItem information that change dynamically along with map type combo box
"""
def __init__(self, map_type="Keyboard", km_item=None, parent=None):
"""
Create main layout, add items and resize window
Args:
map_type (str): map type to display
km_item (bpy.types.KeyMapItem): initial KeyMapItem to display
parent (QWidget): parent widget
"""
super(MapTypeWidget, self).__init__(parent)
self.main_layout = QGridLayout()
self.main_layout.setRowMinimumHeight(0, 20)
self.main_layout.setRowMinimumHeight(1, 20)
self.main_layout.setRowMinimumHeight(2, 20)
self.map_type_combobox = None
self.setLayout(self.main_layout)
# define ui items
self.type_button = None
self.type = None
self.value_combobox = None
self.key_buttons = None
self.map_type_combobox = None
self.repeat_checkbox = None
self.reset_button = None
self.redraw(map_type)
if km_item:
self._init_items(map_type, km_item)
self.setMinimumWidth(300)
def _init_items(self, map_type, km_item):
"""
initialize values of ui according to km_item
Args:
map_type (str): map type to display
km_item (bpy.types.KeyMapItem): KeyMapItem
Returns:
"""
# type_button
if map_type in ["Keyboard", "Mouse", "NDOF"]:
self.type_button.setText(km_item.to_string())
# type
if map_type == "Keyboard":
self.type.setText(config.KEY_TYPES.inverse[km_item.type])
elif map_type == "Tweak":
self.value_combobox.setCurrentText(config.TWEAK_VALUES.inverse[km_item.value])
elif map_type == "Mouse":
self.type.setCurrentText(config.MOUSE_TYPES.inverse[km_item.type])
elif map_type == "NDOF":
self.type.setCurrentText(config.NDOF_TYPES.inverse[km_item.type])
elif map_type == "Timer":
self.type.setCurrentText(config.TIMER_TYPES.inverse[km_item.type])
# value
if map_type in ["Keyboard", "Mouse", "NDOF"]:
self.value_combobox.setCurrentText(config.CLICK_VALUES.inverse[km_item.value])
elif map_type == "Tweak":
self.value_combobox.setCurrentText(config.TWEAK_VALUES.inverse[km_item.value])
# key_buttons
if map_type in ["Keyboard", "Tweak", "Mouse", "NDOF"]:
for key in self.key_buttons.keys():
if key == "key_modifier":
key_modifier = km_item.key_modifier
if key_modifier:
self.key_buttons["key_modifier"].setChecked(False)
self.key_buttons["key_modifier"].setText(" ")
else:
self.key_buttons["key_modifier"].setChecked(True)
self.key_buttons["key_modifier"].setText(key_modifier)
else:
self.key_buttons[key].setChecked(getattr(km_item, key))
# repeat button
if map_type == "Keyboard":
self.repeat_checkbox.setChecked(km_item.repeat)
def redraw(self, map_type):
"""
update and redraw ui
Args:
map_type: map type to display
Returns:
"""
if map_type == "Keyboard":
self.draw_keyboard_ui()
elif map_type == "Tweak":
self.draw_tweak_ui()
elif map_type == "Mouse":
self.draw_mouse_ui()
elif map_type == "NDOF":
self.draw_ndof_ui()
elif map_type == "Text Input":
self.draw_textinput_ui()
elif map_type == "Timer":
self.draw_timer_ui()
self.main_layout.setMargin(0)
self.main_layout.setSpacing(0)
def generate_key_buttons(self):
"""
return key buttons
Returns (doct): dictionary of key name and QPushButton object
"""
return {"any": QPushButton("Any"), "shift": QPushButton("Shift"),
"ctrl": QPushButton("Ctrl"), "alt": QPushButton("Alt"),
"oskey": QPushButton("Cmd"), "key_modifier": QPushButton(" ")}
def draw_keyboard_ui(self):
self.clear()
self.map_type_combobox = QComboBox()
self.map_type_combobox.addItems(config.MAP_TYPES)
self.main_layout.addWidget(self.map_type_combobox, 0, 1, 1, 2)
self.type_button = QPushButton("A")
self.main_layout.addWidget(self.type_button, 0, 3, 1, 2)
self.reset_button = QPushButton("reset")
self.reset_button.setFixedWidth(40)
self.main_layout.addWidget(self.reset_button, 0, 5, 1, 1)
self.type = QPushButton("A")
self.main_layout.addWidget(self.type, 1, 0, 1, 2)
self.value_combobox = QComboBox()
self.value_combobox.addItems(config.CLICK_VALUES)
self.main_layout.addWidget(self.value_combobox, 1, 2, 1, 2)
self.repeat_checkbox = QCheckBox("repeat")
self.main_layout.addWidget(self.repeat_checkbox, 1, 4, 1, 2)
self.key_buttons = self.generate_key_buttons()
for index, button in enumerate(self.key_buttons.values()):
button.setFixedWidth(50)
button.setCheckable(True)
self.main_layout.addWidget(button, 2, index, 1, 1)
def draw_tweak_ui(self):
self.clear()
sub_widget = QWidget()
sub_layout = QGridLayout()
sub_widget.setLayout(sub_layout)
sub_layout.setMargin(0)
self.main_layout.addWidget(sub_widget, 0, 0, 1, 5)
self.map_type_combobox = QComboBox()
self.map_type_combobox.addItems(config.MAP_TYPES)
self.map_type_combobox.setCurrentIndex(1)
self.type = QComboBox()
self.type.addItems(config.TWEAK_TYPES)
self.value_combobox = QComboBox()
self.value_combobox.addItems(config.TWEAK_VALUES)
sub_layout.addWidget(self.map_type_combobox, 0, 1, 1, 1)
sub_layout.addWidget(self.type, 0, 2, 1, 1)
sub_layout.addWidget(self.value_combobox, 0, 3, 1, 1)
self.reset_button = QPushButton("reset")
self.reset_button.setFixedWidth(40)
self.main_layout.addWidget(self.reset_button, 0, 5, 1, 1)
self.key_buttons = self.generate_key_buttons()
for index, button in enumerate(self.key_buttons.values()):
button.setFixedWidth(50)
button.setCheckable(True)
self.main_layout.addWidget(button, 1, index, 1, 1)
def draw_mouse_ui(self):
self.clear()
self.map_type_combobox = QComboBox()
self.map_type_combobox.addItems(config.MAP_TYPES)
self.map_type_combobox.setCurrentIndex(2)
self.main_layout.addWidget(self.map_type_combobox, 0, 1, 1, 2)
self.type_button = QPushButton("Left Mouse")
self.main_layout.addWidget(self.type_button, 0, 3, 1, 2)
self.reset_button = QPushButton("reset")
self.reset_button.setFixedWidth(40)
self.main_layout.addWidget(self.reset_button, 0, 5, 1, 1)
self.type = QComboBox()
self.type.addItems(config.MOUSE_TYPES)
self.main_layout.addWidget(self.type, 1, 0, 1, 3)
self.value_combobox = QComboBox()
self.value_combobox.addItems(config.CLICK_VALUES)
self.main_layout.addWidget(self.value_combobox, 1, 3, 1, 3)
self.key_buttons = self.generate_key_buttons()
for index, button in enumerate(self.key_buttons.values()):
button.setFixedWidth(50)
button.setCheckable(True)
self.main_layout.addWidget(button, 2, index, 1, 1)
def draw_ndof_ui(self):
self.clear()
self.map_type_combobox = QComboBox()
self.map_type_combobox.addItems(config.MAP_TYPES)
self.map_type_combobox.setCurrentIndex(3)
self.main_layout.addWidget(self.map_type_combobox, 0, 1, 1, 2)
self.type_button = QPushButton("NDOF Motion")
self.main_layout.addWidget(self.type_button, 0, 3, 1, 2)
self.reset_button = QPushButton("reset")
self.reset_button.setFixedWidth(40)
self.main_layout.addWidget(self.reset_button, 0, 5, 1, 1)
self.type = QComboBox()
self.type.addItems(config.NDOF_TYPES)
self.main_layout.addWidget(self.type, 1, 0, 1, 3)
self.value_combobox = QComboBox()
self.value_combobox.addItems(config.CLICK_VALUES)
self.value_combobox.setCurrentText("Nothing")
self.main_layout.addWidget(self.value_combobox, 1, 3, 1, 3)
self.key_buttons = self.generate_key_buttons()
for index, button in enumerate(self.key_buttons.values()):
button.setFixedWidth(50)
button.setCheckable(True)
self.main_layout.addWidget(button, 2, index, 1, 1)
def draw_textinput_ui(self):
self.clear()
self.map_type_combobox = QComboBox()
self.map_type_combobox.addItems(config.MAP_TYPES)
self.map_type_combobox.setCurrentIndex(4)
self.main_layout.addWidget(self.map_type_combobox, 0, 1, 1, 2)
null_widget = QWidget()
self.main_layout.addWidget(null_widget, 0, 3, 1, 2)
self.reset_button = QPushButton("reset")
self.reset_button.setFixedWidth(40)
self.main_layout.addWidget(self.reset_button, 0, 5, 1, 1)
def draw_timer_ui(self):
self.clear()
self.map_type_combobox = QComboBox()
self.map_type_combobox.addItems(config.MAP_TYPES)
self.map_type_combobox.setCurrentIndex(5)
self.main_layout.addWidget(self.map_type_combobox, 0, 1, 1, 2)
self.type = QComboBox()
self.type.addItems(config.TIMER_TYPES)
self.main_layout.addWidget(self.type, 0, 3, 1, 2)
self.reset_button = QPushButton("reset")
self.reset_button = QPushButton("reset")
self.reset_button.setFixedWidth(40)
self.main_layout.addWidget(self.reset_button, 0, 5, 1, 1)
def clear(self):
for i in range(self.main_layout.count()):
child = self.main_layout.itemAt(i)
child.widget().deleteLater()
class KeymapChangeDialog(QDialog):
"""
Popup dialog to set keymap, which is displayed When keymap item double clicked
"""
def __init__(self, km_item, parent=None):
"""
Create main layout, add items and resize window
Args:
km_item (bpy.types.KeyMapItem): initial KeyMapItem to display
parent (QWidget): parent widget
"""
super(KeymapChangeDialog, self).__init__(parent)
self.main_layout = QGridLayout()
self.main_layout.setRowMinimumHeight(0, 20)
self.main_layout.setRowMinimumHeight(1, 20)
self.setLayout(self.main_layout)
keymap_name = QLabel(km_item.name)
keymap_name.setFixedHeight(20)
self.main_layout.addWidget(keymap_name, 0, 0, 1, 3)
self.map_types_widget = MapTypeWidget(map_type=config.MAP_TYPES.inverse[km_item.map_type], km_item=km_item)
self.map_types_widget.map_type_combobox.currentTextChanged.connect(self._redraw)
self.main_layout.addWidget(self.map_types_widget, 0, 4, 3, 6)
identifier = QLineEdit(km_item.idname)
self.main_layout.addWidget(identifier, 1, 0, 1, 4)
self.main_layout.setMargin(5)
def _redraw(self):
"""
redraw MapTypeWidget when its map_type_combobox is changed
Returns:
"""
map_type = self.map_types_widget.map_type_combobox.currentText()
self.map_types_widget.deleteLater()
self.map_types_widget = MapTypeWidget(map_type=map_type)
self.main_layout.addWidget(self.map_types_widget, 0, 4, 3, 6)
self.map_types_widget.map_type_combobox.currentTextChanged.connect(self._redraw)