|
28 | 28 | ) |
29 | 29 | from PyQt6.QtCore import ( |
30 | 30 | QFileInfo, |
| 31 | + QMimeData, |
31 | 32 | QMimeDatabase, |
32 | 33 | QPoint, |
33 | 34 | QPointF, |
|
42 | 43 | QGuiApplication, |
43 | 44 | QImage, |
44 | 45 | QKeyEvent, |
| 46 | + QKeySequence, |
45 | 47 | QMouseEvent, |
46 | 48 | QPainter, |
47 | 49 | QPalette, |
@@ -256,6 +258,14 @@ def contextMenuEvent(self, event): |
256 | 258 | actionNextAfterPaste = actions[actions.index(actionPaste) + 1] |
257 | 259 | menu.insertAction(actionNextAfterPaste, self.parent.actionPasteImage) |
258 | 260 |
|
| 261 | + actionCopy = menu.findChild(QAction, "edit-copy") |
| 262 | + actionCopy.triggered.disconnect() |
| 263 | + actionCopy.triggered.connect(self.copy) |
| 264 | + |
| 265 | + actionCut = menu.findChild(QAction, "edit-cut") |
| 266 | + actionCut.triggered.disconnect() |
| 267 | + actionCut.triggered.connect(self.cut) |
| 268 | + |
259 | 269 | text = self.toPlainText() |
260 | 270 | if not text: |
261 | 271 | menu.exec(event.globalPos()) |
@@ -372,6 +382,10 @@ def keyPressEvent(self, event): |
372 | 382 | self.moveCursor(QTextCursor.MoveOperation.Down, mode) |
373 | 383 | if self.textCursor().position() == oldPos: |
374 | 384 | self.moveCursor(QTextCursor.MoveOperation.End, mode) |
| 385 | + elif event.matches(QKeySequence.StandardKey.Copy): |
| 386 | + self.copy() |
| 387 | + elif event.matches(QKeySequence.StandardKey.Cut): |
| 388 | + self.cut() |
375 | 389 | elif cursor.selectedText() and self.isSurroundKey(key): |
376 | 390 | self.surroundText(cursor, event, key) |
377 | 391 | else: |
@@ -646,6 +660,18 @@ def insertFromMimeData(self, source): |
646 | 660 | return |
647 | 661 | return super().insertFromMimeData(source) |
648 | 662 |
|
| 663 | + def copy(self): |
| 664 | + """Copy only text, not html, to make sure it is inserted back verbatim.""" |
| 665 | + if text := self.textCursor().selectedText(): |
| 666 | + text = text.replace("\u2029", "\n") |
| 667 | + data = QMimeData() |
| 668 | + data.setText(text) |
| 669 | + QGuiApplication.clipboard().setMimeData(data) |
| 670 | + |
| 671 | + def cut(self): |
| 672 | + self.copy() |
| 673 | + self.textCursor().removeSelectedText() |
| 674 | + |
649 | 675 |
|
650 | 676 | class LineNumberArea(QWidget): |
651 | 677 | def __init__(self, editor): |
|
0 commit comments