added submenu for opening presets, moved code

This commit is contained in:
tassaron 2017-06-07 23:22:55 -04:00
parent 6093e701e1
commit 292d21c203
8 changed files with 140 additions and 121 deletions

View File

@ -2,6 +2,9 @@ from PyQt4 import QtGui
class Component:
def __init__(self):
self.currentPreset = None
def __str__(self):
return self.__doc__
@ -72,7 +75,8 @@ class Component:
image = Image.new("RGBA", (width, height), (0,0,0,0))
return image
def loadPreset(self, presetDict):
def loadPreset(self, presetDict, presetName=None):
self.currentPreset = presetName
# update widgets using a preset dict
def savePreset(self):

View File

@ -69,7 +69,8 @@ class Component(__base__.Component):
r, g, b = self.color1
return Image.new("RGBA", (width, height), (r, g, b, 255))
def loadPreset(self, pr):
def loadPreset(self, pr, presetName=None):
self.currentPreset = presetName
self.page.lineEdit_color1.setText('%s,%s,%s' % pr['color1'])
self.page.lineEdit_color2.setText('%s,%s,%s' % pr['color2'])

View File

@ -48,7 +48,8 @@ class Component(__base__.Component):
frame.paste(image)
return frame
def loadPreset(self, pr):
def loadPreset(self, pr, presetName=None):
self.currentPreset = presetName
self.page.lineEdit_image.setText(pr['image'])
def savePreset(self):
@ -60,7 +61,7 @@ class Component(__base__.Component):
imgDir = self.settings.value("backgroundDir", os.path.expanduser("~"))
filename = QtGui.QFileDialog.getOpenFileName(
self.page, "Choose Image", imgDir, "Image Files (*.jpg *.png)")
if filename:
if filename:
self.settings.setValue("backgroundDir", os.path.dirname(filename))
self.page.lineEdit_image.setText(filename)
self.update()

View File

@ -37,7 +37,8 @@ class Component(__base__.Component):
self.visColor = self.RGBFromString(self.page.lineEdit_visColor.text())
self.parent.drawPreview()
def loadPreset(self, pr):
def loadPreset(self, pr, presetName=None):
self.currentPreset = presetName
self.page.lineEdit_visColor.setText('%s,%s,%s' % pr['visColor'])
btnStyle = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*pr['visColor']).name()

View File

@ -78,7 +78,8 @@ class Component(__base__.Component):
x = self.xPosition - offset
return x, self.yPosition
def loadPreset(self, pr):
def loadPreset(self, pr, presetName=None):
self.currentPreset = presetName
self.page.lineEdit_title.setText(pr['title'])
font = QFont()
font.fromString(pr['titleFont'])

View File

@ -128,7 +128,8 @@ class Component(__base__.Component):
def frameRender(self, moduleNo, arrayNo, frameNo):
return self.video.frame(frameNo)
def loadPreset(self, pr):
def loadPreset(self, pr, presetName=None):
self.currentPreset = presetName
self.page.lineEdit_video.setText(pr['video'])
self.page.checkBox_loop.setChecked(pr['loop'])
@ -144,7 +145,7 @@ class Component(__base__.Component):
self.page, "Choose Video",
imgDir, "Video Files (*.mp4 *.mov)"
)
if filename:
if filename:
self.settings.setValue("backgroundDir", os.path.dirname(filename))
self.page.lineEdit_video.setText(filename)
self.update()

View File

