graphics_node_item.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """
  2. QGraphics representation class for a node
  3. """
  4. from PyQt5.QtWidgets import QGraphicsItem, QStyleOptionGraphicsItem, QWidget
  5. from PyQt5.Qt import QRectF, QPainter, QFont, Qt
  6. class GraphicsNodeItem(QGraphicsItem):
  7. def __init__(self, node_id, node_type=""):
  8. QGraphicsItem.__init__(self)
  9. self.node_id = node_id
  10. self._type = node_type
  11. self._font = QFont()
  12. self._font.setPixelSize(12)
  13. def set_type(self, node_type):
  14. self._type = node_type
  15. self.update(self.boundingRect())
  16. def get_type(self):
  17. return self._type
  18. def boundingRect(self):
  19. bbox = QRectF(0, 0, 120, 120)
  20. return bbox
  21. def paint(self, painter, option, widget=None):
  22. # type: (QPainter, QStyleOptionGraphicsItem, QWidget) -> None
  23. painter.setFont(self._font)
  24. painter.drawRect(self.boundingRect())
  25. text_box = QRectF(0, 100, 120, 20)
  26. painter.drawRect(text_box)
  27. painter.drawText(text_box, Qt.AlignCenter | Qt.TextDontClip, self._type)