Skip to content

Commit

Permalink
Node: Add getParentNodes method
Browse files Browse the repository at this point in the history
  • Loading branch information
don4get committed Oct 4, 2020
1 parent 1fb7c74 commit ce7c5bb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 42 deletions.
30 changes: 15 additions & 15 deletions nodedge/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,6 @@ def isOutput(self):
"""
return not self.isInput

def delete(self):
"""
Delete this :class:`~nodedge.socket.Socket`
from :class:`~nodedge.scene.Scene`.
"""
self.graphicsSocket.setParentItem(None)
self.node.scene.grScene.removeItem(self.graphicsSocket)
del self.graphicsSocket

def __repr__(self):
return (
f"0x{hex(id(self))[-4:]} Socket({self.index}, "
f"{self.location}, {self.socketType}, {self.allowMultiEdges})"
)

@property
def hasAnyEdge(self) -> bool:
"""
Expand All @@ -150,6 +135,21 @@ def pos(self) -> QPointF:
)
return ret

def delete(self):
"""
Delete this :class:`~nodedge.socket.Socket`
from :class:`~nodedge.scene.Scene`.
"""
self.graphicsSocket.setParentItem(None)
self.node.scene.grScene.removeItem(self.graphicsSocket)
del self.graphicsSocket

def __repr__(self):
return (
f"0x{hex(id(self))[-4:]} Socket({self.index}, "
f"{self.location}, {self.socketType}, {self.allowMultiEdges})"
)

def isConnected(self, edge: "Edge") -> bool: # type: ignore
"""
Returns ``True`` if :class:`~nodedge.edge.Edge` is connected to this
Expand Down
50 changes: 39 additions & 11 deletions nodedge/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def markChildrenDirty(self, newValue: bool = True) -> None:
``False`` to un-dirty them.
:type newValue: ``bool``
"""
for otherNode in self.getChildrenNodes():
for otherNode in self.getChildNodes():
otherNode.isDirty = newValue

def markDescendantsDirty(self, newValue: bool = True) -> None:
Expand All @@ -405,7 +405,7 @@ def markDescendantsDirty(self, newValue: bool = True) -> None:
``False`` to un-dirty them.
:type newValue: ``bool``
"""
for otherNode in self.getChildrenNodes():
for otherNode in self.getChildNodes():
otherNode.isDirty = newValue
otherNode.markChildrenDirty(newValue)

Expand All @@ -425,7 +425,7 @@ def markChildrenInvalid(self, newValue: bool = True) -> None:
them valid.
:type newValue: ``bool``
"""
for otherNode in self.getChildrenNodes():
for otherNode in self.getChildNodes():
otherNode.isInvalid = newValue

