Added cut/copy/paste to document context menu

This commit is contained in:
Veronica K. B. Olsen
2020-09-06 13:24:33 +02:00
parent 3457037908
commit d4f7d8eb67
+41 -22
View File
@@ -735,38 +735,57 @@ class GuiDocEditor(QTextEdit):
if not self.spellCheck: if not self.spellCheck:
return return
theCursor = self.cursorForPosition(thePos) userCursor = self.textCursor()
theCursor.select(QTextCursor.WordUnderCursor) userSelection = userCursor.hasSelection()
theWord = theCursor.selectedText().strip().strip(self.nonWord) mnuContext = QMenu()
if theWord == "":
return
if userSelection:
mnuCut = QAction("Cut", mnuContext)
mnuCut.triggered.connect(lambda: self.docAction(nwDocAction.CUT))
mnuContext.addAction(mnuCut)
mnuCopy = QAction("Copy", mnuContext)
mnuCopy.triggered.connect(lambda: self.docAction(nwDocAction.COPY))
mnuContext.addAction(mnuCopy)
mnuPaste = QAction("Paste", mnuContext)
mnuPaste.triggered.connect(lambda: self.docAction(nwDocAction.PASTE))
mnuContext.addAction(mnuPaste)
# Check if we should look up the spelling
posCursor = self.cursorForPosition(thePos)
posCursor.select(QTextCursor.WordUnderCursor)
theWord = posCursor.selectedText().strip().strip(self.nonWord)
spellCheck = theWord != ""
if spellCheck:
logger.verbose("Looking up '%s' in the dictionary" % theWord) logger.verbose("Looking up '%s' in the dictionary" % theWord)
if self.theDict.checkWord(theWord): spellCheck &= not self.theDict.checkWord(theWord)
return
if spellCheck:
mnuContext.addSeparator()
mnuHead = QAction("Spelling Suggestion(s)", mnuContext)
mnuContext.addAction(mnuHead)
mnuSuggest = QMenu()
mnuHead = QAction("Spelling Suggestion(s)", mnuSuggest)
mnuSuggest.addAction(mnuHead)
mnuSuggest.addSeparator()
theSuggest = self.theDict.suggestWords(theWord) theSuggest = self.theDict.suggestWords(theWord)
if len(theSuggest) > 0: if len(theSuggest) > 0:
for aWord in theSuggest: for aWord in theSuggest:
mnuWord = QAction(aWord, mnuSuggest) mnuWord = QAction(aWord, mnuContext)
mnuWord.triggered.connect( mnuWord.triggered.connect(
lambda thePos, aWord=aWord : self._correctWord(theCursor, aWord) lambda thePos, aWord=aWord : self._correctWord(posCursor, aWord)
) )
mnuSuggest.addAction(mnuWord) mnuContext.addAction(mnuWord)
mnuSuggest.addSeparator() mnuContext.addSeparator()
mnuAdd = QAction("Add Word to Dictionary", mnuSuggest) mnuAdd = QAction("Add Word to Dictionary", mnuContext)
mnuAdd.triggered.connect(lambda thePos : self._addWord(theCursor)) mnuAdd.triggered.connect(lambda thePos : self._addWord(posCursor))
mnuSuggest.addAction(mnuAdd) mnuContext.addAction(mnuAdd)
else:
mnuHead = QAction("No Suggestions", mnuSuggest)
mnuSuggest.addAction(mnuHead)
mnuSuggest.exec_(self.viewport().mapToGlobal(thePos)) else:
mnuHead = QAction("No Suggestions", mnuContext)
mnuContext.addAction(mnuHead)
mnuContext.exec_(self.viewport().mapToGlobal(thePos))
return return