Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update annotations and add deleteLater to several temporary objects #1629

Merged
merged 10 commits into from
Nov 28, 2023
5 changes: 5 additions & 0 deletions novelwriter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def main(sysArgs: list | None = None):
"config=",
"data=",
"testmode",
"meminfo"
]

helpMsg = (
Expand All @@ -92,6 +93,7 @@ def main(sysArgs: list | None = None):
" -v, --version Print program version and exit.\n"
" --info Print additional runtime information.\n"
" --debug Print debug output. Includes --info.\n"
" --meminfo Show memory usage information in the status bar.\n"
" --style= Sets Qt5 style flag. Defaults to 'Fusion'.\n"
" --config= Alternative config file.\n"
" --data= Alternative user data path.\n"
Expand Down Expand Up @@ -127,6 +129,7 @@ def main(sysArgs: list | None = None):
elif inOpt == "--info":
logLevel = logging.INFO
elif inOpt == "--debug":
CONFIG.isDebug = True
logLevel = logging.DEBUG
logFormat = "[{asctime:}] {filename:>17}:{lineno:<4d} {levelname:8} {message:}"
elif inOpt == "--style":
Expand All @@ -137,6 +140,8 @@ def main(sysArgs: list | None = None):
dataPath = inArg
elif inOpt == "--testmode":
testMode = True
elif inOpt == "--meminfo":
CONFIG.memInfo = True

# Setup Logging
pkgLogger = logging.getLogger(__package__)
Expand Down
4 changes: 2 additions & 2 deletions novelwriter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ def __init__(self) -> None:
# Other System Info
self.hostName = QSysInfo.machineHostName()
self.kernelVer = QSysInfo.kernelVersion()
self.isDebug = False
self.isDebug = False # True if running in debug mode
self.memInfo = False # True if displaying mem info in status bar

# Packages
self.hasEnchant = False # The pyenchant package
Expand Down Expand Up @@ -485,7 +486,6 @@ def initConfig(self, confPath: str | Path | None = None,

self._recentObj.loadCache()
self._checkOptionalPackages()
self.isDebug = logger.getEffectiveLevel() == logging.DEBUG

logger.debug("Config instance initialised")

Expand Down
2 changes: 1 addition & 1 deletion novelwriter/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def getHandleHeaderCount(self, tHandle: str) -> int:
return 0

def getTableOfContents(
self, rHandle: str, maxDepth: int, skipExcl: bool = True
self, rHandle: str | None, maxDepth: int, skipExcl: bool = True
) -> list[tuple[str, int, str, int]]:
"""Generate a table of contents up to a maximum depth."""
tOrder = []
Expand Down
57 changes: 28 additions & 29 deletions novelwriter/dialogs/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

from datetime import datetime

from PyQt5.QtGui import QCursor
from PyQt5.QtGui import QCloseEvent, QCursor
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
qApp, QDialog, QDialogButtonBox, QHBoxLayout, QLabel, QTabWidget,
Expand All @@ -44,7 +44,7 @@

class GuiAbout(QDialog):

def __init__(self, parent: QWidget):
def __init__(self, parent: QWidget) -> None:
super().__init__(parent=parent)

logger.debug("Create: GuiAbout")
Expand Down Expand Up @@ -101,7 +101,7 @@ def __init__(self, parent: QWidget):

# OK Button
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok)
self.buttonBox.accepted.connect(self._doClose)
self.buttonBox.accepted.connect(self.close)

self.outerBox.addLayout(self.innerBox)
self.outerBox.addWidget(self.buttonBox)
Expand All @@ -111,13 +111,12 @@ def __init__(self, parent: QWidget):

return

def __del__(self): # pragma: no cover
def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: GuiAbout")
return

def populateGUI(self):
"""Populate tabs with text.
"""
def populateGUI(self) -> None:
"""Populate tabs with text."""
qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
self._setStyleSheet()
self._fillAboutPage()
Expand All @@ -127,19 +126,27 @@ def populateGUI(self):
qApp.restoreOverrideCursor()
return

def showReleaseNotes(self):
"""Show the release notes.
"""
def showReleaseNotes(self) -> None:
"""Show the release notes."""
self.tabBox.setCurrentWidget(self.pageNotes)
return

##
# Events
##

def closeEvent(self, event: QCloseEvent) -> None:
"""Capture the close event and perform cleanup."""
event.accept()
self.deleteLater()
return

##
# Internal Functions
##

def _fillAboutPage(self):
"""Generate the content for the About page.
"""
def _fillAboutPage(self) -> None:
"""Generate the content for the About page."""
aboutMsg = (
"<h2>{title1}</h2>"
"<p>{copy}</p>"
Expand Down Expand Up @@ -181,9 +188,8 @@ def _fillAboutPage(self):

return

def _fillNotesPage(self):
"""Load the content for the Release Notes page.
"""
def _fillNotesPage(self) -> None:
"""Load the content for the Release Notes page."""
docPath = CONFIG.assetPath("text") / "release_notes.htm"
docText = readTextFile(docPath)
if docText:
Expand All @@ -192,9 +198,8 @@ def _fillNotesPage(self):
self.pageNotes.setHtml("Error loading release notes text ...")
return

def _fillCreditsPage(self):
"""Load the content for the Credits page.
"""
def _fillCreditsPage(self) -> None:
"""Load the content for the Credits page."""
docPath = CONFIG.assetPath("text") / "credits_en.htm"
docText = readTextFile(docPath)
if docText:
Expand All @@ -203,9 +208,8 @@ def _fillCreditsPage(self):
self.pageCredits.setHtml("Error loading credits text ...")
return

def _fillLicensePage(self):
"""Load the content for the Licence page.
"""
def _fillLicensePage(self) -> None:
"""Load the content for the Licence page."""
docPath = CONFIG.assetPath("text") / "gplv3_en.htm"
docText = readTextFile(docPath)
if docText:
Expand All @@ -214,9 +218,8 @@ def _fillLicensePage(self):
self.pageLicense.setHtml("Error loading licence text ...")
return

def _setStyleSheet(self):
"""Set stylesheet for all browser tabs
"""
def _setStyleSheet(self) -> None:
"""Set stylesheet for all browser tabs."""
styleSheet = (
"h1, h2, h3, h4 {{"
" color: rgb({hColR},{hColG},{hColB});"
Expand All @@ -242,8 +245,4 @@ def _setStyleSheet(self):

return

def _doClose(self):
self.close()
return

# END Class GuiAbout
33 changes: 21 additions & 12 deletions novelwriter/dialogs/docmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

import logging

from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QCloseEvent
from PyQt5.QtCore import Qt, QSize, pyqtSlot
from PyQt5.QtWidgets import (
QAbstractItemView, QDialog, QDialogButtonBox, QGridLayout, QLabel,
QListWidget, QListWidgetItem, QVBoxLayout, QWidget
Expand Down Expand Up @@ -108,13 +109,12 @@ def __init__(self, parent: QWidget, sHandle: str, itemList: list[str]) -> None:

return

def __del__(self): # pragma: no cover
def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: GuiDocMerge")
return

def getData(self):
"""Return the user's choices.
"""
def getData(self) -> dict:
"""Return the user's choices."""
finalItems = []
for i in range(self.listBox.count()):
item = self.listBox.item(i)
Expand All @@ -127,12 +127,22 @@ def getData(self):
return self._data

##
# Slots
# Events
##

def _resetList(self):
"""Reset the content of the list box to its original state.
"""
def closeEvent(self, event: QCloseEvent) -> None:
"""Capture the close event and perform cleanup."""
event.accept()
self.deleteLater()
return

##
# Private Slots
##

@pyqtSlot()
def _resetList(self) -> None:
"""Reset the content of the list box to its original state."""
logger.debug("Resetting list box content")
sHandle = self._data.get("sHandle", None)
itemList = self._data.get("origItems", [])
Expand All @@ -143,9 +153,8 @@ def _resetList(self):
# Internal Functions
##

def _loadContent(self, sHandle, itemList):
"""Load content from a given list of items.
"""
def _loadContent(self, sHandle: str, itemList: list[str]) -> None:
"""Load content from a given list of items."""
self._data = {}
self._data["sHandle"] = sHandle
self._data["origItems"] = itemList
Expand Down
37 changes: 24 additions & 13 deletions novelwriter/dialogs/docsplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@

import logging

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QCloseEvent
from PyQt5.QtCore import Qt, pyqtSlot
from PyQt5.QtWidgets import (
QDialog, QVBoxLayout, QComboBox, QListWidget, QAbstractItemView,
QListWidgetItem, QDialogButtonBox, QLabel, QGridLayout
QAbstractItemView, QComboBox, QDialog, QDialogButtonBox, QGridLayout,
QLabel, QListWidget, QListWidgetItem, QVBoxLayout, QWidget
)

from novelwriter import CONFIG, SHARED
Expand All @@ -45,7 +46,7 @@ class GuiDocSplit(QDialog):
LEVEL_ROLE = Qt.ItemDataRole.UserRole + 1
LABEL_ROLE = Qt.ItemDataRole.UserRole + 2

def __init__(self, parent, sHandle):
def __init__(self, parent: QWidget, sHandle: str) -> None:
super().__init__(parent=parent)

logger.debug("Create: GuiDocSplit")
Expand Down Expand Up @@ -138,11 +139,11 @@ def __init__(self, parent, sHandle):

return

def __del__(self): # pragma: no cover
def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: GuiDocSplit")
return

def getData(self):
def getData(self) -> tuple[dict, list]:
"""Return the user's choices. Also save the users options for
the next time the dialog is used.
"""
Expand All @@ -167,6 +168,7 @@ def getData(self):
self._data["docHierarchy"] = docHierarchy
self._data["moveToTrash"] = moveToTrash

logger.debug("Saving State: GuiDocSplit")
pOptions = SHARED.project.options
pOptions.setValue("GuiDocSplit", "spLevel", spLevel)
pOptions.setValue("GuiDocSplit", "intoFolder", intoFolder)
Expand All @@ -175,12 +177,22 @@ def getData(self):
return self._data, self._text

##
# Slots
# Events
##

def _reloadList(self):
"""Reload the content of the list box.
"""
def closeEvent(self, event: QCloseEvent) -> None:
"""Capture the close event and perform cleanup."""
event.accept()
self.deleteLater()
return

##
# Private Slots
##

@pyqtSlot()
def _reloadList(self) -> None:
"""Reload the content of the list box."""
sHandle = self._data.get("sHandle", None)
self._loadContent(sHandle)
return
Expand All @@ -189,9 +201,8 @@ def _reloadList(self):
# Internal Functions
##

def _loadContent(self, sHandle):
"""Load content from a given source item.
"""
def _loadContent(self, sHandle: str) -> None:
"""Load content from a given source item."""
self._data = {}
self._data["sHandle"] = sHandle

Expand Down
22 changes: 17 additions & 5 deletions novelwriter/dialogs/editlabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import logging

from PyQt5.QtWidgets import (
QDialog, QVBoxLayout, QLineEdit, QLabel, QDialogButtonBox, QHBoxLayout
QDialog, QDialogButtonBox, QHBoxLayout, QLabel, QLineEdit, QVBoxLayout,
QWidget
)

from novelwriter import CONFIG
Expand All @@ -36,9 +37,10 @@

class GuiEditLabel(QDialog):

def __init__(self, parent, text=""):
def __init__(self, parent: QWidget, text: str = "") -> None:
super().__init__(parent=parent)

logger.debug("Create: GuiEditLabel")
self.setObjectName("GuiEditLabel")
self.setWindowTitle(self.tr("Item Label"))

Expand Down Expand Up @@ -70,16 +72,26 @@ def __init__(self, parent, text=""):

self.setLayout(self.outerBox)

logger.debug("Ready: GuiEditLabel")

return

def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: GuiEditLabel")
return

@property
def itemLabel(self):
def itemLabel(self) -> str:
return self.labelValue.text()

@classmethod
def getLabel(cls, parent, text):
def getLabel(cls, parent: QWidget, text: str) -> tuple[str, bool]:
"""Pop the dialog and return the result."""
cls = GuiEditLabel(parent, text=text)
cls.exec_()
return cls.itemLabel, cls.result() == QDialog.Accepted
label = cls.itemLabel
accepted = cls.result() == QDialog.Accepted
cls.deleteLater()
return label, accepted

# END Class GuiEditLabel
Loading