Allow renaming of Macros.

This commit is contained in:
AccentuSoft
2022-12-19 15:43:56 +02:00
parent fc15db46a3
commit 9abeb5e0ec
2 changed files with 33 additions and 6 deletions

View File

@@ -152,6 +152,21 @@ class ResolutionManager:
return macroUID
def renameMacro(self, oldName: str, newName: str) -> bool:
if oldName != newName:
with self.mainWindow.macrosLock:
if newName in self.macros:
self.mainWindow.MESSAGEHANDLER.warning('The specified name already exists. '
'Macro names must be unique.',
popUp=True)
return False
if oldName not in self.macros:
self.mainWindow.MESSAGEHANDLER.error('Attempting to rename a nonexistent macro.', popUp=True)
return False
oldMacro = self.macros.pop(oldName)
self.macros[newName] = oldMacro
return True
def deleteMacro(self, macroUID: str) -> bool:
# We don't have to worry about running macros, because the details of the macro are saved in memory.
# We do however want to get the thread lock because of potential race conditions.

View File

@@ -22,7 +22,7 @@ from os.path import abspath, dirname
from msgpack import load
from pathlib import Path
from datetime import datetime
from typing import Union
from typing import Union, Any
from PySide6 import QtWidgets, QtGui, QtCore, QtCharts
from Core import MessageHandler, SettingsObject
@@ -4662,12 +4662,13 @@ class MacroDialog(QtWidgets.QDialog):
def updateMacroTree(self) -> None:
self.macroTree.clear()
allMacros = self.mainWindowObject.RESOLUTIONMANAGER.macros
with self.mainWindowObject.macrosLock:
allMacros = self.mainWindowObject.RESOLUTIONMANAGER.macros
for macro in allMacros:
newMacro = MacroTreeItem(macro, allMacros[macro])
self.macroTree.addTopLevelItem(newMacro)
self.macroTree.setItemWidget(newMacro, 1, newMacro.deleteButton)
for macro in allMacros:
newMacro = MacroTreeItem(macro, allMacros[macro])
self.macroTree.addTopLevelItem(newMacro)
self.macroTree.setItemWidget(newMacro, 1, newMacro.deleteButton)
def createMacro(self) -> None:
createMacroDialog = MacroCreatorDialog(self.resolutionList)
@@ -4728,6 +4729,8 @@ class MacroTreeItem(QtWidgets.QTreeWidgetItem):
def __init__(self, uid: str, resolutionList: list):
super(MacroTreeItem, self).__init__()
self.setText(0, uid)
self.uid = uid
self.setFlags(QtCore.Qt.ItemFlag.ItemIsEditable | self.flags())
self.deleteButton = QtWidgets.QPushButton('X')
self.deleteButton.clicked.connect(self.removeSelf)
@@ -4737,6 +4740,15 @@ class MacroTreeItem(QtWidgets.QTreeWidgetItem):
resolutionItem.setText(0, 'Resolution: ' + resolution[0])
self.addChild(resolutionItem)
def setData(self, column: int, role: int, value: Any):
if self.treeWidget() is not None:
if self.treeWidget().mainWindowObject.RESOLUTIONMANAGER.renameMacro(self.uid, value):
super().setData(column, role, value)
self.uid = value
else:
# Happens during initialization
super().setData(column, role, value)
def removeSelf(self):
self.treeWidget().deleteMacro(self)