|
6 | 6 | PIPE_DEFAULT_COLOR, PIPE_ACTIVE_COLOR, |
7 | 7 | PIPE_HIGHLIGHT_COLOR, PIPE_DISABLED_COLOR, |
8 | 8 | PIPE_STYLE_DASHED, PIPE_STYLE_DEFAULT, PIPE_STYLE_DOTTED, |
9 | | - PIPE_LAYOUT_STRAIGHT, PIPE_WIDTH, IN_PORT, OUT_PORT, Z_VAL_PIPE |
| 9 | + PIPE_LAYOUT_STRAIGHT, PIPE_WIDTH, IN_PORT, OUT_PORT, Z_VAL_PIPE, |
| 10 | + Z_VAL_NODE_WIDGET |
10 | 11 | ) |
11 | 12 | from NodeGraphQt.qgraphics.port import PortItem |
12 | 13 |
|
@@ -130,7 +131,7 @@ def paint(self, painter, option, widget): |
130 | 131 |
|
131 | 132 | painter.restore() # QPaintDevice: Cannot destroy paint device that is being painted |
132 | 133 |
|
133 | | - def draw_path(self, start_port, end_port, cursor_pos=None): |
| 134 | + def draw_path(self, start_port, end_port=None, cursor_pos=None): |
134 | 135 | """ |
135 | 136 | Draws the path between ports. |
136 | 137 |
|
@@ -182,6 +183,10 @@ def draw_path(self, start_port, end_port, cursor_pos=None): |
182 | 183 | path.cubicTo(ctr_point1, ctr_point2, pos2) |
183 | 184 | self.setPath(path) |
184 | 185 |
|
| 186 | + def reset_path(self): |
| 187 | + path = QtGui.QPainterPath(QtCore.QPointF(0.0, 0.0)) |
| 188 | + self.setPath(path) |
| 189 | + |
185 | 190 | def calc_distance(self, p1, p2): |
186 | 191 | x = math.pow((p2.x() - p1.x()), 2) |
187 | 192 | y = math.pow((p2.y() - p1.y()), 2) |
@@ -293,3 +298,76 @@ def delete(self): |
293 | 298 | # TODO: not sure if we need this...? |
294 | 299 | del self |
295 | 300 |
|
| 301 | + |
| 302 | +class LivePipe(Pipe): |
| 303 | + |
| 304 | + def __init__(self): |
| 305 | + super(LivePipe, self).__init__() |
| 306 | + self.setZValue(Z_VAL_NODE_WIDGET + 1) |
| 307 | + self.shift_selected = False |
| 308 | + |
| 309 | + def paint(self, painter, option, widget): |
| 310 | + """ |
| 311 | + Draws the connection line. |
| 312 | +
|
| 313 | + Args: |
| 314 | + painter (QtGui.QPainter): painter used for drawing the item. |
| 315 | + option (QtGui.QStyleOptionGraphicsItem): |
| 316 | + used to describe the parameters needed to draw. |
| 317 | + widget (QtWidgets.QWidget): not used. |
| 318 | + """ |
| 319 | + color = QtGui.QColor(*PIPE_ACTIVE_COLOR) |
| 320 | + pen_style = PIPE_STYLES.get(PIPE_STYLE_DASHED) |
| 321 | + pen_width = PIPE_WIDTH + 0.35 |
| 322 | + |
| 323 | + pen = QtGui.QPen(color, pen_width) |
| 324 | + pen.setStyle(pen_style) |
| 325 | + pen.setCapStyle(QtCore.Qt.RoundCap) |
| 326 | + |
| 327 | + painter.save() |
| 328 | + painter.setPen(pen) |
| 329 | + painter.setRenderHint(painter.Antialiasing, True) |
| 330 | + painter.drawPath(self.path()) |
| 331 | + |
| 332 | + cen_x = self.path().pointAtPercent(0.5).x() |
| 333 | + cen_y = self.path().pointAtPercent(0.5).y() |
| 334 | + loc_pt = self.path().pointAtPercent(0.9) |
| 335 | + tgt_pt = self.path().pointAtPercent(1.0) |
| 336 | + |
| 337 | + dist = math.hypot(tgt_pt.x() - cen_x, tgt_pt.y() - cen_y) |
| 338 | + if dist < 0.05: |
| 339 | + painter.restore() |
| 340 | + return |
| 341 | + |
| 342 | + # draw circle |
| 343 | + size = 10.0 |
| 344 | + if dist < 50.0: |
| 345 | + size *= (dist / 50.0) |
| 346 | + rect = QtCore.QRectF(cen_x-(size/2), cen_y-(size/2), size, size) |
| 347 | + painter.setBrush(color) |
| 348 | + painter.setPen(QtGui.QPen(color.darker(130), pen_width)) |
| 349 | + painter.drawEllipse(rect) |
| 350 | + |
| 351 | + # draw arrow |
| 352 | + color.setAlpha(255) |
| 353 | + painter.setBrush(color.darker(200)) |
| 354 | + |
| 355 | + pen_width = 0.6 |
| 356 | + if dist < 1.0: |
| 357 | + pen_width *= 1.0 + dist |
| 358 | + painter.setPen(QtGui.QPen(color, pen_width)) |
| 359 | + |
| 360 | + transform = QtGui.QTransform() |
| 361 | + transform.translate(tgt_pt.x(), tgt_pt.y()) |
| 362 | + |
| 363 | + radians = math.atan2(tgt_pt.y() - loc_pt.y(), |
| 364 | + tgt_pt.x() - loc_pt.x()) |
| 365 | + degrees = math.degrees(radians) + 90 |
| 366 | + transform.rotate(degrees) |
| 367 | + |
| 368 | + scale = 1.0 |
| 369 | + if dist < 20.0: |
| 370 | + scale = dist / 20.0 |
| 371 | + transform.scale(scale, scale) |
| 372 | + painter.drawPolygon(transform.map(self._arrow)) |
| 373 | + painter.restore() |
0 commit comments