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)
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)
+7 -9
View File
@@ -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
+11 -1
View File
@@ -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
+3 -2
View File
@@ -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
+1
View File
@@ -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)