im_scene.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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:
  31. return
  32. elif isinstance(item, GraphicsNodeItem):
  33. self._parent.tableWidget.setRowCount(0)
  34. self._parent.tableWidget.blockSignals(True)
  35. self._parent.plainTextEdit.appendPlainText("Selected node of type {}".format(item.get_type()))
  36. attrs = commons.get_attributes_of_node(self._cur_model, item.node_id)
  37. for attr in attrs:
  38. table_item_key = QTableWidgetItem(attr.key)
  39. table_item_key.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
  40. table_item_val = QTableWidgetItem(attr.val)
  41. cur_row_cnt = self._parent.tableWidget.rowCount()
  42. self._parent.tableWidget.insertRow(cur_row_cnt)
  43. self._parent.tableWidget.setItem(cur_row_cnt, 0, table_item_key)
  44. self._parent.tableWidget.setItem(cur_row_cnt, 1, table_item_val)
  45. self._parent.tableWidget.blockSignals(False)
  46. elif isinstance(item, GraphicsEdgeItem):
  47. self._parent.plainTextEdit.appendPlainText("Selected edge")
  48. else:
  49. pass
  50. QGraphicsScene.mousePressEvent(self, event)
  51. def mouseReleaseEvent(self, event):
  52. if event.button() == Qt.LeftButton and self._mode == Mode.CONNECT:
  53. item = self.itemAt(event.scenePos(), QTransform())
  54. if not item:
  55. return
  56. if item == self.connect_from_item:
  57. self._parent.plainTextEdit.appendPlainText("Error: Cannot connect same elements")
  58. return
  59. self.draw_edge(self.connect_from_item, item, is_new=True)
  60. if event.button() == Qt.LeftButton and self._mode == Mode.SELECT:
  61. item = self.itemAt(event.scenePos(), QTransform())
  62. if not item:
  63. return
  64. for item in self.items():
  65. try:
  66. item.__hack__() # hack to check if item is of edge type
  67. item.redraw()
  68. except AttributeError:
  69. continue
  70. QGraphicsScene.mouseReleaseEvent(self, event)
  71. def keyPressEvent(self, event):
  72. if not self._mode == Mode.SELECT:
  73. return
  74. # del deletes elements
  75. if event.key() == Qt.Key_Delete:
  76. self._handle_keypress_delete(self.selectedItems())
  77. elif event.key() == Qt.Key_A:
  78. self._handle_keypress_attribute(self.selectedItems())
  79. else:
  80. pass
  81. QGraphicsScene.keyPressEvent(self, event)
  82. def draw_edge(self, from_item, to_item, is_new, edge_id=None):
  83. # type: (GraphicsNodeItem, GraphicsNodeItem, bool, str) -> None
  84. """
  85. Draw edge on screen between the node items "from_item" and "to_item".
  86. If is_new, additionally instantiate the edge in the model (if supported).
  87. """
  88. from_type = from_item.get_type()
  89. to_type = to_item.get_type()
  90. if is_new:
  91. if not commons.is_edge_supported(from_type, to_type):
  92. self._parent.plainTextEdit.appendPlainText("Error: Edge not supported between types {} and {}".format(from_type, to_type))
  93. return
  94. line = GraphicsEdgeItem(from_item, to_item, edge_id)
  95. line.setFlag(QGraphicsItem.ItemIsMovable, False)
  96. line.setFlag(QGraphicsItem.ItemIsSelectable, False)
  97. self.addItem(line)
  98. line.redraw()
  99. if is_new:
  100. edge_id = mvops.add_edge(self._cur_model, from_item.node_id, to_item.node_id)
  101. line.edge_id = edge_id
  102. self._parent.plainTextEdit.appendPlainText("Added edge between {} and {} to model".format(from_type, to_type))
  103. def _handle_keypress_delete(self, selected):
  104. if not len(selected) == 1:
  105. return
  106. item = selected[0]
  107. if isinstance(item, GraphicsNodeItem):
  108. # delete node in model (also deletes edges connected to it in model)
  109. self._parent.plainTextEdit.appendPlainText("Deleting node of type {}".format(item.get_type()))
  110. mvops.delete_node(self._cur_model, item.node_id)
  111. # in view, delete edges that were connected to this node as well
  112. # modelverse does this on its own so do not delete edges explicitly here
  113. for edge in self.items():
  114. if not isinstance(edge, GraphicsEdgeItem):
  115. continue
  116. if edge.from_item.node_id == item.node_id or edge.to_item.node_id == item.node_id:
  117. self.removeItem(edge)
  118. self.removeItem(item)
  119. if isinstance(item, GraphicsEdgeItem):
  120. self._parent.plainTextEdit.appendPlainText("Deleting edge")
  121. mvops.delete_edge(self._cur_model, item.edge_id)
  122. self.removeItem(item)
  123. def _handle_keypress_attribute(self, selected):
  124. if not len(selected) == 1:
  125. return
  126. item = selected[0]
  127. if not isinstance(item, GraphicsNodeItem):
  128. return
  129. # quickly check if attributing of such a node is allowed
  130. QApplication.setOverrideCursor(Qt.WaitCursor)
  131. self._parent.plainTextEdit.appendPlainText("Checking if attributing nodes of type {} is allowed ...".format(item.get_type()))
  132. if not mvops.is_attributing_allowed(item.get_type()):
  133. self._parent.plainTextEdit.appendPlainText("Error: Not allowed".format(item.get_type()))
  134. QApplication.restoreOverrideCursor()
  135. return
  136. QApplication.restoreOverrideCursor()
  137. self._parent.plainTextEdit.appendPlainText("Yes")
  138. # ask user for key value
  139. key, ok = QInputDialog.getText(self._parent, "New attribute", "Key value")
  140. if not ok or not key:
  141. return
  142. # check if key value already used for this node
  143. attrs = commons.get_attributes_of_node(self._cur_model, item.node_id)
  144. for attr in attrs:
  145. if attr.key == key:
  146. self._parent.plainTextEdit.appendPlainText("Error: Already such a key for the node: {}".format(key))
  147. return
  148. self._parent.add_new_attribute(key)