Skip to content

Edge

An edge links an output socket to an input socket. The remaining modules implement the interactions around it: dragging, rerouting, cutting and snapping.

nodeeditor.node_edge

A module containing NodeEditor's class for representing Edge and Edge Type Constants.

Edge

Bases: Serializable

Class for representing Edge in NodeEditor.

input_index property writable

input_index

Read position of this Edge on its end (input) :class:~nodeeditor.node_socket.Socket.

0 is read first, 1 next, and so on. Only meaningful for Edges whose end_socket is an input socket; the owning :class:~nodeeditor.node_socket.Socket owns and compacts the value, so assign it from code only together with :meth:~nodeeditor.node_socket.Socket.compactEdgeOrder. Use :class:~nodeeditor.node_multi_input_node.MultiInputNode for a safe reordering API.

:getter: current read position, or -1 when not attached to an input socket :setter: coerces to int; anything invalid becomes -1 :type: int

start_socket property writable

start_socket

Start socket

:getter: Returns start :class:~nodeeditor.node_socket.Socket :setter: Sets start :class:~nodeeditor.node_socket.Socket safely :type: :class:~nodeeditor.node_socket.Socket

end_socket property writable

end_socket

End socket

:getter: Returns end :class:~nodeeditor.node_socket.Socket or None if not set :setter: Sets end :class:~nodeeditor.node_socket.Socket safely :type: :class:~nodeeditor.node_socket.Socket or None

edge_type property writable

edge_type

Edge type

:getter: get edge type constant for current Edge. See :ref:edge-type-constants :setter: sets new edge type. On background, creates new :class:~nodeeditor.node_graphics_edge.QDMGraphicsEdge child class if necessary, adds this QGraphicsPathItem to the QGraphicsScene and updates edge sockets positions.

label property writable

label

Code-set text drawn on top of the edge.

:getter: current label text ("" when unset) :setter: sets new label text; ""/None hides the label. Not user-editable in the view -- change only from code, both at creation time (Edge(..., label="...")) and at runtime (edge.label = "...").

getLabel

getLabel()

Return the current code-set edge label.

setLabel

setLabel(text)

Set the text drawn on top of the edge ("" hides it).

getEdgeValidators classmethod

getEdgeValidators()

Return the list of Edge Validator Callbacks

registerEdgeValidator classmethod

registerEdgeValidator(validator_callback)

Register Edge Validator Callback

:param validator_callback: A function handle to validate Edge :type validator_callback: function

validateEdge classmethod

validateEdge(start_socket, end_socket)

Validate Edge agains all registered Edge Validator Callbacks

:param start_socket: Starting :class:~nodeeditor.node_socket.Socket of Edge to check :type start_socket: :class:~nodeeditor.node_socket.Socket :param end_socket: Target/End :class:~nodeeditor.node_socket.Socket of Edge to check :type end_socket: :class:~nodeeditor.node_socket.Socket :return: True if the Edge is valid or False if not :rtype: bool

reconnect

reconnect(from_socket, to_socket)

Helper function which reconnects edge from_socket to to_socket

getGraphicsEdgeClass

getGraphicsEdgeClass()

Returns the class representing Graphics Edge

createEdgeClassInstance

createEdgeClassInstance()

Create instance of grEdge class :return: Instance of grEdge class representing the Graphics Edge in the grScene

getOtherSocket

getOtherSocket(known_socket)

Returns the opposite socket on this Edge

:param known_socket: Provide known :class:~nodeeditor.node_socket.Socket to be able to determine the opposite one. :type known_socket: :class:~nodeeditor.node_socket.Socket :return: The oposite socket on this Edge or None :rtype: :class:~nodeeditor.node_socket.Socket or None

doSelect

doSelect(new_state=True)

Provide the safe selecting/deselecting operation. In the background it takes care about the flags, notifications and storing history for undo/redo.

:param new_state: True if you want to select the Edge, False if you want to deselect the Edge :type new_state: bool

updatePositions

updatePositions()

Updates the internal Graphics Edge positions.

Collapsed-group aware: if an endpoint lives in a collapsed Group, the endpoint is overridden to that group's per-edge stub row. Logical sockets are never mutated. Internal edges of a collapsed group stay hidden.

