Skip to content

Commit

Permalink
Merge branch 'dev' into feature/improve-edge-button-look
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisFederico committed Feb 11, 2022
2 parents 07232b4 + 541f358 commit 3842496
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 62 deletions.
92 changes: 48 additions & 44 deletions pyflow/blocks/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,42 +144,6 @@ def paint(
painter.setBrush(Qt.BrushStyle.NoBrush)
painter.drawPath(path_outline.simplified())

def get_socket_pos(self, socket: Socket) -> Tuple[float]:
"""Get a socket position to place them on the block sides."""
if socket.socket_type == "input":
y = 0
sockets = self.sockets_in
else:
y = self.height
sockets = self.sockets_out

# Sockets are evenly spaced out on the whole block width
space_between_sockets = self.width / (len(sockets) + 1)
x = space_between_sockets * (sockets.index(socket) + 1)

return x, y

def update_sockets(self):
"""Update the sockets positions."""

def x_end_position(socket: Socket) -> float:
"""x-coordinate of the end point of the first edge."""
if not socket.edges:
return 0
return socket.edges[0].destination.x()

def x_start_position(socket: Socket) -> float:
"""x-coordinate of the start point of the first edge."""
if not socket.edges:
return 0
return socket.edges[0].source.x()

self.sockets_in.sort(key=x_start_position)
self.sockets_out.sort(key=x_end_position)

for socket in self.sockets_in + self.sockets_out:
socket.setPos(*self.get_socket_pos(socket))

def add_socket(self, socket: Socket):
"""Add a socket to the block."""
if socket.socket_type == "input":
Expand All @@ -203,14 +167,7 @@ def mouseMoveEvent(self, event: QGraphicsSceneMouseEvent):
# Update the position of the sockets of this block
# and the block it is connected to
self.update_sockets()
for socket in self.sockets_in:
for edge in socket.edges:
if edge.source_socket is not None:
edge.source_socket.block.update_sockets()
for socket in self.sockets_out:
for edge in socket.edges:
if edge.destination_socket is not None:
edge.destination_socket.block.update_sockets()
self.update_neighbors_sockets()

def remove(self):
"""Remove the block from the scene containing it."""
Expand Down Expand Up @@ -249,6 +206,53 @@ def update_size_grip(self):
int(self.edge_size * 1.7),
)

def get_socket_pos(self, socket: Socket) -> Tuple[float]:
"""Get a socket position to place them on the block sides."""
if socket.socket_type == "input":
y = 0
sockets = self.sockets_in
else:
y = self.height
sockets = self.sockets_out

# Sockets are evenly spaced out on the whole block width
space_between_sockets = self.width / (len(sockets) + 1)
x = space_between_sockets * (sockets.index(socket) + 1)

return x, y

def update_sockets(self):
"""Update the sockets positions."""

def x_end_position(socket: Socket) -> float:
"""x-coordinate of the end point of the first edge."""
if not socket.edges:
return 0
return socket.edges[0].destination.x()

def x_start_position(socket: Socket) -> float:
"""x-coordinate of the start point of the first edge."""
if not socket.edges:
return 0
return socket.edges[0].source.x()

self.sockets_in.sort(key=x_start_position)
self.sockets_out.sort(key=x_end_position)

for socket in self.sockets_in + self.sockets_out:
socket.setPos(*self.get_socket_pos(socket))

def update_neighbors_sockets(self):
"""Update the sockets positions of all neighboring blocks."""
for socket in self.sockets_in:
for edge in socket.edges:
if edge.source_socket is not None:
edge.source_socket.block.update_sockets()
for socket in self.sockets_out:
for edge in socket.edges:
if edge.destination_socket is not None:
edge.destination_socket.block.update_sockets()

def update_all(self):
"""Update sockets and title."""
self.update_sockets()
Expand Down
4 changes: 1 addition & 3 deletions pyflow/blocks/codeblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ def init_run_all_button(self):

def init_add_edge_button(self):
"""Initialize the add edge button."""

add_edge_button = AddEdgeButton(block=self)

return add_edge_button

def handle_run_right(self):
Expand Down Expand Up @@ -184,7 +182,7 @@ def update_run_all_button(self):

