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
|
'''
Preset manager object handles all interactions with presets, including
the context menu accessed from MainWindow.
'''
from PyQt5 import QtCore, QtWidgets, uic
import string
import os
import logging
from ..toolkit import badName
from ..core import Core
from .actions import *
log = logging.getLogger('AVP.Gui.PresetManager')
class PresetManager(QtWidgets.QDialog):
def __init__(self, parent):
super().__init__()
uic.loadUi(
os.path.join(Core.wd, 'gui', 'presetmanager.ui'), self)
self.parent = parent
self.core = parent.core
self.settings = parent.settings
self.presetDir = parent.presetDir
if not self.settings.value('presetDir'):
self.settings.setValue(
"presetDir",
os.path.join(parent.dataDir, 'projects'))
self.findPresets()
# window
self.lastFilter = '*'
self.presetRows = [] # list of (comp, vers, name) tuples
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
# connect button signals
self.pushButton_delete.clicked.connect(
self.openDeletePresetDialog
)
self.pushButton_rename.clicked.connect(
self.openRenamePresetDialog
)
self.pushButton_import.clicked.connect(
self.openImportDialog
)
self.pushButton_export.clicked.connect(
self.openExportDialog
)
self.pushButton_close.clicked.connect(
self.close
)
# create filter box and preset list
self.drawFilterList()
self.comboBox_filter.currentIndexChanged.connect(
lambda: self.drawPresetList(
self.comboBox_filter.currentText(),
self.lineEdit_search.text()
)
)
# make auto-completion for search bar
self.autocomplete = QtCore.QStringListModel()
completer = QtWidgets.QCompleter()
completer.setModel(self.autocomplete)
self.lineEdit_search.setCompleter(completer)
self.lineEdit_search.textChanged.connect(
lambda: self.drawPresetList(
self.comboBox_filter.currentText(),
self.lineEdit_search.text()
)
)
self.drawPresetList('*')
def show_(self):
'''Open a new preset manager window from the mainwindow'''
self.findPresets()
self.drawFilterList()
self.drawPresetList('*')
self.show()
def findPresets(self):
log.debug("Searching %s for presets", self.presetDir)
parseList = []
for dirpath, dirnames, filenames in os.walk(self.presetDir):
# anything without a subdirectory must be a preset folder
if dirnames:
continue
for preset in filenames:
compName = os.path.basename(os.path.dirname(dirpath))
if compName not in self.core.compNames:
continue
compVers = os.path.basename(dirpath)
try:
parseList.append((compName, int(compVers), preset))
except ValueError:
continue
self.presets = {
compName: [
(vers, preset)
for name, vers, preset in parseList
if name == compName
]
for compName, _, __ in parseList
}
def drawPresetList(self, compFilter=None, presetFilter=''):
self.listWidget_presets.clear()
if compFilter:
self.lastFilter = str(compFilter)
else:
compFilter = str(self.lastFilter)
self.presetRows = []
presetNames = []
for component, presets in self.presets.items():
if compFilter != '*' and component != compFilter:
continue
for vers, preset in presets:
if not presetFilter or presetFilter in preset:
self.listWidget_presets.addItem(
'%s: %s' % (component, preset)
)
self.presetRows.append((component, vers, preset))
if preset not in presetNames:
presetNames.append(preset)
self.autocomplete.setStringList(presetNames)
def drawFilterList(self):
self.comboBox_filter.clear()
self.comboBox_filter.addItem('*')
for component in self.presets:
self.comboBox_filter.addItem(component)
def clearPreset(self, compI=None):
'''Functions on mainwindow level from the context menu'''
compI = self.parent.listWidget_componentList.currentRow()
action = ClearPreset(self.parent, compI)
self.parent.undoStack.push(action)
def openSavePresetDialog(self):
'''Functions on mainwindow level from the context menu'''
selectedComponents = self.core.selectedComponents
componentList = self.parent.listWidget_componentList
if componentList.currentRow() == -1:
return
while True:
index = componentList.currentRow()
currentPreset = selectedComponents[index].currentPreset
newName, OK = QtWidgets.QInputDialog.getText(
self.parent,
'Audio Visualizer',
'New Preset Name:',
QtWidgets.QLineEdit.Normal,
currentPreset
)
if OK:
if badName(newName):
self.warnMessage(self.parent)
continue
if newName:
if index != -1:
selectedComponents[index].currentPreset = newName
saveValueStore = \
selectedComponents[index].savePreset()
saveValueStore['preset'] = newName
componentName = str(selectedComponents[index]).strip()
vers = selectedComponents[index].version
self.createNewPreset(
componentName, vers, newName,
saveValueStore, window=self.parent)
self.findPresets()
self.drawPresetList()
self.openPreset(newName, index)
break
def createNewPreset(
self, compName, vers, filename, saveValueStore, **kwargs):
path = os.path.join(self.presetDir, compName, str(vers), filename)
if self.presetExists(path, **kwargs):
return
self.core.createPresetFile(compName, vers, filename, saveValueStore)
def presetExists(self, path, **kwargs):
if os.path.exists(path):
window = kwargs.get("window", self)
ch = self.parent.showMessage(
msg="%s already exists! Overwrite it?" %
os.path.basename(path),
showCancel=True,
icon='Warning',
parent=window)
if not ch:
# user clicked cancel
return True
return False
def openPreset(self, presetName, compPos=None):
componentList = self.parent.listWidget_componentList
index = compPos if compPos is not None else componentList.currentRow()
if index == -1:
return
action = OpenPreset(self, presetName, index)
self.parent.undoStack.push(action)
def _openPreset(self, presetName, index):
selectedComponents = self.core.selectedComponents
componentName = selectedComponents[index].name.strip()
version = selectedComponents[index].version
dirname = os.path.join(self.presetDir, componentName, str(version))
filepath = os.path.join(dirname, presetName)
self.core.openPreset(filepath, index, presetName)
self.parent.updateComponentTitle(index)
self.parent.drawPreview()
def openDeletePresetDialog(self):
row = self.getPresetRow()
if row == -1:
return
comp, vers, name = self.presetRows[row]
ch = self.parent.showMessage(
msg='Really delete %s?' % name,
showCancel=True,
icon='Warning',
parent=self
)
if not ch:
return
self.deletePreset(comp, vers, name)
def deletePreset(self, comp, vers, name):
action = DeletePreset(self, comp, vers, name)
self.parent.undoStack.push(action)
def warnMessage(self, window=None):
self.parent.showMessage(
msg='Preset names must contain only letters, '
'numbers, and spaces.',
parent=window if window else self)
def getPresetRow(self):
row = self.listWidget_presets.currentRow()
if row > -1:
return row
# check if component selected in MainWindow has preset loaded
componentList = self.parent.listWidget_componentList
compIndex = componentList.currentRow()
if compIndex == -1:
return compIndex
preset = self.core.selectedComponents[compIndex].currentPreset
if preset is None:
return -1
else:
rowTuple = (
self.core.selectedComponents[compIndex].name,
self.core.selectedComponents[compIndex].version,
preset
)
for i, tup in enumerate(self.presetRows):
if rowTuple == tup:
index = i
break
else:
return -1
return index
def openRenamePresetDialog(self):
presetList = self.listWidget_presets
index = self.getPresetRow()
if index == -1:
return
while True:
newName, OK = QtWidgets.QInputDialog.getText(
self,
'Preset Manager',
'Rename Preset:',
QtWidgets.QLineEdit.Normal,
self.presetRows[index][2]
)
if OK:
if badName(newName):
self.warnMessage()
continue
if newName:
comp, vers, oldName = self.presetRows[index]
path = os.path.join(
self.presetDir, comp, str(vers))
newPath = os.path.join(path, newName)
if self.presetExists(newPath):
return
action = RenamePreset(self, path, oldName, newName)
self.parent.undoStack.push(action)
break
def renamePreset(self, path, oldName, newName):
oldPath = os.path.join(path, oldName)
newPath = os.path.join(path, newName)
if os.path.exists(newPath):
os.remove(newPath)
os.rename(oldPath, newPath)
self.findPresets()
self.drawPresetList()
path = os.path.dirname(newPath)
for i, comp in enumerate(self.core.selectedComponents):
if self.core.getPresetDir(comp) == path \
and comp.currentPreset == oldName:
self.core.openPreset(newPath, i, newName)
self.parent.updateComponentTitle(i, False)
self.parent.drawPreview()
def openImportDialog(self):
filename, _ = QtWidgets.QFileDialog.getOpenFileName(
self, "Import Preset File",
self.settings.value("presetDir"),
"Preset Files (*.avl)")
if filename:
# get installed path & ask user to overwrite if needed
path = ''
while True:
if path:
if self.presetExists(path):
break
else:
if os.path.exists(path):
os.remove(path)
success, path = self.core.importPreset(filename)
if success:
break
self.findPresets()
self.drawPresetList()
self.settings.setValue("presetDir", os.path.dirname(filename))
def openExportDialog(self):
index = self.getPresetRow()
if index == -1:
return
filename, _ = QtWidgets.QFileDialog.getSaveFileName(
self, "Export Preset",
self.settings.value("presetDir"),
"Preset Files (*.avl)")
if filename:
comp, vers, name = self.presetRows[index]
if not self.core.exportPreset(filename, comp, vers, name):
self.parent.showMessage(
msg='Couldn\'t export %s.' % filename,
parent=self
)
self.settings.setValue("presetDir", os.path.dirname(filename))
def clearPresetListSelection(self):
self.listWidget_presets.setCurrentRow(-1)
|