remove_from_sockets

remove_from_sockets()

Helper function which sets start and end :class:~nodeeditor.node_socket.Socket to None

remove

remove(silent_for_socket=None, silent=False)

Safely remove this Edge.

Removes Graphics Edge from the QGraphicsScene and it's reference to all GC to clean it up. Notifies nodes previously connected :class:~nodeeditor.node_node.Node (s) about this event.

Triggers Nodes':

  • :py:meth:~nodeeditor.node_node.Node.onEdgeConnectionChanged
  • :py:meth:~nodeeditor.node_node.Node.onInputChanged

:param silent_for_socket: :class:~nodeeditor.node_socket.Socket of a :class:~nodeeditor.node_node.Node which won't be notified, when this Edge is going to be removed :type silent_for_socket: :class:~nodeeditor.node_socket.Socket :param silent: True if no events should be triggered during removing :type silent: bool

serialize

serialize()

Serialize this Edge into an OrderedDict for storing in memory or on disk.

:return: data serialized in OrderedDict :rtype: OrderedDict

deserialize

deserialize(
    data, hashmap={}, restore_id=True, *args, **kwargs
)

Deserialize this Edge from a dict of serialized data.

Both sockets are looked up in the hashmap, so they have to be deserialized before the edge that connects them.

:param data: Dictionary containing serialized data :type data: dict :param hashmap: Helper dictionary containing references (by id == key) to existing objects :type hashmap: dict :param restore_id: True if we are creating new Edges. False is useful when loading existing Edges of which we want to keep the existing object's id. :type restore_id: bool :return: True if deserialization was successful, otherwise False :rtype: bool

nodeeditor.node_edge_dragging

A module containing the Edge Dragging functionality

EdgeDragging

Class handling the dragging of a new :class:~nodeeditor.node_edge.Edge between sockets

getEdgeClass

getEdgeClass()

Helper function to get the Edge class. Using what Scene class provides

updateDestination

updateDestination(x, y)

Update the end point of our dragging edge

:param x: new X scene position :param y: new Y scene position

edgeDragStart

edgeDragStart(item)

Code handling the start of a dragging an Edge operation

edgeDragEnd

edgeDragEnd(item)

Code handling the end of the dragging an Edge operation. If this code returns True then skip the rest of the mouse event processing. Can be called with None to cancel the edge dragging mode

:param item: Item in the Graphics Scene where we ended dragging an Edge :type item: QGraphicsItem

cancelDragEdge

cancelDragEdge()

Cancel the edge dragging operation and clean up

nodeeditor.node_edge_intersect

A module containing the intersecting nodes functionality. If a node gets dragged and dropped on an existing edge it will intersect that edge.

EdgeIntersect

Class handling the intersection of a dragged node with an existing edge

enterState

enterState(node)

Initialize when we enter the state

:param node: :class:~nodeeditor.node_node.Node which we started to drag :type node: :class:~nodeeditor.node_node.Node

leaveState

leaveState(scene_pos_x, scene_pos_y)

Deinit when we leave this state

:param scene_pos_x: scene position x :type scene_pos_x: float :param scene_pos_y: scene position y :type scene_pos_y: float

dropNode

dropNode(node, scene_pos_x, scene_pos_y)

Code handling the dropping of a node on an existing edge.

:param scene_pos_x: scene position x :type scene_pos_x: float :param scene_pos_y: scene position y :type scene_pos_y: float

hotZoneRect

hotZoneRect(node)

Returns A QRectF of creating a box around a node

:param node: :class:~nodeeditor.node_node.Node for which we want to get QRectF describing its position and area :type node: :class:~nodeeditor.node_node.Node :return: QRectF describing node's position and area :rtype: QRectF

update

update(scene_pos_x, scene_pos_y)

Updating during mouse move when grView is in this state

:param scene_pos_x: scene position x :type scene_pos_x: float :param scene_pos_y: scene position y :type scene_pos_y: float

intersect

intersect(node_box)

Checking for intersection of a rectangle (usually a Node) with edges in the scene

:param node_box: QRectF for which we want find intersecting Edges :type node_box: QRectF :return: :class:~nodeeditor.node_edge.Edge or None if the node is being cut by an Edge :rtype: :class:~nodeeditor.node_edge.Edge

