From 24ab6f2d3beaad64aed46105e57556b8a4974aec Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 16 May 2021 00:19:40 +0200 Subject: [PATCH] Make the editor dictionary internal --- nw/core/spellcheck.py | 6 +++--- nw/dialogs/projsettings.py | 2 +- nw/gui/doceditor.py | 34 +++++++++++++++++++++------------- nw/gui/statusbar.py | 7 ++++--- nw/guimain.py | 3 +++ 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/nw/core/spellcheck.py b/nw/core/spellcheck.py index 6ea8bb92..8b148426 100644 --- a/nw/core/spellcheck.py +++ b/nw/core/spellcheck.py @@ -152,7 +152,7 @@ class NWSpellEnchant(NWSpellCheck): except Exception: logger.error("Failed to load enchant spell checking for language %s" % theLang) - self.theDict = NWSpellEnchantDummy() + self.theDict = FakeEnchant() self.spellLanguage = None self._readProjectDictionary(projectDict) @@ -208,7 +208,7 @@ class NWSpellEnchant(NWSpellCheck): # END Class NWSpellEnchant -class NWSpellEnchantDummy: +class FakeEnchant: """Fallback for when Enchant is selected, but not installed. """ def __init__(self): @@ -223,7 +223,7 @@ class NWSpellEnchantDummy: def add_to_session(self, theWord): return -# END Class NWSpellEnchantDummy +# END Class FakeEnchant # =============================================================================================== # # Fallback SpellChecking Using difflib diff --git a/nw/dialogs/projsettings.py b/nw/dialogs/projsettings.py index 38952eed..014fd32b 100644 --- a/nw/dialogs/projsettings.py +++ b/nw/dialogs/projsettings.py @@ -207,7 +207,7 @@ class GuiProjectEditMain(QWidget): self.spellLang = QComboBox(self) self.spellLang.setMaximumWidth(xW) - theDict = self.theParent.docEditor.theDict + theDict = self.theParent.docEditor.currentDictionary() self.spellLang.addItem(self.tr("Default"), "None") if theDict is not None: for spTag, spProv in theDict.listDictionaries(): diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 6736accb..ef0e685f 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -64,6 +64,9 @@ class GuiDocEditor(QTextEdit): Qt.Key_PageUp, Qt.Key_PageDown ) + # Custom Signals + spellDictionaryChanged = pyqtSignal(str, str) + def __init__(self, theParent): QTextEdit.__init__(self, theParent) @@ -84,7 +87,7 @@ class GuiDocEditor(QTextEdit): self._docHeaders = [] # Record of headers in the file self._spellCheck = False # Flag for spell checking enabled - self.theDict = None # The current spell check dictionary + self._theDict = None # The current spell check dictionary self._nonWord = "\"'" # Characters to not include in spell checking # Document Variables @@ -579,6 +582,11 @@ class GuiDocEditor(QTextEdit): """ return self._qDocument.isEmpty() + def currentDictionary(self): + """Return the current dictionary object. + """ + return self._theDict + ## # Getters ## @@ -670,10 +678,10 @@ class GuiDocEditor(QTextEdit): else: theLang = self.theProject.projSpell - self.theDict.setLanguage(theLang, self.theProject.projDict) - theTag, theProvider = self.theDict.describeDict() + self._theDict.setLanguage(theLang, self.theProject.projDict) + _, theProvider = self._theDict.describeDict() - self.theParent.statusBar.setLanguage(theLang, theProvider) + self.spellDictionaryChanged.emit(theLang, theProvider) if not self._bigDoc: self.spellCheckDocument() @@ -689,7 +697,7 @@ class GuiDocEditor(QTextEdit): if theMode is None: theMode = not self._spellCheck - if self.theDict.spellLanguage is None: + if self._theDict.spellLanguage is None: theMode = False self._spellCheck = theMode @@ -987,7 +995,7 @@ class GuiDocEditor(QTextEdit): return ## - # Signals and Slots + # Slots ## @pyqtSlot(int, int, int) @@ -1092,14 +1100,14 @@ class GuiDocEditor(QTextEdit): if spellCheck: logger.verbose("Looking up '%s' in the dictionary" % theWord) - spellCheck &= not self.theDict.checkWord(theWord) + spellCheck &= not self._theDict.checkWord(theWord) if spellCheck: mnuContext.addSeparator() mnuHead = QAction(self.tr("Spelling Suggestion(s)"), mnuContext) mnuContext.addAction(mnuHead) - theSuggest = self.theDict.suggestWords(theWord)[:15] + theSuggest = self._theDict.suggestWords(theWord)[:15] if len(theSuggest) > 0: for aWord in theSuggest: mnuWord = QAction("%s %s" % (nwUnicode.U_ENDASH, aWord), mnuContext) @@ -1144,8 +1152,8 @@ class GuiDocEditor(QTextEdit): """ theWord = theCursor.selectedText().strip().strip(self._nonWord) logger.debug("Added '%s' to project dictionary" % theWord) - self.theDict.addWord(theWord) - self.hLight.setDict(self.theDict) + self._theDict.addWord(theWord) + self.hLight.setDict(self._theDict) self.hLight.rehighlightBlock(theCursor.block()) return @@ -1816,11 +1824,11 @@ class GuiDocEditor(QTextEdit): """ if self.mainConf.spellTool == nwConst.SP_ENCHANT: from nw.core.spellcheck import NWSpellEnchant - self.theDict = NWSpellEnchant() + self._theDict = NWSpellEnchant() else: - self.theDict = NWSpellSimple() + self._theDict = NWSpellSimple() - self.hLight.setDict(self.theDict) + self.hLight.setDict(self._theDict) return diff --git a/nw/gui/statusbar.py b/nw/gui/statusbar.py index 4691202c..d3490bf4 100644 --- a/nw/gui/statusbar.py +++ b/nw/gui/statusbar.py @@ -29,7 +29,7 @@ import logging from time import time -from PyQt5.QtCore import QLocale +from PyQt5.QtCore import QLocale, pyqtSlot from PyQt5.QtGui import QColor, QPainter from PyQt5.QtWidgets import qApp, QStatusBar, QLabel, QAbstractButton @@ -123,7 +123,7 @@ class GuiMainStatus(QStatusBar): """Reset all widgets on the status bar to default values. """ self.setRefTime(None) - self.setLanguage(None) + self.setLanguage(None, "") self.setStats(0, 0) self.setProjectStatus(None) self.setDocumentStatus(None) @@ -147,7 +147,8 @@ class GuiMainStatus(QStatusBar): qApp.processEvents() return - def setLanguage(self, theLanguage, theProvider=""): + @pyqtSlot(str, str) + def setLanguage(self, theLanguage, theProvider): """Set the language code for the spell checker. """ if theLanguage is None: diff --git a/nw/guimain.py b/nw/guimain.py index 863553d9..491671bb 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -119,6 +119,9 @@ class GuiMain(QMainWindow): self.projMeta = GuiOutlineDetails(self) self.mainMenu = GuiMainMenu(self) + # Signals Between Main Elements + self.docEditor.spellDictionaryChanged.connect(self.statusBar.setLanguage) + # Minor GUI Elements self.statusIcons = [] self.importIcons = []