def get_add_edge_button_pos(self) -> Tuple[int, int]:
"""Get the position where to place the add edge button."""
return (self.width / 2, self.height + 25)
return (self.width / 2, self.height + int(2.5 * self.add_edge_button.radius))

def update_add_edge_button(self):
"""Change the geometry of the add edge button."""
Expand Down
1 change: 1 addition & 0 deletions pyflow/core/add_edge_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from PyQt5.QtWidgets import QGraphicsItem, QStyleOptionGraphicsItem, QWidget

if TYPE_CHECKING:
from PyQt5.QtWidgets import QGraphicsSceneHoverEvent
from pyflow.blocks.executableblock import ExecutableBlock
from pyflow.core.edge import Edge

Expand Down
34 changes: 19 additions & 15 deletions pyflow/graphics/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
from pyflow.core.socket import Socket
from pyflow.core.edge import Edge
from pyflow.blocks.block import Block
from pyflow.logging import get_logger
from pyflow.blocks import __file__ as BLOCK_INIT_PATH

BLOCK_PATH = pathlib.Path(BLOCK_INIT_PATH).parent
BLOCKFILES_PATH = os.path.join(BLOCK_PATH, "blockfiles")

EPS: float = 1e-10 # To check if blocks are of size 0
LOGGER = get_logger(__name__)


class View(QGraphicsView):
Expand Down Expand Up @@ -460,7 +462,9 @@ def drag_edge(self, event: QMouseEvent, action="press"):
"""Create an edge by drag and drop."""

# edge creation / destruction if control is pressed
ctrl_pressed = QApplication.keyboardModifiers() == Qt.KeyboardModifier.ControlModifier
ctrl_pressed = (
QApplication.keyboardModifiers() == Qt.KeyboardModifier.ControlModifier
)
if event is None or (action != "move" and ctrl_pressed):
return event

Expand All @@ -469,24 +473,24 @@ def drag_edge(self, event: QMouseEvent, action="press"):

scene = self.scene()
if action == "press":

# If we press an existing output socket, create a new edge from it.
if (
isinstance(item_at_click, Socket)
and self.mode != self.MODE_EDGE_DRAG
and item_at_click.socket_type != "input"
and item_at_click.socket_type == "output"
):
# Delete existing edges
for edge in item_at_click.edges:
edge.remove()

self.mode = self.MODE_EDGE_DRAG
self.edge_drag = Edge(
source_socket=item_at_click,
destination=self.mapToScene(event.pos()),
)
old_edges = item_at_click.edges
for edge in old_edges:
edge.remove()
self.mode = self.MODE_EDGE_DRAG
scene.addItem(self.edge_drag)
LOGGER.debug("Start draging edge from existing socket.")
return
# If it is the add edge button, create a new edge and a new socket for this edge
# If it is the add edge button, create a new socket and a new edge from it.
elif (
isinstance(item_at_click, AddEdgeButton)
and self.mode != self.MODE_EDGE_DRAG
Expand All @@ -498,9 +502,10 @@ def drag_edge(self, event: QMouseEvent, action="press"):
destination=self.mapToScene(event.pos()),
)
scene.addItem(self.edge_drag)
LOGGER.debug("Start draging edge from new socket.")
return
elif action == "release":
if self.mode == self.MODE_EDGE_DRAG:
elif self.mode == self.MODE_EDGE_DRAG:
if action == "release":
block_below_mouse = self.get_block_below_mouse(event.pos())
if (
block_below_mouse is not None
Expand All @@ -512,14 +517,13 @@ def drag_edge(self, event: QMouseEvent, action="press"):
"Created edge by dragging", set_modified=True
)
else:
LOGGER.debug("Removed socket from edge release.")
self.edge_drag.source_socket.remove()
self.edge_drag = None
self.mode = self.MODE_NOOP
self.scene().update_all_blocks_sockets()
elif action == "move":
if self.mode == self.MODE_EDGE_DRAG:
elif action == "move":
self.edge_drag.destination = self.mapToScene(event.pos())
self.scene().update_all_blocks_sockets()
self.scene().update_all_blocks_sockets()
return event

def toggle_socket(self, event: QMouseEvent) -> Optional[QMouseEvent]:
Expand Down

0 comments on commit 3842496

Please sign in to comment.