From bb1e54b31eb6157ef041764cfccd60484a0e02d8 Mon Sep 17 00:00:00 2001
From: tassaron
Date: Thu, 8 Jun 2017 16:50:48 -0400
Subject: saved preset titles, code clean-ups
componentList drag'n'drop disabled for now; will work on it in another branch
---
components/color.py | 3 +-
components/image.py | 3 +-
components/original.py | 3 +-
components/text.py | 3 +-
components/video.py | 3 +-
core.py | 58 +++++++++++++-----
mainwindow.py | 159 +++++++++++++++++++++++++++++--------------------
mainwindow.ui | 2 +-
presetmanager.py | 42 +++++++------
9 files changed, 169 insertions(+), 107 deletions(-)
diff --git a/components/color.py b/components/color.py
index b13f54e..a86927b 100644
--- a/components/color.py
+++ b/components/color.py
@@ -70,7 +70,7 @@ class Component(__base__.Component):
return Image.new("RGBA", (width, height), (r, g, b, 255))
def loadPreset(self, pr, presetName=None):
- self.currentPreset = presetName
+ self.currentPreset = presetName if presetName else pr['preset']
self.page.lineEdit_color1.setText('%s,%s,%s' % pr['color1'])
self.page.lineEdit_color2.setText('%s,%s,%s' % pr['color2'])
@@ -85,6 +85,7 @@ class Component(__base__.Component):
def savePreset(self):
return {
+ 'preset': self.currentPreset,
'color1': self.color1,
'color2': self.color2,
}
diff --git a/components/image.py b/components/image.py
index 6ccddb6..441e0e1 100644
--- a/components/image.py
+++ b/components/image.py
@@ -49,11 +49,12 @@ class Component(__base__.Component):
return frame
def loadPreset(self, pr, presetName=None):
- self.currentPreset = presetName
+ self.currentPreset = presetName if presetName else pr['preset']
self.page.lineEdit_image.setText(pr['image'])
def savePreset(self):
return {
+ 'preset': self.currentPreset,
'image': self.imagePath,
}
diff --git a/components/original.py b/components/original.py
index a2059d1..7873f43 100644
--- a/components/original.py
+++ b/components/original.py
@@ -38,7 +38,7 @@ class Component(__base__.Component):
self.parent.drawPreview()
def loadPreset(self, pr, presetName=None):
- self.currentPreset = presetName
+ self.currentPreset = presetName if presetName else pr['preset']
self.page.lineEdit_visColor.setText('%s,%s,%s' % pr['visColor'])
btnStyle = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*pr['visColor']).name()
@@ -47,6 +47,7 @@ class Component(__base__.Component):
def savePreset(self):
return {
+ 'preset': self.currentPreset,
'layout': self.layout,
'visColor': self.visColor,
}
diff --git a/components/text.py b/components/text.py
index 1725a41..68cffca 100644
--- a/components/text.py
+++ b/components/text.py
@@ -79,7 +79,7 @@ class Component(__base__.Component):
return x, self.yPosition
def loadPreset(self, pr, presetName=None):
- self.currentPreset = presetName
+ self.currentPreset = presetName if presetName else pr['preset']
self.page.lineEdit_title.setText(pr['title'])
font = QFont()
font.fromString(pr['titleFont'])
@@ -95,6 +95,7 @@ class Component(__base__.Component):
def savePreset(self):
return {
+ 'preset': self.currentPreset,
'title': self.title,
'titleFont': self.titleFont.toString(),
'alignment': self.alignment,
diff --git a/components/video.py b/components/video.py
index e636224..c529658 100644
--- a/components/video.py
+++ b/components/video.py
@@ -129,12 +129,13 @@ class Component(__base__.Component):
return self.video.frame(frameNo)
def loadPreset(self, pr, presetName=None):
- self.currentPreset = presetName
+ self.currentPreset = presetName if presetName else pr['preset']
self.page.lineEdit_video.setText(pr['video'])
self.page.checkBox_loop.setChecked(pr['loop'])
def savePreset(self):
return {
+ 'preset': self.currentPreset,
'video': self.videoPath,
'loop': self.loopVideo,
}
diff --git a/core.py b/core.py
index 0fa5ec5..797749d 100644
--- a/core.py
+++ b/core.py
@@ -6,29 +6,34 @@ from os.path import expanduser
import subprocess as sp
import numpy
from PIL import Image
-import tempfile
+#import tempfile
from shutil import rmtree
-import atexit
+#import atexit
import time
from collections import OrderedDict
import json
from importlib import import_module
+from PyQt4.QtGui import QDesktopServices
class Core():
def __init__(self):
self.FFMPEG_BIN = self.findFfmpeg()
- self.tempDir = os.path.join(
- tempfile.gettempdir(), 'audio-visualizer-python-data')
- if not os.path.exists(self.tempDir):
- os.makedirs(self.tempDir)
- atexit.register(self.deleteTempDir)
+ #self.tempDir = os.path.join(
+ # tempfile.gettempdir(), 'audio-visualizer-python-data')
+ #if not os.path.exists(self.tempDir):
+ # os.makedirs(self.tempDir)
+ #atexit.register(self.deleteTempDir)
+ self.dataDir = QDesktopServices.storageLocation(
+ QDesktopServices.DataLocation)
+ self.presetDir = os.path.join(self.dataDir, 'presets')
self.wd = os.path.dirname(os.path.realpath(__file__))
self.loadEncoderOptions()
self.modules = self.findComponents()
self.selectedComponents = []
+ self.selectedModules = []
def findComponents(self):
def findComponents():
@@ -45,19 +50,40 @@ class Core():
for name in findComponents()]
def insertComponent(self, compPos, moduleIndex):
+ if compPos < 0:
+ compPos = len(self.selectedComponents) -1
self.selectedComponents.insert(
compPos,
- self.modules[moduleIndex].Component())
- return compPos #if compPos > -1 else len(self.selectedComponents)-1
+ self.modules[moduleIndex].Component()
+ )
+ self.selectedModules.insert(
+ compPos,
+ moduleIndex
+ )
+ return compPos
def moveComponent(self, startI, endI):
comp = self.selectedComponents.pop(startI)
- i = self.selectedComponents.insert(endI, comp)
- return i
+ self.selectedComponents.insert(endI, comp)
+ i = self.selectedModules.pop(startI)
+ self.selectedModules.insert(endI, i)
+ return endI
def updateComponent(self, i):
+ print('updating %s' % self.selectedComponents[i])
self.selectedComponents[i].update()
+ def moduleIndexFor(self, compIndex):
+ return self.selectedModules[compIndex]
+
+ 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)
+ with open(filepath, 'w') as f:
+ f.write(Core.stringOrderedDict(saveValueStore))
+
def loadEncoderOptions(self):
file_path = os.path.join(self.wd, 'encoder-options.json')
with open(file_path) as json_file:
@@ -139,11 +165,11 @@ class Core():
return completeAudioArray
- def deleteTempDir(self):
- try:
- rmtree(self.tempDir)
- except FileNotFoundError:
- pass
+ #def deleteTempDir(self):
+ # try:
+ # rmtree(self.tempDir)
+ # except FileNotFoundError:
+ # pass
def cancel(self):
self.canceled = True
diff --git a/mainwindow.py b/mainwindow.py
index f24fc66..8812e7f 100644
--- a/mainwindow.py
+++ b/mainwindow.py
@@ -3,7 +3,7 @@ from queue import Queue
from collections import OrderedDict
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import QSettings, Qt
-from PyQt4.QtGui import QDesktopServices, QMenu
+from PyQt4.QtGui import QMenu
import sys
import os
import signal
@@ -55,12 +55,11 @@ class MainWindow(QtCore.QObject):
self.core = core.Core()
self.pages = [] # widgets of component settings
- self.componentRows = {} # QListWidgetItems
+ self.componentRows = [] # (moduleIndex, QListWidgetItem) tuples
self.lastAutosave = time.time()
# Create data directory, load/create settings
- self.dataDir = QDesktopServices.storageLocation(
- QDesktopServices.DataLocation)
+ self.dataDir = self.core.dataDir
self.presetManager = PresetManager(
uic.loadUi(
os.path.join(os.path.dirname(os.path.realpath(__file__)),
@@ -73,7 +72,7 @@ class MainWindow(QtCore.QObject):
if not os.path.exists(self.dataDir):
os.makedirs(self.dataDir)
for neededDirectory in (
- self.presetManager.presetDir, self.settings.value("projectDir")):
+ self.core.presetDir, self.settings.value("projectDir")):
if not os.path.exists(neededDirectory):
os.mkdir(neededDirectory)
@@ -90,6 +89,8 @@ class MainWindow(QtCore.QObject):
self.timer.start(500)
# Begin decorating the window and connecting events
+ componentList = self.window.listWidget_componentList
+
window.toolButton_selectAudioFile.clicked.connect(
self.openInputFileDialog)
@@ -120,7 +121,7 @@ class MainWindow(QtCore.QObject):
codec = window.comboBox_videoCodec.itemText(i)
if codec == self.settings.value('outputVideoCodec'):
window.comboBox_videoCodec.setCurrentIndex(i)
- print(codec)
+ #print(codec)
for i in range(window.comboBox_audioCodec.count()):
codec = window.comboBox_audioCodec.itemText(i)
@@ -157,17 +158,17 @@ class MainWindow(QtCore.QObject):
self.window.pushButton_addComponent.setMenu(self.compMenu)
- self.window.listWidget_componentList.dropEvent = self.componentMoved
- self.window.listWidget_componentList.clicked.connect(
+ componentList.dropEvent = self.componentListChanged
+ componentList.clicked.connect(
lambda _: self.changeComponentWidget())
self.window.pushButton_removeComponent.clicked.connect(
lambda _: self.removeComponent())
- self.window.listWidget_componentList.setContextMenuPolicy(
+ componentList.setContextMenuPolicy(
QtCore.Qt.CustomContextMenu)
- self.window.listWidget_componentList.connect(
- self.window.listWidget_componentList,
+ componentList.connect(
+ componentList,
QtCore.SIGNAL("customContextMenuRequested(QPoint)"),
self.componentContextMenu)
@@ -182,9 +183,11 @@ class MainWindow(QtCore.QObject):
self.updateResolution)
self.window.pushButton_listMoveUp.clicked.connect(
- self.moveComponentUp)
- #self.window.pushButton_listMoveDown.clicked.connect(
- # self.moveComponentDown)
+ lambda: self.moveComponent(-1)
+ )
+ self.window.pushButton_listMoveDown.clicked.connect(
+ lambda: self.moveComponent(1)
+ )
# Configure the Projects Menu
self.projectMenu = QMenu()
@@ -210,8 +213,7 @@ class MainWindow(QtCore.QObject):
# Show the window and load current project
window.show()
self.currentProject = self.settings.value("currentProject")
- if self.currentProject and os.path.exists(self.autosavePath) \
- and filecmp.cmp(self.autosavePath, self.currentProject):
+ if self.autosaveExists():
# delete autosave if it's identical to the project
os.remove(self.autosavePath)
@@ -235,6 +237,14 @@ class MainWindow(QtCore.QObject):
self.previewThread.wait()
self.autosave()
+ def updateComponentTitle(self, pos):
+ if pos < 0:
+ pos = len(self.core.selectedComponents)-1
+ title = str(self.core.selectedComponents[pos])
+ if self.core.selectedComponents[pos].currentPreset:
+ title += ' - %s' % self.core.selectedComponents[pos].currentPreset
+ self.window.listWidget_componentList.item(pos).setText(title)
+
def updateCodecs(self):
containerWidget = self.window.comboBox_videoContainer
vCodecWidget = self.window.comboBox_videoCodec
@@ -270,12 +280,20 @@ class MainWindow(QtCore.QObject):
self.settings.setValue('outputAudioBitrate', currentAudioBitrate)
def autosave(self):
- if time.time() - self.lastAutosave >= 2.0:
+ if not self.currentProject:
if os.path.exists(self.autosavePath):
os.remove(self.autosavePath)
+ elif time.time() - self.lastAutosave >= 2.0:
self.createProjectFile(self.autosavePath)
self.lastAutosave = time.time()
+ def autosaveExists(self):
+ if self.currentProject and os.path.exists(self.autosavePath) \
+ and filecmp.cmp(self.autosavePath, self.currentProject):
+ return True
+ else:
+ return False
+
def openInputFileDialog(self):
inputDir = self.settings.value("inputDir", expanduser("~"))
@@ -393,88 +411,94 @@ class MainWindow(QtCore.QObject):
def insertComponent(self, moduleIndex, compPos=0):
componentList = self.window.listWidget_componentList
+ stackedWidget = self.window.stackedWidget
+ if compPos < 0:
+ compPos = componentList.count()
index = self.core.insertComponent(
compPos, moduleIndex)
row = componentList.insertItem(
index,
self.core.selectedComponents[index].__doc__)
- self.componentRows[index] = componentList.row(row)
+ self.componentRows.insert(compPos, (moduleIndex, row))
componentList.setCurrentRow(index)
self.pages.insert(index, self.core.selectedComponents[index].widget(self))
- self.window.stackedWidget.insertWidget(index, self.pages[index])
- self.window.stackedWidget.setCurrentIndex(index)
+ stackedWidget.insertWidget(index, self.pages[index])
+ stackedWidget.setCurrentIndex(index)
+
self.core.updateComponent(index)
def removeComponent(self):
- for selected in self.window.listWidget_componentList.selectedItems():
- index = self.window.listWidget_componentList.row(selected)
+ componentList = self.window.listWidget_componentList
+
+ for selected in componentList.selectedItems():
+ index = componentList.row(selected)
self.window.stackedWidget.removeWidget(self.pages[index])
- self.window.listWidget_componentList.takeItem(index)
+ componentList.takeItem(index)
+ self.componentRows.pop(index)
self.core.selectedComponents.pop(index)
self.pages.pop(index)
self.changeComponentWidget()
self.drawPreview()
- def changeComponentWidget(self):
- selected = self.window.listWidget_componentList.selectedItems()
- if selected:
- index = self.window.listWidget_componentList.row(selected[0])
- self.window.stackedWidget.setCurrentIndex(index)
+ def moveComponent(self, change):
+ '''Moves a component relatively from its current position'''
+ componentList = self.window.listWidget_componentList
+ stackedWidget = self.window.stackedWidget
- def moveComponentUp(self):
- row = self.window.listWidget_componentList.currentRow()
- if row > 0:
- self.core.moveComponent(row, row - 1)
- page = self.pages.pop(row)
- self.pages.insert(row - 1, page)
+ row = componentList.currentRow()
+ newRow = row + change
+ if newRow > -1 and newRow < componentList.count():
+ self.core.moveComponent(row, newRow)
# update widgets
- componentList = self.window.listWidget_componentList
- stackedWidget = self.window.stackedWidget
+ page = self.pages.pop(row)
+ self.pages.insert(newRow, page)
item = componentList.takeItem(row)
- componentList.insertItem(row - 1, item)
+ newItem = componentList.insertItem(newRow, item)
widget = stackedWidget.removeWidget(page)
- stackedWidget.insertWidget(row - 1, page)
- componentList.setCurrentRow(row - 1)
- stackedWidget.setCurrentIndex(row - 1)
- self.drawPreview()
- '''
- def moveComponentDown(self):
- row = self.window.listWidget_componentList.currentRow()
- if row != -1 and row < len(self.pages)+1:
- module = self.selectedComponents[row]
- self.selectedComponents.pop(row)
- self.selectedComponents.insert(row + 1, module)
- page = self.pages[row]
- self.pages.pop(row)
- self.pages.insert(row + 1, page)
- item = self.window.listWidget_componentList.takeItem(row)
- self.window.listWidget_componentList.insertItem(row + 1, item)
- widget = self.window.stackedWidget.removeWidget(page)
- self.window.stackedWidget.insertWidget(row + 1, page)
- self.window.listWidget_componentList.setCurrentRow(row + 1)
- self.window.stackedWidget.setCurrentIndex(row + 1)
+ stackedWidget.insertWidget(newRow, page)
+ componentList.setCurrentRow(newRow)
+ stackedWidget.setCurrentIndex(newRow)
+ self.componentRows.pop(row)
+ self.componentRows.insert(newRow, (self.core.moduleIndexFor(row), newItem))
self.drawPreview()
- '''
- def componentMoved(self, event):
- widget = self.window.listWidget_componentList
- for i in range(widget.count()):
- pass
- #print(widget.item(i) == self.componentRows[i])
+
+ def componentListChanged(self, *args):
+ '''Update all our tracking variables to match the widget'''
+ pass
+
+ def changeComponentWidget(self):
+ selected = self.window.listWidget_componentList.selectedItems()
+ if selected:
+ index = self.window.listWidget_componentList.row(selected[0])
+ self.window.stackedWidget.setCurrentIndex(index)
def openPresetManager(self):
'''Preset manager for importing, exporting, renaming, deleting'''
self.presetManager.show()
- def createNewProject(self):
- self.currentProject = None
+ def clear(self):
+ '''Get a blank slate'''
self.core.selectedComponents = []
self.window.listWidget_componentList.clear()
for widget in self.pages:
self.window.stackedWidget.removeWidget(widget)
self.pages = []
+
+ def createNewProject(self):
+ if self.autosaveExists():
+ ch = self.showMessage(
+ msg="You have unsaved changes in project '%s'. "
+ "Save before starting a new project?"
+ % os.path.basename(self.currentProject)[:-4],
+ showCancel=True)
+ if ch:
+ self.saveCurrentProject()
+
+ self.clear()
+ self.currentProject = None
self.settings.setValue("currentProject", None)
self.drawPreview()
@@ -496,6 +520,8 @@ class MainWindow(QtCore.QObject):
def createProjectFile(self, filepath):
if not filepath.endswith(".avp"):
filepath += '.avp'
+ if os.path.exists(filepath):
+ os.remove(filepath)
with open(filepath, 'w') as f:
print('creating %s' % filepath)
f.write('[Components]\n')
@@ -520,7 +546,7 @@ class MainWindow(QtCore.QObject):
if not filepath or not os.path.exists(filepath) \
or not filepath.endswith('.avp'):
return
- self.createNewProject()
+ self.clear()
self.currentProject = filepath
self.settings.setValue("currentProject", filepath)
self.settings.setValue("projectDir", os.path.dirname(filepath))
@@ -558,6 +584,7 @@ class MainWindow(QtCore.QObject):
saveValueStore = dict(eval(line))
self.core.selectedComponents[-1].loadPreset(
saveValueStore)
+ self.updateComponentTitle(-1)
i = 0
except (IndexError, ValueError, NameError, SyntaxError,
AttributeError, TypeError) as e:
diff --git a/mainwindow.ui b/mainwindow.ui
index e809ee8..af47cee 100644
--- a/mainwindow.ui
+++ b/mainwindow.ui
@@ -188,7 +188,7 @@
true
- true
+ false
false
diff --git a/presetmanager.py b/presetmanager.py
index 50efd8d..268754e 100644
--- a/presetmanager.py
+++ b/presetmanager.py
@@ -10,7 +10,8 @@ class PresetManager(QtGui.QDialog):
def __init__(self, window, parent):
super().__init__()
self.parent = parent
- self.presetDir = os.path.join(self.parent.dataDir, 'presets')
+ self.core = self.parent.core
+ self.presetDir = self.core.presetDir
self.window = window
self.findPresets()
self.lastFilter = '*'
@@ -83,11 +84,14 @@ class PresetManager(QtGui.QDialog):
def openSavePresetDialog(self):
window = self.parent.window
- if window.listWidget_componentList.currentRow() == -1:
+ self.selectedComponents = self.parent.core.selectedComponents
+ componentList = window.listWidget_componentList
+
+ if componentList.currentRow() == -1:
return
while True:
- index = window.listWidget_componentList.currentRow()
- currentPreset = self.parent.selectedComponents[index].currentPreset
+ index = componentList.currentRow()
+ currentPreset = self.selectedComponents[index].currentPreset
newName, OK = QtGui.QInputDialog.getText(
self.parent.window,
'Audio Visualizer',
@@ -109,35 +113,34 @@ class PresetManager(QtGui.QDialog):
if newName:
if index != -1:
saveValueStore = \
- self.parent.selectedComponents[index].savePreset()
- componentName = str(self.parent.selectedComponents[index]).strip()
- vers = self.parent.selectedComponents[index].version()
+ self.selectedComponents[index].savePreset()
+ componentName = str(self.selectedComponents[index]).strip()
+ vers = self.selectedComponents[index].version()
self.createPresetFile(
componentName, vers, saveValueStore, newName)
- self.parent.selectedComponents[index].currentPreset = newName
+ self.selectedComponents[index].currentPreset = 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):
+ path = os.path.join(self.presetDir, compName, str(vers), filename)
+ if os.path.exists(path):
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.core.createPresetFile(compName, vers, saveValueStore, filename)
self.drawPresetList()
def openPreset(self, presetName):
- index = self.parent.window.listWidget_componentList.currentRow()
+ componentList = self.parent.window.listWidget_componentList
+ selectedComponents = self.parent.core.selectedComponents
+
+ index = componentList.currentRow()
if index == -1:
return
- componentName = str(self.parent.selectedComponents[index]).strip()
- version = self.parent.selectedComponents[index].version()
+ componentName = str(selectedComponents[index]).strip()
+ version = selectedComponents[index].version()
dirname = os.path.join(self.presetDir, componentName, str(version))
filepath = os.path.join(dirname, presetName)
if not os.path.exists(filepath):
@@ -146,9 +149,10 @@ class PresetManager(QtGui.QDialog):
for line in f:
saveValueStore = dict(eval(line.strip()))
break
- self.parent.selectedComponents[index].loadPreset(
+ selectedComponents[index].loadPreset(
saveValueStore,
presetName
)
+ self.parent.updateComponentTitle(index)
self.parent.drawPreview()
--
cgit v1.2.3