From 87c786975a0176c22e071cb1445a59a341f943dd Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Fri, 10 May 2019 20:58:05 +0200 Subject: [PATCH] Reimplemented the spell check replace word code --- nw/gui/doceditor.py | 76 ++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index cd03c712..82baf8e3 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -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