Clean up some redundant code and fix annotations

This commit is contained in:
Veronica Berglyd Olsen
2023-11-29 16:05:14 +01:00
parent 1d1dae0c7e
commit b526315084
8 changed files with 29 additions and 57 deletions
-13
View File
@@ -38,7 +38,6 @@ from urllib.request import pathname2url
from PyQt5.QtGui import QDesktopServices from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QCoreApplication, QUrl from PyQt5.QtCore import QCoreApplication, QUrl
from PyQt5.QtWidgets import QWidget, qApp
from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout
from novelwriter.error import logException from novelwriter.error import logException
@@ -508,18 +507,6 @@ def openExternalPath(path: Path) -> bool:
return False return False
# =============================================================================================== #
# Other Functions
# =============================================================================================== #
def getGuiItem(objName: str) -> QWidget | None:
"""Returns a QtWidget based on its objectName."""
for qWidget in qApp.topLevelWidgets():
if qWidget.objectName() == objName:
return qWidget
return None
# =============================================================================================== # # =============================================================================================== #
# Classes # Classes
# =============================================================================================== # # =============================================================================================== #
+1 -1
View File
@@ -80,7 +80,7 @@ class NWProject:
return return
def __del__(self): # pragma: no cover def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: NWProject") logger.debug("Delete: NWProject")
return return
+1 -1
View File
@@ -56,7 +56,7 @@ class NWSpellEnchant:
logger.debug("Ready: NWSpellEnchant") logger.debug("Ready: NWSpellEnchant")
return return
def __del__(self): # pragma: no cover def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: NWSpellEnchant") logger.debug("Delete: NWSpellEnchant")
return return
+1 -1
View File
@@ -50,7 +50,7 @@ class GuiTextDocument(QTextDocument):
return return
def __del__(self): # pragma: no cover def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: GuiTextDocument") logger.debug("Delete: GuiTextDocument")
return return
+1 -1
View File
@@ -1650,7 +1650,7 @@ class _TreeContextMenu(QMenu):
return return
def __del__(self): # pragma: no cover def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: _TreeContextMenu") logger.debug("Delete: _TreeContextMenu")
return return
+17 -23
View File
@@ -66,7 +66,7 @@ class GuiManuscript(QDialog):
D_KEY = Qt.ItemDataRole.UserRole D_KEY = Qt.ItemDataRole.UserRole
def __init__(self, mainGui: GuiMain): def __init__(self, mainGui: GuiMain) -> None:
super().__init__(parent=mainGui) super().__init__(parent=mainGui)
logger.debug("Create: GuiManuscript") logger.debug("Create: GuiManuscript")
@@ -173,7 +173,7 @@ class GuiManuscript(QDialog):
self.btnBuild.clicked.connect(self._buildManuscript) self.btnBuild.clicked.connect(self._buildManuscript)
self.btnClose = QPushButton(self.tr("Close")) self.btnClose = QPushButton(self.tr("Close"))
self.btnClose.clicked.connect(self._doClose) self.btnClose.clicked.connect(self.close)
self.processBox = QGridLayout() self.processBox = QGridLayout()
self.processBox.addWidget(self.btnPreview, 0, 0) self.processBox.addWidget(self.btnPreview, 0, 0)
@@ -217,11 +217,11 @@ class GuiManuscript(QDialog):
return return
def __del__(self): # pragma: no cover def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: GuiManuscript") logger.debug("Delete: GuiManuscript")
return return
def loadContent(self): def loadContent(self) -> None:
"""Load dialog content from project data.""" """Load dialog content from project data."""
if len(self._builds) == 0: if len(self._builds) == 0:
build = BuildSettings() build = BuildSettings()
@@ -255,7 +255,7 @@ class GuiManuscript(QDialog):
# Events # Events
## ##
def closeEvent(self, event: QCloseEvent): def closeEvent(self, event: QCloseEvent) -> None:
"""Capture the user closing the window so we can save GUI """Capture the user closing the window so we can save GUI
settings. We also check that we don't have a build settings settings. We also check that we don't have a build settings
dialog open. dialog open.
@@ -274,7 +274,7 @@ class GuiManuscript(QDialog):
## ##
@pyqtSlot() @pyqtSlot()
def _createNewBuild(self): def _createNewBuild(self) -> None:
"""Open the build settings dialog for a new build.""" """Open the build settings dialog for a new build."""
build = BuildSettings() build = BuildSettings()
build.setName(self.tr("My Manuscript")) build.setName(self.tr("My Manuscript"))
@@ -282,7 +282,7 @@ class GuiManuscript(QDialog):
return return
@pyqtSlot() @pyqtSlot()
def _editSelectedBuild(self): def _editSelectedBuild(self) -> None:
"""Edit the currently selected build settings entry.""" """Edit the currently selected build settings entry."""
build = self._getSelectedBuild() build = self._getSelectedBuild()
if build is not None: if build is not None:
@@ -299,7 +299,7 @@ class GuiManuscript(QDialog):
return return
@pyqtSlot() @pyqtSlot()
def _deleteSelectedBuild(self): def _deleteSelectedBuild(self) -> None:
"""Delete the currently selected build settings entry.""" """Delete the currently selected build settings entry."""
build = self._getSelectedBuild() build = self._getSelectedBuild()
if build is not None: if build is not None:
@@ -309,7 +309,7 @@ class GuiManuscript(QDialog):
return return
@pyqtSlot(BuildSettings) @pyqtSlot(BuildSettings)
def _processNewSettings(self, build: BuildSettings): def _processNewSettings(self, build: BuildSettings) -> None:
"""Process new build settings from the settings dialog.""" """Process new build settings from the settings dialog."""
self._builds.setBuild(build) self._builds.setBuild(build)
self._updateBuildItem(build) self._updateBuildItem(build)
@@ -319,7 +319,7 @@ class GuiManuscript(QDialog):
return return
@pyqtSlot() @pyqtSlot()
def _generatePreview(self): def _generatePreview(self) -> None:
"""Run the document builder on the current build settings for """Run the document builder on the current build settings for
the preview widget. the preview widget.
""" """
@@ -359,7 +359,7 @@ class GuiManuscript(QDialog):
return return
@pyqtSlot() @pyqtSlot()
def _buildManuscript(self): def _buildManuscript(self) -> None:
"""Open the build dialog and build the manuscript.""" """Open the build dialog and build the manuscript."""
build = self._getSelectedBuild() build = self._getSelectedBuild()
if isinstance(build, BuildSettings): if isinstance(build, BuildSettings):
@@ -373,24 +373,18 @@ class GuiManuscript(QDialog):
return return
@pyqtSlot() @pyqtSlot()
def _printDocument(self): def _printDocument(self) -> None:
"""Open the print preview dialog.""" """Open the print preview dialog."""
thePreview = QPrintPreviewDialog(self) thePreview = QPrintPreviewDialog(self)
thePreview.paintRequested.connect(self.docPreview.printPreview) thePreview.paintRequested.connect(self.docPreview.printPreview)
thePreview.exec_() thePreview.exec_()
return return
@pyqtSlot()
def _doClose(self):
"""Forward the close button to the default close method."""
self.close()
return
## ##
# Internal Functions # Internal Functions
## ##
def _updatePreview(self, data: dict, build: BuildSettings): def _updatePreview(self, data: dict, build: BuildSettings) -> None:
"""Update the preview widget and set relevant values.""" """Update the preview widget and set relevant values."""
self.docPreview.setContent(data) self.docPreview.setContent(data)
self.docPreview.setBuildName(build.name) self.docPreview.setBuildName(build.name)
@@ -415,7 +409,7 @@ class GuiManuscript(QDialog):
return build return build
return None return None
def _saveSettings(self): def _saveSettings(self) -> None:
"""Save the user GUI settings.""" """Save the user GUI settings."""
buildOrder = [] buildOrder = []
for i in range(self.buildList.count()): for i in range(self.buildList.count()):
@@ -454,7 +448,7 @@ class GuiManuscript(QDialog):
return return
def _openSettingsDialog(self, build: BuildSettings): def _openSettingsDialog(self, build: BuildSettings) -> None:
"""Open the build settings dialog.""" """Open the build settings dialog."""
for obj in self.mainGui.children(): for obj in self.mainGui.children():
# Don't open a second dialog if one exists # Don't open a second dialog if one exists
@@ -475,7 +469,7 @@ class GuiManuscript(QDialog):
return return
def _updateBuildsList(self): def _updateBuildsList(self) -> None:
"""Update the list of available builds.""" """Update the list of available builds."""
self.buildList.clear() self.buildList.clear()
for key, name in self._builds.builds(): for key, name in self._builds.builds():
@@ -487,7 +481,7 @@ class GuiManuscript(QDialog):
self._buildMap[key] = bItem self._buildMap[key] = bItem
return return
def _updateBuildItem(self, build: BuildSettings): def _updateBuildItem(self, build: BuildSettings) -> None:
"""Update the entry of a specific build item.""" """Update the entry of a specific build item."""
bItem = self._buildMap.get(build.buildID, None) bItem = self._buildMap.get(build.buildID, None)
if isinstance(bItem, QListWidgetItem): if isinstance(bItem, QListWidgetItem):
+1 -1
View File
@@ -80,7 +80,7 @@ class GuiProjectWizard(QWizard):
return return
def __del__(self): # pragma: no cover def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: GuiProjectWizard") logger.debug("Delete: GuiProjectWizard")
return return
+7 -16
View File
@@ -21,8 +21,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations from __future__ import annotations
import time import time
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QDesktopServices
import pytest import pytest
from pathlib import Path from pathlib import Path
@@ -31,14 +29,16 @@ from xml.etree import ElementTree as ET
from tools import writeFile from tools import writeFile
from mocked import causeOSError from mocked import causeOSError
from novelwriter.guimain import GuiMain from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QUrl
from novelwriter.common import ( from novelwriter.common import (
checkBool, checkFloat, checkHandle, checkInt, checkIntTuple, checkPath, checkBool, checkFloat, checkHandle, checkInt, checkIntTuple, checkPath,
checkString, checkStringNone, checkUuid, formatInt, formatTime, checkString, checkStringNone, checkUuid, formatInt, formatTime,
formatTimeStamp, fuzzyTime, getFileSize, getGuiItem, hexToInt, isHandle, formatTimeStamp, fuzzyTime, getFileSize, hexToInt, isHandle, isItemClass,
isItemClass, isItemLayout, isItemType, isTitleTag, jsonEncode, isItemLayout, isItemType, isTitleTag, jsonEncode, makeFileNameSafe, minmax,
makeFileNameSafe, minmax, numberToRoman, NWConfigParser, openExternalPath, numberToRoman, NWConfigParser, openExternalPath, readTextFile, simplified,
readTextFile, simplified, transferCase, xmlIndent, yesNo transferCase, xmlIndent, yesNo
) )
@@ -655,15 +655,6 @@ def testBaseCommon_openExternalPath(monkeypatch, tstPaths):
# END Test testBaseCommon_openExternalPath # END Test testBaseCommon_openExternalPath
@pytest.mark.base
def testBaseCommon_getGuiItem(nwGUI):
"""Check the GUI item function."""
assert getGuiItem("gibberish") is None
assert isinstance(getGuiItem("GuiMain"), GuiMain)
# END Test testBaseCommon_getGuiItem
@pytest.mark.base @pytest.mark.base
def testBaseCommon_NWConfigParser(fncPath): def testBaseCommon_NWConfigParser(fncPath):
"""Test the NWConfigParser subclass.""" """Test the NWConfigParser subclass."""