aboutsummaryrefslogtreecommitdiff
path: root/core.py
diff options
context:
space:
mode:
Diffstat (limited to 'core.py')
-rw-r--r--core.py125
1 files changed, 47 insertions, 78 deletions
diff --git a/core.py b/core.py
index 5617c81..ef369c9 100644
--- a/core.py
+++ b/core.py
@@ -6,9 +6,7 @@ from os.path import expanduser
import subprocess as sp
import numpy
from PIL import Image
-#import tempfile
from shutil import rmtree
-#import atexit
import time
from collections import OrderedDict
import json
@@ -21,11 +19,6 @@ 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.dataDir = QDesktopServices.storageLocation(
QDesktopServices.DataLocation)
self.presetDir = os.path.join(self.dataDir, 'presets')
@@ -34,7 +27,8 @@ class Core():
self.findComponents()
self.selectedComponents = []
- self.modifiedComponents = {}
+ # copies of named presets to detect modification
+ self.savedPresets = {}
def findComponents(self):
def findComponents():
@@ -54,7 +48,6 @@ class Core():
def componentListChanged(self):
for i, component in enumerate(self.selectedComponents):
component.compPos = i
- # print('Modified Components: ', self.modifiedComponents)
def insertComponent(self, compPos, moduleIndex):
if compPos < 0:
@@ -66,76 +59,52 @@ class Core():
compPos,
component)
- newDict = {}
- for i, val in self.modifiedComponents.items():
- if i >= compPos:
- newDict[i+1] = bool(val)
- else:
- newDict[i] = bool(val)
- self.modifiedComponents.clear()
- self.modifiedComponents = newDict
-
self.componentListChanged()
return compPos
def moveComponent(self, startI, endI):
- def insert(target, comp):
- self.selectedComponents.insert(target, comp)
-
comp = self.selectedComponents.pop(startI)
- insert(endI, comp)
-
- try:
- oldModified = self.modifiedComponents.pop(startI)
- if endI in self.modifiedComponents:
- self.modifiedComponents[startI] = \
- self.modifiedComponents.pop(endI)
- self.modifiedComponents[endI] = oldModified
- except KeyError:
- pass
+ self.selectedComponents.insert(endI, comp)
self.componentListChanged()
return endI
def removeComponent(self, i):
self.selectedComponents.pop(i)
- try:
- self.modifiedComponents.pop(i)
- except KeyError:
- pass
-
- newDict = {}
- for index, val in self.modifiedComponents.items():
- if index >= i:
- newDict[index-1] = bool(val)
- else:
- newDict[index] = bool(val)
- self.modifiedComponents.clear()
- self.modifiedComponents = newDict
-
self.componentListChanged()
def updateComponent(self, i):
# print('updating %s' % self.selectedComponents[i])
self.selectedComponents[i].update()
- def componentModified(self, i):
- '''Triggered by mainwindow.updateComponentTitle()
- Tracks temporary state of whether components are modified or not
- for retrieval upon loading a project file'''
- self.modifiedComponents[i] = True
-
- def componentUnmodified(self, i):
- try:
- self.modifiedComponents.pop(i)
- except KeyError:
- pass
-
def moduleIndexFor(self, compName):
compNames = [mod.Component.__doc__ for mod in self.modules]
index = compNames.index(compName)
return self.moduleIndexes[index]
+ def openPreset(self, filepath, compIndex, presetName):
+ '''Applies a preset to a specific component'''
+ saveValueStore = self.getPreset(filepath)
+ if not saveValueStore:
+ return False
+
+ self.selectedComponents[compIndex].loadPreset(
+ saveValueStore,
+ presetName
+ )
+ self.savedPresets[presetName] = dict(saveValueStore)
+ return True
+
+ def getPreset(self, filepath):
+ '''Returns the preset dict stored at this filepath'''
+ if not os.path.exists(filepath):
+ return False
+ with open(filepath, 'r') as f:
+ for line in f:
+ saveValueStore = Core.presetFromString(line.strip())
+ break
+ return saveValueStore
+
def openProject(self, loader, filepath):
'''loader is the object calling this method (mainwindow/command)
which implements an insertComponent method'''
@@ -143,15 +112,29 @@ class Core():
if errcode == 0:
for i, tup in enumerate(data['Components']):
name, vers, preset = tup
+
+ # add loaded named presets to savedPresets dict
+ if 'preset' in preset and preset['preset'] != None:
+ nam = preset['preset']
+ filepath2 = os.path.join(
+ self.presetDir, name, str(vers), nam)
+ origSaveValueStore = self.getPreset(filepath2)
+ self.savedPresets[nam] = dict(origSaveValueStore)
+
+ # insert component into the loader
loader.insertComponent(
self.moduleIndexFor(name), -1)
- self.selectedComponents[-1].loadPreset(
- preset)
- if data['Modified'][i] == True:
- self.componentModified(i)
- loader.updateComponentTitle(i, True)
+
+ if 'preset' in preset and preset['preset'] != None:
+ self.selectedComponents[-1].loadPreset(
+ preset
+ )
else:
- loader.updateComponentTitle(i)
+ self.selectedComponents[-1].loadPreset(
+ preset,
+ preset['preset']
+ )
+
elif errcode == 1:
typ, value, _ = data
if typ.__name__ == KeyError:
@@ -174,7 +157,7 @@ class Core():
with open(filepath, 'r') as f:
def parseLine(line):
'''Decides if a given avp or avl line is a section header'''
- validSections = ('Components', 'Modified')
+ validSections = ('Components')
line = line.strip()
newSection = ''
@@ -207,8 +190,6 @@ class Core():
lastCompPreset)
)
i = 0
- if line and section == 'Modified':
- data[section].append(eval(line))
return 0, data
except:
return 1, sys.exc_info()
@@ -294,12 +275,6 @@ class Core():
f.write('%s\n' % str(comp))
f.write('%s\n' % str(comp.version()))
f.write('%s\n' % Core.presetToString(saveValueStore))
- f.write('[Modified]\n')
- for i in range(len(self.selectedComponents)):
- if i in self.modifiedComponents:
- f.write('%s\n' % repr(True))
- else:
- f.write('%s\n' % repr(False))
return True
except:
return False
@@ -385,12 +360,6 @@ class Core():
return completeAudioArray
- #def deleteTempDir(self):
- # try:
- # rmtree(self.tempDir)
- # except FileNotFoundError:
- # pass
-
def cancel(self):
self.canceled = True