Spell checker context menu can now add words to dictionary for current project

This commit is contained in:
Veronica K. B. Olsen
2019-05-10 23:11:46 +02:00
parent f26cb7c281
commit d6409a5bf9
4 changed files with 62 additions and 16 deletions
+26 -5
View File
@@ -37,6 +37,7 @@ class GuiDocEditor(QTextEdit):
self.mainConf = nw.CONFIG
self.theParent = theParent
self.docChanged = False
self.pwlFile = None
# Document Variables
self.charCount = 0
@@ -54,7 +55,8 @@ class GuiDocEditor(QTextEdit):
# Core Elements
self.theDoc = self.document()
self.theDict = enchant.Dict(self.mainConf.spellLanguage)
self.hLight = GuiDocHighlighter(self.theDoc, self.theDict)
self.hLight = GuiDocHighlighter(self.theDoc)
self.hLight.setDict(self.theDict)
# Context Menu
self.setContextMenuPolicy(Qt.CustomContextMenu)
@@ -113,6 +115,13 @@ class GuiDocEditor(QTextEdit):
self.setDocumentChanged(False)
return True
def setPwl(self, pwlFile):
if pwlFile is not None:
self.pwlFile = pwlFile
self.theDict = enchant.DictWithPWL(self.mainConf.spellLanguage,pwlFile)
self.hLight.setDict(self.theDict)
return
def getText(self):
theText = self.toPlainText()
return theText
@@ -174,23 +183,27 @@ class GuiDocEditor(QTextEdit):
theCursor = self.cursorForPosition(thePos)
theCursor.select(QTextCursor.WordUnderCursor)
theText = theCursor.selectedText()
if theText == "":
theWord = theCursor.selectedText()
if theWord == "":
return
mnuSuggest = QMenu()
spIcon = QIcon.fromTheme("tools-check-spelling")
if self.theDict.check(theText):
if self.theDict.check(theWord):
mnuHead = QAction(spIcon,"No Suggestion", mnuSuggest)
mnuSuggest.addAction(mnuHead)
else:
mnuHead = QAction(spIcon,"Spelling Suggestion", mnuSuggest)
mnuSuggest.addAction(mnuHead)
mnuSuggest.addSeparator()
for aWord in self.theDict.suggest(theText):
for aWord in self.theDict.suggest(theWord):
mnuWord = QAction(aWord, mnuSuggest)
mnuWord.triggered.connect(lambda thePos, aWord=aWord : self._correctWord(theCursor, aWord))
mnuSuggest.addAction(mnuWord)
mnuSuggest.addSeparator()
mnuAdd = QAction("Add Word to Dictionary", mnuSuggest)
mnuAdd.triggered.connect(lambda thePos : self._addWord(theCursor))
mnuSuggest.addAction(mnuAdd)
mnuSuggest.exec_(self.viewport().mapToGlobal(thePos))
@@ -206,6 +219,14 @@ class GuiDocEditor(QTextEdit):
self.setTextCursor(theCursor)
return
def _addWord(self, theCursor):
theWord = theCursor.selectedText()
logger.info("Added '%s' to project dictionary" % theWord)
self.theDict.add_to_pwl(theWord)
self.hLight.setDict(self.theDict)
self.hLight.rehighlightBlock(theCursor.block())
return
def _docChange(self, thePos, charsRemoved, charsAdded):
self.lastEdit = time()
self.setDocumentChanged(True)
+6 -2
View File
@@ -20,13 +20,13 @@ logger = logging.getLogger(__name__)
class GuiDocHighlighter(QSyntaxHighlighter):
def __init__(self, theDoc, theDict):
def __init__(self, theDoc):
QSyntaxHighlighter.__init__(self, theDoc)
logger.debug("Initialising DocHighlighter ...")
self.mainConf = nw.CONFIG
self.theDoc = theDoc
self.theDict = theDict
self.theDict = None
self.hRules = []
self.colHead = QColor( 0,155,200)
@@ -151,6 +151,10 @@ class GuiDocHighlighter(QSyntaxHighlighter):
return
def setDict(self, theDict):
self.theDict = theDict
return
def _makeFormat(self, fmtCol=None, fmtStyle=None, fmtSize=None):
theFormat = QTextCharFormat()
+4 -2
View File
@@ -147,8 +147,9 @@ class GuiMain(QMainWindow):
def newProject(self):
logger.info("Creating new project")
self.treeView.clearTree()
self.theProject.newProject()
self.treeView.buildTree()
if self.saveProject():
self.theProject.newProject()
self.treeView.buildTree()
return
def openProject(self, projFile=None):
@@ -161,6 +162,7 @@ class GuiMain(QMainWindow):
self.treeView.buildTree()
self.mainMenu.updateRecentProjects()
self._setWindowTitle(self.theProject.projName)
self.docEditor.setPwl(path.join(self.theProject.projMeta,"wordlist.txt"))
return True
def saveProject(self):
+26 -7
View File
@@ -41,6 +41,8 @@ class NWProject():
self.treeRoots = None
self.trashRoot = None
self.projPath = None
self.projMeta = None
self.projCache = None
self.projFile = None
self.projName = None
self.bookTitle = None
@@ -120,6 +122,8 @@ class NWProject():
self.treeRoots = []
self.trashRoot = None
self.projPath = None
self.projMeta = None
self.projCache = None
self.projFile = "nwProject.nwx"
self.projName = ""
self.bookTitle = ""
@@ -149,6 +153,12 @@ class NWProject():
self.projPath = path.dirname(fileName)
logger.debug("Opening project: %s" % self.projPath)
self.projMeta = path.join(self.projPath,"meta")
self.projCache = path.join(self.projPath,"cache")
if not self._checkFolder(self.projMeta): return
if not self._checkFolder(self.projCache): return
nwXML = etree.parse(fileName)
xRoot = nwXML.getroot()
@@ -210,13 +220,12 @@ class NWProject():
self.theParent.makeAlert("Project path not set, cannot save.",2)
return False
if not path.isdir(self.projPath):
try:
mkdir(self.projPath)
logger.info("Created folder %s" % self.projPath)
except Exception as e:
self.theParent.makeAlert(["Could not create folder.",str(e)],2)
return False
self.projMeta = path.join(self.projPath,"meta")
self.projCache = path.join(self.projPath,"cache")
if not self._checkFolder(self.projPath): return
if not self._checkFolder(self.projMeta): return
if not self._checkFolder(self.projCache): return
logger.debug("Saving project: %s" % self.projPath)
@@ -335,6 +344,16 @@ class NWProject():
# Internal Functions
##
def _checkFolder(self, thePath):
if not path.isdir(thePath):
try:
mkdir(thePath)
logger.info("Created folder %s" % thePath)
except Exception as e:
self.theParent.makeAlert(["Could not create folder.",str(e)],2)
return False
return True
def _scanProjectFolder(self):
if self.projPath is None: