|
| 1 | +from PySide2 import QtWidgets, QtGui, QtCore |
| 2 | + |
| 3 | +from node_editor.gui.port import Port |
| 4 | + |
| 5 | + |
| 6 | +class Node(QtWidgets.QGraphicsPathItem): |
| 7 | + def __init__(self): |
| 8 | + super(Node, self).__init__() |
| 9 | + |
| 10 | + self.setFlag(QtWidgets.QGraphicsPathItem.ItemIsMovable) |
| 11 | + self.setFlag(QtWidgets.QGraphicsPathItem.ItemIsSelectable) |
| 12 | + |
| 13 | + self._title_text = "Title" |
| 14 | + self._type_text = "base" |
| 15 | + |
| 16 | + self._width = 30 # The Width of the node |
| 17 | + self._height = 30 # the height of the node |
| 18 | + self._ports = [] # A list of ports |
| 19 | + |
| 20 | + self.node_color = QtGui.QColor(20, 20, 20, 200) |
| 21 | + |
| 22 | + self.title_path = QtGui.QPainterPath() # The path for the title |
| 23 | + self.type_path = QtGui.QPainterPath() # The path for the type |
| 24 | + self.misc_path = QtGui.QPainterPath() # a bunch of other stuff |
| 25 | + |
| 26 | + self.horizontal_margin = 30 # horizontal margin |
| 27 | + self.vertical_margin = 15 # vertical margin |
| 28 | + |
| 29 | + def paint(self, painter, option=None, widget=None): |
| 30 | + if self.isSelected(): |
| 31 | + painter.setPen(QtGui.QPen(QtGui.QColor(241, 175, 0), 2)) |
| 32 | + painter.setBrush(self.node_color) |
| 33 | + else: |
| 34 | + painter.setPen(self.node_color.lighter()) |
| 35 | + painter.setBrush(self.node_color) |
| 36 | + |
| 37 | + painter.drawPath(self.path()) |
| 38 | + painter.setPen(QtCore.Qt.NoPen) |
| 39 | + painter.setBrush(QtCore.Qt.white) |
| 40 | + |
| 41 | + painter.drawPath(self.title_path) |
| 42 | + painter.drawPath(self.type_path) |
| 43 | + painter.drawPath(self.misc_path) |
| 44 | + |
| 45 | + def add_port(self, name, is_output=False, flags=0, ptr=None): |
| 46 | + port = Port(self, self.scene()) |
| 47 | + port.set_is_output(is_output) |
| 48 | + port.set_name(name) |
| 49 | + port.set_node(node=self) |
| 50 | + port.set_port_flags(flags) |
| 51 | + port.set_ptr(ptr) |
| 52 | + |
| 53 | + self._ports.append(port) |
| 54 | + |
| 55 | + def build(self): |
| 56 | + """ Build the node |
| 57 | + """ |
| 58 | + |
| 59 | + self.title_path = QtGui.QPainterPath() # reset |
| 60 | + self.type_path = QtGui.QPainterPath() # The path for the type |
| 61 | + self.misc_path = QtGui.QPainterPath() # a bunch of other stuff |
| 62 | + |
| 63 | + total_width = 0 |
| 64 | + total_height = 0 |
| 65 | + path = QtGui.QPainterPath() # The main path |
| 66 | + |
| 67 | + # The fonts what will be used |
| 68 | + title_font = QtGui.QFont("Lucida Sans Unicode", pointSize=16) |
| 69 | + title_type_font = QtGui.QFont("Lucida Sans Unicode", pointSize=8) |
| 70 | + port_font = QtGui.QFont("Lucida Sans Unicode") |
| 71 | + |
| 72 | + # Get the dimentions of the title and type |
| 73 | + title_dim = { |
| 74 | + "w": QtGui.QFontMetrics(title_font).width(self._title_text), |
| 75 | + "h": QtGui.QFontMetrics(title_font).height(), |
| 76 | + } |
| 77 | + |
| 78 | + title_type_dim = { |
| 79 | + "w": QtGui.QFontMetrics(title_type_font).width("(" + self._type_text + ")"), |
| 80 | + "h": QtGui.QFontMetrics(title_type_font).height(), |
| 81 | + } |
| 82 | + |
| 83 | + # Get the max width |
| 84 | + for dim in [title_dim["w"], title_type_dim["w"]]: |
| 85 | + if dim > total_width: |
| 86 | + total_width = dim |
| 87 | + |
| 88 | + # Add both the title and type height together for the total height |
| 89 | + for dim in [title_dim["h"], title_type_dim["h"]]: |
| 90 | + total_height += dim |
| 91 | + |
| 92 | + # Add the heigth for each of the ports |
| 93 | + for port in self._ports: |
| 94 | + port_dim = { |
| 95 | + "w": QtGui.QFontMetrics(port_font).width(port.name()), |
| 96 | + "h": QtGui.QFontMetrics(port_font).height(), |
| 97 | + } |
| 98 | + |
| 99 | + if port_dim["w"] > total_width: |
| 100 | + total_width = port_dim["w"] |
| 101 | + |
| 102 | + total_height += port_dim["h"] |
| 103 | + |
| 104 | + # Add the margin to the total_width |
| 105 | + total_width += self.horizontal_margin |
| 106 | + total_height += self.vertical_margin |
| 107 | + |
| 108 | + # Draw the background rectangle |
| 109 | + path.addRoundedRect( |
| 110 | + -total_width / 2, -total_height / 2, total_width, total_height, 5, 5 |
| 111 | + ) |
| 112 | + |
| 113 | + # Draw the title |
| 114 | + self.title_path.addText( |
| 115 | + -title_dim["w"] / 2, |
| 116 | + (-total_height / 2) + title_dim["h"], |
| 117 | + title_font, |
| 118 | + self._title_text, |
| 119 | + ) |
| 120 | + |
| 121 | + # Draw the type |
| 122 | + self.type_path.addText( |
| 123 | + -title_type_dim["w"] / 2, |
| 124 | + (-total_height / 2) + title_dim["h"] + title_type_dim["h"], |
| 125 | + title_type_font, |
| 126 | + "(" + self._type_text + ")", |
| 127 | + ) |
| 128 | + |
| 129 | + y = (-total_height / 2) + title_dim["h"] + title_type_dim["h"] + port_dim["h"] |
| 130 | + |
| 131 | + for port in self._ports: |
| 132 | + if port.is_output(): |
| 133 | + port.setPos(total_width / 2 - 10, y) |
| 134 | + else: |
| 135 | + port.setPos(-total_width / 2 + 10, y) |
| 136 | + y += port_dim["h"] |
| 137 | + |
| 138 | + self.setPath(path) |
| 139 | + |
| 140 | + self._width = total_width |
| 141 | + self._height = total_height |
| 142 | + |
| 143 | + def select_connections(self, value): |
| 144 | + for port in self._ports: |
| 145 | + for connection in port.connections(): |
| 146 | + connection._do_highlight = value |
| 147 | + connection.update_path() |
| 148 | + |
0 commit comments