Reimplemented the spell check replace word code

This commit is contained in:
Veronica K. B. Olsen
2019-05-10 20:58:05 +02:00
parent 5adb2f9f97
commit 87c786975a
+37 -39
View File
@@ -17,8 +17,8 @@ import enchant
from time import time from time import time
from PyQt5.QtWidgets import QTextEdit, QAction, QMenu from PyQt5.QtWidgets import QTextEdit, QAction, QMenu
from PyQt5.QtCore import Qt, QTimer, QEvent, pyqtSignal from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QTextCursor, QTextOption, QMouseEvent from PyQt5.QtGui import QTextCursor, QTextOption, QIcon
from nw.gui.dochighlight import GuiDocHighlighter from nw.gui.dochighlight import GuiDocHighlighter
from nw.gui.wordcounter import WordCounter from nw.gui.wordcounter import WordCounter
@@ -56,6 +56,10 @@ class GuiDocEditor(QTextEdit):
self.theDict = enchant.Dict(self.mainConf.spellLanguage) self.theDict = enchant.Dict(self.mainConf.spellLanguage)
self.hLight = GuiDocHighlighter(self.theDoc, self.theDict) self.hLight = GuiDocHighlighter(self.theDoc, self.theDict)
# Context Menu
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self._openContextMenu)
# Editor State # Editor State
self.hasSelection = False self.hasSelection = False
@@ -155,42 +159,44 @@ class GuiDocEditor(QTextEdit):
QTextEdit.keyPressEvent(self, keyEvent) QTextEdit.keyPressEvent(self, keyEvent)
return return
def contextMenuEvent(self, theEvent):
"""Intercept the context menu and insert spelling suggestions, if any.
Uses the custom QAction class SpellAction from the same example code.
Adapted from: https://nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check
"""
mnuSpell = self.createStandardContextMenu(theEvent.pos())
theCursor = self.cursorForPosition(theEvent.pos())
theCursor.select(QTextCursor.WordUnderCursor)
self.setTextCursor(theCursor)
if self.textCursor().hasSelection():
theText = self.textCursor().selectedText()
if not self.theDict.check(theText):
mnuSuggest = QMenu("Spelling Suggestions")
for aWord in self.theDict.suggest(theText):
action = SpellAction(aWord, mnuSuggest)
action.correct.connect(self._correctWord)
mnuSuggest.addAction(action)
if len(mnuSuggest.actions()) > 0:
theActions = mnuSpell.actions()
mnuSpell.insertSeparator(theActions[0])
mnuSpell.insertMenu(theActions[0], mnuSuggest)
mnuSpell.exec_(theEvent.globalPos())
return
## ##
# Internal Functions # Internal Functions
## ##
def _correctWord(self, word): def _openContextMenu(self, thePos):
theCursor = self.textCursor()
theCursor = self.cursorForPosition(thePos)
theCursor.select(QTextCursor.WordUnderCursor)
theText = theCursor.selectedText()
if theText == "":
return
mnuSuggest = QMenu()
spIcon = QIcon.fromTheme("tools-check-spelling")
if self.theDict.check(theText):
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):
mnuWord = QAction(aWord, mnuSuggest)
mnuWord.triggered.connect(lambda thePos, aWord=aWord : self._correctWord(theCursor, aWord))
mnuSuggest.addAction(mnuWord)
mnuSuggest.exec_(self.viewport().mapToGlobal(thePos))
return
def _correctWord(self, theCursor, theWord):
xPos = theCursor.selectionStart()
theCursor.beginEditBlock() theCursor.beginEditBlock()
theCursor.removeSelectedText() theCursor.removeSelectedText()
theCursor.insertText(word) theCursor.insertText(theWord)
theCursor.endEditBlock() theCursor.endEditBlock()
theCursor.setPosition(xPos)
self.setTextCursor(theCursor)
return return
def _docChange(self, thePos, charsRemoved, charsAdded): def _docChange(self, thePos, charsRemoved, charsAdded):
@@ -316,11 +322,3 @@ class GuiDocEditor(QTextEdit):
return return
# END Class GuiDocEditor # END Class GuiDocEditor
class SpellAction(QAction):
correct = pyqtSignal(str)
def __init__(self, *args):
QAction.__init__(self, *args)
self.triggered.connect(lambda x: self.correct.emit(self.text()))
# END Class SpellAction