Browse Source

allow to overwrite type, enables concrete syntax evolution

Lucas Heer 7 years ago
parent
commit
692c7d0043
2 changed files with 39 additions and 18 deletions
  1. 31 15
      sketchUI/exm_scene.py
  2. 8 3
      sketchUI/mvops.py

+ 31 - 15
sketchUI/exm_scene.py

@@ -1,6 +1,6 @@
 from enum import Enum
 from enum import Enum
 from PyQt5.QtWidgets import QGraphicsScene, QGraphicsItem, QGraphicsLineItem, QGraphicsRectItem, \
 from PyQt5.QtWidgets import QGraphicsScene, QGraphicsItem, QGraphicsLineItem, QGraphicsRectItem, \
-    QGraphicsEllipseItem, QInputDialog, QTableWidgetItem
+    QGraphicsEllipseItem, QInputDialog, QTableWidgetItem, QMessageBox
 from PyQt5.Qt import Qt, QPointF, QPen, QTransform, QApplication
 from PyQt5.Qt import Qt, QPointF, QPen, QTransform, QApplication
 from sketchUI.graphics_edge_item import GraphicsEdgeItem
 from sketchUI.graphics_edge_item import GraphicsEdgeItem
 from sketchUI.graphics_node_item import GraphicsNodeItem, IconType
 from sketchUI.graphics_node_item import GraphicsNodeItem, IconType
@@ -283,9 +283,20 @@ class SketchScene(QGraphicsScene):
             # user canceled or empty type string
             # user canceled or empty type string
             return
             return
 
 
+        reload = False
         if node_type in commons.get_available_types():
         if node_type in commons.get_available_types():
-            self._parent.plainTextEdit.appendPlainText("Error: Already such a type: {}".format(node_type))
-            return
+            # There is already such a type registered. Overwrite its conrete syntax?
+            box = QMessageBox()
+            box.setText("Type {} already exists".format(node_type))
+            box.setInformativeText("Do you want to overwrite it?")
+            box.setStandardButtons(QMessageBox.No | QMessageBox.Yes)
+            box.setDefaultButton(QMessageBox.No)
+            ret = box.exec_()
+            if ret == QMessageBox.Yes:
+                reload = True
+                pass
+            else:
+                return
 
 
         # perform add local or global?
         # perform add local or global?
         scope, ok = QInputDialog.getItem(self._parent, "Select scope", "Scope", ["Local", "Global"])
         scope, ok = QInputDialog.getItem(self._parent, "Select scope", "Scope", ["Local", "Global"])
@@ -306,7 +317,7 @@ class SketchScene(QGraphicsScene):
         self._parent.plainTextEdit.appendPlainText("Capturing concrete syntax of group ...")
         self._parent.plainTextEdit.appendPlainText("Capturing concrete syntax of group ...")
         self._parent.plainTextEdit.repaint()
         self._parent.plainTextEdit.repaint()
         # create concrete syntax model for the sketched elements
         # create concrete syntax model for the sketched elements
-        csm = mvops.new_concrete_syntax_model(node_type, IconType.PRIMITIVE)
+        csm = mvops.new_concrete_syntax_model(node_type, IconType.PRIMITIVE, overwrite=True)
         if not csm:
         if not csm:
             self._parent.plainTextEdit.appendPlainText("Error: Concrete syntax for type {} already exists".format(node_type))
             self._parent.plainTextEdit.appendPlainText("Error: Concrete syntax for type {} already exists".format(node_type))
             return
             return
@@ -353,17 +364,22 @@ class SketchScene(QGraphicsScene):
             else:
             else:
                 print("Dont know how to capture CS of item {}".format(item))
                 print("Dont know how to capture CS of item {}".format(item))
 
 
-        # update view: replace group by actual node item with newly populated CS
-        csm_content = mvops.get_consyn_of(node_type)
-        nodeitem = GraphicsNodeItem(nodeid, node_type, csm_content)
-        nodeitem.setPos(group.scenePos())
-        nodeitem.setFlag(QGraphicsItem.ItemIsSelectable, True)
-        nodeitem.setFlag(QGraphicsItem.ItemIsMovable, True)
-        self.removeItem(group)
-        self.addItem(nodeitem)
-        self._parent.populate_types()
-        QApplication.restoreOverrideCursor()
-        self._parent.plainTextEdit.appendPlainText("OK")
+        if reload:
+            # reload whole scene because concrete syntax of a type has changed
+            self._parent._load_model()
+            QApplication.restoreOverrideCursor()
+        else:
+            # update view: replace group by actual node item with newly populated CS
+            csm_content = mvops.get_consyn_of(node_type)
+            nodeitem = GraphicsNodeItem(nodeid, node_type, csm_content)
+            nodeitem.setPos(group.scenePos())
+            nodeitem.setFlag(QGraphicsItem.ItemIsSelectable, True)
+            nodeitem.setFlag(QGraphicsItem.ItemIsMovable, True)
+            self.removeItem(group)
+            self.addItem(nodeitem)
+            self._parent.populate_types()
+            QApplication.restoreOverrideCursor()
+            self._parent.plainTextEdit.appendPlainText("OK")
 
 
     def _handle_keypress_delete(self, selected):
     def _handle_keypress_delete(self, selected):
         if len(selected) == 1 and isinstance(selected[0], GraphicsEdgeItem):
         if len(selected) == 1 and isinstance(selected[0], GraphicsEdgeItem):

+ 8 - 3
sketchUI/mvops.py

@@ -39,17 +39,22 @@ def get_consyn_of(node_type):
             return mv.element_list_nice(csm)
             return mv.element_list_nice(csm)
     return []
     return []
 
 
-def new_concrete_syntax_model(type_id, icon_type):
-    # type: (str, IconType) -> str
+def new_concrete_syntax_model(type_id, icon_type, overwrite=False):
+    # type: (str, IconType, bool) -> str
     """
     """
     Add a new concrete syntax model for type "type_id" and representation type "icon_type" to the modelverse.
     Add a new concrete syntax model for type "type_id" and representation type "icon_type" to the modelverse.
+    If overwrite, an already existing CS model for the same type is deleted.
     Returns the path of the newly created model.
     Returns the path of the newly created model.
     """
     """
     csm = "models/consyn/" + type_id
     csm = "models/consyn/" + type_id
     try:
     try:
         mv.model_add(csm, "formalisms/consynMM")
         mv.model_add(csm, "formalisms/consynMM")
     except mv.ModelExists:
     except mv.ModelExists:
-        return ""
+        if not overwrite:
+            return ""
+        else:
+            mv.model_delete(csm)
+            mv.model_add(csm, "formalisms/consynMM")
 
 
     icon = mv.instantiate(csm, "Icon")
     icon = mv.instantiate(csm, "Icon")
     mv.attr_assign(csm, icon, "typeID", type_id)
     mv.attr_assign(csm, icon, "typeID", type_id)