Reimplemented the spell check replace word code
This commit is contained in:
+37
-39
@@ -17,8 +17,8 @@ import enchant
|
||||
from time import time
|
||||
|
||||
from PyQt5.QtWidgets import QTextEdit, QAction, QMenu
|
||||
from PyQt5.QtCore import Qt, QTimer, QEvent, pyqtSignal
|
||||
from PyQt5.QtGui import QTextCursor, QTextOption, QMouseEvent
|
||||
from PyQt5.QtCore import Qt, QTimer
|
||||
from PyQt5.QtGui import QTextCursor, QTextOption, QIcon
|
||||
|
||||
from nw.gui.dochighlight import GuiDocHighlighter
|
||||
from nw.gui.wordcounter import WordCounter
|
||||
@@ -56,6 +56,10 @@ class GuiDocEditor(QTextEdit):
|
||||
self.theDict = enchant.Dict(self.mainConf.spellLanguage)
|
||||
self.hLight = GuiDocHighlighter(self.theDoc, self.theDict)
|
||||
|
||||
# Context Menu
|
||||
self.setContextMenuPolicy(Qt.CustomContextMenu)
|
||||
self.customContextMenuRequested.connect(self._openContextMenu)
|
||||
|
||||
# Editor State
|
||||
self.hasSelection = False
|
||||
|
||||
@@ -155,42 +159,44 @@ class GuiDocEditor(QTextEdit):
|
||||
QTextEdit.keyPressEvent(self, keyEvent)
|
||||
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
|
||||
##
|
||||
|
||||
def _correctWord(self, word):
|
||||
theCursor = self.textCursor()
|
||||
def _openContextMenu(self, thePos):
|
||||
|
||||
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.removeSelectedText()
|
||||
theCursor.insertText(word)
|
||||
theCursor.insertText(theWord)
|
||||
theCursor.endEditBlock()
|
||||
theCursor.setPosition(xPos)
|
||||
self.setTextCursor(theCursor)
|
||||
return
|
||||
|
||||
def _docChange(self, thePos, charsRemoved, charsAdded):
|
||||
@@ -316,11 +322,3 @@ class GuiDocEditor(QTextEdit):
|
||||
return
|
||||
|
||||
# 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
|
||||
|
||||
Reference in New Issue
Block a user