| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
from PyQt6.QtWidgets import QPlainTextEdit |
| 23 |
from PyQt6.QtWidgets import QTextEdit |
| 24 |
from PyQt6.QtCore import Qt |
| 25 |
from PyQt6.QtGui import QColor |
| 26 |
from PyQt6.QtGui import QPalette |
| 27 |
from PyQt6.QtGui import QTextCharFormat |
| 28 |
from PyQt6.QtGui import QWheelEvent |
| 29 |
|
| 30 |
|
| 31 |
class Editor(QPlainTextEdit): |
| 32 |
|
| 33 |
def __init__(self, *args, **kwargs): |
| 34 |
super(Editor, self).__init__(*args, **kwargs) |
| 35 |
|
| 36 |
self._cursor_moved() |
| 37 |
self.cursorPositionChanged.connect(self._cursor_moved) |
| 38 |
|
| 39 |
def _cursor_moved(self) -> None: |
| 40 |
selections = [] |
| 41 |
|
| 42 |
|
| 43 |
cur_line = QTextEdit.ExtraSelection() |
| 44 |
bgcolor = QPalette().color( |
| 45 |
QPalette.ColorGroup.Active, |
| 46 |
QPalette.ColorRole.Window, |
| 47 |
).lighter() |
| 48 |
cur_line.format.setBackground(bgcolor) |
| 49 |
cur_line.format.setProperty( |
| 50 |
QTextCharFormat.Property.FullWidthSelection, |
| 51 |
True |
| 52 |
) |
| 53 |
cur_line.cursor = self.textCursor() |
| 54 |
cur_line.cursor.clearSelection() |
| 55 |
selections.append(cur_line) |
| 56 |
|
| 57 |
|
| 58 |
self.setExtraSelections(selections) |
| 59 |
|
| 60 |
def wheelEvent(self, event: QWheelEvent | None) -> None: |
| 61 |
if event is not None and event.modifiers().value == Qt.Modifier.CTRL.value: |
| 62 |
event.accept() |
| 63 |
self.zoom(1 if event.angleDelta().y()>0 else -1) |
| 64 |
else: |
| 65 |
super(Editor, self).wheelEvent(event) |
| 66 |
|
| 67 |
def zoom(self, incr: int) -> None: |
| 68 |
font = self.font() |
| 69 |
point_size = font.pointSize() |
| 70 |
point_size += incr |
| 71 |
if point_size < 1: |
| 72 |
return |
| 73 |
font.setPointSize(point_size) |
| 74 |
self.setFont(font) |