Skip to content

Commit

Permalink
Refactor project signal proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo committed Nov 23, 2024
1 parent 1bf42ce commit c1ed064
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 47 deletions.
17 changes: 5 additions & 12 deletions novelwriter/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions novelwriter/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions novelwriter/core/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]
Expand Down
8 changes: 2 additions & 6 deletions novelwriter/core/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
57 changes: 34 additions & 23 deletions novelwriter/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

##
Expand Down
1 change: 1 addition & 0 deletions tests/test_core/test_core_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c1ed064

Please sign in to comment.