def paint(self, painter, option, widget): """ Draws the node base not the ports. Args: painter (QtGui.QPainter): painter used for drawing the item. option (QtGui.QStyleOptionGraphicsItem): used to describe the parameters needed to draw. widget (QtWidgets.QWidget): not used. """ painter.save() bg_border = 1.0 rect = QtCore.QRectF(0.5 - (bg_border / 2), 0.5 - (bg_border / 2), self._width + bg_border, self._height + bg_border) radius = 2 border_color = QtGui.QColor(*self.border_color) path = QtGui.QPainterPath() path.addRoundedRect(rect, radius, radius) rect = self.boundingRect() bg_color = QtGui.QColor(*self.color) painter.setBrush(bg_color) painter.setPen(QtCore.Qt.NoPen) painter.drawRoundedRect(rect, radius, radius) if self.selected and NODE_SEL_COLOR: painter.setBrush(QtGui.QColor(*NODE_SEL_COLOR)) painter.drawRoundedRect(rect, radius, radius) label_rect = QtCore.QRectF(rect.left() + (radius / 2), rect.top() + (radius / 2), self._width - (radius / 1.25), 28) path = QtGui.QPainterPath() path.addRoundedRect(label_rect, radius / 1.5, radius / 1.5) painter.setBrush(QtGui.QColor(0, 0, 0, 50)) painter.fillPath(path, painter.brush()) border_width = 0.8 if self.selected and NODE_SEL_BORDER_COLOR: border_width = 1.2 border_color = QtGui.QColor(*NODE_SEL_BORDER_COLOR) border_rect = QtCore.QRectF(rect.left() - (border_width / 2), rect.top() - (border_width / 2), rect.width() + border_width, rect.height() + border_width) pen = QtGui.QPen(border_color, border_width) pen.setCosmetic(self.viewer().get_zoom() < 0.0) path = QtGui.QPainterPath() path.addRoundedRect(border_rect, radius, radius) painter.setBrush(QtCore.Qt.NoBrush) painter.setPen(pen) painter.drawPath(path) painter.restore()
def paint(self, painter, option, widget): """ Draws the sizer on the bottom right corner. Args: painter (QtGui.QPainter): painter used for drawing the item. option (QtGui.QStyleOptionGraphicsItem): used to describe the parameters needed to draw. widget (QtWidgets.QWidget): not used. """ painter.save() rect = self.boundingRect() item = self.parentItem() if item and item.selected: color = QtGui.QColor(*NODE_SEL_BORDER_COLOR) else: color = QtGui.QColor(*item.color) color = color.darker(50) path = QtGui.QPainterPath() path.moveTo(rect.topRight()) path.lineTo(rect.bottomRight()) path.lineTo(rect.bottomLeft()) painter.setBrush(color) painter.setPen(QtCore.Qt.NoPen) painter.fillPath(path, painter.brush()) painter.restore()
def paint(self, painter, option, widget): """ Draws the circular port. Args: painter (QtGui.QPainter): painter used for drawing the item. option (QtGui.QStyleOptionGraphicsItem): used to describe the parameters needed to draw. widget (QtWidgets.QWidget): not used. """ painter.save() rect = QtCore.QRectF(0.0, 0.8, self._width, self._height) painter.setBrush(QtGui.QColor(0, 0, 0, 200)) painter.setPen(QtGui.QPen(QtGui.QColor(0, 0, 0, 255), 1.8)) path = QtGui.QPainterPath() path.addEllipse(rect) painter.drawPath(path) if self._hovered: color = QtGui.QColor(*PORT_HOVER_COLOR) border_color = QtGui.QColor(*PORT_HOVER_BORDER_COLOR) elif self.connected_pipes: color = QtGui.QColor(*PORT_ACTIVE_COLOR) border_color = QtGui.QColor(*PORT_ACTIVE_BORDER_COLOR) else: color = QtGui.QColor(*self.color) border_color = QtGui.QColor(*self.border_color) painter.setBrush(color) pen = QtGui.QPen(border_color, 1.5) painter.setPen(pen) painter.drawEllipse(self.boundingRect()) painter.restore()
def mouseMoveEvent(self, event): alt_modifier = event.modifiers() == QtCore.Qt.AltModifier shift_modifier = event.modifiers() == QtCore.Qt.ShiftModifier if self.MMB_state and alt_modifier: pos_x = (event.x() - self._previous_pos.x()) zoom = 0.1 if pos_x > 0 else -0.1 self._set_viewer_zoom(zoom) elif self.MMB_state or (self.LMB_state and alt_modifier): pos_x = (event.x() - self._previous_pos.x()) pos_y = (event.y() - self._previous_pos.y()) self._set_viewer_pan(pos_x, pos_y) if self.LMB_state and self._rubber_band.isVisible(): rect = QtCore.QRect(self._origin_pos, event.pos()).normalized() map_rect = self.mapToScene(rect).boundingRect() path = QtGui.QPainterPath() path.addRect(map_rect) self._rubber_band.setGeometry(rect) self.scene().setSelectionArea(path, QtCore.Qt.IntersectsItemShape) self.scene().update(map_rect) if shift_modifier and self._prev_selection: for node in self._prev_selection: if node not in self.selected_nodes(): node.selected = True self._previous_pos = event.pos() super(NodeViewer, self).mouseMoveEvent(event)
def drawBackground(self, painter, rect): painter.save() bg_color = QtGui.QColor(*self._bg_color) painter.setRenderHint(QtGui.QPainter.Antialiasing, False) painter.setBrush(bg_color) painter.drawRect(rect) if not self._grid: painter.restore() return zoom = self.viewer().get_zoom() grid_size = 20 if zoom > -0.5: pen = QtGui.QPen(QtGui.QColor(*self.grid_color), 0.65) self._draw_grid(painter, rect, pen, grid_size) color = bg_color.darker(150) if zoom < -0.0: color = color.darker(100 - int(zoom * 110)) pen = QtGui.QPen(color, 0.65) self._draw_grid(painter, rect, pen, grid_size * 8) # fix border issue on the scene edge. pen = QtGui.QPen(bg_color, 2) pen.setCosmetic(True) path = QtGui.QPainterPath() path.addRect(rect) painter.setBrush(QtCore.Qt.NoBrush) painter.setPen(pen) painter.drawPath(path) painter.restore()
def paint(self, painter, option, widget): """ Draws the backdrop rect. Args: painter (QtGui.QPainter): painter used for drawing the item. option (QtGui.QStyleOptionGraphicsItem): used to describe the parameters needed to draw. widget (QtWidgets.QWidget): not used. """ painter.save() rect = self.boundingRect() color = (self.color[0], self.color[1], self.color[2], 50) painter.setBrush(QtGui.QColor(*color)) painter.setPen(QtCore.Qt.NoPen) painter.drawRect(rect) top_rect = QtCore.QRectF(0.0, 0.0, rect.width(), 20.0) painter.setBrush(QtGui.QColor(*self.color)) painter.setPen(QtCore.Qt.NoPen) painter.drawRect(top_rect) if self.backdrop_text: painter.setPen(QtGui.QColor(*self.text_color)) txt_rect = QtCore.QRectF(top_rect.x() + 5.0, top_rect.height() + 2.0, rect.width() - 5.0, rect.height()) painter.setPen(QtGui.QColor(*self.text_color)) painter.drawText(txt_rect, QtCore.Qt.AlignLeft | QtCore.Qt.TextWordWrap, self.backdrop_text) if self.selected and NODE_SEL_COLOR: sel_color = [x for x in NODE_SEL_COLOR] sel_color[-1] = 10 painter.setBrush(QtGui.QColor(*sel_color)) painter.setPen(QtCore.Qt.NoPen) painter.drawRect(rect) txt_rect = QtCore.QRectF(top_rect.x(), top_rect.y() + 1.2, rect.width(), top_rect.height()) painter.setPen(QtGui.QColor(*self.text_color)) painter.drawText(txt_rect, QtCore.Qt.AlignCenter, self.name) path = QtGui.QPainterPath() path.addRect(rect) border_color = self.color if self.selected and NODE_SEL_BORDER_COLOR: border_color = NODE_SEL_BORDER_COLOR painter.setBrush(QtCore.Qt.NoBrush) painter.setPen(QtGui.QPen(QtGui.QColor(*border_color), 1)) painter.drawPath(path) painter.restore()
def paint(self, painter, option, widget): """ Draws the circular port. Args: painter (QtGui.QPainter): painter used for drawing the item. option (QtGui.QStyleOptionGraphicsItem): used to describe the parameters needed to draw. widget (QtWidgets.QWidget): not used. """ painter.save() rect = QtCore.QRectF(0.0, 0.8, self._width, self._height) path = QtGui.QPainterPath() path.addEllipse(rect) if self._hovered: color = QtGui.QColor(*PORT_HOVER_COLOR) border_color = QtGui.QColor(*PORT_HOVER_BORDER_COLOR) elif self.connected_pipes: color = QtGui.QColor(*PORT_ACTIVE_COLOR) border_color = QtGui.QColor(*PORT_ACTIVE_BORDER_COLOR) else: color = QtGui.QColor(*self.color) border_color = QtGui.QColor(*self.border_color) painter.setBrush(color) pen = QtGui.QPen(border_color, 1.5) painter.setPen(pen) painter.drawEllipse(self.boundingRect()) if self.connected_pipes and not self._hovered: painter.setBrush(border_color) w = self._width / 2.5 h = self._height / 2.5 rect = QtCore.QRectF(self.boundingRect().center().x() - w / 2, self.boundingRect().center().y() - h / 2, w, h) painter.drawEllipse(rect) elif self._hovered: if self.multi_connection: painter.setBrush(color) w = self._width / 1.8 h = self._height / 1.8 else: painter.setBrush(border_color) w = self._width / 3.5 h = self._height / 3.5 rect = QtCore.QRectF(self.boundingRect().center().x() - w / 2, self.boundingRect().center().y() - h / 2, w, h) painter.drawEllipse(rect) painter.restore()
def draw_path(self, start_port, end_port, cursor_pos=None): """ Draws the path between ports. Args: start_port (PortItem): port used to draw the starting point. end_port (PortItem): port used to draw the end point. cursor_pos (QtCore.QPointF): cursor position if specified this will be the draw end point. """ if not start_port: return offset = (start_port.boundingRect().width() / 2) pos1 = start_port.scenePos() pos1.setX(pos1.x() + offset) pos1.setY(pos1.y() + offset) if cursor_pos: pos2 = cursor_pos elif end_port: offset = start_port.boundingRect().width() / 2 pos2 = end_port.scenePos() pos2.setX(pos2.x() + offset) pos2.setY(pos2.y() + offset) else: return line = QtCore.QLineF(pos1, pos2) path = QtGui.QPainterPath() path.moveTo(line.x1(), line.y1()) if self.viewer_pipe_layout() == PIPE_LAYOUT_STRAIGHT: path.lineTo(pos2) self.setPath(path) return ctr_offset_x1, ctr_offset_x2 = pos1.x(), pos2.x() tangent = ctr_offset_x1 - ctr_offset_x2 tangent = (tangent * -1) if tangent < 0 else tangent max_width = start_port.node.boundingRect().width() / 2 tangent = max_width if tangent > max_width else tangent if start_port.port_type == IN_PORT: ctr_offset_x1 -= tangent ctr_offset_x2 += tangent else: ctr_offset_x1 += tangent ctr_offset_x2 -= tangent ctr_point1 = QtCore.QPointF(ctr_offset_x1, pos1.y()) ctr_point2 = QtCore.QPointF(ctr_offset_x2, pos2.y()) path.cubicTo(ctr_point1, ctr_point2, pos2) self.setPath(path)
def mouseMoveEvent(self, event): if self.ALT_state and self.SHIFT_state: if self.LMB_state and self._SLICER_PIPE.isVisible(): p1 = self._SLICER_PIPE.path().pointAtPercent(0) p2 = self.mapToScene(self._previous_pos) self._SLICER_PIPE.draw_path(p1, p2) self._SLICER_PIPE.show() self._previous_pos = event.pos() super(NodeViewer, self).mouseMoveEvent(event) return if self.MMB_state and self.ALT_state: pos_x = (event.x() - self._previous_pos.x()) zoom = 0.1 if pos_x > 0 else -0.1 self._set_viewer_zoom(zoom, 0.05) elif self.MMB_state or (self.LMB_state and self.ALT_state): pos_x = (event.x() - self._previous_pos.x()) pos_y = (event.y() - self._previous_pos.y()) self._set_viewer_pan(pos_x, pos_y) if self.LMB_state and self._rubber_band.isVisible(): rect = QtCore.QRect(self._origin_pos, event.pos()).normalized() map_rect = self.mapToScene(rect).boundingRect() path = QtGui.QPainterPath() path.addRect(map_rect) self._rubber_band.setGeometry(rect) self.scene().setSelectionArea(path, QtCore.Qt.IntersectsItemShape) self.scene().update(map_rect) if self.SHIFT_state and self._prev_selection: for node in self._prev_selection: if node not in self.selected_nodes(): node.selected = True self._previous_pos = event.pos() super(NodeViewer, self).mouseMoveEvent(event)
def mouseMoveEvent(self, event): if self.ALT_state and self.SHIFT_state: if self.LMB_state and self._SLICER_PIPE.isVisible(): p1 = self._SLICER_PIPE.path().pointAtPercent(0) p2 = self.mapToScene(self._previous_pos) self._SLICER_PIPE.draw_path(p1, p2) self._SLICER_PIPE.show() self._previous_pos = event.pos() super(NodeViewer, self).mouseMoveEvent(event) return if self.MMB_state and self.ALT_state: pos_x = (event.x() - self._previous_pos.x()) zoom = 0.1 if pos_x > 0 else -0.1 self._set_viewer_zoom(zoom, 0.05, pos=event.pos()) elif self.MMB_state or (self.LMB_state and self.ALT_state): pos_x = (event.x() - self._previous_pos.x()) pos_y = (event.y() - self._previous_pos.y()) self._set_viewer_pan(pos_x, pos_y) if self.LMB_state and self._rubber_band.isVisible(): rect = QtCore.QRect(self._origin_pos, event.pos()).normalized() map_rect = self.mapToScene(rect).boundingRect() path = QtGui.QPainterPath() path.addRect(map_rect) self._rubber_band.setGeometry(rect) self.scene().setSelectionArea(path, QtCore.Qt.IntersectsItemShape) self.scene().update(map_rect) if self.SHIFT_state or self.CTRL_state: nodes, pipes = self.selected_items() for pipe in self._prev_selection_pipes: pipe.setSelected(True) for node in self._prev_selection_nodes: node.selected = True if self.CTRL_state: for pipe in pipes: pipe.setSelected(False) for node in nodes: node.selected = False elif self.LMB_state: self.COLLIDING_state = False nodes = self.selected_nodes() if len(nodes) == 1: node = nodes[0] for pipe in self.selected_pipes(): pipe.setSelected(False) for item in node.collidingItems(): if isinstance(item, Pipe) and item.isVisible(): if not item.input_port: continue if not item.input_port.node is node and \ not item.output_port.node is node: item.setSelected(True) self.COLLIDING_state = True break self._previous_pos = event.pos() super(NodeViewer, self).mouseMoveEvent(event)
def reset_path(self): path = QtGui.QPainterPath(QtCore.QPointF(0.0, 0.0)) self.setPath(path)
def draw_path(self, p1, p2): path = QtGui.QPainterPath() path.moveTo(p1) path.lineTo(p2) self.setPath(path)