-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract edge dragging from graphics view
- Loading branch information
Showing
9 changed files
with
306 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Edge dragging module containing :class:`~nodedge.edge_dragging.EdgeDragging` class. | ||
""" | ||
|
||
import logging | ||
from enum import IntEnum | ||
from typing import Optional | ||
|
||
from nodedge.edge import Edge, EdgeType | ||
from nodedge.graphics_socket import GraphicsSocket | ||
from nodedge.socket import Socket | ||
from nodedge.utils import dumpException | ||
|
||
|
||
class EdgeDraggingMode(IntEnum): | ||
""" | ||
:class:`~nodedge.graphics_view.DragMode` class. | ||
""" | ||
|
||
NOOP = 1 #: Mode representing ready state | ||
EDGE_DRAG = 2 #: Mode representing when we drag edge state | ||
EDGE_CUT = 3 #: Mode representing when we draw a cutting edge | ||
|
||
|
||
class EdgeDragging: | ||
""":class:`~nodedge.edge_dragging.EdgeDragging` class .""" | ||
|
||
def __init__(self, graphicsView: "GraphicsView"): # type: ignore | ||
self.graphicsView = graphicsView | ||
self.dragEdge: Optional[Edge] = None | ||
self.dragStartSocket: Optional[Socket] = None | ||
self.mode: EdgeDraggingMode = EdgeDraggingMode.NOOP | ||
|
||
self.__logger = logging.getLogger(__name__) | ||
self.__logger.setLevel(logging.INFO) | ||
|
||
def startEdgeDragging(self, graphicsSocket: GraphicsSocket): | ||
""" | ||
Handle the start of dragging an :class:`~nodedge.edge.Edge` operation. | ||
:param graphicsSocket: The socket being connected to another one. | ||
:type graphicsSocket: :class:`~nodedge.graphics_socket.GraphicsSocket` | ||
""" | ||
try: | ||
self.__logger.debug("Assign socket.") | ||
self.dragStartSocket = graphicsSocket.socket | ||
self.dragEdge = Edge( | ||
self.graphicsView.graphicsScene.scene, | ||
graphicsSocket.socket, | ||
edgeType=EdgeType.BEZIER, | ||
) | ||
except Exception as e: | ||
dumpException(e) | ||
|
||
def endEdgeDragging(self, graphicsSocket: GraphicsSocket): | ||
""" | ||
Handle the end of dragging an :class:`~nodedge.edge.Edge` operation. | ||
:param graphicsSocket: socket being connected | ||
:type graphicsSocket: :class:`~nodedge.graphics_socket.GraphicsSocket` | ||
:return: True is the operation is a success, false otherwise. | ||
:rtype: ``bool`` | ||
""" | ||
self.mode = EdgeDraggingMode.NOOP | ||
self.__logger.debug(f"Drag mode: {self.mode}") | ||
|
||
# noinspection PyBroadException | ||
try: | ||
if self.dragEdge is not None: | ||
# Don't notify sockets about removing drag_edge | ||
self.dragEdge.remove(silent=True) | ||
except Exception: | ||
self.__logger.warning("Impossible to remove dragEdge") | ||
self.dragEdge = None | ||
|
||
try: | ||
if self.dragStartSocket is not None: | ||
if not self.dragStartSocket.allowMultiEdges: | ||
self.dragStartSocket.removeAllEdges() | ||
|
||
if not graphicsSocket.socket.allowMultiEdges: | ||
graphicsSocket.socket.removeAllEdges() | ||
|
||
newEdge = Edge( | ||
self.graphicsView.graphicsScene.scene, | ||
self.dragStartSocket, | ||
graphicsSocket.socket, | ||
edgeType=EdgeType.CIRCUIT, | ||
) | ||
graphicsSocket.socket.addEdge(newEdge) | ||
self.__logger.debug( | ||
f"New edge created: {newEdge} connecting" | ||
f"\n|||| {newEdge.sourceSocket} to" | ||
f"\n |||| {newEdge.targetSocket}" | ||
) | ||
|
||
socket: Optional[Socket] | ||
for socket in [self.dragStartSocket, graphicsSocket.socket]: | ||
if socket is not None: | ||
socket.node.onEdgeConnectionChanged(newEdge) | ||
|
||
if socket.isInput: | ||
socket.node.onInputChanged(socket) | ||
|
||
self.graphicsView.graphicsScene.scene.history.store( | ||
"Create a new edge by dragging" | ||
) | ||
self.__logger.debug("Socket assigned.") | ||
return True | ||
except Exception as e: | ||
dumpException(e) | ||
|
||
self.__logger.debug("Drag edge successful.") | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.