From c3a4b840fb69b0347b286691c400d7eab5efa3f0 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 14 Sep 2023 16:26:05 +0200 Subject: [PATCH] Improve handling of spell check state --- novelwriter/gui/doceditor.py | 29 +++++++++++++---------------- novelwriter/gui/dochighlight.py | 16 +++++++--------- novelwriter/gui/editordocument.py | 12 +++++++++++- novelwriter/gui/mainmenu.py | 5 +++-- novelwriter/guimain.py | 1 + 5 files changed, 35 insertions(+), 28 deletions(-) diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index 6f7ced9f..60c7b00e 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -80,6 +80,7 @@ class GuiDocEditor(QPlainTextEdit): loadDocumentTagRequest = pyqtSignal(str, Enum) novelStructureChanged = pyqtSignal() novelItemMetaChanged = pyqtSignal(str) + spellCheckStateChanged = pyqtSignal(bool) def __init__(self, mainGui: GuiMain) -> None: super().__init__(parent=mainGui) @@ -95,7 +96,6 @@ class GuiDocEditor(QPlainTextEdit): self._docChanged = False # Flag for changed status of document self._docHandle = None # The handle of the open document - self._spellCheck = False # Flag for spell checking enabled self._nonWord = "\"'" # Characters to not include in spell checking self._vpMargin = 0 # The editor viewport margin, set during init @@ -125,6 +125,7 @@ class GuiDocEditor(QPlainTextEdit): # Connect Signals self._qDocument.contentsChange.connect(self._docChange) self.selectionChanged.connect(self._updateSelectedStatus) + self.spellCheckStateChanged.connect(self._qDocument.setSpellCheckState) # Document Title self.docHeader = GuiDocEditHeader(self) @@ -609,25 +610,21 @@ class GuiDocEditor(QPlainTextEdit): current status saved in this class. """ if state is None: - state = not self._spellCheck - - if not CONFIG.hasEnchant: - if state: - SHARED.info(self.tr( - "Spell checking requires the package PyEnchant. " - "It does not appear to be installed." - )) - state = False + state = not SHARED.project.data.spellCheck if SHARED.spelling.spellLanguage is None: state = False - self._spellCheck = state - self.mainGui.mainMenu.setSpellCheck(state) + if state and not CONFIG.hasEnchant: + SHARED.info(self.tr( + "Spell checking requires the package PyEnchant. " + "It does not appear to be installed." + )) + state = False + SHARED.project.data.setSpellCheck(state) - self._qDocument.syntaxHighlighter.setSpellCheck(state) - if state is False: - self.spellCheckDocument() + self.spellCheckStateChanged.emit(state) + self.spellCheckDocument() logger.debug("Spell check is set to '%s'", str(state)) @@ -1021,7 +1018,7 @@ class GuiDocEditor(QPlainTextEdit): aSPar.triggered.connect(lambda: self._makePosSelection(QTextCursor.BlockUnderCursor, pos)) # Spell Checking - if self._spellCheck: + if SHARED.project.data.spellCheck: word, cPos, cLen, suggest = self._qDocument.spellErrorAtPos(pCursor.position()) if word and cPos >= 0 and cLen > 0: logger.debug("Word '%s' is misspelled", word) diff --git a/novelwriter/gui/dochighlight.py b/novelwriter/gui/dochighlight.py index 9bc8d953..64d1f34e 100644 --- a/novelwriter/gui/dochighlight.py +++ b/novelwriter/gui/dochighlight.py @@ -376,20 +376,18 @@ class GuiDocHighlighter(QSyntaxHighlighter): spFmt.merge(xFmt[xM]) self.setFormat(x, 1, spFmt) - if not self._spellCheck: - return - data = self.currentBlockUserData() if not isinstance(data, TextBlockData): data = TextBlockData() self.setCurrentBlockUserData(data) - for xPos, xLen in data.spellCheck(text): - for x in range(xPos, xPos+xLen): - spFmt = self.format(x) - spFmt.setUnderlineColor(self._colSpell) - spFmt.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) - self.setFormat(x, 1, spFmt) + if self._spellCheck: + for xPos, xLen in data.spellCheck(text): + for x in range(xPos, xPos+xLen): + spFmt = self.format(x) + spFmt.setUnderlineColor(self._colSpell) + spFmt.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) + self.setFormat(x, 1, spFmt) return diff --git a/novelwriter/gui/editordocument.py b/novelwriter/gui/editordocument.py index 793d82bc..160e85ed 100644 --- a/novelwriter/gui/editordocument.py +++ b/novelwriter/gui/editordocument.py @@ -28,7 +28,7 @@ import logging from time import time from PyQt5.QtGui import QTextCursor, QTextDocument -from PyQt5.QtCore import QObject +from PyQt5.QtCore import QObject, pyqtSlot from PyQt5.QtWidgets import QPlainTextDocumentLayout, qApp from novelwriter import SHARED @@ -113,4 +113,14 @@ class GuiTextDocument(QTextDocument): return word, cPos, cLen, SHARED.spelling.suggestWords(word) return "", -1, -1, [] + ## + # Public Slots + ## + + @pyqtSlot(bool) + def setSpellCheckState(self, state: bool) -> None: + """Set the spell check state of the syntax highlighter.""" + self._syntax.setSpellCheck(state) + return + # END Class GuiTextDocument diff --git a/novelwriter/gui/mainmenu.py b/novelwriter/gui/mainmenu.py index 55813a71..45374ada 100644 --- a/novelwriter/gui/mainmenu.py +++ b/novelwriter/gui/mainmenu.py @@ -78,10 +78,11 @@ class GuiMainMenu(QMenuBar): return ## - # Update Menu on Settings Changed + # Public Slots ## - def setSpellCheck(self, state: bool) -> None: + @pyqtSlot(bool) + def setSpellCheckState(self, state: bool) -> None: """Forward spell check check state to its action.""" self.aSpellCheck.setChecked(state) return diff --git a/novelwriter/guimain.py b/novelwriter/guimain.py index b19f585f..4e792a04 100644 --- a/novelwriter/guimain.py +++ b/novelwriter/guimain.py @@ -266,6 +266,7 @@ class GuiMain(QMainWindow): self.docEditor.novelStructureChanged.connect(self.novelView.refreshTree) self.docEditor.novelItemMetaChanged.connect(self.novelView.updateNovelItemMeta) self.docEditor.statusMessage.connect(self.mainStatus.setStatusMessage) + self.docEditor.spellCheckStateChanged.connect(self.mainMenu.setSpellCheckState) self.docViewer.loadDocumentTagRequest.connect(self._followTag)