def test_shapeFromPath(self): path = QPainterPath() path.addRect(10, 10, 20, 20) pen = QPen(QColor("#FFF"), 2.0) path = shapeFromPath(path, pen) self.assertGreaterEqual(area(path.controlPointRect()), (20 + 2.0)**2)
def boundingRect(self): """ Returns the shape bounding rect. :rtype: QRectF """ path = QPainterPath() path.addPath(self.selection) path.addPolygon(self.head) for shape in self.handles: path.addEllipse(shape) for shape in self.anchors.values(): path.addEllipse(shape) return path.controlPointRect()
class ControlButton(QWidget): def __init__(self, index, name, func, parent): super().__init__(parent) self.index = index self.name = name self.function = func self.resize(TILE_WIDTH << 2, TILE_HEIGHT << 2) self.show() self.opacity = None self.setMouseTracking(True) self.color = QColor(50 * self.index, 255, 255 - 10 * self.index) self.gradient = QRadialGradient( 50 + 10 * self.index, 50 - 10 * self.index, 500 - 50 * self.index, 50 + 10 * self.index, 100 - 10 * self.index) self.gradient.setColorAt(0, QColor(200, 200, 200)) self.gradient.setColorAt(0.5, QColor(50 * self.index, 255, 255 - 10 * self.index)) self.gradient.setColorAt(1, QColor(200, 200, 200)) self.brush = QBrush(self.gradient) self.path = QPainterPath() rect = QRectF(TILE_WIDTH * 1.5, TILE_HEIGHT * 1.5, TILE_WIDTH * 1.5, TILE_HEIGHT * 1.5) total = self.parent().total - 1 if total < 3: total = 3 if self.index == 0: self.path.arcMoveTo(rect, 90) self.path.arcTo(rect, 90, 360) else: self.path.arcMoveTo(QRectF(self.rect()), 90 + self.index * 360 // total) self.path.arcTo(QRectF(self.rect()), 90 + self.index * 360 // total, 360 // total) self.path.arcTo(rect, 90 + self.index * 360 // total + 360 // total, -360 // total) self.path.arcTo(QRectF(self.rect()), 90 + self.index * 360 // total, 0) def paintEvent(self, event): painter = QPainter(self) painter.setBrush(self.brush) if self.opacity is None or self.parent().vanishing: painter.setOpacity(self.parent().opacity / 255) else: painter.setOpacity(self.opacity / 255) pen = QPen(QColor(100, 100, 100, 150)) pen.setWidth(10) painter.setPen(pen) painter.drawPath(self.path) painter.setPen(QColor(0, 0, 0)) painter.drawText(self.path.controlPointRect(), Qt.AlignCenter, self.name)
class GraphicsPathObject(QGraphicsObject): """A QGraphicsObject subclass implementing an interface similar to QGraphicsPathItem, and also adding a positionChanged() signal """ positionChanged = Signal([], ["QPointF"]) def __init__(self, parent=None, **kwargs): QGraphicsObject.__init__(self, parent, **kwargs) self.setFlag(QGraphicsObject.ItemSendsGeometryChanges) self.__path = QPainterPath() self.__brush = QBrush(Qt.NoBrush) self.__pen = QPen() self.__boundingRect = None def setPath(self, path): """Set the items `path` (:class:`QPainterPath`). """ if not isinstance(path, QPainterPath): raise TypeError("%r, 'QPainterPath' expected" % type(path)) if self.__path != path: self.prepareGeometryChange() # Need to store a copy of object so the shape can't be mutated # without properly updating the geometry. self.__path = QPainterPath(path) self.__boundingRect = None self.update() def path(self): """Return the items path. """ return QPainterPath(self.__path) def setBrush(self, brush): """Set the items `brush` (:class:`QBrush`) """ if not isinstance(brush, QBrush): brush = QBrush(brush) if self.__brush != brush: self.__brush = QBrush(brush) self.update() def brush(self): """Return the items brush. """ return QBrush(self.__brush) def setPen(self, pen): """Set the items outline `pen` (:class:`QPen`). """ if not isinstance(pen, QPen): pen = QPen(pen) if self.__pen != pen: self.prepareGeometryChange() self.__pen = QPen(pen) self.__boundingRect = None self.update() def pen(self): """Return the items pen. """ return QPen(self.__pen) def paint(self, painter, option, widget=None): if self.__path.isEmpty(): return painter.save() painter.setPen(self.__pen) painter.setBrush(self.__brush) painter.drawPath(self.__path) painter.restore() def boundingRect(self): if self.__boundingRect is None: br = self.__path.controlPointRect() pen_w = self.__pen.widthF() self.__boundingRect = br.adjusted(-pen_w, -pen_w, pen_w, pen_w) return self.__boundingRect def shape(self): return shapeFromPath(self.__path, self.__pen) def itemChange(self, change, value): if change == QGraphicsObject.ItemPositionHasChanged: pos = qunwrap(value) self.positionChanged.emit() self.positionChanged[QPointF].emit(pos) return QGraphicsObject.itemChange(self, change, value)