From c1ed0647fef4ee5c23826e8b672c4b1b0f80bc51 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 23 Nov 2024 19:56:24 +0100 Subject: [PATCH] Refactor project signal proxies --- novelwriter/core/index.py | 17 +++------ novelwriter/core/project.py | 8 ++--- novelwriter/core/status.py | 4 +-- novelwriter/core/tree.py | 8 ++--- novelwriter/shared.py | 57 ++++++++++++++++++------------ tests/test_core/test_core_index.py | 1 + 6 files changed, 48 insertions(+), 47 deletions(-) diff --git a/novelwriter/core/index.py b/novelwriter/core/index.py index 81f5e6846..382fae345 100644 --- a/novelwriter/core/index.py +++ b/novelwriter/core/index.py @@ -122,7 +122,7 @@ def clear(self) -> None: self._itemIndex.clear() self._indexChange = 0.0 self._rootChange = {} - SHARED.indexSignalProxy({"event": "clearIndex"}) + SHARED.emitIndexCleared(self._project) return def rebuild(self) -> None: @@ -133,7 +133,7 @@ def rebuild(self) -> None: text = self._project.storage.getDocumentText(nwItem.itemHandle) self.scanText(nwItem.itemHandle, text, blockSignal=True) self._indexBroken = False - SHARED.indexSignalProxy({"event": "buildIndex"}) + SHARED.emitIndexAvailable(self._project) return def deleteHandle(self, tHandle: str) -> None: @@ -143,10 +143,7 @@ def deleteHandle(self, tHandle: str) -> None: for tTag in delTags: del self._tagsIndex[tTag] del self._itemIndex[tHandle] - SHARED.indexSignalProxy({ - "event": "updateTags", - "deleted": delTags, - }) + SHARED.emitIndexChangedTags(self._project, [], delTags) return def reIndexHandle(self, tHandle: str | None) -> None: @@ -212,7 +209,7 @@ def loadIndex(self) -> bool: self.reIndexHandle(fHandle) self._indexChange = time() - SHARED.indexSignalProxy({"event": "buildIndex"}) + SHARED.emitIndexAvailable(self._project) logger.debug("Index loaded in %.3f ms", (time() - tStart)*1000) @@ -367,11 +364,7 @@ def _scanActive(self, tHandle: str, nwItem: NWItem, text: str, tags: dict[str, b del self._tagsIndex[tTag] deleted.append(tTag) if updated or deleted: - SHARED.indexSignalProxy({ - "event": "updateTags", - "updated": updated, - "deleted": deleted, - }) + SHARED.emitIndexChangedTags(self._project, updated, deleted) return diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index ced91fd10..31a449193 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -44,7 +44,7 @@ from novelwriter.core.projectdata import NWProjectData from novelwriter.core.projectxml import ProjectXMLReader, ProjectXMLWriter, XMLReadState from novelwriter.core.sessions import NWSessionLog -from novelwriter.core.status import T_StatusKinds, T_UpdateEntry +from novelwriter.core.status import T_StatusKind, T_UpdateEntry from novelwriter.core.storage import NWStorage, NWStorageOpen from novelwriter.core.tree import NWTree from novelwriter.enum import nwItemClass, nwItemLayout, nwItemType @@ -532,15 +532,15 @@ def countStatus(self) -> None: self._data.itemImport.increment(nwItem.itemImport) return - def updateStatus(self, kind: T_StatusKinds, update: T_UpdateEntry) -> None: + def updateStatus(self, kind: T_StatusKind, update: T_UpdateEntry) -> None: """Update status or import entries.""" if kind == "s": self._data.itemStatus.update(update) - SHARED.projectSignalProxy({"event": "statusLabels", "kind": kind}) + SHARED.emitStatusLabelsChanged(self, kind) self._tree.refreshAllItems() elif kind == "i": self._data.itemImport.update(update) - SHARED.projectSignalProxy({"event": "statusLabels", "kind": kind}) + SHARED.emitStatusLabelsChanged(self, kind) self._tree.refreshAllItems() return diff --git a/novelwriter/core/status.py b/novelwriter/core/status.py index 7935e5aa2..358f9ced9 100644 --- a/novelwriter/core/status.py +++ b/novelwriter/core/status.py @@ -66,7 +66,7 @@ def duplicate(cls, source: StatusEntry) -> StatusEntry: NO_ENTRY = StatusEntry("", QColor(0, 0, 0), nwStatusShape.SQUARE, QIcon(), 0) T_UpdateEntry = list[tuple[str | None, StatusEntry]] -T_StatusKinds = Literal["s", "i"] +T_StatusKind = Literal["s", "i"] class NWStatus: @@ -76,7 +76,7 @@ class NWStatus: __slots__ = ("_store", "_default", "_prefix", "_height") - def __init__(self, prefix: T_StatusKinds) -> None: + def __init__(self, prefix: T_StatusKind) -> None: self._store: dict[str, StatusEntry] = {} self._default = None self._prefix = prefix[:1] diff --git a/novelwriter/core/tree.py b/novelwriter/core/tree.py index 1f919542f..a39b289eb 100644 --- a/novelwriter/core/tree.py +++ b/novelwriter/core/tree.py @@ -469,13 +469,9 @@ def _itemChange(self, item: NWItem, change: nwChange) -> None: tHandle = item.itemHandle logger.debug("Item change: %s -> %s", tHandle, change.name) self._project.setProjectChanged(True) - SHARED.projectSignalProxy( - {"event": "itemChanged", "handle": tHandle, "change": change} - ) + SHARED.emitProjectItemChanged(self._project, tHandle, change) if item.isRootType(): - SHARED.projectSignalProxy( - {"event": "rootChanged", "handle": tHandle, "change": change} - ) + SHARED.emitRootFolderChanged(self._project, tHandle, change) return def _getTrashNode(self) -> ProjectNode | None: diff --git a/novelwriter/shared.py b/novelwriter/shared.py index b3a1b04f3..3b0cae22a 100644 --- a/novelwriter/shared.py +++ b/novelwriter/shared.py @@ -42,6 +42,7 @@ if TYPE_CHECKING: # pragma: no cover from novelwriter.core.project import NWProject + from novelwriter.core.status import T_StatusKind from novelwriter.gui.theme import GuiTheme from novelwriter.guimain import GuiMain @@ -314,35 +315,45 @@ def createNewNote(self, tag: str, itemClass: nwItemClass) -> None: return ## - # Signal Proxy + # Signal Proxies ## - def indexSignalProxy(self, data: dict) -> None: - """Emit signals on behalf of the index.""" - event = data.get("event") - logger.debug("Signal Proxy: %s", event) - if event == "updateTags": - self.indexChangedTags.emit(data.get("updated", []), data.get("deleted", [])) - elif event == "clearIndex": + def emitIndexChangedTags( + self, project: NWProject, updated: list[str], deleted: list[str] + ) -> None: + """Emit the indexChangedTags signal.""" + if self._project and self._project.data.uuid == project.data.uuid: + self.indexChangedTags.emit(updated, deleted) + return + + def emitIndexCleared(self, project: NWProject) -> None: + """Emit the indexCleared signal.""" + if self._project and self._project.data.uuid == project.data.uuid: self.indexCleared.emit() - elif event == "buildIndex": + return + + def emitIndexAvailable(self, project: NWProject) -> None: + """Emit the indexAvailable signal.""" + if self._project and self._project.data.uuid == project.data.uuid: self.indexAvailable.emit() return - def projectSignalProxy(self, data: dict) -> None: - """Emit signals on project data change.""" - event = data.get("event") - logger.debug("Signal Proxy: %s", event) - if event == "statusLabels": - self.statusLabelsChanged.emit(data.get("kind", "")) - elif event == "itemChanged": - self.projectItemChanged.emit( - data.get("handle", ""), data.get("change", nwChange.UPDATE) - ) - elif event == "rootChanged": - self.rootFolderChanged.emit( - data.get("handle", ""), data.get("change", nwChange.UPDATE) - ) + def emitStatusLabelsChanged(self, project: NWProject, kind: T_StatusKind) -> None: + """Emit the statusLabelsChanged signal.""" + if self._project and self._project.data.uuid == project.data.uuid: + self.statusLabelsChanged.emit(kind) + return + + def emitProjectItemChanged(self, project: NWProject, handle: str, change: nwChange) -> None: + """Emit the projectItemChanged signal.""" + if self._project and self._project.data.uuid == project.data.uuid: + self.projectItemChanged.emit(handle, change) + return + + def emitRootFolderChanged(self, project: NWProject, handle: str, change: nwChange) -> None: + """Emit the rootFolderChanged signal.""" + if self._project and self._project.data.uuid == project.data.uuid: + self.rootFolderChanged.emit(handle, change) return ## diff --git a/tests/test_core/test_core_index.py b/tests/test_core/test_core_index.py index 2b64273be..21e42f3ef 100644 --- a/tests/test_core/test_core_index.py +++ b/tests/test_core/test_core_index.py @@ -153,6 +153,7 @@ def testCoreIndex_LoadSave(qtbot, monkeypatch, prjLipsum, mockGUI, tstPaths): assert "7a992350f3eb6" in index._itemIndex # Close Project + SHARED._project = project # Otherwise the signal is not emitted with qtbot.waitSignal(SHARED.indexCleared, timeout=1000): # Regression test for issue #1718 project.closeProject()