Some tweaking in the highlichter class, and added the spelling suggestion context menu.
This commit is contained in:
+61
-3
@@ -16,9 +16,9 @@ import enchant
|
||||
|
||||
from time import time
|
||||
|
||||
from PyQt5.QtWidgets import QTextEdit
|
||||
from PyQt5.QtCore import Qt, QTimer
|
||||
from PyQt5.QtGui import QTextCursor, QTextOption
|
||||
from PyQt5.QtWidgets import QTextEdit, QAction, QMenu
|
||||
from PyQt5.QtCore import Qt, QTimer, QEvent, pyqtSignal
|
||||
from PyQt5.QtGui import QTextCursor, QTextOption, QMouseEvent
|
||||
|
||||
from nw.gui.dochighlight import GuiDocHighlighter
|
||||
from nw.gui.wordcounter import WordCounter
|
||||
@@ -148,6 +148,56 @@ class GuiDocEditor(QTextEdit):
|
||||
QTextEdit.keyPressEvent(self, keyEvent)
|
||||
return
|
||||
|
||||
def mousePressEvent(self, theEvent):
|
||||
"""Capture right click events and rewrite them to left button event. This moves the cursor
|
||||
to the location of the pointer. Needed to select the word under the right click.
|
||||
Adapted from: https://nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check
|
||||
"""
|
||||
if theEvent.button() == Qt.RightButton:
|
||||
theEvent = QMouseEvent(
|
||||
QEvent.MouseButtonPress, theEvent.pos(), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier
|
||||
)
|
||||
QTextEdit.mousePressEvent(self, theEvent)
|
||||
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()
|
||||
theCursor = self.textCursor()
|
||||
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()
|
||||
theCursor.beginEditBlock()
|
||||
theCursor.removeSelectedText()
|
||||
theCursor.insertText(word)
|
||||
theCursor.endEditBlock()
|
||||
return
|
||||
|
||||
def _docChange(self, thePos, charsRemoved, charsAdded):
|
||||
self.lastEdit = time()
|
||||
if not self.wcTimer.isActive():
|
||||
@@ -270,3 +320,11 @@ 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