Package node_manager_fkie :: Package editor :: Module line_number_widget
[frames] | no frames]

Source Code for Module node_manager_fkie.editor.line_number_widget

  1  # The MIT License 
  2  # 
  3  # Copyright (c) 2009 John Schember <john@nachtimwald.com> 
  4  # 
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy 
  6  # of this software and associated documentation files (the "Software"), to deal 
  7  # in the Software without restriction, including without limitation the rights 
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
  9  # copies of the Software, and to permit persons to whom the Software is 
 10  # furnished to do so, subject to the following conditions: 
 11  # 
 12  # The above copyright notice and this permission notice shall be included in 
 13  # all copies or substantial portions of the Software. 
 14  # 
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 21  # THE SOFTWARE 
 22   
 23  from python_qt_binding.QtCore import Qt 
 24  from python_qt_binding.QtGui import QPainter 
 25  try: 
 26      from python_qt_binding.QtGui import QWidget, QFrame, QHBoxLayout 
 27  except: 
 28      from python_qt_binding.QtWidgets import QWidget, QFrame, QHBoxLayout 
 29   
 30   
31 -class LineNumberWidget(QFrame):
32
33 - class NumberBar(QWidget):
34
35 - def __init__(self, *args):
36 QWidget.__init__(self, *args) 37 self.edit = None 38 # it is the highest line that is currently visible. 39 self.highest_line = 0
40
41 - def set_text_edit(self, edit):
42 self.edit = edit
43
44 - def update(self, *args):
45 # the +4 is used to compensate for the current line being bold. 46 width = self.fontMetrics().width(str(self.highest_line)) + 4 47 if self.width() != width: 48 self.setFixedWidth(width) 49 QWidget.update(self, *args)
50
51 - def paintEvent(self, event):
52 contents_y = self.edit.verticalScrollBar().value() 53 page_bottom = contents_y + self.edit.viewport().height() 54 font_metrics = self.fontMetrics() 55 current_block = self.edit.document().findBlock(self.edit.textCursor().position()) 56 painter = QPainter(self) 57 painter.setPen(Qt.darkGray) 58 line_count = 0 59 # Iterate over all text blocks in the document. 60 block = self.edit.document().begin() 61 while block.isValid(): 62 line_count += 1 63 # the top left position of the block in the document 64 position = self.edit.document().documentLayout().blockBoundingRect(block).topLeft() 65 # check if the position of the block is out side of visible area 66 if position.y() > page_bottom: 67 break 68 # we want the line number for the selected line to be bold. 69 bold = False 70 if block == current_block: 71 bold = True 72 font = painter.font() 73 font.setBold(True) 74 painter.setFont(font) 75 painter.setPen(Qt.black) 76 # Draw the line number right justified at the y position of the 77 # line. 3 is the magic padding number. drawText(x, y, text) 78 painter.drawText(self.width() - font_metrics.width(str(line_count)) - 3, round(position.y()) - contents_y + font_metrics.ascent() + self.edit.document().documentMargin(), str(line_count)) 79 if bold: 80 font = painter.font() 81 font.setBold(False) 82 painter.setFont(font) 83 painter.setPen(Qt.darkGray) 84 85 block = block.next() 86 87 self.highest_line = line_count 88 painter.end() 89 QWidget.paintEvent(self, event)
90
91 - def __init__(self, editor, *args):
92 QFrame.__init__(self, *args) 93 94 self.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken) 95 self.edit = editor 96 97 self.number_bar = self.NumberBar() 98 self.number_bar.set_text_edit(self.edit) 99 100 hbox = QHBoxLayout(self) 101 hbox.setSpacing(0) 102 # hbox.setMargin(0) # removed: it is not supported by Qt5 103 hbox.addWidget(self.number_bar) 104 hbox.addWidget(self.edit) 105 106 self.edit.installEventFilter(self) 107 self.edit.viewport().installEventFilter(self)
108
109 - def eventFilter(self, obj, event):
110 # Update the line numbers for all events on the text edit and the viewport. 111 # This is easier than connecting all necessary signals. 112 if obj in (self.edit, self.edit.viewport()): 113 self.number_bar.update() 114 return False 115 return QFrame.eventFilter(obj, event)
116
117 - def get_text_edit(self):
118 return self.edit
119