isConnected

isConnected(node)

Return True if node got any connections

:param node: :class:~nodeeditor.node_node.Node which connections to check :type node: :class:~nodeeditor.node_node.Node :return:

nodeeditor.node_edge_rerouting

A module containing the Edge Rerouting functionality

EdgeRerouting

Class handling the re-routing of existing edges to another socket

print

print(*args)

Helper function to better control debug printing to console for this feature

getEdgeClass

getEdgeClass()

Helper function to get the Edge class. Using what the Scene class provides

getAffectedEdges

getAffectedEdges()

Get a list of all edges connected to the self.start_socket where we started the re-routing

:return: List of all edges affected by the rerouting started from this self.start_socket :class:~nodeeditor.node_socket.Socket :rtype: list

setAffectedEdgesVisible

setAffectedEdgesVisible(visibility=True)

Show/Hide all edges connected to the self.start_socket where we started the re-routing

:param visibility: True if all the affected :class:~nodeeditor.node_edge.Edge (s) should be shown or hidden :type visibility: bool

resetRerouting

resetRerouting()

Reset to default state. Init this feature internal variables

clearReroutingEdges

clearReroutingEdges()

Remove the helping dashed edges from the :class:~nodeeditor.node_scene.Scene

updateScenePos

updateScenePos(x, y)

Update position of all the rerouting edges (dashed ones). Called from mouseMove event to update to new mouse position

:param x: new X position :type x: float :param y: new Y position :type y: float

startRerouting

startRerouting(socket)

Method to start the re-routing. Called from the grView's state machine.

:param socket: :class:~nodeeditor.node_socket.Socket where we started the re-routing :type socket: :class:~nodeeditor.node_socket.Socket

stopRerouting

stopRerouting(target=None)

Method for stopping the re-routing

:param target: Target where we ended the rerouting (usually released mouse button). Provide Socket or None to cancel :type target: :class:~nodeeditor.node_socket.Socket or None

nodeeditor.node_edge_snapping

A module containing the Edge Snapping functions which are used in :class:~nodeeditor.node_graphics_view.QDMGraphicsView class.

EdgeSnapping

Class handling the snapping of a dragged edge to a nearby socket

getSnappedSocketItem

getSnappedSocketItem(event)

Returns :class:~nodeeditor.node_graphics_socket.QDMGraphicsSocket which we should snap to

getSnappedToSocketPosition

getSnappedToSocketPosition(scenepos)

Returns grSocket and Scene position to nearest Socket or original position if no nearby Socket found

:param scenepos: From which point should I snap? :type scenepos: QPointF :return: grSocket and Scene postion to nearest socket

nodeeditor.node_edge_validators

A module containing the Edge Validator functions which can be registered as callbacks to :class:~nodeeditor.node_edge.Edge class.

Example of registering Edge Validator callbacks: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You can register validation callbacks once for example on the bottom of node_edge.py file or on the application start with calling this:

.. code-block:: python

from nodeeditor.node_edge_validators import *

Edge.registerEdgeValidator(edge_validator_debug)
Edge.registerEdgeValidator(edge_cannot_connect_two_outputs_or_two_inputs)
Edge.registerEdgeValidator(edge_cannot_connect_input_and_output_of_same_node)
Edge.registerEdgeValidator(edge_cannot_connect_input_and_output_of_different_type)

print_error

print_error(*args)

Helper method which prints to console if DEBUG is set to True

edge_validator_debug

edge_validator_debug(input, output)

This will consider edge always valid, however writes bunch of debug stuff into console

edge_cannot_connect_two_outputs_or_two_inputs

edge_cannot_connect_two_outputs_or_two_inputs(
    input, output
)

Edge is invalid if it connects 2 output sockets or 2 input sockets

edge_cannot_connect_input_and_output_of_same_node

edge_cannot_connect_input_and_output_of_same_node(
    input, output
)

Edge is invalid if it connects the same node

edge_cannot_connect_input_and_output_of_different_type

edge_cannot_connect_input_and_output_of_different_type(
    input, output
)

Edge is invalid if it connects sockets with different colors