def markDescendantsInvalid(self, newValue: bool = True) -> None:
Expand All @@ -438,7 +438,7 @@ def markDescendantsInvalid(self, newValue: bool = True) -> None:
:type newValue: ``bool``
"""

for otherNode in self.getChildrenNodes():
for otherNode in self.getChildNodes():
otherNode.isInvalid = newValue
otherNode.markChildrenInvalid(newValue)

Expand All @@ -456,24 +456,52 @@ def evalChildren(self) -> None:
"""
Evaluate children of this node
"""
for node in self.getChildrenNodes():
for node in self.getChildNodes():
# TODO: Investigate if we want to evaluate all the children of the child
node.eval()

def getChildrenNodes(self) -> List["Node"]:
def getChildNodes(self) -> List["Node"]:
"""
Retrieve all children connected to this node outputs.
:return: list of `Nodes` connected to this node from all outputs
:rtype: List[:class:`~nodedge.node.Node`]
"""
if not self.outputSockets:
return self._getRelativeNodes("child")

def getParentNodes(self) -> List["Node"]:
"""
Retrieve all parents connected to this node outputs.
:return: list of `Nodes` connected to this node from all inputs
:rtype: List[:class:`~nodedge.node.Node`]
"""
return self._getRelativeNodes("parent")

def _getRelativeNodes(self, relationship: str) -> List["Node"]:
"""
Protected method to get relative nodes.
:param relationship: "child" or "parent"
:type relationship: str
:return: relative nodes
:rtype: List[:class:`~nodedge.node.Node`]
"""
if relationship == "child":
socketList: List[Socket] = self.outputSockets
elif relationship == "parent":
socketList = self.inputSockets
else:
raise NotImplementedError

if not socketList:
self.__logger.debug("Socket list is empty")
return []

otherNodes = []
for outputSocket in self.outputSockets:
for edge in outputSocket.edges:
otherNode = edge.getOtherSocket(outputSocket).node
otherNodes: List["Node"] = []
for socket in socketList:
for edge in socket.edges:
otherNode = edge.getOtherSocket(socket).node
otherNodes.append(otherNode)

return otherNodes
Expand Down
49 changes: 33 additions & 16 deletions tests/node_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,35 +125,35 @@ def test_remove(undefinedNode: Node, qtbot):


def test_markChildrenDirty(connectedNode: Node):
childNode = connectedNode.getChildrenNodes()[0]
grandChildNode = childNode.getChildrenNodes()[0]
childNode = connectedNode.getChildNodes()[0]
grandChildNode = childNode.getChildNodes()[0]
assert childNode.isDirty is False
connectedNode.markChildrenDirty(True)
assert childNode.isDirty is True
assert grandChildNode.isDirty is False # type: ignore


def test_markDescendantsDirty(connectedNode: Node):
childNode = connectedNode.getChildrenNodes()[0]
grandChildNode = childNode.getChildrenNodes()[0]
childNode = connectedNode.getChildNodes()[0]
grandChildNode = childNode.getChildNodes()[0]
assert childNode.isDirty is False
connectedNode.markDescendantsDirty(True)
assert childNode.isDirty is True
assert grandChildNode.isDirty is True # type: ignore


def test_markChildrenInvalid(connectedNode: Node):
childNode = connectedNode.getChildrenNodes()[0]
grandChildNode = childNode.getChildrenNodes()[0]
childNode = connectedNode.getChildNodes()[0]
grandChildNode = childNode.getChildNodes()[0]
assert childNode.isInvalid is False
connectedNode.markChildrenInvalid(True)
assert childNode.isInvalid is True
assert grandChildNode.isInvalid is False # type: ignore


def test_markDescendantsInvalid(connectedNode: Node):
childNode = connectedNode.getChildrenNodes()[0]
grandChildNode = childNode.getChildrenNodes()[0]
childNode = connectedNode.getChildNodes()[0]
grandChildNode = childNode.getChildNodes()[0]
assert childNode.isInvalid is False
connectedNode.markDescendantsInvalid(True)
assert childNode.isInvalid is True
Expand All @@ -176,15 +176,15 @@ def test_evalChildren(connectedNode: Node):

connectedNode.evalChildren()

childNode = connectedNode.getChildrenNodes()[0]
grandChildNode = childNode.getChildrenNodes()[0]
childNode = connectedNode.getChildNodes()[0]
grandChildNode = childNode.getChildNodes()[0]
assert childNode.isDirty is False
assert childNode.isInvalid is False
assert grandChildNode.isDirty is True
assert grandChildNode.isInvalid is True


def test_getChildrenNodes(emptyScene):
def test_getChildNodes(emptyScene):
node1 = Node(emptyScene, outputSocketTypes=[1])
node2 = Node(emptyScene, inputSocketTypes=[1], outputSocketTypes=[1])
node3 = Node(emptyScene, inputSocketTypes=[1])
Expand All @@ -196,13 +196,30 @@ def test_getChildrenNodes(emptyScene):
emptyScene, node2.outputSockets[0], node3.inputSockets[0]
) # noqa: F841

assert node1.getChildrenNodes() == [node2]
assert node1.getChildNodes() == [node2]
node2.remove()
assert node1.getChildrenNodes() == []
assert node1.getChildNodes() == []


def test_getParentNodes(emptyScene):
node1 = Node(emptyScene, outputSocketTypes=[1])
node2 = Node(emptyScene, inputSocketTypes=[1], outputSocketTypes=[1])
node3 = Node(emptyScene, inputSocketTypes=[1])

edge12 = Edge(
emptyScene, node1.outputSockets[0], node2.inputSockets[0]
) # noqa: F841
edge23 = Edge(
emptyScene, node2.outputSockets[0], node3.inputSockets[0]
) # noqa: F841

assert node2.getParentNodes() == [node1]
node1.remove()
assert node2.getParentNodes() == []


def test_inputNodeAt(connectedNode: Node):
childNode = connectedNode.getChildrenNodes()[0]
childNode = connectedNode.getChildNodes()[0]

assert childNode.inputNodeAt(0) == connectedNode

Expand All @@ -213,8 +230,8 @@ def test_inputNodeAt(connectedNode: Node):

def test_outputNodesAt(connectedNode: Node):
scene = connectedNode.scene
childNode = connectedNode.getChildrenNodes()[0]
grandChildNode = childNode.getChildrenNodes()[0]
childNode = connectedNode.getChildNodes()[0]
grandChildNode = childNode.getChildNodes()[0]

edge = Edge(scene, connectedNode.outputSockets[0], grandChildNode.inputSockets[0])

Expand Down

0 comments on commit ce7c5bb

Please sign in to comment.