undoable preset open, rename, and delete'

This commit is contained in:
tassaron 2017-08-17 20:12:46 -04:00
parent 43ea3bfd73
commit 87e762a8aa
3 changed files with 107 additions and 25 deletions

View File

@ -83,7 +83,7 @@ class Core:
) )
# init component's widget for loading/saving presets # init component's widget for loading/saving presets
component.widget(loader) component.widget(loader)
# use autoUpdate() method before update() this 1 time to set attrs # use autoUpdate() method before update() this 1 time to set attrs
component._autoUpdate() component._autoUpdate()
else: else:
moduleIndex = -1 moduleIndex = -1
@ -169,7 +169,7 @@ class Core:
def getPresetDir(self, comp): def getPresetDir(self, comp):
'''Get the preset subdir for a particular version of a component''' '''Get the preset subdir for a particular version of a component'''
return os.path.join(Core.presetDir, str(comp), str(comp.version)) return os.path.join(Core.presetDir, comp.name, str(comp.version))
def openProject(self, loader, filepath): def openProject(self, loader, filepath):
''' loader is the object calling this method which must have ''' loader is the object calling this method which must have

View File

@ -2,7 +2,14 @@
QCommand classes for every undoable user action performed in the MainWindow QCommand classes for every undoable user action performed in the MainWindow
''' '''
from PyQt5.QtWidgets import QUndoCommand from PyQt5.QtWidgets import QUndoCommand
import os
from core import Core
# =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~=~=~=~=~=~=~=~=~=~=~=~=~
# COMPONENT ACTIONS
# =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~=~=~=~=~=~=~=~=~=~=~=~=~
class AddComponent(QUndoCommand): class AddComponent(QUndoCommand):
def __init__(self, parent, compI, moduleI): def __init__(self, parent, compI, moduleI):
@ -85,6 +92,10 @@ class MoveComponent(QUndoCommand):
self.do(self.newRow, self.row) self.do(self.newRow, self.row)
# =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~=~=~=~=~=~=~=~=~=~=~=~=~
# PRESET ACTIONS
# =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~=~=~=~=~=~=~=~=~=~=~=~=~
class ClearPreset(QUndoCommand): class ClearPreset(QUndoCommand):
def __init__(self, parent, compI): def __init__(self, parent, compI):
super().__init__("Clear preset") super().__init__("Clear preset")
@ -101,3 +112,71 @@ class ClearPreset(QUndoCommand):
def undo(self): def undo(self):
self.parent.core.selectedComponents[self.compI].loadPreset(self.store) self.parent.core.selectedComponents[self.compI].loadPreset(self.store)
self.parent.updateComponentTitle(self.compI, self.store) self.parent.updateComponentTitle(self.compI, self.store)
class OpenPreset(QUndoCommand):
def __init__(self, parent, presetName, compI):
super().__init__("Open %s preset" % presetName)
self.parent = parent
self.presetName = presetName
self.compI = compI
comp = self.parent.core.selectedComponents[compI]
self.store = comp.savePreset()
self.store['preset'] = str(comp.currentPreset)
def redo(self):
self.parent._openPreset(self.presetName, self.compI)
def undo(self):
self.parent.core.selectedComponents[self.compI].loadPreset(
self.store)
self.parent.parent.updateComponentTitle(self.compI, self.store)
class RenamePreset(QUndoCommand):
def __init__(self, parent, path, oldName, newName):
super().__init__('Rename preset')
self.parent = parent
self.path = path
self.oldName = oldName
self.newName = newName
def redo(self):
self.parent.renamePreset(self.path, self.oldName, self.newName)
def undo(self):
self.parent.renamePreset(self.path, self.newName, self.oldName)
class DeletePreset(QUndoCommand):
def __init__(self, parent, compName, vers, presetFile):
self.parent = parent
self.preset = (compName, vers, presetFile)
self.path = os.path.join(
Core.presetDir, compName, str(vers), presetFile
)
self.store = self.parent.core.getPreset(self.path)
self.presetName = self.store['preset']
super().__init__('Delete %s preset (%s)' % (self.presetName, compName))
self.loadedPresets = [
i for i, comp in enumerate(self.parent.core.selectedComponents)
if self.presetName == str(comp.currentPreset)
]
def redo(self):
os.remove(self.path)
for i in self.loadedPresets:
self.parent.core.clearPreset(i)
self.parent.parent.updateComponentTitle(i, False)
self.parent.findPresets()
self.parent.drawPresetList()
def undo(self):
self.parent.createNewPreset(*self.preset, self.store)
selectedComponents = self.parent.core.selectedComponents
for i in self.loadedPresets:
selectedComponents[i].currentPreset = self.presetName
self.parent.parent.updateComponentTitle(i)
self.parent.findPresets()
self.parent.drawPresetList()

