made basic export function, moved more code into core
This commit is contained in:
parent
b048312882
commit
59c2c090ab
70
core.py
70
core.py
|
@ -77,19 +77,69 @@ class Core():
|
|||
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 importPreset(self, filepath):
|
||||
print(filepath)
|
||||
|
||||
def exportPreset(self, exportName, compName, vers, origName):
|
||||
pass
|
||||
def exportPreset(self, exportPath, compName, vers, origName):
|
||||
internalPath = os.path.join(self.presetDir, compName, str(vers), origName)
|
||||
if not os.path.exists(internalPath):
|
||||
return
|
||||
if os.path.exists(exportPath):
|
||||
os.remove(exportPath)
|
||||
with open(internalPath, 'r') as f:
|
||||
internalData = [line for line in f]
|
||||
try:
|
||||
saveValueStore = dict(eval(internalData[0].strip()))
|
||||
self.createPresetFile(
|
||||
compName, vers,
|
||||
origName, saveValueStore,
|
||||
exportPath
|
||||
)
|
||||
except:
|
||||
# TODO: add proper warning message
|
||||
print('couldn\'t export %s' % exportPath)
|
||||
|
||||
def createPresetFile(
|
||||
self, compName, vers, presetName, saveValueStore, filepath=''):
|
||||
'''Create a preset file (.avl) at filepath using args.
|
||||
Or if filepath is empty, create an internal preset using
|
||||
the args for the filepath.'''
|
||||
if not filepath:
|
||||
dirname = os.path.join(self.presetDir, compName, str(vers))
|
||||
if not os.path.exists(dirname):
|
||||
os.makedirs(dirname)
|
||||
filepath = os.path.join(dirname, presetName)
|
||||
internal = True
|
||||
else:
|
||||
if not filepath.endswith('.avl'):
|
||||
filepath += '.avl'
|
||||
internal = False
|
||||
|
||||
with open(filepath, 'w') as f:
|
||||
if not internal:
|
||||
f.write('[Components]\n')
|
||||
f.write('%s\n' % compName)
|
||||
f.write('%s\n' % str(vers))
|
||||
f.write(Core.stringOrderedDict(saveValueStore))
|
||||
|
||||
def createProjectFile(self, filepath):
|
||||
'''Create a project file (.avp) using the current program state'''
|
||||
try:
|
||||
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')
|
||||
for comp in self.selectedComponents:
|
||||
saveValueStore = comp.savePreset()
|
||||
f.write('%s\n' % str(comp))
|
||||
f.write('%s\n' % str(comp.version()))
|
||||
f.write('%s\n' % Core.stringOrderedDict(saveValueStore))
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
def loadEncoderOptions(self):
|
||||
file_path = os.path.join(self.wd, 'encoder-options.json')
|
||||
|
|
|
@ -284,7 +284,7 @@ class MainWindow(QtCore.QObject):
|
|||
if os.path.exists(self.autosavePath):
|
||||
os.remove(self.autosavePath)
|
||||
elif time.time() - self.lastAutosave >= 2.0:
|
||||
self.createProjectFile(self.autosavePath)
|
||||
self.core.createProjectFile(self.autosavePath)
|
||||
self.lastAutosave = time.time()
|
||||
|
||||
def autosaveExists(self):
|
||||
|
@ -504,7 +504,7 @@ class MainWindow(QtCore.QObject):
|
|||
|
||||
def saveCurrentProject(self):
|
||||
if self.currentProject:
|
||||
self.createProjectFile(self.currentProject)
|
||||
self.core.createProjectFile(self.currentProject)
|
||||
else:
|
||||
self.openSaveProjectDialog()
|
||||
|
||||
|
@ -515,25 +515,11 @@ class MainWindow(QtCore.QObject):
|
|||
"Project Files (*.avp)")
|
||||
if not filename:
|
||||
return
|
||||
self.createProjectFile(filename)
|
||||
self.settings.setValue("projectDir", os.path.dirname(filename))
|
||||
self.settings.setValue("currentProject", filename)
|
||||
self.currentProject = filename
|
||||
|
||||
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')
|
||||
for comp in self.core.selectedComponents:
|
||||
saveValueStore = comp.savePreset()
|
||||
f.write('%s\n' % str(comp))
|
||||
f.write('%s\n' % str(comp.version()))
|
||||
f.write('%s\n' % core.Core.stringOrderedDict(saveValueStore))
|
||||
if filepath != self.autosavePath:
|
||||
self.settings.setValue("projectDir", os.path.dirname(filepath))
|
||||
self.settings.setValue("currentProject", filepath)
|
||||
self.currentProject = filepath
|
||||
self.core.createProjectFile(filename)
|
||||
|
||||
def openOpenProjectDialog(self):
|
||||
filename = QtGui.QFileDialog.getOpenFileName(
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from PyQt4 import QtGui
|
||||
from PyQt4 import QtGui, QtCore
|
||||
from collections import OrderedDict
|
||||
import string
|
||||
import os
|
||||
|
@ -13,10 +13,12 @@ class PresetManager(QtGui.QDialog):
|
|||
self.core = parent.core
|
||||
self.settings = parent.settings
|
||||
self.presetDir = self.core.presetDir
|
||||
self.window = window
|
||||
self.findPresets()
|
||||
|
||||
self.lastFilter = '*'
|
||||
self.presetRows = [] # list of (comp, vers, name) tuples
|
||||
self.window = window
|
||||
self.window.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
|
||||
|
||||
# connect button signals
|
||||
self.window.pushButton_delete.clicked.connect(self.openDeletePresetDialog)
|
||||
|
@ -130,17 +132,17 @@ class PresetManager(QtGui.QDialog):
|
|||
componentName = str(self.selectedComponents[index]).strip()
|
||||
vers = self.selectedComponents[index].version()
|
||||
self.createNewPreset(
|
||||
componentName, vers, saveValueStore, newName)
|
||||
componentName, vers, newName, saveValueStore)
|
||||
self.selectedComponents[index].currentPreset = newName
|
||||
self.findPresets()
|
||||
self.drawPresetList()
|
||||
break
|
||||
|
||||
def createNewPreset(self, compName, vers, saveValueStore, filename):
|
||||
def createNewPreset(self, compName, vers, filename, saveValueStore):
|
||||
path = os.path.join(self.presetDir, compName, str(vers), filename)
|
||||
if self.presetExists(path):
|
||||
return
|
||||
self.core.createPresetFile(compName, vers, saveValueStore, filename)
|
||||
self.core.createPresetFile(compName, vers, filename, saveValueStore)
|
||||
|
||||
def presetExists(self, path):
|
||||
if os.path.exists(path):
|
||||
|
|
|
@ -3,7 +3,10 @@
|
|||
<class>presetmanager</class>
|
||||
<widget class="QWidget" name="presetmanager">
|
||||
<property name="windowModality">
|
||||
<enum>Qt::ApplicationModal</enum>
|
||||
<enum>Qt::NonModal</enum>
|
||||
</property>
|
||||
<property name="enabled">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
|
@ -92,6 +95,9 @@
|
|||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_rename">
|
||||
<property name="enabled">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Rename</string>
|
||||
</property>
|
||||
|
|
Reference in New Issue