Make a few minor improvements, including fixing #1495

This commit is contained in:
Veronica Berglyd Olsen
2023-08-20 22:09:02 +02:00
parent ed3e4bca6c
commit 55423174ed
4 changed files with 18 additions and 19 deletions
+1 -2
View File
@@ -512,8 +512,7 @@ def sha256sum(path: str | Path) -> str | None:
# =============================================================================================== # # =============================================================================================== #
def getGuiItem(objName: str) -> QWidget | None: def getGuiItem(objName: str) -> QWidget | None:
"""Returns a QtWidget based on its objectName. """Returns a QtWidget based on its objectName."""
"""
for qWidget in qApp.topLevelWidgets(): for qWidget in qApp.topLevelWidgets():
if qWidget.objectName() == objName: if qWidget.objectName() == objName:
return qWidget return qWidget
+6 -7
View File
@@ -160,7 +160,7 @@ class NWProject(QObject):
"""Add a new file with a given label and parent item.""" """Add a new file with a given label and parent item."""
return self._tree.create(label, parent, nwItemType.FILE) return self._tree.create(label, parent, nwItemType.FILE)
def writeNewFile(self, tHandle: str, hLevel: int, isDocument: bool, addText: str = "") -> bool: def writeNewFile(self, tHandle: str, hLevel: int, isDocument: bool, text: str = "") -> bool:
"""Write content to a new document after it is created. This """Write content to a new document after it is created. This
will not run if the file exists and is not empty. will not run if the file exists and is not empty.
""" """
@@ -175,7 +175,7 @@ class NWProject(QObject):
return False return False
hshText = "#"*minmax(hLevel, 1, 4) hshText = "#"*minmax(hLevel, 1, 4)
newText = f"{hshText} {tItem.itemName}\n\n{addText}" newText = f"{hshText} {tItem.itemName}\n\n{text}"
if tItem.isNovelLike() and isDocument: if tItem.isNovelLike() and isDocument:
tItem.setLayout(nwItemLayout.DOCUMENT) tItem.setLayout(nwItemLayout.DOCUMENT)
else: else:
@@ -503,7 +503,7 @@ class NWProject(QObject):
order has been altered, or a file is orphaned, this function is order has been altered, or a file is orphaned, this function is
capable of handling it. capable of handling it.
""" """
sentItems = [] sentItems = set()
iterItems = self._tree.handles() iterItems = self._tree.handles()
n = 0 n = 0
nMax = min(len(iterItems), 10000) nMax = min(len(iterItems), 10000)
@@ -515,13 +515,12 @@ class NWProject(QObject):
# Technically a bug # Technically a bug
continue continue
elif tItem.itemParent is None: elif tItem.itemParent is None:
# Item is a root, or already been identified as an # Item is a root, or already been identified as orphaned
# orphaned item sentItems.add(tHandle)
sentItems.append(tHandle)
yield tItem yield tItem
elif tItem.itemParent in sentItems: elif tItem.itemParent in sentItems:
# Item's parent has been sent, so all is fine # Item's parent has been sent, so all is fine
sentItems.append(tHandle) sentItems.add(tHandle)
yield tItem yield tItem
elif tItem.itemParent in iterItems: elif tItem.itemParent in iterItems:
# Item's parent exists, but hasn't been sent yet, so add # Item's parent exists, but hasn't been sent yet, so add
+9 -9
View File
@@ -213,15 +213,15 @@ class UserDictionary:
def load(self) -> None: def load(self) -> None:
"""Load the user's dictionary.""" """Load the user's dictionary."""
self._path = self._project.storage.getMetaFile(nwFiles.DICT_FILE) self._path = self._project.storage.getMetaFile(nwFiles.DICT_FILE)
if not isinstance(self._path, Path): self._words = set()
return if isinstance(self._path, Path) and self._path.is_file():
try: try:
with open(self._path, mode="r", encoding="utf-8") as fObj: with open(self._path, mode="r", encoding="utf-8") as fObj:
data = json.load(fObj) data = json.load(fObj)
self._words = set(data.get("novelWriter.userDict", [])) self._words = set(data.get("novelWriter.userDict", []))
except Exception: except Exception:
logger.error("Failed to load user dictionary") logger.error("Failed to load user dictionary")
logException() logException()
return return
def save(self) -> None: def save(self) -> None:
+2 -1
View File
@@ -448,6 +448,7 @@ class GuiMain(QMainWindow):
self._changeView(nwView.PROJECT) self._changeView(nwView.PROJECT)
# Try to open the project # Try to open the project
tStart = time()
if not SHARED.openProject(projFile): if not SHARED.openProject(projFile):
# The project open failed. # The project open failed.
lockStatus = SHARED.projectLock lockStatus = SHARED.projectLock
@@ -522,7 +523,7 @@ class GuiMain(QMainWindow):
self.docEditor.setDocumentChanged(False) self.docEditor.setDocumentChanged(False)
SHARED.project.setProjectChanged(False) SHARED.project.setProjectChanged(False)
logger.debug("Project load complete") logger.debug("Project loaded in %.3f ms", (time() - tStart)*1000)
return True return True