def setShapeRect(self, rect): """ Set the item's shape `rect`. The item should be confined within this rect. """ path = QPainterPath() path.addEllipse(rect) self.setPath(path) self.__shapeRect = rect
def test_graphicspathobject(self): obj = GraphicsPathObject() path = QPainterPath() obj.setFlag(GraphicsPathObject.ItemIsMovable) path.addEllipse(20, 20, 50, 50) obj.setPath(path) self.assertEqual(obj.path(), path) self.assertTrue(obj.path() is not path, msg="setPath stores the path not a copy") brect = obj.boundingRect() self.assertTrue(brect.contains(path.boundingRect())) with self.assertRaises(TypeError): obj.setPath("This is not a path") brush = QBrush(QColor("#ffbb11")) obj.setBrush(brush) self.assertEqual(obj.brush(), brush) self.assertTrue(obj.brush() is not brush, "setBrush stores the brush not a copy") pen = QPen(QColor("#FFFFFF"), 1.4) obj.setPen(pen) self.assertEqual(obj.pen(), pen) self.assertTrue(obj.pen() is not pen, "setPen stores the pen not a copy") brect = obj.boundingRect() self.assertGreaterEqual(area(brect), (50 + 1.4 * 2) ** 2) self.assertIsInstance(obj.shape(), QPainterPath) positions = [] obj.positionChanged[QPointF].connect(positions.append) pos = QPointF(10, 10) obj.setPos(pos) self.assertEqual(positions, [pos]) self.scene.addItem(obj) self.view.show() self.app.exec_()
def test_layout(self): file_desc, disc_desc, bayes_desc = self.widget_desc() file_item = NodeItem() file_item.setWidgetDescription(file_desc) file_item.setPos(0, 150) self.scene.add_node_item(file_item) bayes_item = NodeItem() bayes_item.setWidgetDescription(bayes_desc) bayes_item.setPos(200, 0) self.scene.add_node_item(bayes_item) disc_item = NodeItem() disc_item.setWidgetDescription(disc_desc) disc_item.setPos(200, 300) self.scene.add_node_item(disc_item) link = LinkItem() link.setSourceItem(file_item) link.setSinkItem(disc_item) self.scene.add_link_item(link) link = LinkItem() link.setSourceItem(file_item) link.setSinkItem(bayes_item) self.scene.add_link_item(link) layout = AnchorLayout() self.scene.addItem(layout) self.scene.set_anchor_layout(layout) layout.invalidateNode(file_item) layout.activate() p1, p2 = file_item.outputAnchorItem.anchorPositions() self.assertGreater(p1, p2) self.scene.node_item_position_changed.connect(layout.invalidateNode) path = QPainterPath() path.addEllipse(125, 0, 50, 300) def advance(): t = time.clock() bayes_item.setPos(path.pointAtPercent(t % 1.0)) disc_item.setPos(path.pointAtPercent((t + 0.5) % 1.0)) self.singleShot(20, advance) advance() self.app.exec_()
def test_layout(self): one_desc, negate_desc, cons_desc = self.widget_desc() one_item = NodeItem() one_item.setWidgetDescription(one_desc) one_item.setPos(0, 150) self.scene.add_node_item(one_item) cons_item = NodeItem() cons_item.setWidgetDescription(cons_desc) cons_item.setPos(200, 0) self.scene.add_node_item(cons_item) negate_item = NodeItem() negate_item.setWidgetDescription(negate_desc) negate_item.setPos(200, 300) self.scene.add_node_item(negate_item) link = LinkItem() link.setSourceItem(one_item) link.setSinkItem(negate_item) self.scene.add_link_item(link) link = LinkItem() link.setSourceItem(one_item) link.setSinkItem(cons_item) self.scene.add_link_item(link) layout = AnchorLayout() self.scene.addItem(layout) self.scene.set_anchor_layout(layout) layout.invalidateNode(one_item) layout.activate() p1, p2 = one_item.outputAnchorItem.anchorPositions() self.assertTrue(p1 > p2) self.scene.node_item_position_changed.connect(layout.invalidateNode) path = QPainterPath() path.addEllipse(125, 0, 50, 300) def advance(): t = time.process_time() cons_item.setPos(path.pointAtPercent(t % 1.0)) negate_item.setPos(path.pointAtPercent((t + 0.5) % 1.0)) timer = QTimer(negate_item, interval=5) timer.start() timer.timeout.connect(advance) self.qWait() timer.stop()
def test_graphicspathobject(self): obj = GraphicsPathObject() path = QPainterPath() obj.setFlag(GraphicsPathObject.ItemIsMovable) path.addEllipse(20, 20, 50, 50) obj.setPath(path) self.assertEqual(obj.path(), path) self.assertTrue(obj.path() is not path, msg="setPath stores the path not a copy") brect = obj.boundingRect() self.assertTrue(brect.contains(path.boundingRect())) with self.assertRaises(TypeError): obj.setPath("This is not a path") brush = QBrush(QColor("#ffbb11")) obj.setBrush(brush) self.assertEqual(obj.brush(), brush) self.assertTrue(obj.brush() is not brush, "setBrush stores the brush not a copy") pen = QPen(QColor("#FFFFFF"), 1.4) obj.setPen(pen) self.assertEqual(obj.pen(), pen) self.assertTrue(obj.pen() is not pen, "setPen stores the pen not a copy") brect = obj.boundingRect() self.assertGreaterEqual(area(brect), (50 + 1.4 * 2)**2) self.assertIsInstance(obj.shape(), QPainterPath) positions = [] obj.positionChanged[QPointF].connect(positions.append) pos = QPointF(10, 10) obj.setPos(pos) self.assertEqual(positions, [pos]) self.scene.addItem(obj) self.view.show() self.qWait()
def test_layout(self): one_desc, negate_desc, cons_desc = self.widget_desc() one_item = NodeItem() one_item.setWidgetDescription(one_desc) one_item.setPos(0, 150) self.scene.add_node_item(one_item) cons_item = NodeItem() cons_item.setWidgetDescription(cons_desc) cons_item.setPos(200, 0) self.scene.add_node_item(cons_item) negate_item = NodeItem() negate_item.setWidgetDescription(negate_desc) negate_item.setPos(200, 300) self.scene.add_node_item(negate_item) link = LinkItem() link.setSourceItem(one_item) link.setSinkItem(negate_item) self.scene.add_link_item(link) link = LinkItem() link.setSourceItem(one_item) link.setSinkItem(cons_item) self.scene.add_link_item(link) layout = AnchorLayout() self.scene.addItem(layout) self.scene.set_anchor_layout(layout) layout.invalidateNode(one_item) layout.activate() p1, p2 = one_item.outputAnchorItem.anchorPositions() self.assertTrue(p1 > p2) self.scene.node_item_position_changed.connect(layout.invalidateNode) path = QPainterPath() path.addEllipse(125, 0, 50, 300) def advance(): t = time.process_time() cons_item.setPos(path.pointAtPercent(t % 1.0)) negate_item.setPos(path.pointAtPercent((t + 0.5) % 1.0)) timer = QTimer(negate_item, interval=20) timer.start() timer.timeout.connect(advance) self.app.exec_()
def ellipse_path(center, a, b, rotation=0): if not isinstance(center, QPointF): center = QPointF(*center) brect = QRectF(-a, -b, 2 * a, 2 * b) path = QPainterPath() path.addEllipse(brect) if rotation != 0: transform = QTransform().rotate(rotation) path = transform.map(path) path.translate(center) return path
def __init__(self, parent=None, anchor=0, **kwargs): GraphicsPathObject.__init__(self, parent, **kwargs) self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, False) self.setAcceptedMouseButtons(Qt.LeftButton) self.__constraint = 0 self.__constraintFunc = None self.__anchor = 0 self.__initialPosition = None self.setAnchor(anchor) path = QPainterPath() path.addEllipse(QRectF(-4, -4, 8, 8)) self.setPath(path) self.setBrush(QBrush(Qt.lightGray, Qt.SolidPattern))
def path_from_graphics(graphics): """ Return a constructed `QPainterPath` for a KEGG pathway graphics element. """ path = QPainterPath() x, y, w, h = [int(graphics.get(c, 0)) for c in ["x", "y", "width", "height"]] type = graphics.get("type", "rectangle") if type == "rectangle": path.addRect(QRectF(x - w / 2, y - h / 2, w, h)) elif type == "roundrectangle": path.addRoundedRect(QRectF(x - w / 2, y - h / 2, w, h), 10, 10) elif type == "circle": path.addEllipse(QRectF(x - w / 2, y - h / 2, w, h)) else: ValueError("Unknown graphics type %r." % type) return path
def _define_symbols(): """ Add symbol ? to ScatterPlotItemSymbols, reflect the triangle to point upwards """ symbols = pyqtgraph.graphicsItems.ScatterPlotItem.Symbols path = QPainterPath() path.addEllipse(QRectF(-0.35, -0.35, 0.7, 0.7)) path.moveTo(-0.5, 0.5) path.lineTo(0.5, -0.5) path.moveTo(-0.5, -0.5) path.lineTo(0.5, 0.5) symbols["?"] = path tr = QTransform() tr.rotate(180) symbols['t'] = tr.map(symbols['t'])
def test_anchoritem(self): anchoritem = NodeAnchorItem(None) self.scene.addItem(anchoritem) path = QPainterPath() path.addEllipse(0, 0, 100, 100) anchoritem.setAnchorPath(path) anchor = AnchorPoint() anchoritem.addAnchor(anchor) ellipse1 = QGraphicsEllipseItem(-3, -3, 6, 6) ellipse2 = QGraphicsEllipseItem(-3, -3, 6, 6) self.scene.addItem(ellipse1) self.scene.addItem(ellipse2) anchor.scenePositionChanged.connect(ellipse1.setPos) with self.assertRaises(ValueError): anchoritem.addAnchor(anchor) anchor1 = AnchorPoint() anchoritem.addAnchor(anchor1) anchor1.scenePositionChanged.connect(ellipse2.setPos) self.assertSequenceEqual(anchoritem.anchorPoints(), [anchor, anchor1]) self.assertSequenceEqual(anchoritem.anchorPositions(), [0.5, 0.5]) anchoritem.setAnchorPositions([0.5, 0.0]) self.assertSequenceEqual(anchoritem.anchorPositions(), [0.5, 0.0]) def advance(): t = anchoritem.anchorPositions() t = [(t + 0.05) % 1.0 for t in t] anchoritem.setAnchorPositions(t) timer = QTimer(anchoritem, interval=10) timer.start() timer.timeout.connect(advance) self.qWait() timer.stop()
def test_anchoritem(self): anchoritem = NodeAnchorItem(None) self.scene.addItem(anchoritem) path = QPainterPath() path.addEllipse(0, 0, 100, 100) anchoritem.setAnchorPath(path) anchor = AnchorPoint() anchoritem.addAnchor(anchor) ellipse1 = QGraphicsEllipseItem(-3, -3, 6, 6) ellipse2 = QGraphicsEllipseItem(-3, -3, 6, 6) self.scene.addItem(ellipse1) self.scene.addItem(ellipse2) anchor.scenePositionChanged.connect(ellipse1.setPos) with self.assertRaises(ValueError): anchoritem.addAnchor(anchor) anchor1 = AnchorPoint() anchoritem.addAnchor(anchor1) anchor1.scenePositionChanged.connect(ellipse2.setPos) self.assertSequenceEqual(anchoritem.anchorPoints(), [anchor, anchor1]) self.assertSequenceEqual(anchoritem.anchorPositions(), [0.5, 0.5]) anchoritem.setAnchorPositions([0.5, 0.0]) self.assertSequenceEqual(anchoritem.anchorPositions(), [0.5, 0.0]) def advance(): t = anchoritem.anchorPositions() t = [(t + 0.05) % 1.0 for t in t] anchoritem.setAnchorPositions(t) timer = QTimer(anchoritem, interval=20) timer.start() timer.timeout.connect(advance) self.app.exec_()
def __init__(self, parent=None, anchor=Free, constraint=Qt.Orientation(0), **kwargs): # type: (Optional[QGraphicsItem], Anchor, Qt.Orientation, Any) -> None super().__init__(parent, **kwargs) self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, False) self.setAcceptedMouseButtons(Qt.LeftButton) self.__constraint = constraint # type: Qt.Orientation self.__constraintFunc = None # type: Optional[ConstraintFunc] self.__anchor = ControlPoint.Free self.__initialPosition = None # type: Optional[QPointF] self.setAnchor(anchor) path = QPainterPath() path.addEllipse(QRectF(-4, -4, 8, 8)) self.setPath(path) self.setBrush(QBrush(Qt.lightGray, Qt.SolidPattern))
def test_anchoritem(self): anchoritem = NodeAnchorItem(None) anchoritem.setAnimationEnabled(False) self.scene.addItem(anchoritem) path = QPainterPath() path.addEllipse(0, 0, 100, 100) anchoritem.setAnchorPath(path) anchor = AnchorPoint() anchoritem.addAnchor(anchor) ellipse1 = QGraphicsEllipseItem(-3, -3, 6, 6) ellipse2 = QGraphicsEllipseItem(-3, -3, 6, 6) self.scene.addItem(ellipse1) self.scene.addItem(ellipse2) anchor.scenePositionChanged.connect(ellipse1.setPos) with self.assertRaises(ValueError): anchoritem.addAnchor(anchor) anchor1 = AnchorPoint() anchoritem.addAnchor(anchor1) anchor1.scenePositionChanged.connect(ellipse2.setPos) self.assertSequenceEqual(anchoritem.anchorPoints(), [anchor, anchor1]) self.assertSequenceEqual(anchoritem.anchorPositions(), [2 / 3, 1 / 3]) anchoritem.setAnchorPositions([0.5, 0.0]) self.assertSequenceEqual(anchoritem.anchorPositions(), [0.5, 0.0]) def advance(): t = anchoritem.anchorPositions() t = [(t + 0.05) % 1.0 for t in t] anchoritem.setAnchorPositions(t) timer = QTimer(anchoritem, interval=10) timer.start() timer.timeout.connect(advance) self.qWait() timer.stop() anchoritem.setAnchorOpen(True) anchoritem.setHovered(True) self.assertEqual(*[p.scenePos() for p in anchoritem.anchorPoints()]) anchoritem.setAnchorOpen(False) self.assertNotEqual(*[p.scenePos() for p in anchoritem.anchorPoints()]) anchoritem.setAnchorOpen(False) anchoritem.setHovered(True) self.assertNotEqual(*[p.scenePos() for p in anchoritem.anchorPoints()]) anchoritem = NodeAnchorItem(None) anchoritem.setSignals([ InputSignal("first", "object", "set_first"), InputSignal("second", "object", "set_second") ]) self.assertListEqual( anchoritem._NodeAnchorItem__pathStroker.dashPattern(), list(anchoritem._NodeAnchorItem__unanchoredDash)) anchoritem.setAnchorOpen(True) anchoritem.setHovered(True) self.assertListEqual( anchoritem._NodeAnchorItem__pathStroker.dashPattern(), list(anchoritem._NodeAnchorItem__channelDash))