Fixed some bits and added spellcheck to the highlighting class using pyenchant

This commit is contained in:
Veronica K. B. Olsen
2019-05-04 21:39:05 +02:00
parent e55c92d83a
commit 7c391eb660
4 changed files with 32 additions and 7 deletions
+1
View File
@@ -35,3 +35,4 @@ Future features that will be added:
* python3-pyqt5 * python3-pyqt5
* python3-appdirs * python3-appdirs
* python3-lxml * python3-lxml
* python3-enchant
+6 -2
View File
@@ -12,6 +12,7 @@
import logging import logging
import nw import nw
import enchant
from time import time from time import time
@@ -49,6 +50,11 @@ class GuiDocEditor(QTextEdit):
self.typSQClose = self.mainConf.fmtSingleQuotes[1] self.typSQClose = self.mainConf.fmtSingleQuotes[1]
self.typApos = self.mainConf.fmtApostrophe 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 # Editor State
self.hasSelection = False self.hasSelection = False
@@ -60,8 +66,6 @@ class GuiDocEditor(QTextEdit):
mLR = self.mainConf.textMargin[1] mLR = self.mainConf.textMargin[1]
self.setViewportMargins(mLR,mTB,mLR,mTB) self.setViewportMargins(mLR,mTB,mLR,mTB)
self.theDoc = self.document()
self.hLight = GuiDocHighlighter(self.theDoc)
self.theDoc.setDocumentMargin(0) self.theDoc.setDocumentMargin(0)
self.theDoc.contentsChange.connect(self._docChange) self.theDoc.contentsChange.connect(self._docChange)
if self.mainConf.doJustify: if self.mainConf.doJustify:
+24 -5
View File
@@ -13,19 +13,20 @@
import logging import logging
import nw import nw
from PyQt5.QtCore import QRegularExpression from PyQt5.QtCore import QRegularExpression, Qt
from PyQt5.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter from PyQt5.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class GuiDocHighlighter(QSyntaxHighlighter): class GuiDocHighlighter(QSyntaxHighlighter):
def __init__(self, theDoc): def __init__(self, theDoc, theDict):
QSyntaxHighlighter.__init__(self, theDoc) QSyntaxHighlighter.__init__(self, theDoc)
logger.debug("Initialising DocHighlighter ...") logger.debug("Initialising DocHighlighter ...")
self.mainConf = nw.CONFIG self.mainConf = nw.CONFIG
self.theDoc = theDoc self.theDoc = theDoc
self.theDict = theDict
self.hRules = [] self.hRules = []
self.colHead = ( 0,155,200,255) self.colHead = ( 0,155,200,255)
@@ -38,6 +39,8 @@ class GuiDocHighlighter(QSyntaxHighlighter):
self.colKey = (200, 46, 0,255) self.colKey = (200, 46, 0,255)
self.colVal = (184,200, 0,255) self.colVal = (184,200, 0,255)
self.colSpell = QColor(200,46,0)
self.hStyles = { self.hStyles = {
"header1" : self._makeFormat(self.colHead, "bold",1.8), "header1" : self._makeFormat(self.colHead, "bold",1.8),
"header2" : self._makeFormat(self.colHead, "bold",1.6), "header2" : self._makeFormat(self.colHead, "bold",1.6),
@@ -141,7 +144,8 @@ class GuiDocHighlighter(QSyntaxHighlighter):
)) ))
# Build a QRegExp for each pattern # 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") logger.debug("DocHighlighter initialisation complete")
@@ -174,6 +178,7 @@ class GuiDocHighlighter(QSyntaxHighlighter):
return theFormat return theFormat
def highlightBlock(self, theText): def highlightBlock(self, theText):
for rX, xFmt in self.rules: for rX, xFmt in self.rules:
rxItt = rX.globalMatch(theText, 0) rxItt = rX.globalMatch(theText, 0)
while rxItt.hasNext(): while rxItt.hasNext():
@@ -181,10 +186,24 @@ class GuiDocHighlighter(QSyntaxHighlighter):
for xM in xFmt.keys(): for xM in xFmt.keys():
xPos = rxMatch.capturedStart(xM) xPos = rxMatch.capturedStart(xM)
xLen = rxMatch.capturedLength(xM) xLen = rxMatch.capturedLength(xM)
# logger.verbose("Captured[%d]: '%s'" % (nth,rxMatch.captured(nth)))
# print(rxMatch.capturedTexts())
self.setFormat(xPos, xLen, xFmt[xM]) self.setFormat(xPos, xLen, xFmt[xM])
self.setCurrentBlockState(0) 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 # END Class DocHighlighter
+1
View File
@@ -368,6 +368,7 @@ class GuiDocTree(QTreeWidget):
self.addTopLevelItem(newItem) self.addTopLevelItem(newItem)
self.orphRoot = newItem self.orphRoot = newItem
newItem.setExpanded(True) newItem.setExpanded(True)
newItem.setIcon(self.C_NAME, QIcon.fromTheme("dialog-warning"))
return return
def _cleanOrphanedRoot(self): def _cleanOrphanedRoot(self):