def test_distance(self): self.assertEqual(0.0, distance(QtCore.QPointF(0, 0), QtCore.QPointF(0, 0))) self.assertEqual(10.0, distance(QtCore.QPointF(0, 0), QtCore.QPointF(10, 0))) self.assertEqual(10.0, distance(QtCore.QPointF(0, 0), QtCore.QPointF(0, 10))) self.assertEqual(14.0, distance(QtCore.QPointF(-4, 0), QtCore.QPointF(10, 0))) self.assertEqual(14.0, distance(QtCore.QPointF(0, -4), QtCore.QPointF(0, 10))) self.assertEqual(10.0, distance(QtCore.QPointF(0, 8), QtCore.QPointF(6, 0))) self.assertEqual(10.0, distance(QtCore.QPointF(0, -8), QtCore.QPointF(-6, 0)))
def test_distance(): assert 0.0 == distance(QtCore.QPointF(0, 0), QtCore.QPointF(0, 0)) assert 10.0 == distance(QtCore.QPointF(0, 0), QtCore.QPointF(10, 0)) assert 10.0 == distance(QtCore.QPointF(0, 0), QtCore.QPointF(0, 10)) assert 14.0 == distance(QtCore.QPointF(-4, 0), QtCore.QPointF(10, 0)) assert 14.0 == distance(QtCore.QPointF(0, -4), QtCore.QPointF(0, 10)) assert 10.0 == distance(QtCore.QPointF(0, 8), QtCore.QPointF(6, 0)) assert 10.0 == distance(QtCore.QPointF(0, -8), QtCore.QPointF(-6, 0))
def test_distance(self): self.assertEqual(0.0, distance(QtCore.QPointF(0, 0), QtCore.QPointF(0, 0))) self.assertEqual(10.0, distance(QtCore.QPointF(0, 0), QtCore.QPointF(10, 0))) self.assertEqual(10.0, distance(QtCore.QPointF(0, 0), QtCore.QPointF(0, 10))) self.assertEqual( 14.0, distance(QtCore.QPointF(-4, 0), QtCore.QPointF(10, 0))) self.assertEqual( 14.0, distance(QtCore.QPointF(0, -4), QtCore.QPointF(0, 10))) self.assertEqual(10.0, distance(QtCore.QPointF(0, 8), QtCore.QPointF(6, 0))) self.assertEqual( 10.0, distance(QtCore.QPointF(0, -8), QtCore.QPointF(-6, 0)))
def anchorMove(self, node, mousePos): """ Move the selected anchor point. :type node: AbstractNode :type mousePos: QtCore.QPointF """ nodePos = node.pos() snapToGrid = self.session.action('toggle_grid').isChecked() mousePos = snap(mousePos, self.diagram.GridSize, snapToGrid) path = self.mapFromItem(node, node.painterPath()) if path.contains(mousePos): # Mouse is inside the shape => use this position as anchor point. pos = nodePos if distance(mousePos, nodePos) < 10.0 else mousePos else: # Mouse is outside the shape => use the intersection point as anchor point. pos = node.intersection(QtCore.QLineF(mousePos, nodePos)) for pair in set(permutations([-1, -1, 0, 0, 1, 1], 2)): p = pos + QtCore.QPointF(*pair) if path.contains(p): pos = p break node.setAnchor(self, pos)
def anchorMove(self, node, mousePos): """ Move the selected anchor point. :type node: AbstractNode :type mousePos: QtCore.QPointF """ # Only allow anchor movement for concept nodes if node.type() != Item.ConceptIRINode: node.setAnchor(self, node.pos()) return nodePos = node.pos() snapToGrid = self.session.action('toggle_grid').isChecked() mousePos = snap(mousePos, self.diagram.GridSize, snapToGrid) path = self.mapFromItem(node, node.painterPath()) breakpoint = (self.breakpoints[-1] if node == self.target else self.breakpoints[0]) \ if len(self.breakpoints) > 0 else self.other(node).anchor(self) if path.contains(breakpoint): # If the source is inside the node then there will be no intersection if path.contains(self.other(node).anchor(self)): return # Breakpoint is inside the shape => use the source anchor breakpoint = self.other(node).anchor(self) if path.contains(mousePos): # Mouse is inside the shape => use its position as the endpoint. endpoint = mousePos else: # Mouse is outside the shape => use the intersection as the endpoint. endpoint = node.intersection(QtCore.QLineF(nodePos, mousePos)) if distance(nodePos, endpoint) < 10.0: # When close enough use the node center as the anchor point. pos = nodePos else: # Otherwise compute the closest intersection between the breakpoint and the endpoint. pos = node.intersection(QtCore.QLineF(breakpoint, endpoint)) minDistance = distance(breakpoint, pos) for intersection in node.intersections( QtCore.QLineF(breakpoint, endpoint)): intersDistance = distance(breakpoint, intersection) if (intersDistance < minDistance): minDistance = intersDistance pos = intersection if not path.contains(pos): # Ensure anchor is inside the path lineToBreakpoint = QtCore.QLineF(breakpoint, endpoint) direction = lineToBreakpoint.unitVector() normal = lineToBreakpoint.normalVector().unitVector() if path.contains( pos + QtCore.QPointF(direction.dx(), direction.dy())): pos = pos + QtCore.QPointF(direction.dx(), direction.dy()) elif path.contains( pos - QtCore.QPointF(direction.dx(), direction.dy())): pos = pos - QtCore.QPointF(direction.dx(), direction.dy()) elif path.contains(pos + QtCore.QPointF(normal.dx(), normal.dy())): pos = pos + QtCore.QPointF(normal.dx(), normal.dy()) elif path.contains(pos - QtCore.QPointF(normal.dx(), normal.dy())): pos = pos - QtCore.QPointF(normal.dx(), normal.dy()) else: # Lower right corner pos = pos - QtCore.QPointF(0.5, 0.5) node.setAnchor(self, pos)