From 7c391eb66060033b8c54bbcfaaf99d6e8e79f843 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sat, 4 May 2019 21:39:05 +0200 Subject: [PATCH] Fixed some bits and added spellcheck to the highlighting class using pyenchant --- README.md | 1 + nw/gui/doceditor.py | 8 ++++++-- nw/gui/dochighlight.py | 29 ++++++++++++++++++++++++----- nw/gui/doctree.py | 1 + 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b8b657f6..2120bb20 100644 --- a/README.md +++ b/README.md @@ -35,3 +35,4 @@ Future features that will be added: * python3-pyqt5 * python3-appdirs * python3-lxml +* python3-enchant diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 2e9f1297..e17a3090 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -12,6 +12,7 @@ import logging import nw +import enchant from time import time @@ -49,6 +50,11 @@ class GuiDocEditor(QTextEdit): self.typSQClose = self.mainConf.fmtSingleQuotes[1] self.typApos = self.mainConf.fmtApostrophe + # Core Elements + self.theDoc = self.document() + self.theDict = enchant.Dict("en_GB") + self.hLight = GuiDocHighlighter(self.theDoc, self.theDict) + # Editor State self.hasSelection = False @@ -60,8 +66,6 @@ class GuiDocEditor(QTextEdit): mLR = self.mainConf.textMargin[1] self.setViewportMargins(mLR,mTB,mLR,mTB) - self.theDoc = self.document() - self.hLight = GuiDocHighlighter(self.theDoc) self.theDoc.setDocumentMargin(0) self.theDoc.contentsChange.connect(self._docChange) if self.mainConf.doJustify: diff --git a/nw/gui/dochighlight.py b/nw/gui/dochighlight.py index 3a931b22..e6319d51 100644 --- a/nw/gui/dochighlight.py +++ b/nw/gui/dochighlight.py @@ -13,19 +13,20 @@ import logging import nw -from PyQt5.QtCore import QRegularExpression +from PyQt5.QtCore import QRegularExpression, Qt from PyQt5.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter logger = logging.getLogger(__name__) class GuiDocHighlighter(QSyntaxHighlighter): - def __init__(self, theDoc): + def __init__(self, theDoc, theDict): QSyntaxHighlighter.__init__(self, theDoc) logger.debug("Initialising DocHighlighter ...") self.mainConf = nw.CONFIG self.theDoc = theDoc + self.theDict = theDict self.hRules = [] self.colHead = ( 0,155,200,255) @@ -38,6 +39,8 @@ class GuiDocHighlighter(QSyntaxHighlighter): self.colKey = (200, 46, 0,255) self.colVal = (184,200, 0,255) + self.colSpell = QColor(200,46,0) + self.hStyles = { "header1" : self._makeFormat(self.colHead, "bold",1.8), "header2" : self._makeFormat(self.colHead, "bold",1.6), @@ -141,7 +144,8 @@ class GuiDocHighlighter(QSyntaxHighlighter): )) # Build a QRegExp for each pattern - self.rules = [(QRegularExpression(a),b) for (a,b) in self.hRules] + self.rules = [(QRegularExpression(a),b) for (a,b) in self.hRules] + self.spellRx = QRegularExpression(r"[\w\'{:s}]+".format(self.mainConf.fmtSingleQuotes[1])) logger.debug("DocHighlighter initialisation complete") @@ -174,6 +178,7 @@ class GuiDocHighlighter(QSyntaxHighlighter): return theFormat def highlightBlock(self, theText): + for rX, xFmt in self.rules: rxItt = rX.globalMatch(theText, 0) while rxItt.hasNext(): @@ -181,10 +186,24 @@ class GuiDocHighlighter(QSyntaxHighlighter): for xM in xFmt.keys(): xPos = rxMatch.capturedStart(xM) xLen = rxMatch.capturedLength(xM) - # logger.verbose("Captured[%d]: '%s'" % (nth,rxMatch.captured(nth))) - # print(rxMatch.capturedTexts()) self.setFormat(xPos, xLen, xFmt[xM]) self.setCurrentBlockState(0) + if self.theDict is None: + return + + rxSpell = self.spellRx.globalMatch(theText, 0) + while rxSpell.hasNext(): + rxMatch = rxSpell.next() + if not self.theDict.check(rxMatch.captured(0)): + xPos = rxMatch.capturedStart(0) + xLen = rxMatch.capturedLength(0) + spFmt = self.format(xPos) + spFmt.setUnderlineColor(self.colSpell) + spFmt.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) + self.setFormat(xPos, xLen, spFmt) + + return + # END Class DocHighlighter diff --git a/nw/gui/doctree.py b/nw/gui/doctree.py index e1f2c8f9..deec592b 100644 --- a/nw/gui/doctree.py +++ b/nw/gui/doctree.py @@ -368,6 +368,7 @@ class GuiDocTree(QTreeWidget): self.addTopLevelItem(newItem) self.orphRoot = newItem newItem.setExpanded(True) + newItem.setIcon(self.C_NAME, QIcon.fromTheme("dialog-warning")) return def _cleanOrphanedRoot(self):