Improve handling of spell check state

This commit is contained in:
Veronica Berglyd Olsen
2023-09-14 16:26:05 +02:00
parent 309c7de5de
commit c3a4b840fb
5 changed files with 35 additions and 28 deletions
+13 -16
View File
@@ -80,6 +80,7 @@ class GuiDocEditor(QPlainTextEdit):
loadDocumentTagRequest = pyqtSignal(str, Enum) loadDocumentTagRequest = pyqtSignal(str, Enum)
novelStructureChanged = pyqtSignal() novelStructureChanged = pyqtSignal()
novelItemMetaChanged = pyqtSignal(str) novelItemMetaChanged = pyqtSignal(str)
spellCheckStateChanged = pyqtSignal(bool)
def __init__(self, mainGui: GuiMain) -> None: def __init__(self, mainGui: GuiMain) -> None:
super().__init__(parent=mainGui) super().__init__(parent=mainGui)
@@ -95,7 +96,6 @@ class GuiDocEditor(QPlainTextEdit):
self._docChanged = False # Flag for changed status of document self._docChanged = False # Flag for changed status of document
self._docHandle = None # The handle of the open 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._nonWord = "\"'" # Characters to not include in spell checking
self._vpMargin = 0 # The editor viewport margin, set during init self._vpMargin = 0 # The editor viewport margin, set during init
@@ -125,6 +125,7 @@ class GuiDocEditor(QPlainTextEdit):
# Connect Signals # Connect Signals
self._qDocument.contentsChange.connect(self._docChange) self._qDocument.contentsChange.connect(self._docChange)
self.selectionChanged.connect(self._updateSelectedStatus) self.selectionChanged.connect(self._updateSelectedStatus)
self.spellCheckStateChanged.connect(self._qDocument.setSpellCheckState)
# Document Title # Document Title
self.docHeader = GuiDocEditHeader(self) self.docHeader = GuiDocEditHeader(self)
@@ -609,25 +610,21 @@ class GuiDocEditor(QPlainTextEdit):
current status saved in this class. current status saved in this class.
""" """
if state is None: if state is None:
state = not self._spellCheck state = not SHARED.project.data.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
if SHARED.spelling.spellLanguage is None: if SHARED.spelling.spellLanguage is None:
state = False state = False
self._spellCheck = state if state and not CONFIG.hasEnchant:
self.mainGui.mainMenu.setSpellCheck(state) SHARED.info(self.tr(
"Spell checking requires the package PyEnchant. "
"It does not appear to be installed."
))
state = False
SHARED.project.data.setSpellCheck(state) SHARED.project.data.setSpellCheck(state)
self._qDocument.syntaxHighlighter.setSpellCheck(state) self.spellCheckStateChanged.emit(state)
if state is False: self.spellCheckDocument()
self.spellCheckDocument()
logger.debug("Spell check is set to '%s'", str(state)) 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)) aSPar.triggered.connect(lambda: self._makePosSelection(QTextCursor.BlockUnderCursor, pos))
# Spell Checking # Spell Checking
if self._spellCheck: if SHARED.project.data.spellCheck:
word, cPos, cLen, suggest = self._qDocument.spellErrorAtPos(pCursor.position()) word, cPos, cLen, suggest = self._qDocument.spellErrorAtPos(pCursor.position())
if word and cPos >= 0 and cLen > 0: if word and cPos >= 0 and cLen > 0:
logger.debug("Word '%s' is misspelled", word) logger.debug("Word '%s' is misspelled", word)
+7 -9
View File
@@ -376,20 +376,18 @@ class GuiDocHighlighter(QSyntaxHighlighter):
spFmt.merge(xFmt[xM]) spFmt.merge(xFmt[xM])
self.setFormat(x, 1, spFmt) self.setFormat(x, 1, spFmt)
if not self._spellCheck:
return
data = self.currentBlockUserData() data = self.currentBlockUserData()
if not isinstance(data, TextBlockData): if not isinstance(data, TextBlockData):
data = TextBlockData() data = TextBlockData()
self.setCurrentBlockUserData(data) self.setCurrentBlockUserData(data)
for xPos, xLen in data.spellCheck(text): if self._spellCheck:
for x in range(xPos, xPos+xLen): for xPos, xLen in data.spellCheck(text):
spFmt = self.format(x) for x in range(xPos, xPos+xLen):
spFmt.setUnderlineColor(self._colSpell) spFmt = self.format(x)
spFmt.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) spFmt.setUnderlineColor(self._colSpell)
self.setFormat(x, 1, spFmt) spFmt.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline)
self.setFormat(x, 1, spFmt)
return return
+11 -1
View File
@@ -28,7 +28,7 @@ import logging
from time import time from time import time
from PyQt5.QtGui import QTextCursor, QTextDocument from PyQt5.QtGui import QTextCursor, QTextDocument
from PyQt5.QtCore import QObject from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtWidgets import QPlainTextDocumentLayout, qApp from PyQt5.QtWidgets import QPlainTextDocumentLayout, qApp
from novelwriter import SHARED from novelwriter import SHARED
@@ -113,4 +113,14 @@ class GuiTextDocument(QTextDocument):
return word, cPos, cLen, SHARED.spelling.suggestWords(word) return word, cPos, cLen, SHARED.spelling.suggestWords(word)
return "", -1, -1, [] 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 # END Class GuiTextDocument
+3 -2
View File
@@ -78,10 +78,11 @@ class GuiMainMenu(QMenuBar):
return 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.""" """Forward spell check check state to its action."""
self.aSpellCheck.setChecked(state) self.aSpellCheck.setChecked(state)
return return
+1
View File
@@ -266,6 +266,7 @@ class GuiMain(QMainWindow):
self.docEditor.novelStructureChanged.connect(self.novelView.refreshTree) self.docEditor.novelStructureChanged.connect(self.novelView.refreshTree)
self.docEditor.novelItemMetaChanged.connect(self.novelView.updateNovelItemMeta) self.docEditor.novelItemMetaChanged.connect(self.novelView.updateNovelItemMeta)
self.docEditor.statusMessage.connect(self.mainStatus.setStatusMessage) self.docEditor.statusMessage.connect(self.mainStatus.setStatusMessage)
self.docEditor.spellCheckStateChanged.connect(self.mainMenu.setSpellCheckState)
self.docViewer.loadDocumentTagRequest.connect(self._followTag) self.docViewer.loadDocumentTagRequest.connect(self._followTag)