| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
from PyQt5 import QtCore, QtGui, QtWidgets |
| 25 |
|
| 26 |
|
| 27 |
class AnnotateBarBase(QtWidgets.QWidget): |
| 28 |
cursorPositionChanged = QtCore.pyqtSignal() |
| 29 |
|
| 30 |
def __init__(self, edit, parent): |
| 31 |
QtWidgets.QWidget.__init__(self, parent) |
| 32 |
self.edit = edit |
| 33 |
self.edit.updateRequest[QtCore.QRect, int].connect(self.updateContents) |
| 34 |
|
| 35 |
def paintEvent(self, event): |
| 36 |
current_line = self.edit.document().findBlock( |
| 37 |
self.edit.textCursor().position()).blockNumber() + 1 |
| 38 |
|
| 39 |
block = self.edit.firstVisibleBlock() |
| 40 |
line_count = block.blockNumber() |
| 41 |
painter = QtGui.QPainter(self) |
| 42 |
painter.fillRect(event.rect(), self.palette().window()) |
| 43 |
|
| 44 |
|
| 45 |
while block.isValid(): |
| 46 |
line_count += 1 |
| 47 |
rect = self.edit.blockBoundingGeometry(block) |
| 48 |
rect = rect.translated(self.edit.contentOffset()) |
| 49 |
rect.setWidth(self.width()) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
if not block.isVisible() or rect.top() >= event.rect().bottom(): |
| 54 |
break |
| 55 |
self.paint_line(painter, rect, line_count, line_count==current_line) |
| 56 |
block = block.next() |
| 57 |
|
| 58 |
painter.end() |
| 59 |
|
| 60 |
QtWidgets.QWidget.paintEvent(self, event) |
| 61 |
|
| 62 |
def paint_line(self, painter, rect, line_number, is_current): |
| 63 |
pass |
| 64 |
|
| 65 |
def updateContents(self, rect, scroll): |
| 66 |
if scroll: |
| 67 |
self.scroll(0, scroll) |
| 68 |
else: |
| 69 |
self.update(0, rect.y(), self.width(), rect.height()) |
| 70 |
|
| 71 |
def mousePressEvent(self, event): |
| 72 |
if event.button() == QtCore.Qt.LeftButton: |
| 73 |
cursor = self.edit.cursorForPosition(event.pos()) |
| 74 |
cursor.movePosition(QtGui.QTextCursor.StartOfBlock) |
| 75 |
cursor.movePosition(QtGui.QTextCursor.EndOfBlock, |
| 76 |
QtGui.QTextCursor.KeepAnchor) |
| 77 |
self.edit.setTextCursor(cursor) |
| 78 |
self.cursorPositionChanged.emit() |
| 79 |
|
| 80 |
def wheelEvent(self, event): |
| 81 |
self.edit.wheelEvent(event) |
| 82 |
|
| 83 |
|
| 84 |
class LineNumberBar(AnnotateBarBase): |
| 85 |
|
| 86 |
def __init__(self, edit, parent): |
| 87 |
super(LineNumberBar, self).__init__(edit, parent) |
| 88 |
self.adjustWidth(1) |
| 89 |
self.edit.blockCountChanged[int].connect(self.adjustWidth) |
| 90 |
|
| 91 |
def adjustWidth(self, count): |
| 92 |
width = self.fontMetrics().width(str(count)) |
| 93 |
text_margin = self.style().pixelMetric( |
| 94 |
QtWidgets.QStyle.PM_FocusFrameHMargin, None, self) + 1 |
| 95 |
width += text_margin * 2 |
| 96 |
if self.width() != width: |
| 97 |
self.setFixedWidth(width) |
| 98 |
|
| 99 |
def paint_line(self, painter, rect, line_number, is_current): |
| 100 |
text_margin = self.style().pixelMetric( |
| 101 |
QtWidgets.QStyle.PM_FocusFrameHMargin, None, self) + 1 |
| 102 |
painter.drawText(rect.adjusted(text_margin, 0, -text_margin, 0), |
| 103 |
QtCore.Qt.AlignRight, str(line_number)) |
| 104 |
|
| 105 |
|
| 106 |
class AnnotateEditerFrameBase(QtWidgets.QFrame): |
| 107 |
|
| 108 |
def __init__(self, parent = None): |
| 109 |
QtWidgets.QFrame.__init__(self,parent) |
| 110 |
|
| 111 |
self.setFrameStyle(QtWidgets.QFrame.StyledPanel | QtWidgets.QFrame.Sunken) |
| 112 |
|
| 113 |
self.hbox = QtWidgets.QHBoxLayout(self) |
| 114 |
self.hbox.setSpacing(0) |
| 115 |
self.hbox.setContentsMargins(0, 0, 0, 0) |
| 116 |
|
| 117 |
|
| 118 |
class LineNumberEditerFrame(AnnotateEditerFrameBase): |
| 119 |
|
| 120 |
def __init__(self, parent= None): |
| 121 |
super(LineNumberEditerFrame, self).__init__(parent) |
| 122 |
self.edit = QtWidgets.QPlainTextEdit(self) |
| 123 |
self.edit.setFrameStyle(QtWidgets.QFrame.NoFrame) |
| 124 |
|
| 125 |
self.number_bar = LineNumberBar(self.edit, self) |
| 126 |
self.hbox.addWidget(self.number_bar) |
| 127 |
self.hbox.addWidget(self.edit) |
| 128 |
|
| 129 |
def setFocus(self): |
| 130 |
self.edit.setFocus() |