@ -6,9 +6,7 @@ from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import QSettings, Qt
from PyQt4.QtGui import QDesktopServices, QMenu
import sys
import io
import os
import string
import signal
import filecmp
import time
@ -63,24 +61,22 @@ class MainWindow(QtCore.QObject):
# Create data directory, load/create settings
self.dataDir = QDesktopServices.storageLocation(
QDesktopServices.DataLocation)
self.presetManager = PresetManager(
uic.loadUi(
os.path.join(os.path.dirname(os.path.realpath(__file__)),
'presetmanager.ui')),
self)
self.autosavePath = os.path.join(self.dataDir, 'autosave.avp')
self.presetDir = os.path.join(self.dataDir, 'presets')
self.settings = QSettings(
os.path.join(self.dataDir, 'settings.ini'), QSettings.IniFormat)
LoadDefaultSettings(self)
if not os.path.exists(self.dataDir):
os.makedirs(self.dataDir)
for neededDirectory in (
self.presetDir, self.settings.value("projectDir")):
self.presetManager.presetDir, self.settings.value("projectDir")):
if not os.path.exists(neededDirectory):
os.mkdir(neededDirectory)
self.presetManager = PresetManager(
uic.loadUi(
os.path.join(os.path.dirname(os.path.realpath(__file__)),
'presetmanager.ui')),
self)
# Make queues/timers for the preview thread
self.previewQueue = Queue()
self.previewThread = QtCore.QThread(self)
@ -140,7 +136,7 @@ class MainWindow(QtCore.QObject):
)
vBitrate = int(self.settings.value('outputVideoBitrate'))
aBitrate = int(self.settings.value('outputAudioBitrate')[:-1])
aBitrate = int(self.settings.value('outputAudioBitrate'))
window.spinBox_vBitrate.setValue(vBitrate)
window.spinBox_aBitrate.setValue(aBitrate)
@ -420,7 +416,6 @@ class MainWindow(QtCore.QObject):
self.window.stackedWidget.addWidget(self.pages[-1])
self.window.stackedWidget.setCurrentIndex(index)
self.selectedComponents[-1].update()
'''self.updateOpenPresetComboBox(self.selectedComponents[-1])'''
def insertComponent(self, moduleIndex):
self.selectedComponents.insert(
@ -432,7 +427,6 @@ class MainWindow(QtCore.QObject):
self.window.stackedWidget.insertWidget(0, self.pages[0])
self.window.stackedWidget.setCurrentIndex(0)
self.selectedComponents[0].update()
'''self.updateOpenPresetComboBox(self.selectedComponents[0])'''
def removeComponent(self):
for selected in self.window.listWidget_componentList.selectedItems():
@ -449,7 +443,6 @@ class MainWindow(QtCore.QObject):
if selected:
index = self.window.listWidget_componentList.row(selected[0])
self.window.stackedWidget.setCurrentIndex(index)
'''self.updateOpenPresetComboBox(self.selectedComponents[index])'''
def moveComponentUp(self):
row = self.window.listWidget_componentList.currentRow()
@ -489,87 +482,6 @@ class MainWindow(QtCore.QObject):
'''Preset manager for importing, exporting, renaming, deleting'''
self.presetManager.show()
def updateOpenPresetComboBox(self, component):
self.window.comboBox_openPreset.clear()
self.window.comboBox_openPreset.addItem("Component Presets")
destination = os.path.join(
self.presetDir, str(component).strip(), str(component.version()))
if not os.path.exists(destination):
os.makedirs(destination)
for f in os.listdir(destination):
self.window.comboBox_openPreset.addItem(f)
def openSavePresetDialog(self):
if self.window.listWidget_componentList.currentRow() == -1:
return
while True:
newName, OK = QtGui.QInputDialog.getText(
QtGui.QWidget(), 'Audio Visualizer', 'New Preset Name:')
badName = False
for letter in newName:
if letter in string.punctuation:
badName = True
if badName:
# some filesystems don't like bizarre characters
self.showMessage(msg="Preset names must contain only \
letters, numbers, and spaces.")
continue
if OK and newName:
index = self.window.listWidget_componentList.currentRow()
if index != -1:
saveValueStore = \
self.selectedComponents[index].savePreset()
componentName = str(self.selectedComponents[index]).strip()
vers = self.selectedComponents[index].version()
self.createPresetFile(
componentName, vers, saveValueStore, newName)
break
def createPresetFile(
self, componentName, version, saveValueStore, filename):
dirname = os.path.join(self.presetDir, componentName, str(version))
if not os.path.exists(dirname):
os.makedirs(dirname)
filepath = os.path.join(dirname, filename)
if os.path.exists(filepath):
ch = self.showMessage(
msg="%s already exists! Overwrite it?" % filename,
showCancel=True, icon=QtGui.QMessageBox.Warning)
if not ch:
return
# remove old copies of the preset
for i in range(0, self.window.comboBox_openPreset.count()):
if self.window.comboBox_openPreset.itemText(i) == filename:
self.window.comboBox_openPreset.removeItem(i)
with open(filepath, 'w') as f:
f.write(core.Core.stringOrderedDict(saveValueStore))
self.window.comboBox_openPreset.addItem(filename)
self.window.comboBox_openPreset.setCurrentIndex(
self.window.comboBox_openPreset.count()-1)
def openPreset(self):
if self.window.comboBox_openPreset.currentIndex() < 1:
return
index = self.window.listWidget_componentList.currentRow()
if index == -1:
return
filename = self.window.comboBox_openPreset.itemText(
self.window.comboBox_openPreset.currentIndex())
componentName = str(self.selectedComponents[index]).strip()
version = self.selectedComponents[index].version()
dirname = os.path.join(self.presetDir, componentName, str(version))
filepath = os.path.join(dirname, filename)
if not os.path.exists(filepath):
self.window.comboBox_openPreset.removeItem(
self.window.comboBox_openPreset.currentIndex())
return
with open(filepath, 'r') as f:
for line in f:
saveValueStore = dict(eval(line.strip()))
break
self.selectedComponents[index].loadPreset(saveValueStore)
self.drawPreview()
def createNewProject(self):
self.currentProject = None
self.selectedComponents = []
@ -678,7 +590,8 @@ class MainWindow(QtCore.QObject):
def showMessage(self, **kwargs):
msg = QtGui.QMessageBox()
msg.setText(kwargs['msg'])
msg.setIcon(kwargs['icon'] if 'icon' in kwargs else QtGui.QMessageBox.Information)
msg.setIcon(
kwargs['icon'] if 'icon' in kwargs else QtGui.QMessageBox.Information)
msg.setDetailedText(kwargs['detail'] if 'detail' in kwargs else None)
if 'showCancel'in kwargs and kwargs['showCancel']:
msg.setStandardButtons(
@ -691,13 +604,36 @@ class MainWindow(QtCore.QObject):
return False
def componentContextMenu(self, QPos):
'''Appears when right-clicking a component in the list'''
if not self.window.listWidget_componentList.selectedItems():
return
self.presetManager.findPresets()
self.menu = QtGui.QMenu()
menuItem = self.menu.addAction("Save Preset")
self.connect(menuItem, QtCore.SIGNAL("triggered()"), self.openSavePresetDialog)
self.connect(
menuItem,
QtCore.SIGNAL("triggered()"),
self.presetManager.openSavePresetDialog
)
# submenu for opening presets
index = self.window.listWidget_componentList.currentRow()
try:
presets = self.presetManager.presets[str(self.selectedComponents[index])]
self.submenu = QtGui.QMenu("Open Preset")
self.menu.addMenu(self.submenu)
for version, presetName in presets:
menuItem = self.submenu.addAction(presetName)
self.connect(
menuItem,
QtCore.SIGNAL("triggered()"),
lambda presetName=presetName:
self.presetManager.openPreset(presetName)
)
except KeyError as e:
print(e)
parentPosition = self.window.listWidget_componentList.mapToGlobal(QtCore.QPoint(0, 0))
self.menu.move(parentPosition + QPos)
self.menu.show()
def menuItemClicked(self):
currentItemName=str(self.window.listWidget_componentList.currentItem().text() )
print(currentItemName)

View File

@ -1,14 +1,19 @@
from PyQt4 import QtGui
#import sys
from collections import OrderedDict
import string
import os
import core
class PresetManager(QtGui.QDialog):
def __init__(self, window, parent):
super().__init__()
self.parent = parent
self.presetDir = parent.presetDir
self.presetDir = os.path.join(self.parent.dataDir, 'presets')
self.window = window
self.presets = self.findPresets()
self.findPresets()
self.lastFilter = '*'
# create filter box and preset list
self.drawFilterList()
@ -29,7 +34,7 @@ class PresetManager(QtGui.QDialog):
for preset in presetList:
presetNames.append(preset[1])
self.autocomplete.setStringList(presetNames)
self.presets = self.findPresets()
self.findPresets()
self.drawFilterList()
self.drawPresetList('*')
self.window.show()
@ -47,16 +52,23 @@ class PresetManager(QtGui.QDialog):
parseList.append((compName, int(compVers), preset))
except ValueError:
continue
return { compName : \
[ (vers, preset) \
for name, vers, preset in parseList \
if name == compName \
] \
for compName, _, __ in parseList \
}
self.presets =\
{
compName : \
[
(vers, preset) \
for name, vers, preset in parseList \
if name == compName \
] \
for compName, _, __ in parseList \
}
def drawPresetList(self, filter):
def drawPresetList(self, filter=None):
self.window.listWidget_presets.clear()
if filter:
self.lastFilter = str(filter)
else:
filter = str(self.lastFilter)
for component, presets in self.presets.items():
if filter != '*' and component != filter:
continue
@ -68,3 +80,65 @@ class PresetManager(QtGui.QDialog):
self.window.comboBox_filter.addItem('*')
for component in self.presets:
self.window.comboBox_filter.addItem(component)
def openSavePresetDialog(self):
window = self.parent.window
if window.listWidget_componentList.currentRow() == -1:
return
while True:
dialog = QtGui.QInputDialog(
QtGui.QWidget(), 'Audio Visualizer', 'New Preset Name:')
dialog.setTextValue()
newName, OK = dialog.getText()
badName = False
for letter in newName:
if letter in string.punctuation:
badName = True
if badName:
# some filesystems don't like bizarre characters
self.parent.showMessage(msg=\
'''Preset names must contain only letters, numbers, and spaces.''')
continue
if OK and newName:
index = window.listWidget_componentList.currentRow()
if index != -1:
saveValueStore = \
self.parent.selectedComponents[index].savePreset()
componentName = str(self.parent.selectedComponents[index]).strip()
vers = self.parent.selectedComponents[index].version()
self.createPresetFile(
componentName, vers, saveValueStore, newName)
break
def createPresetFile(self, compName, vers, saveValueStore, filename):
dirname = os.path.join(self.presetDir, compName, str(vers))
if not os.path.exists(dirname):
os.makedirs(dirname)
filepath = os.path.join(dirname, filename)
if os.path.exists(filepath):
ch = self.parent.showMessage(
msg="%s already exists! Overwrite it?" % filename,
showCancel=True, icon=QtGui.QMessageBox.Warning)
if not ch:
return
with open(filepath, 'w') as f:
f.write(core.Core.stringOrderedDict(saveValueStore))
self.drawPresetList()
def openPreset(self, presetName):
index = self.parent.window.listWidget_componentList.currentRow()
if index == -1:
return
componentName = str(self.parent.selectedComponents[index]).strip()
version = self.parent.selectedComponents[index].version()
dirname = os.path.join(self.presetDir, componentName, str(version))
filepath = os.path.join(dirname, presetName)
if not os.path.exists(filepath):
return
with open(filepath, 'r') as f:
for line in f:
saveValueStore = dict(eval(line.strip()))
break
self.parent.selectedComponents[index].loadPreset(saveValueStore)
self.parent.drawPreview()