im_scene.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from enum import Enum
  2. from PyQt5.QtWidgets import QGraphicsScene, QGraphicsItem
  3. from PyQt5.Qt import Qt, QTransform
  4. from sketchUI.graphics_edge_item import GraphicsEdgeItem
  5. from sketchUI.graphics_node_item import GraphicsNodeItem
  6. from sketchUI import mvops
  7. class Mode(Enum):
  8. SELECT = 0
  9. CONNECT = 1
  10. class CustomScene(QGraphicsScene):
  11. def __init__(self, model, parent):
  12. QGraphicsScene.__init__(self)
  13. self._mode = None # set by mainwindow at start
  14. self.connect_from_item = None # store the item to draw the connecting line from
  15. self._cur_model = model # the mv model we operate on
  16. self._parent = parent # mainwindow parent
  17. def set_mode(self, mode):
  18. self._mode = mode
  19. def mousePressEvent(self, event):
  20. if event.button() == Qt.LeftButton and self._mode == Mode.CONNECT:
  21. item = self.itemAt(event.scenePos(), QTransform())
  22. if not item:
  23. return
  24. self.connect_from_item = item
  25. QGraphicsScene.mousePressEvent(self, event)
  26. def mouseReleaseEvent(self, event):
  27. if event.button() == Qt.LeftButton and self._mode == Mode.CONNECT:
  28. item = self.itemAt(event.scenePos(), QTransform())
  29. if not item:
  30. return
  31. if item == self.connect_from_item:
  32. self._parent.plainTextEdit.appendPlainText("Error: Cannot connect same elements")
  33. return
  34. self.draw_edge(self.connect_from_item, item, is_new=True)
  35. if event.button() == Qt.LeftButton and self._mode == Mode.SELECT:
  36. item = self.itemAt(event.scenePos(), QTransform())
  37. if not item:
  38. return
  39. for item in self.items():
  40. try:
  41. item.__hack__() # hack to check if item is of edge type
  42. item.redraw()
  43. except AttributeError:
  44. continue
  45. QGraphicsScene.mouseReleaseEvent(self, event)
  46. def draw_edge(self, from_item, to_item, is_new):
  47. # type: (GraphicsNodeItem, GraphicsNodeItem, bool) -> None
  48. """
  49. Draw edge on screen between the node items "from_item" and "to_item".
  50. If is_new, additionally instantiate the edge in the model (if supported).
  51. """
  52. from_type = from_item.get_type()
  53. to_type = to_item.get_type()
  54. if is_new:
  55. if not mvops.is_edge_supported(from_type, to_type):
  56. self._parent.plainTextEdit.appendPlainText("Error: Edge not supported between types {} and {}".format(from_type, to_type))
  57. return
  58. line = GraphicsEdgeItem(from_item, to_item)
  59. line.setFlag(QGraphicsItem.ItemIsMovable, False)
  60. line.setFlag(QGraphicsItem.ItemIsSelectable, False)
  61. self.addItem(line)
  62. line.redraw()
  63. if is_new:
  64. mvops.add_edge(self._cur_model, from_item.node_id, to_item.node_id)
  65. self._parent.plainTextEdit.appendPlainText("Added edge between {} and {} to model".format(from_type, to_type))