def drawTriangle(self, painter: QPainter) -> None: if not self.__showTriangle: return painter.save() painter.setPen(Qt.NoPen) painter.setBrush(self.__triangleColor) # 绘制在右侧中间,根据设定的倒三角的边长设定三个点位置 width: int = self.width() height: int = self.height() midWidth: int = width // 2 midHeight: int = height // 2 pts: QPolygon = QPolygon() if self.__trianglePosition == NavLabel.TrianglePosition.TrianglePosition_Left: pts.append(QPoint(self.__triangleLen, midHeight)) pts.append(QPoint(0, midHeight - self.__triangleLen)) pts.append(QPoint(0, midHeight + self.__triangleLen)) elif self.__trianglePosition == NavLabel.TrianglePosition.TrianglePosition_Right: pts.append(QPoint(width - self.__triangleLen, midHeight)) pts.append(QPoint(width, midHeight - self.__triangleLen)) pts.append(QPoint(width, midHeight + self.__triangleLen)) elif self.__trianglePosition == NavLabel.TrianglePosition.TrianglePosition_Top: pts.append(QPoint(midWidth, self.__triangleLen)) pts.append(QPoint(midWidth - self.__triangleLen, 0)) pts.append(QPoint(midWidth + self.__triangleLen, 0)) elif self.__trianglePosition == NavLabel.TrianglePosition.TrianglePosition_Bottom: pts.append(QPoint(midWidth, height - self.__triangleLen)) pts.append(QPoint(midWidth - self.__triangleLen, height)) pts.append(QPoint(midWidth + self.__triangleLen, height)) painter.drawPolygon(pts) painter.restore()
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 ): # c нуля перезаписываем paintEvent, который есть в родителе painter = QPainter(self) reset_brush = painter.brush() for figure in self.__figures: if not isinstance(figure, Figure): continue if isinstance(figure, Rectangle): painter.setBrush(QBrush(Qt.red)) painter.drawRect( figure.x, figure.y, figure.width, figure.height ) # не нужны круглые скобки, так как это свойства (property) continue if isinstance(figure, Ellipse): painter.setBrush(QBrush(Qt.green)) painter.drawEllipse(figure.x, figure.y, figure.width, figure.height) continue if isinstance(figure, CloseFigure): painter.setBrush(QBrush(Qt.blue)) points = [] # точки, которые будем передавать в drawPolygon # d - это список. Каждый поинт - отдельный словарь for point in figure.d: points.append(QPoint(point['x'], point['y'])) painter.drawPolygon(points) continue
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): painter = QPainter(self) reset_brush = painter.brush() for figure in self.__figures: if not isinstance(figure, Figure): continue if isinstance(figure, Rectangle): painter.setBrush(QBrush(Qt.red)) painter.drawRect(figure.x, figure.y, figure.width, figure.height) continue if isinstance(figure, Ellipse): # ждёт прямоугольник painter.setBrush(QBrush(Qt.green)) painter.drawEllipse(figure.x, figure.y, figure.width, figure.height) continue if isinstance(figure, CloseFigure): painter.setBrush(QBrush(Qt.blue)) points = [] for point in figure: points.append(QPoint(point['x'], point['y'])) painter.drawPolygon(points) continue
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: Optional[QWidget] = ...): painter.setPen(self.pen()) brush = self.brush() painter.setBrush(brush) path = QPainterPath() path.moveTo(self.__sourcePoint) path.cubicTo(self._edge1, self._edge2, self.__destPoint) if self._isDigraph: painter.setBrush(Qt.gray) painter.drawPolygon(self.drawArrow()) path.addPolygon(self.drawArrow()) painter.setBrush(Qt.NoBrush) if self.isSelected(): pen = painter.pen() pen.setColor(self.get_isSelectedPenColor()) else: pen = painter.pen() pen.setColor(self.get_noSelectedPenColor()) painter.setPen(pen) painter.drawPath(path) self.__path = path
def paintEvent(self, event): rect = QRect(0, 0, self.w - 1, self.h - 1) p = QPainter(self) p.setPen(QPen(Qt.black)) p.setBrush(self.color) if self.type == 1: p.drawRect(0, 0, self.w - 1, self.h - 1) p.drawText( rect, Qt.AlignCenter, self.text ) # 이게 더 좋은데? 어떤 방식이지? => # w.setGeometry(self.geometry()) 차이점이 무엇이지? elif self.type == 2: p.drawEllipse(0, 0, self.w - 1, self.h - 1) p.drawText(rect, Qt.AlignCenter, self.text) elif self.type == 3: p.drawEllipse(0, 0, self.w - 1, self.h - 1) p.drawText(rect, Qt.AlignCenter, self.text) elif self.type == 4: points = [ QPoint(self.w / 2, 0), QPoint(0, self.h / 2), QPoint(self.w / 2, self.h), QPoint(self.w, self.h / 2) ] poly = QPolygon(points) p.drawPolygon(poly) self.update() # self.update()
def paintEvent(self, event): painter = QPainter(self) painter.setPen(QPen(Qt.black, 5, Qt.SolidLine)) painter.setBrush(QBrush(Qt.black)) points = [ QPoint(100, 0), QPoint(0, 50), QPoint(100, 100), QPoint(200, 50) ] poly = QPolygon(points) painter.drawPolygon(poly)
def drawCurrentBg_ZFB(self, painter: QPainter) -> None: painter.save() # 圆半径为高度一定比例,计算宽度,将宽度等分 width: int = self.width() // self.__maxStep height: int = self.height() // 3 radius: int = height // 3 initX: int = width // 2 initY: int = self.height() // 2 # 绘制当前圆 painter.setPen(Qt.NoPen) painter.setBrush(self.__currentBackground) for i in range(self.__currentStep - 1): initX += width painter.drawEllipse(QPoint(initX, initY), radius, radius) initX = initX - width // 4 initY = 0 height = self.height() // 4 # 绘制当前上部提示信息背景 bgRect: QRect = QRect(initX, initY, width // 2, height) painter.setBrush(self.__currentBackground) painter.drawRoundedRect(bgRect, height / 2, height / 2) # 绘制当前上部提示信息 font: QFont = QFont() font.setPixelSize(height / 1.9) font.setBold(True) painter.setFont(font) painter.setPen(self.__currentForeground) painter.drawText(bgRect, Qt.AlignCenter, self.__topInfo[self.__currentStep - 1]) # 绘制倒三角 centerX: int = initX + width // 4 offset: int = 10 pts: QPolygon = QPolygon() pts.append(QPoint(centerX - offset, height)) pts.append(QPoint(centerX + offset, height)) pts.append(QPoint(centerX, height + offset)) painter.setPen(Qt.NoPen) painter.drawPolygon(pts) painter.restore()
def drawBg(self, painter: QPainter) -> None: width: int = self.width() height: int = self.height() endX: int = width - self.__arrowSize endY: int = height - self.__arrowSize painter.save() painter.setPen(Qt.NoPen) painter.setBrush(self.__background) pts: QPolygon = QPolygon() if self.__arrowPosition == NavLabel.ArrowPosition.ArrowPosition_Right: self.__bgRect = QRect(0, 0, endX, height) pts.append(QPoint(endX, int(height / 2 - self.__arrowSize))) pts.append(QPoint(endX, int(height / 2 + self.__arrowSize))) pts.append(QPoint(width, int(height / 2))) elif self.__arrowPosition == NavLabel.ArrowPosition.ArrowPosition_Left: self.__bgRect = QRect(self.__arrowSize, 0, width - self.__arrowSize, height) pts.append( QPoint(self.__arrowSize, int(height / 2 - self.__arrowSize))) pts.append( QPoint(self.__arrowSize, int(height / 2 + self.__arrowSize))) pts.append(QPoint(0, int(height / 2))) elif self.__arrowPosition == NavLabel.ArrowPosition.ArrowPosition_Bottom: self.__bgRect = QRect(0, 0, width, endY) pts.append(QPoint(int(width / 2 - self.__arrowSize), endY)) pts.append(QPoint(int(width / 2 + self.__arrowSize), endY)) pts.append(QPoint(int(width / 2), height)) elif self.__arrowPosition == NavLabel.ArrowPosition.ArrowPosition_Top: self.__bgRect = QRect(0, self.__arrowSize, width, height - self.__arrowSize) pts.append( QPoint(int(width / 2 - self.__arrowSize), self.__arrowSize)) pts.append( QPoint(int(width / 2 + self.__arrowSize), self.__arrowSize)) pts.append(QPoint(int(width / 2), 0)) # 绘制圆角矩形和三角箭头 if not self.__showArrow: bgRect = self.rect() painter.drawRoundedRect(self.__bgRect, self.__borderRadius, self.__borderRadius) else: painter.drawRoundedRect(self.__bgRect, self.__borderRadius, self.__borderRadius) painter.drawPolygon(pts) painter.restore()
def paintEvent(self, e): painter = QPainter(self) painter.setPen(QPen(Qt.blue, 7, Qt.DashDotLine)) painter.setBrush(QBrush(Qt.green, Qt.SolidPattern)) painter.drawEllipse(100, 100, 400, 200) points = QPolygon([ QPoint(10, 10), QPoint(10, 100), QPoint(100, 100), QPoint(100, 10), ]) painter.drawPolygon(points)
def paintEvent(self, e): painter = QPainter(self) painter.setPen(QPen(Qt.black, 2, Qt.DashDotLine)) painter.setBrush(QBrush(Qt.green, Qt.DiagCrossPattern)) painter.drawEllipse(100, 100, 400, 200) painter.drawRect(200, 150, 400, 200) points = QPolygon([ QPoint(10, 10), QPoint(10, 100), QPoint(100, 10), QPoint(100, 80) ]) painter.drawPolygon(points)
def paintEvent(self, e): """ Draws the color bar and arrow """ if self.item(): painter = QPainter(self) # Draws the color bar painter.setPen(Qt.NoPen) rect = QRect(0, 0, self.COLOR_BAR_WIDTH, self.COLOR_BAR_HEIGHT) painter.fillRect(rect, self.item().data(renderSetupRoles.NODE_COLOR_BAR)) oldBrush = painter.brush() if self.item().type( ) == renderSetup.RENDER_OVERRIDE_TYPE and self.item( ).isLocalRender(): diameter = rect.width() - 2 rect2 = QRect(rect.x() + 1, rect.y() + (rect.height() - diameter) / 2, diameter, diameter) brush = painter.brush() pen = painter.pen() hints = painter.renderHints() painter.setRenderHint(QPainter.Antialiasing, on=True) painter.setPen(Qt.NoPen) painter.setBrush( QBrush(QColor(67, 79, 70), style=Qt.SolidPattern)) painter.drawEllipse(rect2) painter.setRenderHints(hints) painter.setPen(pen) painter.setBrush(brush) # Draws the arrow painter.setBrush(self.ARROW_COLOR) if self.arrowPoints == BaseDelegate.EXPANDED_ARROW: painter.translate(self.EXPANDED_ARROW_OFFSET, 0.0) else: painter.translate(self.COLLAPSED_ARROW_OFFSET, 0.0) painter.drawPolygon(self.arrowPoints) painter.setBrush(oldBrush) # Draws the node type text painter.setPen(QPen(self.item().data(Qt.TextColorRole))) text = self.item().data(renderSetupRoles.NODE_TYPE_STR) painter.setFont(self.item().data(Qt.FontRole)) painter.drawText(self.NODE_TYPE_TEXT_RECT, text, QTextOption(Qt.AlignLeft | Qt.AlignVCenter))
class QPainterDrawText(unittest.TestCase): def setUp(self): self.painter = QPainter() self.text = "teste!" def tearDown(self): del self.text del self.painter def testDrawText(self): # bug #254 rect = self.painter.drawText(100, 100, 100, 100, Qt.AlignCenter | Qt.TextWordWrap, self.text) self.assert_(isinstance(rect, QRect)) def testDrawTextWithRect(self): # bug #225 rect = QRect(100, 100, 100, 100) newRect = self.painter.drawText(rect, Qt.AlignCenter | Qt.TextWordWrap, self.text) self.assert_(isinstance(newRect, QRect)) def testDrawTextWithRectF(self): """QPainter.drawText(QRectF, ... ,QRectF*) inject code""" rect = QRectF(100, 52.3, 100, 100) newRect = self.painter.drawText(rect, Qt.AlignCenter | Qt.TextWordWrap, self.text) self.assert_(isinstance(newRect, QRectF)) def testDrawOverloads(self): """Calls QPainter.drawLines overloads, if something is wrong Exception and chaos ensues. Bug #395""" self.painter.drawLines([QLine(QPoint(0, 0), QPoint(1, 1))]) self.painter.drawLines([QPoint(0, 0), QPoint(1, 1)]) self.painter.drawLines([QPointF(0, 0), QPointF(1, 1)]) self.painter.drawLines([QLineF(QPointF(0, 0), QPointF(1, 1))]) self.painter.drawPoints([QPoint(0, 0), QPoint(1, 1)]) self.painter.drawPoints([QPointF(0, 0), QPointF(1, 1)]) self.painter.drawConvexPolygon( [QPointF(10.0, 80.0), QPointF(20.0, 10.0), QPointF(80.0, 30.0), QPointF(90.0, 70.0)] ) self.painter.drawConvexPolygon([QPoint(10.0, 80.0), QPoint(20.0, 10.0), QPoint(80.0, 30.0), QPoint(90.0, 70.0)]) self.painter.drawPolygon([QPointF(10.0, 80.0), QPointF(20.0, 10.0), QPointF(80.0, 30.0), QPointF(90.0, 70.0)]) self.painter.drawPolygon([QPoint(10.0, 80.0), QPoint(20.0, 10.0), QPoint(80.0, 30.0), QPoint(90.0, 70.0)]) self.painter.drawPolyline([QPointF(10.0, 80.0), QPointF(20.0, 10.0), QPointF(80.0, 30.0), QPointF(90.0, 70.0)]) self.painter.drawPolyline([QPoint(10.0, 80.0), QPoint(20.0, 10.0), QPoint(80.0, 30.0), QPoint(90.0, 70.0)])
def paintEvent(self, event): rect = QRect(0, 0, self.w - 1, self.h - 1) p = QPainter(self) p.setPen(QPen(Qt.black)) p.setBrush(self.color) if self.type == 1: # xc = self.x + self.w / 2 # 돌리니깐 글자도 같이 돌아가브네.. # yc = self.y + self.h / 2 # p.translate(xc, yc) # p.rotate(45) p.drawRect(0, 0, self.w-1, self.h-1) p.drawText(rect, Qt.AlignCenter, self.text) # 이게 더 좋은데? 어떤 방식이지? => # w.setGeometry(self.geometry()) 차이점이 무엇이지? elif self.type == 2: p.drawEllipse(0, 0, self.w-1, self.h-1) p.drawText(rect, Qt.AlignCenter, self.text) elif self.type == 3: p.drawEllipse(0, 0, self.w-1, self.h-1) p.drawText(rect, Qt.AlignCenter, self.text) elif self.type == 4: p.drawPolygon(self.polygon) self.update() # self.update()
def drawTriangle(self, painter: QPainter) -> None: """ """ if not self.__showTriangle: return # 选中或者悬停显示 if (not self.__hover) and (not self.isChecked()): return painter.save() painter.setPen(Qt.NoPen) painter.setBrush(self.__triangleColor) # 绘制在右侧中间,根据设定的倒三角的边长设定三个点位置 width: int = self.width() height: int = self.height() midWidth: int = width // 2 midHeight: int = height // 2 pts: QPolygon = QPolygon() if self.__trianglePosition == NavButton.TrianglePosition.TRIANGLEPOSITION_LEFT: pts.setPoints(3, self.__triangleLen, midHeight, 0, midHeight - self.__triangleLen, 0, midHeight + self.__triangleLen) elif self.__trianglePosition == NavButton.TrianglePosition.TRIANGLEPOSITION_RIGHT: pts.setPoints(3, width - self.__triangleLen, midHeight, width, midHeight - self.__triangleLen, width, midHeight + self.__triangleLen) elif self.__trianglePosition == NavButton.TrianglePosition.TRIANGLEPOSITION_TOP: pts.setPoints(3, midWidth, self.__triangleLen, midWidth - self.__triangleLen, 0, midWidth + self.__triangleLen, 0) elif self.__trianglePosition == NavButton.TrianglePosition.TRIANGLEPOSITION_BOTTOM: pts.setPoints(3, midWidth, height - self.__triangleLen, midWidth - self.__triangleLen, height, midWidth + self.__triangleLen, height) painter.drawPolygon(pts) painter.restore()
def drawTip(self, painter: QPainter) -> None: painter.save() painter.setPen(Qt.NoPen) painter.setBrush( QBrush(self.__tipColor if self.__valueBrush == Qt.NoBrush else self.__valueBrush)) # 计算当前值对应的百分比 step: float = self.__value / (self.__maxValue - self.__minValue) progress: int = int((self.width() - self.__padding * 2) * step) # 绘制上部分提示信息背景 rect: QRect = QRect(progress, 0, self.__padding * 2, int(self.height() / 2.1)) painter.drawRoundedRect(rect, 2, 2) # 绘制倒三角 centerX: int = rect.center().x() initY: int = rect.height() offset: int = 5 pts: QPolygon = QPolygon() pts.append(QPoint(centerX - offset, initY)) pts.append(QPoint(centerX + offset, initY)) pts.append(QPoint(centerX, initY + offset)) painter.drawPolygon(pts) # 绘制文字 strValue: str = '' if self.__percent: temp: float = self.__value / (self.__maxValue - self.__minValue) * 100 strValue = "%s%%" % int(temp) else: strValue = "%s" % int(self.__value) painter.setPen(self.__textColor) painter.drawText(rect, Qt.AlignCenter, strValue) painter.restore()
def draw(self, painter: QPainter, block_length: int): self.pixel_length = block_length / Block.WIDTH self.scroll_brush = QBrush(Qt.blue) self.scroll_pen = QPen(self.scroll_brush, 2 * self.pixel_length) self.acceleration_brush = QBrush(Qt.red) self.acceleration_pen = QPen(self.acceleration_brush, 2 * self.pixel_length) painter.setPen(self.scroll_pen) painter.setBrush(self.scroll_brush) auto_scroll_type_index = self.auto_scroll_row >> 4 auto_scroll_routine_index = self.auto_scroll_row & 0b0001_1111 if auto_scroll_type_index in [ SPIKE_CEILING_SCROLL, UP_TIL_DOOR_SCROLL, WATER_LEVEL_SCROLL, UP_RIGHT_DIAG_SCROLL, ]: # not visualized return elif auto_scroll_type_index not in [ HORIZONTAL_SCROLL_0, HORIZONTAL_SCROLL_1 ]: # illegal value, those appear in the vanilla ROM, though; so error out return first_movement_command_index = (self.rom.int( AScroll_HorizontalInitMove + auto_scroll_routine_index) + 1) % 256 last_movement_command_index = (self.rom.int( AScroll_HorizontalInitMove + auto_scroll_routine_index + 1)) % 256 self.horizontal_speed = 0 self.vertical_speed = 0 self.current_pos = self._determine_auto_scroll_start(block_length) for movement_command_index in range(first_movement_command_index, last_movement_command_index + 1): movement_command = self.rom.int(AScroll_Movement + movement_command_index) movement_repeat = self.rom.int(AScroll_MovementRepeat + movement_command_index) self._execute_movement_command(painter, movement_command, movement_repeat) stop_marker = QRectF(QPoint(0, 0), QSizeF(10, 10) * self.pixel_length) stop_marker.moveCenter(self.current_pos) painter.setPen(Qt.NoPen) painter.drawRect(stop_marker) painter.setPen(self.scroll_pen) painter.setBrush(self.scroll_brush) painter.setOpacity(0.2) painter.drawPolygon(self.screen_polygon)
class QPainterDrawText(unittest.TestCase): def setUp(self): self.painter = QPainter() self.text = 'teste!' def tearDown(self): del self.text del self.painter def testDrawText(self): # bug #254 rect = self.painter.drawText(100, 100, 100, 100, Qt.AlignCenter | Qt.TextWordWrap, self.text) self.assertTrue(isinstance(rect, QRect)) def testDrawTextWithRect(self): # bug #225 rect = QRect(100, 100, 100, 100) newRect = self.painter.drawText(rect, Qt.AlignCenter | Qt.TextWordWrap, self.text) self.assertTrue(isinstance(newRect, QRect)) def testDrawTextWithRectF(self): '''QPainter.drawText(QRectF, ... ,QRectF*) inject code''' rect = QRectF(100, 52.3, 100, 100) newRect = self.painter.drawText(rect, Qt.AlignCenter | Qt.TextWordWrap, self.text) self.assertTrue(isinstance(newRect, QRectF)) def testDrawOverloads(self): '''Calls QPainter.drawLines overloads, if something is wrong Exception and chaos ensues. Bug #395''' self.painter.drawLines([QLine(QPoint(0,0), QPoint(1,1))]) self.painter.drawLines([QPoint(0,0), QPoint(1,1)]) self.painter.drawLines([QPointF(0,0), QPointF(1,1)]) self.painter.drawLines([QLineF(QPointF(0,0), QPointF(1,1))]) self.painter.drawPoints([QPoint(0,0), QPoint(1,1)]) self.painter.drawPoints([QPointF(0,0), QPointF(1,1)]) self.painter.drawConvexPolygon([QPointF(10.0, 80.0), QPointF(20.0, 10.0), QPointF(80.0, 30.0), QPointF(90.0, 70.0)]) self.painter.drawConvexPolygon([QPoint(10.0, 80.0), QPoint(20.0, 10.0), QPoint(80.0, 30.0), QPoint(90.0, 70.0)]) self.painter.drawPolygon([QPointF(10.0, 80.0), QPointF(20.0, 10.0), QPointF(80.0, 30.0), QPointF(90.0, 70.0)]) self.painter.drawPolygon([QPoint(10.0, 80.0), QPoint(20.0, 10.0), QPoint(80.0, 30.0), QPoint(90.0, 70.0)]) self.painter.drawPolyline([QPointF(10.0, 80.0), QPointF(20.0, 10.0), QPointF(80.0, 30.0), QPointF(90.0, 70.0)]) self.painter.drawPolyline([QPoint(10.0, 80.0), QPoint(20.0, 10.0), QPoint(80.0, 30.0), QPoint(90.0, 70.0)])
def _test_pipedimagerpq(): # vertices of a pentagon (roughly) centered in a 1000 x 1000 square pentagonpts = ( (504.5, 100.0), (100.0, 393.9), (254.5, 869.4), (754.5, 869.4), (909.0, 393.9), ) linepts = ((350, 50), (200, 150), (400, 250), (300, 350), (150, 250), (100, 450)) # start PyQt testapp = QApplication(["PipedImagerPQ"]) # create the list of commands to submit drawcmnds = [] drawcmnds.append({"action": "setTitle", "title": "Tester"}) drawcmnds.append({"action": "show"}) drawcmnds.append({"action": "clear", "color": "black"}) drawcmnds.append({"action": "screenInfo"}) # create the image to be displayed testimage = QImage(500, 500, QImage.Format_ARGB32_Premultiplied) # initialize a black background testimage.fill(0xFF000000) # draw some things in the image testpainter = QPainter(testimage) testpainter.setBrush(QBrush(QColor(0, 255, 0, 128), Qt.SolidPattern)) testpainter.setPen( QPen(QBrush(QColor(255, 0, 0, 255), Qt.SolidPattern), 5.0, Qt.SolidLine, Qt.SquareCap, Qt.MiterJoin)) testpainter.drawRect(QRectF(5.0, 255.0, 240.0, 240.0)) testpainter.setBrush(QBrush(QColor(0, 0, 255, 255), Qt.SolidPattern)) testpainter.setPen( QPen(QBrush(QColor(0, 0, 0, 255), Qt.SolidPattern), 5.0, Qt.DashLine, Qt.RoundCap, Qt.RoundJoin)) testpainter.drawPolygon( QPolygonF([ QPointF(.25 * ptx, .25 * pty + 250) for (ptx, pty) in pentagonpts ])) testpainter.setBrush(Qt.NoBrush) testpainter.setPen( QPen(QBrush(QColor(255, 255, 255, 255), Qt.SolidPattern), 3.0, Qt.DashLine, Qt.RoundCap, Qt.RoundJoin)) testpainter.drawPolyline( QPolygonF([QPointF(pts, pty) for (pts, pty) in linepts])) testpainter.end() # add the image command testimgwidth = testimage.width() testimgheight = testimage.height() testimgstride = testimage.bytesPerLine() # not a good way to get the pixel data testimgdata = bytearray(testimgheight * testimgstride) k = 0 for pty in range(testimgheight): for ptx in range(testimgwidth): pixval = testimage.pixel(ptx, pty) (aval, rgbval) = divmod(pixval, 256 * 256 * 256) (rval, gbval) = divmod(rgbval, 256 * 256) (gval, bval) = divmod(gbval, 256) testimgdata[k] = bval k += 1 testimgdata[k] = gval k += 1 testimgdata[k] = rval k += 1 testimgdata[k] = aval k += 1 testblocksize = 4000 testnumblocks = (testimgheight * testimgstride + testblocksize - 1) // testblocksize drawcmnds.append({ "action": "newImage", "width": testimgwidth, "height": testimgheight, "stride": testimgstride }) for k in range(testnumblocks): if k < (testnumblocks - 1): blkdata = testimgdata[k * testblocksize:(k + 1) * testblocksize] else: blkdata = testimgdata[k * testblocksize:] drawcmnds.append({ "action": "newImage", "blocknum": k + 1, "numblocks": testnumblocks, "startindex": k * testblocksize, "blockdata": blkdata }) # finish the command list drawcmnds.append({"action": "show"}) drawcmnds.append({"action": "exit"}) # create a PipedImagerPQ in this process (cmndrecvpipe, cmndsendpipe) = multiprocessing.Pipe(False) (rspdrecvpipe, rspdsendpipe) = multiprocessing.Pipe(False) testviewer = PipedImagerPQ(cmndrecvpipe, rspdsendpipe) # create a command submitter dialog tester = _CommandSubmitterPQ(testviewer, cmndsendpipe, rspdrecvpipe, drawcmnds) tester.show() # let it all run testresult = testapp.exec_() if testresult != 0: sys.exit(testresult)