Clean up project settings class

This commit is contained in:
Veronica Berglyd Olsen
2023-11-09 18:26:33 +01:00
parent 27f3b8e0f2
commit 2cde1b845e
3 changed files with 95 additions and 123 deletions
+2 -2
View File
@@ -152,14 +152,14 @@ class NWProjectData:
"""Return the initial count of words for novel and note """Return the initial count of words for novel and note
documents. documents.
""" """
return tuple(self._initCounts) return self._initCounts[0], self._initCounts[1]
@property @property
def currCounts(self) -> tuple[int, int]: def currCounts(self) -> tuple[int, int]:
"""Return the current count of words for novel and note """Return the current count of words for novel and note
documents. documents.
""" """
return tuple(self._currCounts) return self._currCounts[0], self._currCounts[1]
@property @property
def lastHandle(self) -> dict[str, str | None]: def lastHandle(self) -> dict[str, str | None]:
+76 -104
View File
@@ -96,17 +96,17 @@ class GuiProjectSettings(NPagedDialog):
return return
def __del__(self): # pragma: no cover def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: GuiProjectSettings") logger.debug("Delete: GuiProjectSettings")
return return
## ##
# Slots # Private Slots
## ##
def _doSave(self): @pyqtSlot()
"""Save settings and close dialog. def _doSave(self) -> None:
""" """Save settings and close dialog."""
project = SHARED.project project = SHARED.project
projName = self.tabMain.editName.text() projName = self.tabMain.editName.text()
bookTitle = self.tabMain.editTitle.text() bookTitle = self.tabMain.editTitle.text()
@@ -140,9 +140,9 @@ class GuiProjectSettings(NPagedDialog):
return return
def _doClose(self): @pyqtSlot()
"""Save settings and close the dialog. def _doClose(self) -> None:
""" """Save settings and close the dialog."""
self._saveGuiSettings() self._saveGuiSettings()
self.reject() self.reject()
return return
@@ -151,9 +151,8 @@ class GuiProjectSettings(NPagedDialog):
# Internal Functions # Internal Functions
## ##
def _focusTab(self, tab): def _focusTab(self, tab: int) -> None:
"""Change which is the focused tab. """Change which is the focused tab."""
"""
if tab == self.TAB_MAIN: if tab == self.TAB_MAIN:
self.setCurrentWidget(self.tabMain) self.setCurrentWidget(self.tabMain)
elif tab == self.TAB_STATUS: elif tab == self.TAB_STATUS:
@@ -164,9 +163,8 @@ class GuiProjectSettings(NPagedDialog):
self.setCurrentWidget(self.tabReplace) self.setCurrentWidget(self.tabReplace)
return return
def _saveGuiSettings(self): def _saveGuiSettings(self) -> None:
"""Save GUI settings. """Save GUI settings."""
"""
winWidth = CONFIG.rpxInt(self.width()) winWidth = CONFIG.rpxInt(self.width())
winHeight = CONFIG.rpxInt(self.height()) winHeight = CONFIG.rpxInt(self.height())
replaceColW = CONFIG.rpxInt(self.tabReplace.listBox.columnWidth(0)) replaceColW = CONFIG.rpxInt(self.tabReplace.listBox.columnWidth(0))
@@ -187,8 +185,8 @@ class GuiProjectSettings(NPagedDialog):
class GuiProjectEditMain(QWidget): class GuiProjectEditMain(QWidget):
def __init__(self, projGui): def __init__(self, parent: QWidget) -> None:
super().__init__(parent=projGui) super().__init__(parent=parent)
# The Form # The Form
self.mainForm = NConfigLayout() self.mainForm = NConfigLayout()
@@ -271,8 +269,8 @@ class GuiProjectEditStatus(QWidget):
COL_ROLE = Qt.ItemDataRole.UserRole + 1 COL_ROLE = Qt.ItemDataRole.UserRole + 1
NUM_ROLE = Qt.ItemDataRole.UserRole + 2 NUM_ROLE = Qt.ItemDataRole.UserRole + 2
def __init__(self, projGui, isStatus): def __init__(self, parent: QWidget, isStatus: bool) -> None:
super().__init__(parent=projGui) super().__init__(parent=parent)
if isStatus: if isStatus:
self.theStatus = SHARED.project.data.itemStatus self.theStatus = SHARED.project.data.itemStatus
@@ -372,9 +370,8 @@ class GuiProjectEditStatus(QWidget):
return return
def getNewList(self): def getNewList(self) -> tuple[list, list]:
"""Return list of entries. """Return list of entries."""
"""
if self.colChanged: if self.colChanged:
newList = [] newList = []
for n in range(self.listBox.topLevelItemCount()): for n in range(self.listBox.topLevelItemCount()):
@@ -394,9 +391,8 @@ class GuiProjectEditStatus(QWidget):
## ##
@pyqtSlot() @pyqtSlot()
def _selectColour(self): def _selectColour(self) -> None:
"""Open a dialog to select the status icon colour. """Open a dialog to select the status icon colour."""
"""
if self.selColour is not None: if self.selColour is not None:
newCol = QColorDialog.getColor( newCol = QColorDialog.getColor(
self.selColour, self, self.tr("Select Colour") self.selColour, self, self.tr("Select Colour")
@@ -410,17 +406,15 @@ class GuiProjectEditStatus(QWidget):
return return
@pyqtSlot() @pyqtSlot()
def _newItem(self): def _newItem(self) -> None:
"""Create a new status item. """Create a new status item."""
"""
self._addItem(None, self.tr("New Item"), (100, 100, 100), 0) self._addItem(None, self.tr("New Item"), (100, 100, 100), 0)
self.colChanged = True self.colChanged = True
return return
@pyqtSlot() @pyqtSlot()
def _delItem(self): def _delItem(self) -> None:
"""Delete a status item. """Delete a status item."""
"""
selItem = self._getSelectedItem() selItem = self._getSelectedItem()
if isinstance(selItem, QTreeWidgetItem): if isinstance(selItem, QTreeWidgetItem):
iRow = self.listBox.indexOfTopLevelItem(selItem) iRow = self.listBox.indexOfTopLevelItem(selItem)
@@ -433,9 +427,8 @@ class GuiProjectEditStatus(QWidget):
return return
@pyqtSlot() @pyqtSlot()
def _saveItem(self): def _saveItem(self) -> None:
"""Save changes made to a status item. """Save changes made to a status item."""
"""
selItem = self._getSelectedItem() selItem = self._getSelectedItem()
if isinstance(selItem, QTreeWidgetItem): if isinstance(selItem, QTreeWidgetItem):
selItem.setText(self.COL_LABEL, simplified(self.editName.text())) selItem.setText(self.COL_LABEL, simplified(self.editName.text()))
@@ -444,11 +437,10 @@ class GuiProjectEditStatus(QWidget):
self.selColour.red(), self.selColour.green(), self.selColour.blue() self.selColour.red(), self.selColour.green(), self.selColour.blue()
)) ))
self.colChanged = True self.colChanged = True
return return
@pyqtSlot() @pyqtSlot()
def _selectedItem(self): def _selectedItem(self) -> None:
"""Extract the info of a selected item and populate the settings """Extract the info of a selected item and populate the settings
boxes and button. If no item is selected, clear the form. boxes and button. If no item is selected, clear the form.
""" """
@@ -456,7 +448,6 @@ class GuiProjectEditStatus(QWidget):
if isinstance(selItem, QTreeWidgetItem): if isinstance(selItem, QTreeWidgetItem):
cols = selItem.data(self.COL_LABEL, self.COL_ROLE) cols = selItem.data(self.COL_LABEL, self.COL_ROLE)
name = selItem.text(self.COL_LABEL) name = selItem.text(self.COL_LABEL)
pixmap = QPixmap(self.iPx, self.iPx) pixmap = QPixmap(self.iPx, self.iPx)
pixmap.fill(QColor(*cols)) pixmap.fill(QColor(*cols))
self.selColour = QColor(*cols) self.selColour = QColor(*cols)
@@ -464,31 +455,27 @@ class GuiProjectEditStatus(QWidget):
self.colButton.setIcon(QIcon(pixmap)) self.colButton.setIcon(QIcon(pixmap))
self.editName.selectAll() self.editName.selectAll()
self.editName.setFocus() self.editName.setFocus()
self.editName.setEnabled(True) self.editName.setEnabled(True)
self.colButton.setEnabled(True) self.colButton.setEnabled(True)
self.saveButton.setEnabled(True) self.saveButton.setEnabled(True)
else: else:
pixmap = QPixmap(self.iPx, self.iPx) pixmap = QPixmap(self.iPx, self.iPx)
pixmap.fill(QColor(100, 100, 100)) pixmap.fill(QColor(100, 100, 100))
self.selColour = QColor(100, 100, 100) self.selColour = QColor(100, 100, 100)
self.editName.setText("") self.editName.setText("")
self.colButton.setIcon(QIcon(pixmap)) self.colButton.setIcon(QIcon(pixmap))
self.editName.setEnabled(False) self.editName.setEnabled(False)
self.colButton.setEnabled(False) self.colButton.setEnabled(False)
self.saveButton.setEnabled(False) self.saveButton.setEnabled(False)
return return
## ##
# Internal Functions # Internal Functions
## ##
def _addItem(self, key, name, cols, count): def _addItem(self, key: str | None, name: str,
"""Add a status item to the list. cols: tuple[int, int, int], count: int) -> None:
""" """Add a status item to the list."""
pixmap = QPixmap(self.iPx, self.iPx) pixmap = QPixmap(self.iPx, self.iPx)
pixmap.fill(QColor(*cols)) pixmap.fill(QColor(*cols))
@@ -504,9 +491,8 @@ class GuiProjectEditStatus(QWidget):
return return
def _moveItem(self, step): def _moveItem(self, step: int) -> None:
"""Move and item up or down step. """Move and item up or down step."""
"""
selItem = self._getSelectedItem() selItem = self._getSelectedItem()
if selItem is None: if selItem is None:
return return
@@ -527,17 +513,15 @@ class GuiProjectEditStatus(QWidget):
return return
def _getSelectedItem(self): def _getSelectedItem(self) -> QTreeWidgetItem | None:
"""Get the currently selected item. """Get the currently selected item."""
"""
selItem = self.listBox.selectedItems() selItem = self.listBox.selectedItems()
if len(selItem) > 0: if len(selItem) > 0:
return selItem[0] return selItem[0]
return None return None
def _usageString(self, nUse): def _usageString(self, nUse: int) -> str:
"""Generate usage string. """Generate usage string."""
"""
if nUse == 0: if nUse == 0:
return self.tr("Not in use") return self.tr("Not in use")
elif nUse == 1: elif nUse == 1:
@@ -553,8 +537,8 @@ class GuiProjectEditReplace(QWidget):
COL_KEY = 0 COL_KEY = 0
COL_REPL = 1 COL_REPL = 1
def __init__(self, projGui): def __init__(self, parent: QWidget) -> None:
super().__init__(parent=projGui) super().__init__(parent=parent)
self.arChanged = False self.arChanged = False
@@ -635,25 +619,23 @@ class GuiProjectEditReplace(QWidget):
return return
def getNewList(self): def getNewList(self) -> dict:
"""Extract the list from the widget. """Extract the list from the widget."""
""" new = {}
newList = {}
for n in range(self.listBox.topLevelItemCount()): for n in range(self.listBox.topLevelItemCount()):
tItem = self.listBox.topLevelItem(n) tItem = self.listBox.topLevelItem(n)
if tItem is not None: if tItem is not None:
aKey = self._stripNotAllowed(tItem.text(0)) aKey = self._stripNotAllowed(tItem.text(0))
aVal = tItem.text(1) aVal = tItem.text(1)
if len(aKey) > 0: if len(aKey) > 0:
newList[aKey] = aVal new[aKey] = aVal
return new
return newList
## ##
# Internal Functions # Internal Functions
## ##
def _selectedItem(self): def _selectedItem(self) -> bool:
"""Extract the details from the selected item and populate the """Extract the details from the selected item and populate the
edit form. edit form.
""" """
@@ -670,63 +652,53 @@ class GuiProjectEditReplace(QWidget):
self.editKey.setFocus() self.editKey.setFocus()
return True return True
def _saveEntry(self): def _saveEntry(self) -> None:
"""Save the form data into the list widget. """Save the form data into the list widget."""
"""
selItem = self._getSelectedItem() selItem = self._getSelectedItem()
if selItem is None: if selItem:
return False newKey = self.editKey.text()
newVal = self.editValue.text()
newKey = self.editKey.text() saveKey = self._stripNotAllowed(newKey)
newVal = self.editValue.text() if len(saveKey) > 0 and len(newVal) > 0:
saveKey = self._stripNotAllowed(newKey) selItem.setText(self.COL_KEY, "<%s>" % saveKey)
selItem.setText(self.COL_REPL, newVal)
if len(saveKey) > 0 and len(newVal) > 0: self.editKey.clear()
selItem.setText(self.COL_KEY, "<%s>" % saveKey) self.editValue.clear()
selItem.setText(self.COL_REPL, newVal) self.editKey.setEnabled(False)
self.editKey.clear() self.editValue.setEnabled(False)
self.editValue.clear() self.listBox.clearSelection()
self.editKey.setEnabled(False) self.arChanged = True
self.editValue.setEnabled(False)
self.listBox.clearSelection()
self.arChanged = True
return return
def _addEntry(self): def _addEntry(self) -> None:
"""Add a new list entry. """Add a new list entry."""
"""
saveKey = "<keyword%d>" % (self.listBox.topLevelItemCount() + 1) saveKey = "<keyword%d>" % (self.listBox.topLevelItemCount() + 1)
newVal = "" newVal = ""
newItem = QTreeWidgetItem([saveKey, newVal]) newItem = QTreeWidgetItem([saveKey, newVal])
self.listBox.addTopLevelItem(newItem) self.listBox.addTopLevelItem(newItem)
return True return
def _delEntry(self): def _delEntry(self) -> None:
"""Delete the selected entry. """Delete the selected entry."""
"""
selItem = self._getSelectedItem() selItem = self._getSelectedItem()
if selItem is None: if selItem:
return False self.listBox.takeTopLevelItem(self.listBox.indexOfTopLevelItem(selItem))
self.listBox.takeTopLevelItem(self.listBox.indexOfTopLevelItem(selItem)) self.arChanged = True
self.arChanged = True return
return True
def _getSelectedItem(self): def _getSelectedItem(self) -> QTreeWidgetItem | None:
"""Extract the currently selected item. """Extract the currently selected item."""
"""
selItem = self.listBox.selectedItems() selItem = self.listBox.selectedItems()
if len(selItem) == 0: if len(selItem) == 0:
return None return None
return selItem[0] return selItem[0]
def _stripNotAllowed(self, theKey): def _stripNotAllowed(self, key: str) -> str:
"""Clean up the replace key string. """Clean up the replace key string."""
""" result = ""
retKey = "" for c in key:
for c in theKey:
if c.isalnum(): if c.isalnum():
retKey += c result += c
return retKey return result
# END Class GuiProjectEditReplace # END Class GuiProjectEditReplace
+17 -17
View File
@@ -155,9 +155,9 @@ def testDlgProjSettings_StatusImport(qtbot, monkeypatch, nwGUI, fncPath, projPat
# Set some values # Set some values
theProject = SHARED.project theProject = SHARED.project
theProject.tree[C.hTitlePage].setStatus(C.sFinished) theProject.tree[C.hTitlePage].setStatus(C.sFinished) # type: ignore
theProject.tree[C.hChapterDoc].setStatus(C.sDraft) theProject.tree[C.hChapterDoc].setStatus(C.sDraft) # type: ignore
theProject.tree[C.hSceneDoc].setStatus(C.sDraft) theProject.tree[C.hSceneDoc].setStatus(C.sDraft) # type: ignore
nwGUI.projView.projTree.setSelectedHandle(C.hPlotRoot) nwGUI.projView.projTree.setSelectedHandle(C.hPlotRoot)
nwGUI.projView.projTree.newTreeItem(nwItemType.FILE, hLevel=1, isNote=True) nwGUI.projView.projTree.newTreeItem(nwItemType.FILE, hLevel=1, isNote=True)
@@ -170,9 +170,9 @@ def testDlgProjSettings_StatusImport(qtbot, monkeypatch, nwGUI, fncPath, projPat
hCharNote = "0000000000011" hCharNote = "0000000000011"
hWorldNote = "0000000000012" hWorldNote = "0000000000012"
theProject.tree[hPlotNote].setImport(C.iMajor) theProject.tree[hPlotNote].setImport(C.iMajor) # type: ignore
theProject.tree[hCharNote].setImport(C.iMajor) theProject.tree[hCharNote].setImport(C.iMajor) # type: ignore
theProject.tree[hWorldNote].setImport(C.iMain) theProject.tree[hWorldNote].setImport(C.iMain) # type: ignore
# Create Dialog # Create Dialog
projSettings = GuiProjectSettings(nwGUI, GuiProjectSettings.TAB_STATUS) projSettings = GuiProjectSettings(nwGUI, GuiProjectSettings.TAB_STATUS)
@@ -367,23 +367,23 @@ def testDlgProjSettings_Replace(qtbot, monkeypatch, nwGUI, fncPath, projPath, mo
tabReplace = projSettings.tabReplace tabReplace = projSettings.tabReplace
assert tabReplace.listBox.topLevelItem(0).text(0) == "<A>" assert tabReplace.listBox.topLevelItem(0).text(0) == "<A>" # type: ignore
assert tabReplace.listBox.topLevelItem(0).text(1) == "B" assert tabReplace.listBox.topLevelItem(0).text(1) == "B" # type: ignore
assert tabReplace.listBox.topLevelItem(1).text(0) == "<C>" assert tabReplace.listBox.topLevelItem(1).text(0) == "<C>" # type: ignore
assert tabReplace.listBox.topLevelItem(1).text(1) == "D" assert tabReplace.listBox.topLevelItem(1).text(1) == "D" # type: ignore
assert tabReplace.listBox.topLevelItemCount() == 2 assert tabReplace.listBox.topLevelItemCount() == 2
# Nothing to save or delete # Nothing to save or delete
tabReplace.listBox.clearSelection() tabReplace.listBox.clearSelection()
assert tabReplace._saveEntry() is False tabReplace._saveEntry()
assert tabReplace._delEntry() is False tabReplace._delEntry()
assert tabReplace.listBox.topLevelItemCount() == 2 assert tabReplace.listBox.topLevelItemCount() == 2
# Create a new entry # Create a new entry
qtbot.mouseClick(tabReplace.addButton, Qt.LeftButton) qtbot.mouseClick(tabReplace.addButton, Qt.LeftButton)
assert tabReplace.listBox.topLevelItemCount() == 3 assert tabReplace.listBox.topLevelItemCount() == 3
assert tabReplace.listBox.topLevelItem(2).text(0) == "<keyword3>" assert tabReplace.listBox.topLevelItem(2).text(0) == "<keyword3>" # type: ignore
assert tabReplace.listBox.topLevelItem(2).text(1) == "" assert tabReplace.listBox.topLevelItem(2).text(1) == "" # type: ignore
# Edit the entry # Edit the entry
tabReplace.listBox.setCurrentItem(tabReplace.listBox.topLevelItem(2)) tabReplace.listBox.setCurrentItem(tabReplace.listBox.topLevelItem(2))
@@ -394,8 +394,8 @@ def testDlgProjSettings_Replace(qtbot, monkeypatch, nwGUI, fncPath, projPath, mo
for c in "With This Stuff ": for c in "With This Stuff ":
qtbot.keyClick(tabReplace.editValue, c, delay=KEY_DELAY) qtbot.keyClick(tabReplace.editValue, c, delay=KEY_DELAY)
qtbot.mouseClick(tabReplace.saveButton, Qt.LeftButton) qtbot.mouseClick(tabReplace.saveButton, Qt.LeftButton)
assert tabReplace.listBox.topLevelItem(2).text(0) == "<This>" assert tabReplace.listBox.topLevelItem(2).text(0) == "<This>" # type: ignore
assert tabReplace.listBox.topLevelItem(2).text(1) == "With This Stuff " assert tabReplace.listBox.topLevelItem(2).text(1) == "With This Stuff " # type: ignore
# Create a new entry again # Create a new entry again
tabReplace.listBox.clearSelection() tabReplace.listBox.clearSelection()
@@ -405,7 +405,7 @@ def testDlgProjSettings_Replace(qtbot, monkeypatch, nwGUI, fncPath, projPath, mo
# The list is sorted, so we must find it # The list is sorted, so we must find it
newIdx = -1 newIdx = -1
for i in range(tabReplace.listBox.topLevelItemCount()): for i in range(tabReplace.listBox.topLevelItemCount()):
if tabReplace.listBox.topLevelItem(i).text(0) == "<keyword4>": if tabReplace.listBox.topLevelItem(i).text(0) == "<keyword4>": # type: ignore
newIdx = i newIdx = i
break break
assert newIdx >= 0 assert newIdx >= 0