Debian Codegraph @

lib/texteditannotate.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2009 Gary van der Merwe <[email protected]>
5 #
6 # Some of this code was coppied from
7 # http://john.nachtimwald.com/better-qplaintextedit-with-line-numbers/
8 # Copyright (C) 2009 John Schember
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 # Iterate over all visible text blocks in the document.
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 # Check if the position of the block is out side of the visible
52 # area.
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()