Skip to content

Commit

Permalink
draft on drawing splines for source and target plugs
Browse files Browse the repository at this point in the history
  • Loading branch information
chrizzFTD committed Oct 22, 2023
1 parent a4b545d commit 695843e
Showing 1 changed file with 64 additions and 7 deletions.
71 changes: 64 additions & 7 deletions grill/views/_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
# - USDView missing tri-state style
# - Ability to move further in canvas after Nodes don't exist
# - Focus with F
# - Taxonomy graph is LR and should go from center to center (edges)

_NO_PEN = QtGui.QPen(QtCore.Qt.NoPen)

Expand All @@ -44,7 +43,7 @@ def _convert_graphviz_to_html_label(label):

class _Node(QtWidgets.QGraphicsTextItem):

def __init__(self, parent=None, label="", color="", fillcolor="", plugs=None, visible=True, active_plugs: set = frozenset()):
def __init__(self, parent=None, label="", color="", fillcolor="", plugs=None, active_plugs: set = frozenset(), visible=True):
super().__init__(parent)
self._edges = []
self._plugs = plugs or {}
Expand Down Expand Up @@ -146,7 +145,7 @@ def _activatePlug(self, edge, plug_index, side, position):


class _Edge(QtWidgets.QGraphicsItem):
def __init__(self, source: _Node, target: _Node, parent: QtWidgets.QGraphicsItem = None, color="#2BB53C", label="", source_plug=None, target_plug=None, is_bidirectional=False):
def __init__(self, source: _Node, target: _Node, *, source_plug=None, target_plug=None, label="", color="", is_bidirectional=False, parent: QtWidgets.QGraphicsItem = None):
super().__init__(parent)
source.add_edge(self)
target.add_edge(self)
Expand Down Expand Up @@ -203,7 +202,7 @@ def boundingRect(self) -> QtCore.QRectF:
else:
top_left, bottom_right = self._line.p1(), self._line.p2()

Check warning on line 203 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L203

Added line #L203 was not covered by tests

width, arrow_size = self._width, self._arrow_size
width, arrow_size = self._width, max(self._arrow_size, 50)
top_shift, bottom_shift = -width - arrow_size, width + arrow_size
return QtCore.QRectF(top_left, bottom_right).normalized().adjusted(top_shift, top_shift, bottom_shift, bottom_shift)

Check warning on line 207 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L205-L207

Added lines #L205 - L207 were not covered by tests

Expand Down Expand Up @@ -264,21 +263,79 @@ def paint(self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionGraphicsI
"""Draw Edge following ``Edge.adjust(...)``"""
painter.setRenderHints(QtGui.QPainter.Antialiasing)
painter.setPen(self._pen)

Check warning on line 265 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L264-L265

Added lines #L264 - L265 were not covered by tests

# self._pen.setCapStyle(QtCore.Qt.PenCapStyle.RoundCap)
if self._is_cycle:
self._draw_rounded_arrow(painter, self._cycle_start_position)

Check warning on line 268 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L267-L268

Added lines #L267 - L268 were not covered by tests
else:
total_colors = enumerate(self._colors)
__, main_color = next(total_colors) # draw first without offset and with current color

Check warning on line 271 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L270-L271

Added lines #L270 - L271 were not covered by tests
painter.drawLine(self._line)

as_spline = not (self._source_plug is None and self._target_plug is None)
if as_spline:
source_pos = self._source.pos()
target_pos = self._target.pos()
target_bounds = self._target.boundingRect()

Check warning on line 277 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L273-L277

Added lines #L273 - L277 were not covered by tests

source_on_left = self._source.boundingRect().center().x() + source_pos.x() < target_bounds.center().x() + target_pos.x()
multip = 1 if source_on_left else -1

Check warning on line 280 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L279-L280

Added lines #L279 - L280 were not covered by tests
######### BEZIER START
# Calculate control points for the cubic bezier curve
length = self._line.length()
if length < 100:
falldown = (length / 100) ** 2

Check warning on line 285 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L283-L285

Added lines #L283 - L285 were not covered by tests
else:
falldown = 1
control_point_shift = multip * 75 * falldown
if self._source_plug is not None:
control_point1 = self._line.p1() + QtCore.QPointF(control_point_shift, 0)

Check warning on line 290 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L287-L290

Added lines #L287 - L290 were not covered by tests
else:
control_point1 = self._line.p1()
if self._target_plug is not None:
control_point2 = self._line.p2() + QtCore.QPointF(-control_point_shift, 0)

Check warning on line 294 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L292-L294

Added lines #L292 - L294 were not covered by tests
else:
control_point2 = self._line.p2()

Check warning on line 296 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L296

Added line #L296 was not covered by tests

self._path = QtGui.QPainterPath()
self._path.moveTo(self._line.p1())
self._path.cubicTo(control_point1, control_point2, self._line.p2())

Check warning on line 300 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L298-L300

Added lines #L298 - L300 were not covered by tests

# PARALLEL
self._parallel_paths = []
for i, color in reversed(list(total_colors)):
stroker = QtGui.QPainterPathStroker()
stroker.setCapStyle(QtCore.Qt.PenCapStyle.RoundCap)
stroker.setWidth(i*5)
parallel_path = stroker.createStroke(self._path)
self._parallel_paths.append((color, parallel_path))

Check warning on line 309 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L303-L309

Added lines #L303 - L309 were not covered by tests

for color, parallel_path in self._parallel_paths:
pen_color = QtGui.QPen(QtGui.QColor(color), self._width)
painter.setPen(pen_color) # Set the color and thickness
painter.drawPath(parallel_path)

Check warning on line 314 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L311-L314

Added lines #L311 - L314 were not covered by tests

painter.setPen(self._pen)
painter.drawPath(self._path)

Check warning on line 317 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L316-L317

Added lines #L316 - L317 were not covered by tests
######### BEZIER END
# painter.drawRect(self.boundingRect()) # for debugging purposes

# ### ARROW HEADS
end_point = self._path.pointAtPercent(1)
start_point = self._path.pointAtPercent(.95)
painter.setPen(self._pen)
self._draw_arrow_head(painter, start_point, end_point)

Check warning on line 325 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L322-L325

Added lines #L322 - L325 were not covered by tests
else:
painter.drawLine(self._line)

Check warning on line 327 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L327

Added line #L327 was not covered by tests

for index, color in total_colors:

Check warning on line 329 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L329

Added line #L329 was not covered by tests
# continue
shift = int((index+1)/2) * 1.5 * 3
self._pen.setColor(color)
painter.setPen(self._pen)
painter.drawLine(_parallel_line(self._line, shift if index % 2 == 0 else -shift, head_offset=11))
self._pen.setColor(main_color)
painter.setPen(self._pen)
self._draw_arrow_head(painter, self._line.p1(), self._line.p2())
if not as_spline:
self._draw_arrow_head(painter, self._line.p1(), self._line.p2())

Check warning on line 338 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L331-L338

Added lines #L331 - L338 were not covered by tests

def _draw_rounded_arrow(self, painter: QtGui.QPainter, source_pos: QtCore.QPointF):
center_x, center_y = source_pos.toTuple()

Check warning on line 341 in grill/views/_graph.py

View check run for this annotation

Codecov / codecov/patch

grill/views/_graph.py#L341

Added line #L341 was not covered by tests
Expand Down

0 comments on commit 695843e

Please sign in to comment.