View File

@ -197,11 +197,15 @@ class PresetManager(QtWidgets.QDialog):
def openPreset(self, presetName, compPos=None): def openPreset(self, presetName, compPos=None):
componentList = self.parent.window.listWidget_componentList componentList = self.parent.window.listWidget_componentList
selectedComponents = self.core.selectedComponents
index = compPos if compPos is not None else componentList.currentRow() index = compPos if compPos is not None else componentList.currentRow()
if index == -1: if index == -1:
return return
action = OpenPreset(self, presetName, index)
self.parent.undoStack.push(action)
def _openPreset(self, presetName, index):
selectedComponents = self.core.selectedComponents
componentName = str(selectedComponents[index]).strip() componentName = str(selectedComponents[index]).strip()
version = selectedComponents[index].version version = selectedComponents[index].version
dirname = os.path.join(self.presetDir, componentName, str(version)) dirname = os.path.join(self.presetDir, componentName, str(version))
@ -225,16 +229,10 @@ class PresetManager(QtWidgets.QDialog):
if not ch: if not ch:
return return
self.deletePreset(comp, vers, name) self.deletePreset(comp, vers, name)
self.findPresets()
self.drawPresetList()
for i, comp in enumerate(self.core.selectedComponents):
if comp.currentPreset == name:
self.clearPreset(i)
def deletePreset(self, comp, vers, name): def deletePreset(self, comp, vers, name):
filepath = os.path.join(self.presetDir, comp, str(vers), name) action = DeletePreset(self, comp, vers, name)
os.remove(filepath) self.parent.undoStack.push(action)
def warnMessage(self, window=None): def warnMessage(self, window=None):
self.parent.showMessage( self.parent.showMessage(
@ -271,7 +269,6 @@ class PresetManager(QtWidgets.QDialog):
return index return index
def openRenamePresetDialog(self): def openRenamePresetDialog(self):
# TODO: maintain consistency by changing this to call createNewPreset()
presetList = self.window.listWidget_presets presetList = self.window.listWidget_presets
index = self.getPresetRow() index = self.getPresetRow()
if index == -1: if index == -1:
@ -294,22 +291,28 @@ class PresetManager(QtWidgets.QDialog):
path = os.path.join( path = os.path.join(
self.presetDir, comp, str(vers)) self.presetDir, comp, str(vers))
newPath = os.path.join(path, newName) newPath = os.path.join(path, newName)
oldPath = os.path.join(path, oldName)
if self.presetExists(newPath): if self.presetExists(newPath):
return return
if os.path.exists(newPath): action = RenamePreset(self, path, oldName, newName)
os.remove(newPath) self.parent.undoStack.push(action)
os.rename(oldPath, newPath)
self.findPresets()
self.drawPresetList()
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()
break 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): def openImportDialog(self):
filename, _ = QtWidgets.QFileDialog.getOpenFileName( filename, _ = QtWidgets.QFileDialog.getOpenFileName(
self.window, "Import Preset File", self.window, "Import Preset File",