Add "color" as an internal attribute

Setting this attribute allows the user to change the color
of a node, either by directly providing an SVG color name or an
hexadecimal color code, or by picking a color with the selector.
This commit is contained in:
Candice Bentéjac 2022-08-02 14:16:11 +02:00
parent 330382ab0c
commit 21d01acc9a
4 changed files with 98 additions and 1 deletions

View file

@ -343,6 +343,19 @@ class StringParam(Param):
return ""
class ColorParam(Param):
"""
"""
def __init__(self, name, label, description, value, uid, group='allParams', advanced=False, semantic='', enabled=True):
super(ColorParam, self).__init__(name=name, label=label, description=description, value=value, uid=uid, group=group, advanced=advanced, semantic=semantic, enabled=enabled)
def validateValue(self, value):
if not isinstance(value, pyCompatibility.basestring) or len(value.split(" ")) > 1:
raise ValueError('ColorParam value should be a string containing either an SVG name or an hexadecimal '
'color code (param: {}, value: {}, type: {})'.format(self.name, value, type(value)))
return value
class Level(Enum):
NONE = 0
NORMAL = 1
@ -508,6 +521,13 @@ class Node(object):
description="Custom label to replace the node's default label.",
value="",
uid=[],
),
ColorParam(
name="color",
label="Color",
description="Custom color for the node (SVG name or hexadecimal code).",
value="",
uid=[],
)
]
inputs = []

View file

@ -532,6 +532,15 @@ class BaseNode(BaseObject):
return label
return self.nameToLabel(self._name)
def getColor(self):
"""
Returns:
str: the user-provided custom color of the node if it exists, empty string otherwise
"""
if self.hasInternalAttribute("color"):
return self.internalAttribute("color").value.strip()
return ""
@Slot(str, result=str)
def nameToLabel(self, name):
"""
@ -1088,6 +1097,7 @@ class BaseNode(BaseObject):
name = Property(str, getName, constant=True)
label = Property(str, getLabel, constant=True)
color = Property(str, getColor, constant=True)
nodeType = Property(str, nodeType.fget, constant=True)
documentation = Property(str, getDocumentation, constant=True)
positionChanged = Signal()

View file

@ -157,6 +157,8 @@ RowLayout {
if (attribute.desc.semantic === 'multiline')
return textArea_component
return textField_component
case "ColorParam":
return color_component
default: return textField_component
}
}
@ -240,6 +242,70 @@ RowLayout {
}
}
Component {
id: color_component
RowLayout {
CheckBox {
id: color_checkbox
Layout.alignment: Qt.AlignLeft
checked: node.color === "" ? false : true
text: "Custom Color"
onClicked: {
if(checked) {
_reconstruction.setAttribute(attribute, "#0000FF")
} else {
_reconstruction.setAttribute(attribute, "")
}
}
}
TextField {
id: colorText
Layout.alignment: Qt.AlignLeft
implicitWidth: 100
enabled: color_checkbox.checked
visible: enabled
text: enabled ? attribute.value : ""
selectByMouse: true
onEditingFinished: setTextFieldAttribute(text)
onAccepted: setTextFieldAttribute(text)
Component.onDestruction: {
if(activeFocus)
setTextFieldAttribute(text)
}
}
Rectangle {
height: colorText.height
width: colorText.width / 2
Layout.alignment: Qt.AlignLeft
visible: color_checkbox.checked
color: color_checkbox.checked ? attribute.value : ""
MouseArea {
anchors.fill: parent
onClicked: colorDialog.open()
}
}
ColorDialog {
id: colorDialog
title: "Please choose a color"
color: attribute.value
onAccepted: {
colorText.text = color
// Artificially trigger change of attribute value
colorText.editingFinished()
close()
}
onRejected: close()
}
Item {
// Dummy item to fill out the space if needed
Layout.fillWidth: true
}
}
}
Component {
id: comboBox_component
ComboBox {

View file

@ -74,6 +74,7 @@ Item {
onInternalAttributesChanged: {
nodeLabel.text = node ? node.label : ""
background.color = (node.color === "" ? Qt.lighter(activePalette.base, 1.4) : node.color)
}
}
@ -142,7 +143,7 @@ Item {
Rectangle {
id: background
anchors.fill: nodeContent
color: Qt.lighter(activePalette.base, 1.4)
color: node.color === "" ? Qt.lighter(activePalette.base, 1.4) : node.color
layer.enabled: true
layer.effect: DropShadow { radius: 3; color: shadowColor }
radius: 3