im_scene.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. from enum import Enum
  2. from PyQt5.QtWidgets import QGraphicsScene, QGraphicsItem, QTableWidgetItem, QInputDialog
  3. from PyQt5.Qt import Qt, QTransform, QApplication
  4. from sketchUI.graphics_edge_item import GraphicsEdgeItem
  5. from sketchUI.graphics_node_item import GraphicsNodeItem
  6. from sketchUI import mvops
  7. import commons
  8. class Mode(Enum):
  9. SELECT = 0
  10. CONNECT = 1
  11. class CustomScene(QGraphicsScene):
  12. def __init__(self, model, parent):
  13. QGraphicsScene.__init__(self)
  14. self._mode = None # set by mainwindow at start
  15. self.connect_from_item = None # store the item to draw the connecting line from
  16. self._cur_model = model # the mv model we operate on
  17. self._parent = parent # mainwindow parent
  18. def set_mode(self, mode):
  19. self._mode = mode
  20. def mousePressEvent(self, event):
  21. if event.button() == Qt.LeftButton and self._mode == Mode.CONNECT:
  22. item = self.itemAt(event.scenePos(), QTransform())
  23. if not item:
  24. return
  25. # store the potential item to be connected from
  26. self.connect_from_item = item
  27. if event.button() == Qt.LeftButton and self._mode == Mode.SELECT:
  28. # load attributes for selected node
  29. item = self.itemAt(event.scenePos(), QTransform())
  30. if not item or not isinstance(item, GraphicsNodeItem):
  31. return
  32. self._parent.tableWidget.setRowCount(0)
  33. self._parent.tableWidget.blockSignals(True)
  34. self._parent.plainTextEdit.appendPlainText("Selected item of type {}".format(item.get_type()))
  35. attrs = commons.get_attributes_of_node(self._cur_model, item.node_id)
  36. for attr in attrs:
  37. table_item_key = QTableWidgetItem(attr.key)
  38. table_item_key.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
  39. table_item_val = QTableWidgetItem(attr.val)
  40. cur_row_cnt = self._parent.tableWidget.rowCount()
  41. self._parent.tableWidget.insertRow(cur_row_cnt)
  42. self._parent.tableWidget.setItem(cur_row_cnt, 0, table_item_key)
  43. self._parent.tableWidget.setItem(cur_row_cnt, 1, table_item_val)
  44. self._parent.tableWidget.blockSignals(False)
  45. QGraphicsScene.mousePressEvent(self, event)
  46. def mouseReleaseEvent(self, event):
  47. if event.button() == Qt.LeftButton and self._mode == Mode.CONNECT:
  48. item = self.itemAt(event.scenePos(), QTransform())
  49. if not item:
  50. return
  51. if item == self.connect_from_item:
  52. self._parent.plainTextEdit.appendPlainText("Error: Cannot connect same elements")
  53. return
  54. self.draw_edge(self.connect_from_item, item, is_new=True)
  55. if event.button() == Qt.LeftButton and self._mode == Mode.SELECT:
  56. item = self.itemAt(event.scenePos(), QTransform())
  57. if not item:
  58. return
  59. for item in self.items():
  60. try:
  61. item.__hack__() # hack to check if item is of edge type
  62. item.redraw()
  63. except AttributeError:
  64. continue
  65. QGraphicsScene.mouseReleaseEvent(self, event)
  66. def keyPressEvent(self, event):
  67. if not self._mode == Mode.SELECT:
  68. return
  69. # del deletes elements
  70. if event.key() == Qt.Key_Delete:
  71. self._handle_keypress_delete(self.selectedItems())
  72. elif event.key() == Qt.Key_A:
  73. self._handle_keypress_attribute(self.selectedItems())
  74. else:
  75. pass
  76. QGraphicsScene.keyPressEvent(self, event)
  77. def draw_edge(self, from_item, to_item, is_new):
  78. # type: (GraphicsNodeItem, GraphicsNodeItem, bool) -> None
  79. """
  80. Draw edge on screen between the node items "from_item" and "to_item".
  81. If is_new, additionally instantiate the edge in the model (if supported).
  82. """
  83. from_type = from_item.get_type()
  84. to_type = to_item.get_type()
  85. if is_new:
  86. if not mvops.is_edge_supported(from_type, to_type):
  87. self._parent.plainTextEdit.appendPlainText("Error: Edge not supported between types {} and {}".format(from_type, to_type))
  88. return
  89. line = GraphicsEdgeItem(from_item, to_item)
  90. line.setFlag(QGraphicsItem.ItemIsMovable, False)
  91. line.setFlag(QGraphicsItem.ItemIsSelectable, False)
  92. self.addItem(line)
  93. line.redraw()
  94. if is_new:
  95. mvops.add_edge(self._cur_model, from_item.node_id, to_item.node_id)
  96. self._parent.plainTextEdit.appendPlainText("Added edge between {} and {} to model".format(from_type, to_type))
  97. def _handle_keypress_delete(self, selected):
  98. for item in selected:
  99. # delete node in model (also deletes edges in model)
  100. if isinstance(item, GraphicsNodeItem):
  101. self._parent.plainTextEdit.appendPlainText("Deleting node of type {}".format(item.get_type()))
  102. mvops.delete_node(self._cur_model, item.node_id)
  103. # in view, delete edges that were connected to this node as well
  104. # modelverse does this on its own so do not delete edges explicitly here
  105. for edge in self.items():
  106. if not isinstance(edge, GraphicsEdgeItem):
  107. continue
  108. if edge.from_item.node_id == item.node_id or edge.to_item.node_id == item.node_id:
  109. self.removeItem(edge)
  110. self.removeItem(item)
  111. def _handle_keypress_attribute(self, selected):
  112. if not len(selected) == 1:
  113. return
  114. item = selected[0]
  115. if not isinstance(item, GraphicsNodeItem):
  116. return
  117. # quickly check if attributing of such a node is allowed
  118. QApplication.setOverrideCursor(Qt.WaitCursor)
  119. self._parent.plainTextEdit.appendPlainText("Checking if attributing nodes of type {} is allowed ...".format(item.get_type()))
  120. if not mvops.is_attributing_allowed(item.get_type()):
  121. self._parent.plainTextEdit.appendPlainText("Error: Not allowed".format(item.get_type()))
  122. QApplication.restoreOverrideCursor()
  123. return
  124. QApplication.restoreOverrideCursor()
  125. self._parent.plainTextEdit.appendPlainText("Yes")
  126. # ask user for key value
  127. key, ok = QInputDialog.getText(self._parent, "New attribute", "Key value")
  128. if not ok or not key:
  129. return
  130. # check if key value already used for this node
  131. attrs = commons.get_attributes_of_node(self._cur_model, item.node_id)
  132. for attr in attrs:
  133. if attr.key == key:
  134. self._parent.plainTextEdit.appendPlainText("Error: Already such a key for the node: {}".format(key))
  135. return
  136. self._parent.add_new_attribute(key)