def drawForeground(self, painter: QtGui.QPainter, rect: QtCore.QRectF): """Draw the foreground pixmap, updates if None.""" if self.foreground_pixmap is None: self.updateForeground(rect) painter.save() painter.resetTransform() # Draw the actual overlay painter.drawPixmap(0, 0, self.foreground_pixmap) painter.restore()
def paint( self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionGraphicsItem, widget: QtWidgets.QWidget = None, ): view = next(iter(self.scene().views())) painter.setRenderHint(QtGui.QPainter.Antialiasing) painter.setFont(self.font) painter.setPen(self.pen) fm = painter.fontMetrics() painter.drawLine(self.line) if not self.line.p1().isNull(): pen = QtGui.QPen(self.pen) pen.setWidth(10) painter.setPen(pen) painter.drawPoints([self.line.p1(), self.line.p2()]) painter.setPen(self.pen) if view is not None and self.text != "": angle = self.line.angle() if 90 < angle < 270: angle -= 180 center = view.mapFromScene(self.line.center()) length = ( view.mapFromScene(QtCore.QRectF(0, 0, self.line.length(), 1)) .boundingRect() .width() ) width = fm.boundingRect(self.text).width() if width < length * 0.9: painter.save() painter.resetTransform() transform = QtGui.QTransform() transform.translate(center.x(), center.y()) transform.rotate(-angle) painter.setTransform(transform) painter.drawText(-width / 2.0, -fm.descent(), self.text) painter.restore()
def paint( self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionGraphicsItem, widget: QtWidgets.QWidget = None, ): super().paint(painter, option, widget) if self.isSelected(): painter.fillRect(self.rect(), QtGui.QBrush(QtGui.QColor(255, 255, 255, 32))) painter.setFont(self.font) fm = painter.fontMetrics() pos = QtCore.QPointF(self.rect().left(), self.rect().top()) pos = painter.transform().map(pos) painter.save() painter.resetTransform() painter.setPen(self.pen()) painter.drawText(pos.x() + 5, pos.y() + fm.ascent(), self.label) painter.restore()