Move all handling of spell check instance to shared class

This commit is contained in:
Veronica Berglyd Olsen
2023-08-24 22:33:53 +02:00
parent b54f81355a
commit a72c824d8b
7 changed files with 23 additions and 45 deletions
+1 -5
View File
@@ -97,6 +97,7 @@ class NWSpellEnchant:
if self._enchant is None: if self._enchant is None:
self._enchant = FakeEnchant() self._enchant = FakeEnchant()
else: else:
self._userDict.load()
for word in self._userDict: for word in self._userDict:
self._enchant.add_to_session(word) self._enchant.add_to_session(word)
@@ -136,11 +137,6 @@ class NWSpellEnchant:
return added return added
def loadUserWordList(self) -> None:
"""Load the user word list from the project."""
self._userDict.load()
return
def listDictionaries(self) -> list[tuple[str, str]]: def listDictionaries(self) -> list[tuple[str, str]]:
"""Wrapper function for pyenchant.""" """Wrapper function for pyenchant."""
retList = [] retList = []
+1 -10
View File
@@ -89,9 +89,6 @@ class GuiProjectSettings(NPagedDialog):
self.buttonBox.rejected.connect(self._doClose) self.buttonBox.rejected.connect(self._doClose)
self.addControls(self.buttonBox) self.addControls(self.buttonBox)
# Flags
self._spellChanged = False
# Focus Tab # Focus Tab
self._focusTab(focusTab) self._focusTab(focusTab)
@@ -103,10 +100,6 @@ class GuiProjectSettings(NPagedDialog):
logger.debug("Delete: GuiProjectSettings") logger.debug("Delete: GuiProjectSettings")
return return
@property
def spellChanged(self):
return self._spellChanged
## ##
# Slots # Slots
## ##
@@ -125,9 +118,7 @@ class GuiProjectSettings(NPagedDialog):
project.data.setTitle(bookTitle) project.data.setTitle(bookTitle)
project.data.setAuthor(bookAuthor) project.data.setAuthor(bookAuthor)
project.data.setDoBackup(doBackup) project.data.setDoBackup(doBackup)
project.data.setSpellLang(spellLang)
# Remember this as updating spell dictionary can be expensive
self._spellChanged = project.data.setSpellLang(spellLang)
if self.tabStatus.colChanged: if self.tabStatus.colChanged:
newList, delList = self.tabStatus.getNewList() newList, delList = self.tabStatus.getNewList()
+1 -20
View File
@@ -73,7 +73,6 @@ class GuiDocEditor(QTextEdit):
statusMessage = pyqtSignal(str) statusMessage = pyqtSignal(str)
docCountsChanged = pyqtSignal(str, int, int, int) docCountsChanged = pyqtSignal(str, int, int, int)
editedStatusChanged = pyqtSignal(bool) editedStatusChanged = pyqtSignal(bool)
spellDictionaryChanged = pyqtSignal(str, str)
loadDocumentTagRequest = pyqtSignal(str, Enum) loadDocumentTagRequest = pyqtSignal(str, Enum)
novelStructureChanged = pyqtSignal() novelStructureChanged = pyqtSignal()
novelItemMetaChanged = pyqtSignal(str) novelItemMetaChanged = pyqtSignal(str)
@@ -301,7 +300,7 @@ class GuiDocEditor(QTextEdit):
self._typPadAfter = CONFIG.fmtPadAfter self._typPadAfter = CONFIG.fmtPadAfter
# Reload spell check and dictionaries # Reload spell check and dictionaries
self.setDictionaries() SHARED.updateSpellCheckLanguage()
# Set font # Set font
textFont = QFont() textFont = QFont()
@@ -691,24 +690,6 @@ class GuiDocEditor(QTextEdit):
# Spell Checking # Spell Checking
## ##
def setDictionaries(self):
"""Set the spell checker dictionary language, and emit the
dictionary changed signal.
"""
if SHARED.project.data.spellLang is None:
theLang = CONFIG.spellLanguage
else:
theLang = SHARED.project.data.spellLang
SHARED.spelling.setLanguage(theLang)
_, theProvider = SHARED.spelling.describeDict()
self.spellDictionaryChanged.emit(str(theLang), str(theProvider))
if not self._bigDoc:
self.spellCheckDocument()
return True
def toggleSpellCheck(self, state: bool) -> None: def toggleSpellCheck(self, state: bool) -> None:
"""This is the main spell check setting function, and this one """This is the main spell check setting function, and this one
should call all other setSpellCheck functions in other classes. should call all other setSpellCheck functions in other classes.
+1 -1
View File
@@ -121,7 +121,7 @@ class GuiMainStatus(QStatusBar):
def clearStatus(self) -> None: def clearStatus(self) -> None:
"""Reset all widgets on the status bar to default values.""" """Reset all widgets on the status bar to default values."""
self.setRefTime(-1.0) self.setRefTime(-1.0)
self.setLanguage(None, "") self.setLanguage(*SHARED.spelling.describeDict())
self.setProjectStats(0, 0) self.setProjectStats(0, 0)
self.setProjectStatus(StatusLED.S_NONE) self.setProjectStatus(StatusLED.S_NONE)
self.setDocumentStatus(StatusLED.S_NONE) self.setDocumentStatus(StatusLED.S_NONE)
+3 -6
View File
@@ -234,6 +234,7 @@ class GuiMain(QMainWindow):
SHARED.projectStatusChanged.connect(self.mainStatus.updateProjectStatus) SHARED.projectStatusChanged.connect(self.mainStatus.updateProjectStatus)
SHARED.projectStatusMessage.connect(self.mainStatus.setStatusMessage) SHARED.projectStatusMessage.connect(self.mainStatus.setStatusMessage)
SHARED.spellLanguageChanged.connect(self.mainStatus.setLanguage)
self.viewsBar.viewChangeRequested.connect(self._changeView) self.viewsBar.viewChangeRequested.connect(self._changeView)
@@ -251,7 +252,6 @@ class GuiMain(QMainWindow):
self.novelView.selectedItemChanged.connect(self.itemDetails.updateViewBox) self.novelView.selectedItemChanged.connect(self.itemDetails.updateViewBox)
self.novelView.openDocumentRequest.connect(self._openDocument) self.novelView.openDocumentRequest.connect(self._openDocument)
self.docEditor.spellDictionaryChanged.connect(self.mainStatus.setLanguage)
self.docEditor.editedStatusChanged.connect(self.mainStatus.updateDocumentStatus) self.docEditor.editedStatusChanged.connect(self.mainStatus.updateDocumentStatus)
self.docEditor.docCountsChanged.connect(self.itemDetails.updateCounts) self.docEditor.docCountsChanged.connect(self.itemDetails.updateCounts)
self.docEditor.docCountsChanged.connect(self.projView.updateCounts) self.docEditor.docCountsChanged.connect(self.projView.updateCounts)
@@ -428,7 +428,6 @@ class GuiMain(QMainWindow):
SHARED.closeProject() SHARED.closeProject()
self.docEditor.setDictionaries()
self._updateWindowTitle() self._updateWindowTitle()
self._changeView(nwView.PROJECT) self._changeView(nwView.PROJECT)
@@ -489,7 +488,6 @@ class GuiMain(QMainWindow):
# Update GUI # Update GUI
self._updateWindowTitle(SHARED.project.data.name) self._updateWindowTitle(SHARED.project.data.name)
self.rebuildTrees() self.rebuildTrees()
self.docEditor.setDictionaries()
self.docEditor.toggleSpellCheck(SHARED.project.data.spellCheck) self.docEditor.toggleSpellCheck(SHARED.project.data.spellCheck)
self.mainStatus.setRefTime(SHARED.project.projOpened) self.mainStatus.setRefTime(SHARED.project.projOpened)
self.projView.openProjectTasks() self.projView.openProjectTasks()
@@ -907,8 +905,7 @@ class GuiMain(QMainWindow):
if dlgProj.result() == QDialog.Accepted: if dlgProj.result() == QDialog.Accepted:
logger.debug("Applying new project settings") logger.debug("Applying new project settings")
if dlgProj.spellChanged: SHARED.updateSpellCheckLanguage()
self.docEditor.setDictionaries()
self.itemDetails.refreshDetails() self.itemDetails.refreshDetails()
self._updateWindowTitle(SHARED.project.data.name) self._updateWindowTitle(SHARED.project.data.name)
@@ -982,7 +979,7 @@ class GuiMain(QMainWindow):
if dlgWords.result() == QDialog.Accepted: if dlgWords.result() == QDialog.Accepted:
logger.debug("Reloading word list") logger.debug("Reloading word list")
self.docEditor.setDictionaries() SHARED.updateSpellCheckLanguage(reload=True)
return True return True
+16 -1
View File
@@ -65,6 +65,10 @@ class SharedData(QObject):
self._idleRefTime = time() self._idleRefTime = time()
return return
##
# Properties
##
@property @property
def mainGui(self) -> GuiMain: def mainGui(self) -> GuiMain:
"""Return the Main GUI instance.""" """Return the Main GUI instance."""
@@ -141,7 +145,7 @@ class SharedData(QObject):
self._lockedBy = self.project.lockStatus self._lockedBy = self.project.lockStatus
self._resetProject() self._resetProject()
self.spelling.loadUserWordList() self.updateSpellCheckLanguage(reload=True)
self._resetIdleTimer() self._resetIdleTimer()
return status return status
@@ -160,6 +164,16 @@ class SharedData(QObject):
self._resetIdleTimer() self._resetIdleTimer()
return return
def updateSpellCheckLanguage(self, reload: bool = False) -> None:
"""Update the active spell check langauge from settings."""
from novelwriter import CONFIG
language = self.project.data.spellLang or CONFIG.spellLanguage
if language != self.spelling.spellLanguage or reload:
self.spelling.setLanguage(language)
_, provider = self.spelling.describeDict()
self.spellLanguageChanged.emit(language, provider)
return
def updateIdleTime(self, currTime: float, userIdle: bool) -> None: def updateIdleTime(self, currTime: float, userIdle: bool) -> None:
"""Update the idle time record. If the userIdle flag is True, """Update the idle time record. If the userIdle flag is True,
the user idle counter is updated with the time difference since the user idle counter is updated with the time difference since
@@ -239,6 +253,7 @@ class SharedData(QObject):
del self._spelling del self._spelling
self._project = NWProject() self._project = NWProject()
self._spelling = NWSpellEnchant(self._project) self._spelling = NWSpellEnchant(self._project)
self.updateSpellCheckLanguage()
return return
def _resetIdleTimer(self) -> None: def _resetIdleTimer(self) -> None:
@@ -43,7 +43,6 @@ def testDlgProjSettings_Dialog(qtbot, monkeypatch, nwGUI):
# Block the GUI blocking thread # Block the GUI blocking thread
monkeypatch.setattr(GuiProjectSettings, "exec_", lambda *a: None) monkeypatch.setattr(GuiProjectSettings, "exec_", lambda *a: None)
monkeypatch.setattr(GuiProjectSettings, "result", lambda *a: QDialog.Accepted) monkeypatch.setattr(GuiProjectSettings, "result", lambda *a: QDialog.Accepted)
monkeypatch.setattr(GuiProjectSettings, "spellChanged", lambda *a: True)
# Check that we cannot open when there is no project # Check that we cannot open when there is no project
nwGUI.mainMenu.aProjectSettings.activate(QAction.Trigger) nwGUI.mainMenu.aProjectSettings.activate(QAction.Trigger)
@@ -129,7 +128,6 @@ def testDlgProjSettings_Main(qtbot, monkeypatch, nwGUI, fncPath, projPath, mockR
assert tabMain.editName.text() == "Project Name" assert tabMain.editName.text() == "Project Name"
assert tabMain.editTitle.text() == "Project Title" assert tabMain.editTitle.text() == "Project Title"
assert tabMain.editAuthor.text() == "Jane Doe" assert tabMain.editAuthor.text() == "Jane Doe"
assert projSettings.spellChanged is False
projSettings._doSave() projSettings._doSave()
assert theProject.data.name == "Project Name" assert theProject.data.name == "Project Name"