def draw(self, painter, scale): painter.save() painter.setPen(QPen(QBrush(QColor(232, 109, 21) if self.state is self.add_point else QColor(21, 144, 232)), self.lineWidth(scale), Qt.SolidLine)) painter.drawPolygon(QPolygon(self.polygon)) for i, poly in enumerate(self.convex_polygons): if poly: painter.setPen(QPen(QBrush(Figure.COLORS[i % len(Figure.COLORS)]), self.lineWidth(scale), Qt.SolidLine)) color = Figure.COLORS[i % len(Figure.COLORS)] color.setAlpha(150) painter.setBrush(QBrush(QColor(color))) painter.drawPolygon(QPolygon(poly)) for point in self.polygon: self.drawControlPoint(painter, point, QColor(31, 174, 222) if point is self.polygon[0] else QColor(222, 79, 31) if point is self.polygon[-1] else QColor(78, 222, 31), scale) Figure.draw(self, painter, scale) painter.restore()
def update_spiral(self): # create a polygon providing the corner points of the spiral polygon = QPolygon() for i in xrange(self.window().width()): x = int(math.cos(i * 0.16) * i) y = int(math.sin(i * 0.16) * i) polygon.append(QPoint(x, y)) return polygon
def QtRectToPoly(rect): x1,y1,x2,y2 = rect.getCoords() poly = QPolygon() poly.push_back(QPoint(x1,y1)) poly.push_back(QPoint(x2,y1)) poly.push_back(QPoint(x2,y2)) poly.push_back(QPoint(x1,y2)) return poly
def paintEvent(self, pe): if self.left_view and self.right_view and self.right_view.model(): vr = self.left_view.visualRect(self.left_view.currentIndex()) self.left_top = self.mapFromGlobal( self.left_view.viewport().mapToGlobal(vr.topRight())).y() self.left_bottom = self.mapFromGlobal( self.left_view.viewport().mapToGlobal(vr.bottomRight())).y() vr_top = self.right_view.visualRect(self.right_view.model().index( 0, 0)) vr = self.right_view.visualRect(self.right_view.model().index( self.right_view.model().rowCount() - 1, 0)) self.right_top = self.mapFromGlobal( self.left_view.viewport().mapToGlobal(vr_top.topLeft())).y() self.right_bottom = self.mapFromGlobal( self.left_view.viewport().mapToGlobal(vr.bottomLeft())).y() w = self.minimumWidth() - 1 p = QPainter(self) p.setBrush(QBrush(QColor(210, 255, 210))) pen = QPen() pen.setColor(Qt.transparent) p.setPen(pen) poly = QPolygon() poly.append(QPoint(0, self.left_top)) poly.append(QPoint(w, self.right_top)) poly.append(QPoint(w, self.right_bottom)) poly.append(QPoint(0, self.left_bottom)) p.drawConvexPolygon(poly) p.setRenderHint(QPainter.Antialiasing) pen.setColor(Qt.GlobalColor.black) pen.setWidth(2) p.setPen(pen) p.drawLine(0, self.left_top, w, self.right_top) p.drawLine(0, self.left_bottom, w, self.right_bottom)
def QtRectToPoly(rect): x1, y1, x2, y2 = rect.getCoords() poly = QPolygon() poly.push_back(QPoint(x1, y1)) poly.push_back(QPoint(x2, y1)) poly.push_back(QPoint(x2, y2)) poly.push_back(QPoint(x1, y2)) return poly
def setUp(self): self.original = QPolygon([QPoint(1, 2), QPoint(3, 4), QPoint(5, 6)])
def drawArrow(element, painter, option): # From windows style but modified to enable AA if option.rect.width() <= 1 or option.rect.height() <= 1: return r = option.rect size = min(r.height(), r.width()) pixmap = QPixmap() pixmapName = "arrow-{0}-{1}-{2}-{3}-{4}".format( "$qt_ia", int(option.state), element, size, option.palette.cacheKey() ) if not QPixmapCache.find(pixmapName, pixmap): border = size / 5 sqsize = 2 * (size / 2) image = QImage(sqsize, sqsize, QImage.Format_ARGB32) image.fill(Qt.transparent) imagePainter = QPainter(image) imagePainter.setRenderHint(QPainter.Antialiasing, True) imagePainter.translate(0.5, 0.5) a = QPolygon() if element == QStyle.PE_IndicatorArrowUp: a.setPoints(3, border, sqsize / 2, sqsize / 2, border, sqsize - border, sqsize / 2) elif element == QStyle.PE_IndicatorArrowDown: a.setPoints(3, border, sqsize / 2, sqsize / 2, sqsize - border, sqsize - border, sqsize / 2) elif element == QStyle.PE_IndicatorArrowRight: a.setPoints(3, sqsize - border, sqsize / 2, sqsize / 2, border, sqsize / 2, sqsize - border) elif element == QStyle.PE_IndicatorArrowLeft: a.setPoints(3, border, sqsize / 2, sqsize / 2, border, sqsize / 2, sqsize - border) else: pass bsx = 0 bsy = 0 if option.state & QStyle.State_Sunken: bsx = qApp.style().pixelMetric(QStyle.PM_ButtonShiftHorizontal) bsy = qApp.style().pixelMetric(QStyle.PM_ButtonShiftVertical) bounds = a.boundingRect() sx = sqsize / 2 - bounds.center().x() - 1 sy = sqsize / 2 - bounds.center().y() - 1 imagePainter.translate(sx + bsx, sy + bsy) if not (option.state & QStyle.State_Enabled): imagePainter.setBrush(option.palette.mid().color()) imagePainter.setPen(option.palette.mid().color()) else: shadow = QColor(0, 0, 0, 100) imagePainter.translate(0, 1) imagePainter.setPen(shadow) imagePainter.setBrush(shadow) foreGround = QColor(255, 255, 255, 210) imagePainter.drawPolygon(a) imagePainter.translate(0, -1) imagePainter.setPen(foreGround) imagePainter.setBrush(foreGround) imagePainter.drawPolygon(a) imagePainter.end() pixmap = QPixmap.fromImage(image) QPixmapCache.insert(pixmapName, pixmap) xOffset = r.x() + (r.width() - size) / 2 yOffset = r.y() + (r.height() - size) / 2 painter.drawPixmap(xOffset, yOffset, pixmap)