-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateseq_pattern_editor.py
344 lines (261 loc) · 13 KB
/
gateseq_pattern_editor.py
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
from PySide6.QtWidgets import QInputDialog, QMessageBox, QPushButton
from PySide6.QtCore import Slot, Qt
from PySide6.QtGui import QUndoCommand, QUndoStack, QKeySequence
from ui_resources.ui_gateseq_pattern_editor import Ui_gateseqPatternEditor
from viatools.gateseq_patterns import GateseqPatternSet
from via_resource_editor import ViaResourceEditor
from gateseq_sequence_edit import GateseqSequenceEdit
class AddRecipeCommand(QUndoCommand):
def __init__(self, pattern, recipe, ui_callback):
super().__init__()
self.setText('Add %d/%d' % (recipe[0], recipe[1]))
self.pattern = pattern
self.recipe = recipe
self.ui_callback = ui_callback
self.old_fill = None
def redo(self):
self.idx = self.pattern.add_data(self.recipe)
self.ui_callback()
def undo(self):
self.pattern.remove_data(self.idx)
self.ui_callback()
class RemoveRecipeCommand(QUndoCommand):
def __init__(self, pattern, idx, ui_callback):
super().__init__()
self.setText('Remove index %d' % (idx + 1))
self.pattern = pattern
self.idx = idx
self.ui_callback = ui_callback
def redo(self):
self.ratio = self.pattern.remove_data(self.idx)
self.ui_callback()
def undo(self):
self.pattern.add_data(self.ratio, self.idx)
self.ui_callback()
class ClearRecipesCommand(QUndoCommand):
def __init__(self, pattern, ui_callback):
super().__init__()
self.setText('Clear sequences')
self.pattern = pattern
self.ui_callback = ui_callback
def redo(self):
self.old_order = self.pattern.clear_data()
self.ui_callback()
def undo(self):
self.pattern.reload_data(self.old_order)
self.ui_callback()
class UpdateStepCommand(QUndoCommand):
def __init__(self, pattern, seq_idx, step_idx, state, ui_callback):
super().__init__()
self.setText('Update sequence %d step %d' % (seq_idx + 1, step_idx + 1))
self.pattern = pattern
self.seq_idx = seq_idx
self.step_idx = step_idx
self.state = state
self.ui_callback = ui_callback
def redo(self):
self.backup = self.pattern.get_data()
self.new_idx = self.pattern.update_step(self.seq_idx, self.step_idx, self.state)
self.ui_callback()
def undo(self):
self.pattern.reload_data(self.backup)
self.ui_callback()
class UpdateLengthCommand(QUndoCommand):
def __init__(self, pattern, idx, length, ui_callback):
super().__init__()
self.setText('Update sequence %d to length' % (idx + 1, length))
self.pattern = pattern
self.idx = idx
self.length = length
self.ui_callback = ui_callback
def redo(self):
self.backup = self.pattern.get_recipe(self.idx)
self.new_idx = self.pattern.update_length(self.idx, self.length)
self.ui_callback()
def undo(self):
self.pattern.reload_recipe(self.new_idx, self.backup)
self.ui_callback()
class ReorderPatternCommand(QUndoCommand):
def __init__(self, pattern, idx_to_move, destination, ui_callback):
super().__init__()
self.setText('Move sequence %d to %d' % (idx_to_move + 1, destination + 1))
self.pattern = pattern
self.idx_to_move = idx_to_move
self.destination = destination
self.ui_callback = ui_callback
def redo(self):
self.pattern.reorder_data(self.idx_to_move, self.destination)
self.redo_destination = self.idx_to_move
self.redo_idx_to_move = self.destination
self.ui_callback()
def undo(self):
self.pattern.reorder_data(self.redo_idx_to_move, self.redo_destination)
self.ui_callback()
class UpdateSortedCommand(QUndoCommand):
def __init__(self, pattern, is_sorted, ui_callback):
super().__init__()
self.setText('Set sorted to %s' % (str(is_sorted)))
self.pattern = pattern
self.is_sorted = is_sorted
self.ui_callback = ui_callback
def redo(self):
if self.is_sorted:
self.old_order = self.pattern.update_sorted(True)
self.ui_callback()
else:
self.old_order = self.pattern.update_sorted(False)
if self.old_order:
self.pattern.reload_data(self.old_order)
self.ui_callback()
def undo(self):
if not self.is_sorted:
self.old_order = self.pattern.update_sorted(True)
self.ui_callback()
else:
self.pattern.update_sorted(False)
self.pattern.reload_data(self.old_order)
self.ui_callback()
class GateseqPatternEditor(ViaResourceEditor, Ui_gateseqPatternEditor):
def __init__(self, resource_dir='./', remote_resources = {}, slug='original', style_text=""):
super().__init__()
self.setupUi(self)
self.setStyleSheet(style_text)
self.remote_resources = remote_resources
# TODO check if new remote resource or set collides with existing local slug
self.set = GateseqPatternSet(resource_dir, slug)
self.set_slug = slug
self.update_resource_sets()
self.update_resources()
self.create_pattern_grid()
for slot_num in range(0, 8):
eval('self.slot%d' % (slot_num+1)).clicked.connect(lambda state=True, x=slot_num: self.switch_slot(x))
if (slot_num < 4):
eval('self.slot%d' % (slot_num+1)).setToolTip("Edit Sequencer I sequence " + str(slot_num + 1))
else:
eval('self.slot%d' % (slot_num+1)).setToolTip("Edit Sequencer II sequence " + str(slot_num - 3))
self.selectResource.activated.connect(self.handle_select_resource)
self.saveResource.clicked.connect(lambda state=True: self.handle_save_resource())
self.switch_slot(0)
self.unsorted_data = None
self.setFocusPolicy(Qt.ClickFocus)
self.initToolTips()
# Edit pattern recipe
@Slot()
def on_length_valueChanged(self):
self.steps.setMaximum(self.length.value())
@Slot()
def on_addEuclidean_clicked(self):
add_recipe = AddRecipeCommand(self.set.resources[self.active_idx], [self.steps.value(), self.length.value()], self.update_resource_ui)
self.resource_undo_stack.push(add_recipe)
@Slot()
def on_clearSequences_clicked(self):
clear_recipes = ClearRecipesCommand(self.set.resources[self.active_idx], self.update_resource_ui)
self.resource_undo_stack.push(clear_recipes)
@Slot()
def on_rulerSize_valueChanged(self):
for editor in self.sequence_editors:
editor.reset_ruler(self.rulerSize.value())
@Slot()
def on_sorted_clicked(self):
self.unsorted_data = self.set.resources[self.active_idx].get_data()
sorted_command = UpdateSortedCommand(self.set.resources[self.active_idx], True, self.update_resource_ui)
self.resource_undo_stack.push(sorted_command)
self.sorted_flag = True
@Slot()
def on_unsorted_clicked(self):
if self.unsorted_data:
self.set.resources[self.active_idx].reload_data(self.unsorted_data)
sorted_command = UpdateSortedCommand(self.set.resources[self.active_idx], False, self.update_resource_ui)
self.resource_undo_stack.push(sorted_command)
self.sorted_flag = False
# Pattern recipe dispaly helpers
def create_pattern_grid(self):
self.sequence_editors = []
for i in range(0, 16):
seq_edit = GateseqSequenceEdit(self)
seq_edit.remove.clicked.connect(lambda state=True, x=i: self.remove_button_pushed(x))
for j in range(0, 64):
seq_edit.step_buttons[j].clicked.connect(lambda state=True, x=i, y=j : self.step_button_pushed(x, y))
seq_edit.length_entry.valueChanged.connect(lambda state=True, x=i : self.update_sequence_length(x))
self.sequence_editors.append(seq_edit)
self.sequenceEditLayout.addWidget(seq_edit)
self.sequenceEditLayout.setContentsMargins(0,0,0,0)
self.update_resource_ui()
def remove_button_pushed(self, idx):
remove_recipe = RemoveRecipeCommand(self.set.resources[self.active_idx], idx, self.update_resource_ui)
self.resource_undo_stack.push(remove_recipe)
def step_button_pushed(self, seq_idx, step_idx):
if self.sequence_editors[seq_idx].step_buttons[step_idx].isChecked():
update_step = UpdateStepCommand(self.set.resources[self.active_idx], self.sequence_editors[seq_idx].unsorted_idx, step_idx, True, self.update_resource_ui)
self.resource_undo_stack.push(update_step)
else:
update_step = UpdateStepCommand(self.set.resources[self.active_idx], self.sequence_editors[seq_idx].unsorted_idx, step_idx, False, self.update_resource_ui)
self.resource_undo_stack.push(update_step)
def update_sequence_length(self, seq_idx):
# Stopping an infinte loop ... ewwwwiiieeee!
old_length = self.set.resources[self.active_idx].get_length(seq_idx)
length = self.sequence_editors[seq_idx].length_entry.value()
if old_length != length:
update_length = UpdateLengthCommand(self.set.resources[self.active_idx], self.sequence_editors[seq_idx].unsorted_idx, length, self.update_resource_ui)
self.resource_undo_stack.push(update_length)
def handle_drop(self, destination_idx):
reorder = ReorderPatternCommand(self.set.resources[self.active_idx], self.dragged_idx, destination_idx, self.update_resource_ui)
self.resource_undo_stack.push(reorder)
def update_resource_ui(self):
self.resourceDescription.setText(self.set.resources[self.active_idx].data['description'])
self.sorted.setChecked(True)
if 'sorted' in self.set.resources[self.active_idx].data:
if self.set.resources[self.active_idx].data['sorted'] is False:
self.unsorted.setChecked(True)
self.sorted_flag = False
else:
self.sorted_flag = True
else:
self.sorted_flag = True
sequences = self.set.resources[self.active_idx].data['sorted_patterns']
idx = -1
for idx, sequence in enumerate(sequences):
for button in self.sequence_editors[idx].step_buttons:
button.setChecked(False)
self.sequence_editors[idx].show()
seq_text_tag = self.set.resources[self.active_idx].get_name(idx)
self.sequence_editors[idx].label.setText(seq_text_tag)
baked = self.set.resources[self.active_idx].expand_sequence(sequence)
for i in range(0,64):
if baked[i % len(baked)] == 1:
self.sequence_editors[idx].step_buttons[i].setChecked(True)
self.sequence_editors[idx].step_buttons[i].setEnabled(True)
self.sequence_editors[idx].step_buttons[i].setToolTip("Toggle value for this step")
for i in range(len(baked), 64):
self.sequence_editors[idx].step_buttons[i].setEnabled(False)
self.sequence_editors[idx].step_buttons[i].setToolTip("Button disabled because outside of sequence length")
self.sequence_editors[idx].length_entry.setValue(len(baked))
self.sequence_editors[idx].set_idx(idx)
self.sequence_editors[idx].unsorted_idx = self.set.resources[self.active_idx].data['unsorted_indices'][idx]
for i in range(idx+1, 16):
self.sequence_editors[i].hide()
if len(sequences) < 16:
self.addEuclidean.setEnabled(True)
else:
self.addEuclidean.setEnabled(False)
if self.set.is_clean():
self.saveResourceSet.setEnabled(False)
else:
self.saveResourceSet.setEnabled(True)
def showEvent(self, event):
super().showEvent(event)
self.undo_stack_init()
def clear_menu(self):
self.undo_action.setVisible(False)
self.redo_action.setVisible(False)
def initToolTips(self):
super().initToolTips()
self.selectResource.setToolTip("Select an available pattern for this mode and open it in the editor")
self.saveResource.setToolTip("Save the edited pattern")
self.sorted.setToolTip("The list of sequences below will be automatically sorted and loaded according to density (hits per step)")
self.unsorted.setToolTip("The list of sequences below can be freely reordered using the on the button on the far left of each row")
self.rulerSize.setToolTip("Set the amount of steps per ruler tick in the sequence display below")
self.length.setToolTip("Length of the euclidean sequence to add")
self.steps.setToolTip("Number of hits in the euclidean sequence to add, restricted to between zero and the Length value")
self.addEuclidean.setToolTip("Add a new euclidean sequence according to the steps and length parameters, disabled when pattern is full (16 seqeunces)")
self.clearSequences.setToolTip("Remove all sequences from the list except for a default sequence that hits on every step")