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 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 updateShape(self, point=None, pos=None): # Main arrow direction params = parameters.instance scale = self.scale ms = min(scale) head_size = params.arrow_head_size * ms width = params.arrow_line_size * ms self.prepareGeometryChange() if point == self.source: p1 = pos else: p1 = self.source.pos() self.setPos(p1) if point == self.target: p2 = pos else: p2 = self.target.pos() tip = p2 - p1 if abs(tip.x()) > 0.001 or abs(tip.y()) > 0.001: # Normalised #ntip = tip/stip ntip = tip # Arrow head base orth = QPointF(ntip.y(), -ntip.x()) * (head_size * 0.5) base_center = tip * (1 - 2 * head_size) base1 = base_center + orth base2 = base_center - orth base_center = tip * (1 - head_size * 1.50) else: ntip = tip base_center = tip base1 = tip base2 = tip self.tip = tip self.base_center = base_center self.base1 = base1 self.base2 = base2 path = QPainterPath() path.lineTo(base_center) path.addPolygon(QPolygonF([base_center, base1, tip, base2])) path.closeSubpath() self.rect = path.controlPointRect().adjusted(-width, -width, 2 * width, 2 * width) self.path = path self.update()
def updateShape(self, point=None, pos=None): # Main arrow direction params = parameters.instance scale = self.scale ms = min(scale) head_size = params.arrow_head_size*ms width = params.arrow_line_size*ms self.prepareGeometryChange() if point == self.source: p1 = pos else: p1 = self.source.pos() self.setPos(p1) if point == self.target: p2 = pos else: p2 = self.target.pos() tip = p2-p1 if abs(tip.x()) > 0.001 or abs(tip.y()) > 0.001: # Normalised #ntip = tip/stip ntip = tip # Arrow head base orth = QPointF(ntip.y(), -ntip.x())*(head_size*0.5) base_center = tip*(1-2*head_size) base1 = base_center + orth base2 = base_center - orth base_center = tip*(1-head_size*1.50) else: ntip = tip base_center = tip base1 = tip base2 = tip self.tip = tip self.base_center = base_center self.base1 = base1 self.base2 = base2 path = QPainterPath() path.lineTo(base_center) path.addPolygon(QPolygonF([base_center, base1, tip, base2])) path.closeSubpath() self.rect = path.controlPointRect().adjusted(-width, -width, 2*width, 2*width) self.path = path self.update()
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: self.positionChanged.emit() self.positionChanged[QPointF].emit(value) return QGraphicsObject.itemChange(self, change, value)