From 4fc73f1e094289b50f828f0a3128d710e1d9ec4c Mon Sep 17 00:00:00 2001 From: tassaron Date: Thu, 8 Jun 2017 20:32:25 -0400 Subject: rename and delete buttons in preset manager --- components/color.py | 16 ++++---- core.py | 10 +++++ presetmanager.py | 116 ++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 118 insertions(+), 24 deletions(-) diff --git a/components/color.py b/components/color.py index a86927b..11c1b19 100644 --- a/components/color.py +++ b/components/color.py @@ -20,14 +20,14 @@ class Component(__base__.Component): page.lineEdit_color1.setText('%s,%s,%s' % self.color1) page.lineEdit_color2.setText('%s,%s,%s' % self.color2) - btnStyle = "QPushButton { background-color : %s; outline: none; }" \ + btnStyle1 = "QPushButton { background-color : %s; outline: none; }" \ % QColor(*self.color1).name() - btnStyle = "QPushButton { background-color : %s; outline: none; }" \ + btnStyle2 = "QPushButton { background-color : %s; outline: none; }" \ % QColor(*self.color2).name() - page.pushButton_color1.setStyleSheet(btnStyle) - page.pushButton_color2.setStyleSheet(btnStyle) + page.pushButton_color1.setStyleSheet(btnStyle1) + page.pushButton_color2.setStyleSheet(btnStyle2) page.pushButton_color1.clicked.connect(lambda: self.pickColor(1)) page.pushButton_color2.clicked.connect(lambda: self.pickColor(2)) @@ -74,14 +74,14 @@ class Component(__base__.Component): self.page.lineEdit_color1.setText('%s,%s,%s' % pr['color1']) self.page.lineEdit_color2.setText('%s,%s,%s' % pr['color2']) - btnStyle = "QPushButton { background-color : %s; outline: none; }" \ + btnStyle1 = "QPushButton { background-color : %s; outline: none; }" \ % QColor(*pr['color1']).name() - btnStyle = "QPushButton { background-color : %s; outline: none; }" \ + btnStyle2 = "QPushButton { background-color : %s; outline: none; }" \ % QColor(*pr['color2']).name() - self.page.pushButton_color1.setStyleSheet(btnStyle) - self.page.pushButton_color2.setStyleSheet(btnStyle) + self.page.pushButton_color1.setStyleSheet(btnStyle1) + self.page.pushButton_color2.setStyleSheet(btnStyle2) def savePreset(self): return { diff --git a/core.py b/core.py index 797749d..06367cf 100644 --- a/core.py +++ b/core.py @@ -14,6 +14,7 @@ from collections import OrderedDict import json from importlib import import_module from PyQt4.QtGui import QDesktopServices +import string class Core(): @@ -177,6 +178,15 @@ class Core(): def reset(self): self.canceled = False + @staticmethod + def badName(name): + '''Returns whether a name contains non-alphanumeric chars''' + badName = False + for letter in name: + if letter in string.punctuation: + badName = True + return badName + @staticmethod def stringOrderedDict(dictionary): sorted_ = OrderedDict(sorted(dictionary.items(), key=lambda t: t[0])) diff --git a/presetmanager.py b/presetmanager.py index 268754e..d7189b1 100644 --- a/presetmanager.py +++ b/presetmanager.py @@ -15,6 +15,11 @@ class PresetManager(QtGui.QDialog): self.window = window self.findPresets() self.lastFilter = '*' + self.presetRows = [] # list of (comp, vers, name) tuples + + # connect button signals + self.window.pushButton_delete.clicked.connect(self.openDeletePresetDialog) + self.window.pushButton_rename.clicked.connect(self.openRenamePresetDialog) # create filter box and preset list self.drawFilterList() @@ -30,6 +35,7 @@ class PresetManager(QtGui.QDialog): self.window.lineEdit_search.setCompleter(completer) def show(self): + '''Open a new preset manager window from the mainwindow''' presetNames = [] for presetList in self.presets.values(): for preset in presetList: @@ -70,11 +76,13 @@ class PresetManager(QtGui.QDialog): self.lastFilter = str(filter) else: filter = str(self.lastFilter) + self.presetRows = [] for component, presets in self.presets.items(): if filter != '*' and component != filter: continue for vers, preset in presets: self.window.listWidget_presets.addItem('%s: %s' % (component, preset)) + self.presetRows.append((component, vers, preset)) def drawFilterList(self): self.window.comboBox_filter.clear() @@ -83,9 +91,10 @@ class PresetManager(QtGui.QDialog): self.window.comboBox_filter.addItem(component) def openSavePresetDialog(self): + '''Functions on mainwindow level from the context menu''' window = self.parent.window self.selectedComponents = self.parent.core.selectedComponents - componentList = window.listWidget_componentList + componentList = self.parent.window.listWidget_componentList if componentList.currentRow() == -1: return @@ -100,15 +109,8 @@ class PresetManager(QtGui.QDialog): currentPreset ) if OK: - 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.') + if core.Core.badName(newName): + self.warnMessage() continue if newName: if index != -1: @@ -116,21 +118,30 @@ class PresetManager(QtGui.QDialog): self.selectedComponents[index].savePreset() componentName = str(self.selectedComponents[index]).strip() vers = self.selectedComponents[index].version() - self.createPresetFile( + self.createNewPreset( componentName, vers, saveValueStore, newName) self.selectedComponents[index].currentPreset = newName + self.findPresets() + self.drawPresetList() break - def createPresetFile(self, compName, vers, saveValueStore, filename): + def createNewPreset(self, compName, vers, saveValueStore, filename): path = os.path.join(self.presetDir, compName, str(vers), filename) + if self.presetExists(path): + return + self.core.createPresetFile(compName, vers, saveValueStore, filename) + + def presetExists(self, path): if os.path.exists(path): ch = self.parent.showMessage( - msg="%s already exists! Overwrite it?" % filename, + msg="%s already exists! Overwrite it?" % + os.path.basename(path), showCancel=True, icon=QtGui.QMessageBox.Warning) if not ch: - return - self.core.createPresetFile(compName, vers, saveValueStore, filename) - self.drawPresetList() + # user clicked cancel + return True + + return False def openPreset(self, presetName): componentList = self.parent.window.listWidget_componentList @@ -156,3 +167,76 @@ class PresetManager(QtGui.QDialog): self.parent.updateComponentTitle(index) self.parent.drawPreview() + def openDeletePresetDialog(self): + selected = self.window.listWidget_presets.selectedItems() + if not selected: + return + row = self.window.listWidget_presets.row(selected[0]) + comp, vers, name = self.presetRows[row] + ch = self.parent.showMessage( + msg='Really delete %s?' % name, + showCancel=True, icon=QtGui.QMessageBox.Warning + ) + if not ch: + return + self.deletePreset(comp, vers, name) + self.findPresets() + self.drawPresetList() + + def deletePreset(self, comp, vers, name): + filepath = os.path.join(self.presetDir, comp, str(vers), name) + os.remove(filepath) + + def warnMessage(self): + self.parent.showMessage( + msg='Preset names must contain only letters, ' + 'numbers, and spaces.') + + def openRenamePresetDialog(self): + presetList = self.window.listWidget_presets + if presetList.currentRow() == -1: + return + + while True: + index = presetList.currentRow() + newName, OK = QtGui.QInputDialog.getText( + self.window, + 'Preset Manager', + 'Rename Preset:', + QtGui.QLineEdit.Normal, + self.presetRows[index][2] + ) + if OK: + if core.Core.badName(newName): + self.warnMessage() + continue + if newName: + comp, vers, oldName = self.presetRows[index] + path = os.path.join( + self.presetDir, comp, str(vers)) + newPath = os.path.join(path, newName) + oldPath = os.path.join(path, oldName) + if self.presetExists(newPath): + return + if os.path.exists(newPath): + os.remove(newPath) + os.rename(oldPath, newPath) + self.findPresets() + self.drawPresetList() + break + + + + + + + + + + + + + + + + -- cgit v1.2.3