121 lines
3.5 KiB
Python
121 lines
3.5 KiB
Python
#!/usr/bin/python
|
|
from qtpy import QtCore, QtGui
|
|
|
|
from OdenGraphQt import BaseNode
|
|
|
|
|
|
def draw_triangle_port(painter, rect, info):
|
|
"""
|
|
Custom paint function for drawing a Triangle shaped port.
|
|
|
|
Args:
|
|
painter (QtGui.QPainter): painter object.
|
|
rect (QtCore.QRectF): port rect used to describe parameters
|
|
needed to draw.
|
|
info (dict): information describing the ports current state.
|
|
{
|
|
'port_type': 'in',
|
|
'color': (0, 0, 0),
|
|
'border_color': (255, 255, 255),
|
|
'multi_connection': False,
|
|
'connected': False,
|
|
'hovered': False,
|
|
}
|
|
"""
|
|
painter.save()
|
|
|
|
size = int(rect.height() / 2)
|
|
triangle = QtGui.QPolygonF()
|
|
triangle.append(QtCore.QPointF(-size, size))
|
|
triangle.append(QtCore.QPointF(0.0, -size))
|
|
triangle.append(QtCore.QPointF(size, size))
|
|
|
|
transform = QtGui.QTransform()
|
|
transform.translate(rect.center().x(), rect.center().y())
|
|
port_poly = transform.map(triangle)
|
|
|
|
# mouse over port color.
|
|
if info['hovered']:
|
|
color = QtGui.QColor(14, 45, 59)
|
|
border_color = QtGui.QColor(136, 255, 35)
|
|
# port connected color.
|
|
elif info['connected']:
|
|
color = QtGui.QColor(195, 60, 60)
|
|
border_color = QtGui.QColor(200, 130, 70)
|
|
# default port color
|
|
else:
|
|
color = QtGui.QColor(*info['color'])
|
|
border_color = QtGui.QColor(*info['border_color'])
|
|
|
|
pen = QtGui.QPen(border_color, 1.8)
|
|
pen.setJoinStyle(QtCore.Qt.PenJoinStyle.MiterJoin)
|
|
|
|
painter.setPen(pen)
|
|
painter.setBrush(color)
|
|
painter.drawPolygon(port_poly)
|
|
|
|
painter.restore()
|
|
|
|
|
|
def draw_square_port(painter, rect, info):
|
|
"""
|
|
Custom paint function for drawing a Square shaped port.
|
|
|
|
Args:
|
|
painter (QtGui.QPainter): painter object.
|
|
rect (QtCore.QRectF): port rect used to describe parameters
|
|
needed to draw.
|
|
info (dict): information describing the ports current state.
|
|
{
|
|
'port_type': 'in',
|
|
'color': (0, 0, 0),
|
|
'border_color': (255, 255, 255),
|
|
'multi_connection': False,
|
|
'connected': False,
|
|
'hovered': False,
|
|
}
|
|
"""
|
|
painter.save()
|
|
|
|
# mouse over port color.
|
|
if info['hovered']:
|
|
color = QtGui.QColor(14, 45, 59)
|
|
border_color = QtGui.QColor(136, 255, 35, 255)
|
|
# port connected color.
|
|
elif info['connected']:
|
|
color = QtGui.QColor(195, 60, 60)
|
|
border_color = QtGui.QColor(200, 130, 70)
|
|
# default port color
|
|
else:
|
|
color = QtGui.QColor(*info['color'])
|
|
border_color = QtGui.QColor(*info['border_color'])
|
|
|
|
pen = QtGui.QPen(border_color, 1.8)
|
|
pen.setJoinStyle(QtCore.Qt.PenJoinStyle.MiterJoin)
|
|
|
|
painter.setPen(pen)
|
|
painter.setBrush(color)
|
|
painter.drawRect(rect)
|
|
|
|
painter.restore()
|
|
|
|
|
|
class CustomPortsNode(BaseNode):
|
|
"""
|
|
example test node with custom shaped ports.
|
|
"""
|
|
|
|
# set a unique node identifier.
|
|
__identifier__ = 'nodes.custom.ports'
|
|
|
|
# set the initial default node name.
|
|
NODE_NAME = 'node'
|
|
|
|
def __init__(self):
|
|
super(CustomPortsNode, self).__init__()
|
|
|
|
# create input and output port.
|
|
self.add_input('in', color=(200, 10, 0))
|
|
self.add_output('default')
|
|
self.add_output('square', painter_func=draw_square_port)
|
|
self.add_output('triangle', painter_func=draw_triangle_port) |