def paintEvent(self, event): contents_y = self.editor.verticalScrollBar().value() page_bottom = contents_y + self.editor.viewport().height() font_metrics = self.fontMetrics() current_block = self.editor.document().findBlock( self.editor.textCursor().position()) painter = QPainter(self) line_count = 0 # Iterate over all text blocks in the document block = self.editor.document().begin() font_size = self.editor.font().pointSize() font = painter.font() font.setPixelSize(font_size) offset = font_metrics.ascent() + font_metrics.descent() color = painter.pen().color() painter.setFont(font) align = Qt.AlignRight while block.isValid(): line_count += 1 # Get top left position of the block in the document and check if the position of the block is # outside of the visible area position = self.editor.document().documentLayout( ).blockBoundingRect(block).topLeft() if position.y() == page_bottom: break rect = QRect(0, round(position.y()) - contents_y, self.width() - 5, font_size + offset) # Draw line rect if block == current_block: painter.setPen(Qt.NoPen) painter.setBrush(QBrush(self.bg)) painter.drawRect( QRect(0, round(position.y()) - contents_y, self.width(), font_size + (offset / 2))) painter.setPen(QPen(color)) # Draw text painter.drawText(rect, align, str(line_count)) block = block.next() self.highest_line = line_count painter.end() super(ScriptEditorNumberBar, self).paintEvent(event)
def create_drop_indicator_pixmap(palette, size, drop_area): border_color = palette.color(QPalette.Active, QPalette.Highlight) background_color = palette.color(QPalette.Active, QPalette.Base) area_background_color = palette.color(QPalette.Active, QPalette.Highlight).lighter(150) pm = QPixmap(size.width(), size.height()) pm.fill(QColor(0, 0, 0, 0)) painter = QPainter(pm) pen = painter.pen() base_rect = QRectF(pm.rect()) painter.fillRect(base_rect, background_color) painter.save() area_rect = QRectF() area_line = QLineF() gradient = QLinearGradient() if drop_area == DropArea.TopDropArea: area_rect = QRectF(base_rect.x(), base_rect.y(), base_rect.width(), base_rect.height() * 0.5) area_line = QLineF(area_rect.bottomLeft(), area_rect.bottomRight()) gradient.setStart(area_rect.topLeft()) gradient.setFinalStop(area_rect.bottomLeft()) gradient.setColorAt(0, area_background_color) gradient.setColorAt(1, area_background_color.lighter(120)) elif drop_area == DropArea.RightDropArea: area_rect = QRectF(base_rect.width() * 0.5, base_rect.y(), base_rect.width() * 0.5, base_rect.height()) area_line = QLineF(area_rect.topLeft(), area_rect.bottomLeft()) gradient.setStart(area_rect.topLeft()) gradient.setFinalStop(area_rect.topRight()) gradient.setColorAt(0, area_background_color.lighter(120)) gradient.setColorAt(1, area_background_color) elif drop_area == DropArea.BottomDropArea: area_rect = QRectF(base_rect.x(), base_rect.height() * 0.5, base_rect.width(), base_rect.height() * 0.5) area_line = QLineF(area_rect.topLeft(), area_rect.topRight()) gradient.setStart(area_rect.topLeft()) gradient.setFinalStop(area_rect.bottomLeft()) gradient.setColorAt(0, area_background_color.lighter(120)) gradient.setColorAt(1, area_background_color) elif drop_area == DropArea.LeftDropArea: area_rect = QRectF(base_rect.x(), base_rect.y(), base_rect.width() * 0.5, base_rect.height()) area_line = QLineF(area_rect.topRight(), area_rect.bottomRight()) gradient.setStart(area_rect.topLeft()) gradient.setFinalStop(area_rect.topRight()) gradient.setColorAt(0, area_background_color) gradient.setColorAt(1, area_background_color.lighter(120)) if area_rect.isValid(): painter.fillRect(area_rect, gradient) pen = painter.pen() pen.setColor(border_color) pen.setStyle(Qt.DashLine) painter.setPen(pen) painter.drawLine(area_line) painter.restore() painter.save() pen = painter.pen() pen.setColor(border_color) pen.setWidth(1) painter.setPen(pen) painter.drawRect(base_rect.adjusted(0, 0, -pen.width(), -pen.width())) painter.restore() return pm
def paint(self, p: QtGui.QPainter, option: QtWidgets.QGraphicsItem.ItemIsSelectable, widget: QtWidgets.QWidget): # pylint: disable=unused-argument # Set color when selected if option.state & QtWidgets.QStyle.State_Selected: fillColor = self.color.darker(150) else: fillColor = self.color # Lighten if mouse over if option.state & QtWidgets.QStyle.State_MouseOver: fillColor = fillColor.lighter(125) # Calculate level of detail lod = option.levelOfDetailFromTransform(p.worldTransform()) if lod < 0.125: p.fillRect(QtCore.QRectF(0, 0, 110, 70), fillColor) return if lod < 0.2: oldBrush = p.brush() p.setBrush(fillColor) p.drawRect(13, 13, 97, 57) p.setBrush(oldBrush) return oldPen = p.pen() oldBrush = p.brush() # Use a copy of the old pen pen = QtGui.QPen(oldPen) if option.state & QtWidgets.QStyle.State_Selected: pen.setWidth(2) else: pen.setWidth(0) p.setPen(pen) # Draw the inside of the chip if option.state & QtWidgets.QStyle.State_Sunken: p.setBrush(QtGui.QBrush(fillColor.darker(120))) else: p.setBrush(QtGui.QBrush(fillColor.darker(100))) p.drawRect(QtCore.QRect(14, 14, 79, 39)) p.setBrush(oldBrush) if lod >= 1: p.setPen(QtGui.QPen(QtCore.Qt.gray, 1)) p.drawLine(15, 54, 94, 54) p.drawLine(94, 53, 94, 15) p.setPen(QtGui.QPen(QtCore.Qt.black, 0)) if lod >= 2: font = QtGui.QFont("Times", 10) font.setStyleStrategy(QtGui.QFont.ForceOutline) p.setFont(font) p.save() p.scale(0.1, 0.1) p.drawText(170, 180, f"Model: VSC-2000 (Very Small Chip) at {self.x}x{self.y}") p.drawText(170, 200, "Serial number: DLWR-WEER-123L-ZZ33-SDSJ") p.drawText(170, 220, "Manufacturer: Chip Manufacturer") p.restore() lines = [] if lod >= 0.5: for i in range(0, 11, 1 if lod > 0.5 else 2): lines.append(QtCore.QLineF(18 + 7*i, 13, 18 + 7*i, 5)) lines.append(QtCore.QLineF(18 + 7*i, 54, 18 + 7*i, 62)) for i in range(0, 7, 1 if lod > 0.5 else 2): lines.append(QtCore.QLineF(5, 18 + i*5, 13, 18 + i*5)) lines.append(QtCore.QLineF(94, 18 + i*5, 102, 18 + i*5)) if lod >= 0.4: lines.append(QtCore.QLineF(25, 35, 35, 35)) lines.append(QtCore.QLineF(35, 30, 35, 40)) lines.append(QtCore.QLineF(35, 30, 45, 35)) lines.append(QtCore.QLineF(35, 40, 45, 35)) lines.append(QtCore.QLineF(45, 30, 45, 40)) lines.append(QtCore.QLineF(45, 35, 55, 35)) p.drawLines(lines) # Draw red ink if self.stuff: p.setPen(QtGui.QPen(QtCore.Qt.red, 1, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin)) p.setBrush(QtCore.Qt.NoBrush) path = QtGui.QPainterPath() path.moveTo(self.stuff[0]) for point in self.stuff[1:]: path.lineTo(point) p.drawPath(path) p.setPen(oldPen) p.setBrush(oldBrush)