def paintEvent(self, event): rect = QRect(10, 20, 80, 60) path = QPainterPath() path.moveTo(20, 80) path.lineTo(20, 30) path.cubicTo(80, 0, 50, 50, 80, 80) startAngle = 30 * 16 arcLength = 120 * 16 painter = QPainter(self) painter.setPen(self.pen) painter.setBrush(self.brush) if self.antialiased: painter.setRenderHint(QPainter.Antialiasing) for x in range(0, self.width(), 100): for y in range(0, self.height(), 100): painter.save() painter.translate(x, y) if self.transformed: painter.translate(50, 50) painter.rotate(60.0) painter.scale(0.6, 0.9) painter.translate(-50, -50) if self.shape == RenderArea.Line: painter.drawLine(rect.bottomLeft(), rect.topRight()) elif self.shape == RenderArea.Points: painter.drawPoints(RenderArea.points) elif self.shape == RenderArea.Polyline: painter.drawPolyline(RenderArea.points) elif self.shape == RenderArea.Polygon: painter.drawPolygon(RenderArea.points) elif self.shape == RenderArea.Rect: painter.drawRect(rect) elif self.shape == RenderArea.RoundedRect: painter.drawRoundedRect(rect, 25, 25, Qt.RelativeSize) elif self.shape == RenderArea.Ellipse: painter.drawEllipse(rect) elif self.shape == RenderArea.Arc: painter.drawArc(rect, startAngle, arcLength) elif self.shape == RenderArea.Chord: painter.drawChord(rect, startAngle, arcLength) elif self.shape == RenderArea.Pie: painter.drawPie(rect, startAngle, arcLength) elif self.shape == RenderArea.Path: painter.drawPath(path) elif self.shape == RenderArea.Text: painter.drawText(rect, Qt.AlignCenter, "PySide 2\nQt %s" % qVersion()) elif self.shape == RenderArea.Pixmap: painter.drawPixmap(10, 10, self.pixmap) painter.restore() painter.setPen(self.palette().dark().color()) painter.setBrush(Qt.NoBrush) painter.drawRect(QRect(0, 0, self.width() - 1, self.height() - 1))
def paintEvent(self, event): paint = QPainter() paint.begin(self) paint.save() size = self.size() width = size.width() height = size.height() units = self.data.get('units') proportion = self.getProportion() horizontalOrientation = self.data.get('horizontal_orientation') if horizontalOrientation: rulerLength = width traceLengthLimit = height else: # vertical orientation rulerLength = height traceLengthLimit = width paint.translate(width, 0) paint.rotate(90) # the length of the traces (lines) small = traceLengthLimit / 6 medium = traceLengthLimit / 4 large = traceLengthLimit / 3 limit = rulerLength / proportion # draw less lines for centimeters if units == 'cm': step = 10 else: step = 5 # begin drawing fontSize = 10 font = QFont('Serif', fontSize) fontMetrics = QFontMetrics(font) # draw background background = self.data.get('background_color') paint.fillRect(0, 0, rulerLength, traceLengthLimit, background) # draw the lines paint.setPen(self.data.get('lines_color')) paint.setFont(font) # the builtin range() doesn't support floats def float_range(current, end, rangeStep): while current < end: yield current current += rangeStep # we skip 0 and start in the first step, since there's no point in drawing the first line/text (it would appear cut off, since we're at the limit) for a in float_range(step, limit, step): position = a * proportion if (a % 100) == 0: lineLength = large if units == 'px': text = '{}{}'.format(str(a), units) else: text = '{}{}'.format(str(int(a / 100)), units) textWidth = fontMetrics.boundingRect(text).width() paint.drawText(position - textWidth / 2, traceLengthLimit / 2 + fontSize / 2, text) elif (a % 50) == 0: lineLength = large # since 'cm' has a different step compared to the other units if units == 'cm': lineLength = medium elif (a % 25) == 0: lineLength = medium else: lineLength = small paint.drawLine(position, 0, position, lineLength) paint.drawLine(position, traceLengthLimit, position, traceLengthLimit - lineLength) # paint the division lines if self.data.get('division_lines'): paint.setPen(self.data.get('divisions_color')) halfPoint = rulerLength / 2 quarterPoint = rulerLength / 4 threeQuarterPoint = 3 / 4 * rulerLength paint.drawLine(quarterPoint, 0, quarterPoint, traceLengthLimit) paint.drawLine(halfPoint, 0, halfPoint, traceLengthLimit) paint.drawLine(threeQuarterPoint, 0, threeQuarterPoint, traceLengthLimit) paint.restore() paint.end()