def draw_arrow_shape(self, painter): """ :param self: :param painter: :return: """ l = self.line() painter.setPen(self._pen) painter.setBrush(self._pen.color) painter.drawLine(l) l2x = l.p2().x() l2 = l.p2() l2y = l.p2().y() back = self._arrow_size / -2 # Draw the arrows if there's enough room. ll = l.length() if ll >= 1 and ll + back > 0: angle = acos(l.dx() / ll) # acos has to be <= 1.0 else: return prop = back / ll if l.dy() >= 0: angle = pipi - angle destArrowP1 = Pf((sin(angle - pi / 3) * self._arrow_size) + l2x, (cos(angle - pi / 3) * self._arrow_size) + l2y) destArrowP2 = Pf((sin(angle - pi + pi / 3) * self._arrow_size) + l2x, (cos(angle - pi + pi / 3) * self._arrow_size) + l2y) l2c = Pf(l.dx() * prop + l2x, l.dy() * prop + l2y) painter.drawPolygon(QtGui.QPolygonF([l2, destArrowP1, l2c, destArrowP2]))
def path(start_point=None, end_point=None, curve_adjustment=None, edge_n=0, edge_count=1, relative=False, rel_dx=0.2, rel_dy=0.4, fixed_dx=20, fixed_dy=20, leaf_x=1, leaf_y=2, thick=1, inner_only=False, curve_dir_start=0, **kwargs): sx, sy = start_point ex, ey = end_point if thick > 0: leaf_x *= thick leaf_y *= thick if relative: dx = abs(rel_dx * (ex - sx)) dy = abs(rel_dy * (ey - sy)) else: dx = fixed_dx dy = fixed_dy dm = direction_multiplier(edge_n, edge_count) dx *= dm if curve_dir_start == BOTTOM_SIDE: if sx > ex and dm > 0: cp1 = (sx + dx, sy + dy) elif dm < 0 and sx < ex: # big honking curve cp1 = ((sx + ex) / 2, sy) else: cp1 = (sx, sy + dy) elif curve_dir_start == TOP_SIDE: cp1 = (sx, sy - dy) elif curve_dir_start == LEFT_SIDE: cp1 = (sx + dx, sy) elif curve_dir_start == RIGHT_SIDE: cp1 = (sx + dx, sy) else: cp1 = (sx + dx, sy) control_points = [cp1] c = adjusted_control_point_list(sx, sy, ex, ey, control_points, curve_adjustment) c1x, c1y = c[0] if inner_only: path = None else: path = QtGui.QPainterPath(Pf(sx, sy)) path.quadTo(c1x - leaf_x, c1y - leaf_y, ex, ey) path.quadTo(c1x + leaf_x, c1y + leaf_y, sx, sy) inner_path = QtGui.QPainterPath(Pf(sx, sy)) inner_path.quadTo(c1x, c1y, ex, ey) return path, inner_path, control_points, c
def draw_arrow_shape_from_points(painter, start_x, start_y, end_x, end_y, color, arrow_size=6): dx = end_x - start_x dy = end_y - start_y length = sqrt(dx * dx + dy * dy) back = arrow_size / -2 # Draw the arrows if there's enough room. if length >= 1 and length + back > 0: angle = acos(dx / length) else: return prop = back / length if dy >= 0: angle = pipi - angle destArrowP1x = (sin(angle - pi / 3) * arrow_size) + end_x destArrowP1y = (cos(angle - pi / 3) * arrow_size) + end_y destArrowP2x = (sin(angle - pi + pi / 3) * arrow_size) + end_x destArrowP2y = (cos(angle - pi + pi / 3) * arrow_size) + end_y l2cx = dx * prop + end_x l2cy = dy * prop + end_y painter.drawLine(start_x, start_y, l2cx, l2cy) path2 = QtGui.QPainterPath(Pf(end_x, end_y)) path2.lineTo(end_x, end_y) path2.lineTo(destArrowP1x, destArrowP1y) path2.lineTo(l2cx, l2cy) path2.lineTo(destArrowP2x, destArrowP2y) painter.fillPath(path2, color)
def compute_magnet(self, rad_angle): s = self._size if self.being_dragged() or True: return Pf(0, 0) angle = math.degrees(rad_angle) if angle > 315 or angle <= 45: # left middle return Pf(0, s.height() / 2) elif 45 < angle <= 135: # center top return Pf(s.width() / 2, 0) elif 135 < angle <= 225: # right middle return Pf(s.width(), s.height() / 2) elif 225 < angle <= 315: # center bottom return Pf(s.width() / 2, s.height())
def icon_path(painter, rect, color=None): sx, sy = 0, 0 w = rect.width() h = rect.height() ex, ey = w, h path = QtGui.QPainterPath(Pf(sx, sy)) path.lineTo(ex, ey) painter.drawPath(path)
def _compute_position(self): """ :return: """ if self.role == g.CURVE_ADJUSTMENT and self._index < len( self.host.adjusted_control_points): p = Pf(*self.host.adjusted_control_points[self._index]) elif self.role == g.START_POINT: p = Pf(self.host.start_point[0], self.host.start_point[1]) elif self.role == g.END_POINT: p = Pf(self.host.end_point[0], self.host.end_point[1]) elif self.role == g.LABEL_START and self.host.label_item: c = self.host.label_item.get_label_start_pos() p = Pf(c.x(), c.y()) else: return False self.setPos(p) return True
def icon_path(painter, rect, color=None, leaf_x=4, leaf_y=4): sx, sy = 0, 0 w = rect.width() h = rect.height() ex, ey = w, h path = QtGui.QPainterPath(Pf(sx, sy)) path.quadTo(ex - leaf_x, ey - leaf_y, ex, ey) path.quadTo(ex + leaf_x, ey - leaf_y, sx, sy) painter.fillPath(path, color)
def path(start_point=None, end_point=None, curve_adjustment=None, edge_n=0, edge_count=1, relative=True, rel_dx=0.2, rel_dy=0.4, fixed_dx=20, fixed_dy=15, curve_dir_start=0, curve_dir_end=0, **kwargs): sx, sy = start_point ex, ey = end_point # edges that go to wrong direction have stronger curvature if relative: dx = abs(rel_dx * (ex - sx)) dy = abs(rel_dy * (ey - sy)) else: dx = fixed_dx dy = fixed_dy dm = direction_multiplier(edge_n, edge_count) dx *= dm if curve_dir_start == BOTTOM_SIDE: if sx > ex and dm > 0: cp1 = (sx + dx * 2, sy + dy) else: cp1 = (sx, sy + dy) elif curve_dir_start == TOP_SIDE: cp1 = (sx, sy - dy) elif curve_dir_start == LEFT_SIDE: cp1 = (sx + dx, sy) elif curve_dir_start == RIGHT_SIDE: cp1 = (sx + dx, sy) else: cp1 = (sx + dx, sy) if curve_dir_end == BOTTOM_SIDE: cp2 = (ex, ey + dy) elif curve_dir_end == TOP_SIDE: cp2 = (ex, ey - dy) elif curve_dir_end == LEFT_SIDE: cp2 = (ex - dx, ey) elif curve_dir_end == RIGHT_SIDE: cp2 = (ex - dx, ey) else: cp2 = (ex - dx, ey) control_points = [cp1, cp2] path = QtGui.QPainterPath(Pf(sx, sy)) c = adjusted_control_point_list(sx, sy, ex, ey, control_points, curve_adjustment) (c1x, c1y), (c2x, c2y) = c path.cubicTo(c1x, c1y, c2x, c2y, ex, ey) return path, path, control_points, c
def icon_path(painter, rect, color=None, rel_dx=0.4, rel_dy=0): sx, sy = 0, 4 w = rect.width() h = rect.height() ex, ey = w, h dx = rel_dx * (ex - sx) dy = rel_dy * (ey - sy) path = QtGui.QPainterPath(Pf(sx, sy)) path.quadTo(sx + dx, sy + dy, ex, ey) painter.drawPath(path)
def path(start_point=None, end_point=None, leaf_x=2, leaf_y=2, thick=1, inner_only=False, **kwargs): sx, sy = start_point dx, dy = end_point if thick > 0: leaf_x *= thick leaf_y *= thick c = [(dx - leaf_x, dy - leaf_y), (dx + leaf_x, dy - leaf_y)] if inner_only: path = None else: path = QtGui.QPainterPath(Pf(sx, sy)) path.quadTo(c[0][0], c[0][1], dx, dy) path.quadTo(c[1][0], c[1][1], sx, sy) inner_path = QtGui.QPainterPath(Pf(sx, sy)) inner_path.lineTo(dx, dy) return path, inner_path, [], []
def arrow_shape_bounding_rect(self): """ If draw_arrow_shape is used, boundingRect should refer to this """ l = self.line() p1 = l.p1() p2 = l.p2() p1x = p1.x() p2x = p2.x() p1y = p1.y() p2y = p2.y() extra = self._arrow_size / 2.0 if p1x > p2x - extra: l = p2x - extra r = p1x + extra else: l = p1x - extra r = p2x + extra if p1y > p2y - extra: t = p2y - extra b = p1y + extra else: t = p1y - extra b = p2y + extra return QtCore.QRectF(Pf(l, t), Pf(r, b))
def icon_path(painter, rect, color=None, rel_dx=0.4, rel_dy=0, leaf_x=1, leaf_y=3): sx, sy = 0, 4 w = rect.width() h = rect.height() ex, ey = w, h dx = rel_dx * (ex - sx) dy = rel_dy * (ey - sy) path = QtGui.QPainterPath(Pf(sx, sy)) path.quadTo(sx + dx - leaf_x, sy + dy - leaf_y, ex, ey) path.quadTo(sx + dx + leaf_x, sy + dy + leaf_y, sx, sy) painter.fillPath(path, color)
def path(start_point=None, end_point=None, curve_adjustment=None, edge_n=0, edge_count=1, relative=True, rel_dx=0.2, rel_dy=0.4, fixed_dx=20, fixed_dy=20, curve_dir_start=0, **kwargs): sx, sy = start_point ex, ey = end_point if relative: dx = abs(rel_dx * (ex - sx)) dy = abs(rel_dy * (ey - sy)) else: dx = fixed_dx dy = fixed_dy dm = direction_multiplier(edge_n, edge_count) dx *= dm if curve_dir_start == BOTTOM_SIDE: if sx > ex and dm > 0: cp1 = (sx + dx, sy + dy) else: cp1 = (sx, sy + dy) elif curve_dir_start == TOP_SIDE: cp1 = (sx, sy - dy) elif curve_dir_start == LEFT_SIDE: cp1 = (sx + dx, sy) elif curve_dir_start == RIGHT_SIDE: cp1 = (sx + dx, sy) else: cp1 = (sx + dx, sy) control_points = [cp1] path = QtGui.QPainterPath(Pf(sx, sy)) c = adjusted_control_point_list(sx, sy, ex, ey, control_points, curve_adjustment) c1x, c1y = c[0] path.quadTo(c1x, c1y, ex, ey) return path, path, control_points, c
def path(start_point=None, end_point=None, curve_adjustment=None, thickness=3, thick=1, start=None, end=None, inner_only=False, **kwargs): if start: scx, scy = start.current_scene_position else: scx, scy = start_point if end: ecx, ecy = end.current_scene_position else: ecx, ecy = end_point inner_path = QtGui.QPainterPath(Pf(scx, scy)) inner_path.lineTo(ecx, ecy) if inner_only: return None, inner_path, [], [] t2 = thickness * 2 if thick > 1: start_ball = start and start.has_visible_label() if start_ball: sx1, sy1, sw, sh = start.boundingRect().getRect() else: sx1, sy1, sw, sh = -5, -5, 10, 10 end_ball = end and end.has_visible_label() if end_ball: ex1, ey1, ew, eh = end.boundingRect().getRect() else: ex1, ey1, ew, eh = -5, -5, 10, 10 sx1 += scx sy1 += scy ex1 += ecx ey1 += ecy c1x = (scx + ecx) / 2 c1y = (scy + ecy) / 2 path1 = QtGui.QPainterPath() path1.addEllipse(sx1 - thickness, sy1 - thickness, sw + t2, sh + t2) path2 = QtGui.QPainterPath() path2.addEllipse(ex1 - thickness, ey1 - thickness, ew + t2, eh + t2) path3 = QtGui.QPainterPath() path3.moveTo(sx1, scy) path3.quadTo(c1x, c1y, ex1, ecy) path3.lineTo(ex1 + ew, ecy) path3.quadTo(c1x, c1y, sx1 + sw, scy) path = path1.united(path2) path = path.united(path3) if start_ball: path1neg = QtGui.QPainterPath() path1neg.addEllipse(sx1, sy1, sw, sh) path = path.subtracted(path1neg) if end_ball: path2neg = QtGui.QPainterPath() path2neg.addEllipse(ex1, ey1, ew, eh) path = path.subtracted(path2neg) else: sx, sy = start_point if end: if end.has_visible_label(): ex1, ey1, ew, eh = end.boundingRect().getRect() else: ex1, ey1, ew, eh = -5, -5, 10, 10 else: ex1 = -10 ey1 = -10 ew = 20 eh = 20 ex1 += ecx ey1 += ecy c1x = (sx + ecx) / 2 c1y = (sy + ecy) / 2 path1 = QtGui.QPainterPath() path1.addEllipse(ex1 - thickness, ey1 - thickness, ew + t2, eh + t2) path1neg = QtGui.QPainterPath() path1neg.addEllipse(ex1, ey1, ew, eh) path2 = QtGui.QPainterPath() path2.moveTo(sx, sy) path2.quadTo(c1x, c1y, ex1, ecy) path2.lineTo(ex1 + ew, ecy) path2.quadTo(c1x, c1y, sx, sy) path = path1.united(path2) path = path.subtracted(path1neg) return path.simplified(), inner_path, [], []
def path(start_point=None, end_point=None, **kwargs): sx, sy = start_point dx, dy = end_point path = QtGui.QPainterPath(Pf(sx, sy)) path.lineTo(dx, dy) return path, path, [], [] # [] = control_points
def to_pf(xy): return Pf(xy[0], xy[1])