From 55423174ed03d44e527606945cc2aaed73ab74e4 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 20 Aug 2023 22:09:02 +0200 Subject: [PATCH] Make a few minor improvements, including fixing #1495 --- novelwriter/common.py | 3 +-- novelwriter/core/project.py | 13 ++++++------- novelwriter/core/spellcheck.py | 18 +++++++++--------- novelwriter/guimain.py | 3 ++- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/novelwriter/common.py b/novelwriter/common.py index 8ccf0bf0..08fd407b 100644 --- a/novelwriter/common.py +++ b/novelwriter/common.py @@ -512,8 +512,7 @@ def sha256sum(path: str | Path) -> str | 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(): if qWidget.objectName() == objName: return qWidget diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index b1abef91..e86776ac 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -160,7 +160,7 @@ class NWProject(QObject): """Add a new file with a given label and parent item.""" 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 will not run if the file exists and is not empty. """ @@ -175,7 +175,7 @@ class NWProject(QObject): return False 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: tItem.setLayout(nwItemLayout.DOCUMENT) else: @@ -503,7 +503,7 @@ class NWProject(QObject): order has been altered, or a file is orphaned, this function is capable of handling it. """ - sentItems = [] + sentItems = set() iterItems = self._tree.handles() n = 0 nMax = min(len(iterItems), 10000) @@ -515,13 +515,12 @@ class NWProject(QObject): # Technically a bug continue elif tItem.itemParent is None: - # Item is a root, or already been identified as an - # orphaned item - sentItems.append(tHandle) + # Item is a root, or already been identified as orphaned + sentItems.add(tHandle) yield tItem elif tItem.itemParent in sentItems: # Item's parent has been sent, so all is fine - sentItems.append(tHandle) + sentItems.add(tHandle) yield tItem elif tItem.itemParent in iterItems: # Item's parent exists, but hasn't been sent yet, so add diff --git a/novelwriter/core/spellcheck.py b/novelwriter/core/spellcheck.py index d7d55d91..0ebfe654 100644 --- a/novelwriter/core/spellcheck.py +++ b/novelwriter/core/spellcheck.py @@ -213,15 +213,15 @@ class UserDictionary: def load(self) -> None: """Load the user's dictionary.""" self._path = self._project.storage.getMetaFile(nwFiles.DICT_FILE) - if not isinstance(self._path, Path): - return - try: - with open(self._path, mode="r", encoding="utf-8") as fObj: - data = json.load(fObj) - self._words = set(data.get("novelWriter.userDict", [])) - except Exception: - logger.error("Failed to load user dictionary") - logException() + self._words = set() + if isinstance(self._path, Path) and self._path.is_file(): + try: + with open(self._path, mode="r", encoding="utf-8") as fObj: + data = json.load(fObj) + self._words = set(data.get("novelWriter.userDict", [])) + except Exception: + logger.error("Failed to load user dictionary") + logException() return def save(self) -> None: diff --git a/novelwriter/guimain.py b/novelwriter/guimain.py index 596ccb95..0d4e90a8 100644 --- a/novelwriter/guimain.py +++ b/novelwriter/guimain.py @@ -448,6 +448,7 @@ class GuiMain(QMainWindow): self._changeView(nwView.PROJECT) # Try to open the project + tStart = time() if not SHARED.openProject(projFile): # The project open failed. lockStatus = SHARED.projectLock @@ -522,7 +523,7 @@ class GuiMain(QMainWindow): self.docEditor.setDocumentChanged(False) SHARED.project.setProjectChanged(False) - logger.debug("Project load complete") + logger.debug("Project loaded in %.3f ms", (time() - tStart)*1000) return True