Add "label" as an internal attribute

Setting the "label" internal attribute allows to give a custom
label to replace the node's default label.
This commit is contained in:
Candice Bentéjac 2022-08-02 12:36:11 +02:00
parent 9bc9e2c129
commit 330382ab0c
4 changed files with 31 additions and 1 deletions

View file

@ -168,6 +168,10 @@ class Attribute(BaseObject):
# TODO: only update the graph if this attribute participates to a UID
if self.isInput:
self.requestGraphUpdate()
# TODO: only call update of the node if the attribute is internal
# Internal attributes are set as inputs
self.requestNodeUpdate()
self.valueChanged.emit()
def upgradeValue(self, exportedValue):
@ -181,6 +185,12 @@ class Attribute(BaseObject):
self.node.graph.markNodesDirty(self.node)
self.node.graph.update()
def requestNodeUpdate(self):
# Update specific node information that do not affect the rest of the graph
# (like internal attributes)
if self.node:
self.node.updateInternalAttributes()
@property
def isOutput(self):
return self._isOutput

View file

@ -501,6 +501,13 @@ class Node(object):
value="",
semantic="multiline",
uid=[0],
),
StringParam(
name="label",
label="Label",
description="Custom label to replace the node's default label.",
value="",
uid=[],
)
]
inputs = []

View file

@ -524,8 +524,12 @@ class BaseNode(BaseObject):
def getLabel(self):
"""
Returns:
str: the high-level label of this node
str: the user-provided label if it exists, the high-level label of this node otherwise
"""
if self.hasInternalAttribute("label"):
label = self.internalAttribute("label").value.strip()
if label:
return label
return self.nameToLabel(self._name)
@Slot(str, result=str)
@ -856,6 +860,9 @@ class BaseNode(BaseObject):
if self.internalFolder != folder:
self.internalFolderChanged.emit()
def updateInternalAttributes(self):
self.internalAttributesChanged.emit()
@property
def internalFolder(self):
return self._internalFolder.format(**self._cmdVars)
@ -1089,6 +1096,7 @@ class BaseNode(BaseObject):
y = Property(float, lambda self: self._position.y, notify=positionChanged)
attributes = Property(BaseObject, getAttributes, constant=True)
internalAttributes = Property(BaseObject, getInternalAttributes, constant=True)
internalAttributesChanged = Signal()
internalFolderChanged = Signal()
internalFolder = Property(str, internalFolder.fget, notify=internalFolderChanged)
depthChanged = Signal()

View file

@ -71,6 +71,10 @@ Item {
root.x = root.node.x
root.y = root.node.y
}
onInternalAttributesChanged: {
nodeLabel.text = node ? node.label : ""
}
}
// Whether an attribute can be displayed as an attribute pin on the node
@ -181,6 +185,7 @@ Item {
// Node Name
Label {
id: nodeLabel
Layout.fillWidth: true
text: node ? node.label : ""
padding: 4