Salome HOME
Issue #394 Undo-ing a Sketch element
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
index 7fe2956f1a9d516ff55781c83c23dde2b344fd96..e1e18df38ca4d451e369bf84579e76e4278d637e 100644 (file)
@@ -26,6 +26,7 @@
 #include <ModelAPI_Validator.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Session.h>
+#include <ModelAPI_ShapeValidator.h>
 
 #include <GeomDataAPI_Point2D.h>
 #include <GeomDataAPI_Point.h>
@@ -51,6 +52,7 @@
 //#include <SketchPlugin_ConstraintRigid.h>
 
 #include <Events_Loop.h>
+#include <Config_PropManager.h>
 
 #include <StdSelect_TypeOfFace.hxx>
 #include <TopoDS_Vertex.hxx>
@@ -119,6 +121,7 @@ void PartSet_Module::registerValidators()
   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
   aFactory->registerValidator("PartSet_RigidValidator", new PartSet_RigidValidator);
   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
+  aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
   aFactory->registerValidator("PartSet_SketchValidator", new PartSet_SketchValidator);
 }
 
@@ -136,6 +139,14 @@ void PartSet_Module::registerFilters()
             new ModuleBase_FilterCustom(aSelectFilter));
 }
 
+void PartSet_Module::registerProperties()
+{
+  Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
+                                   PLANE_SIZE);
+  Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
+                                   Config_Prop::Integer, SKETCH_WIDTH);
+}
+
 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation) 
 {
   if (theOperation->isEditOperation())
@@ -175,9 +186,13 @@ void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
 
 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
 {
-  if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
+  if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
     mySketchMgr->startSketch(theOperation);
   }
+  else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
+    mySketchMgr->startNestedSketch(theOperation);
+  }
+
   if (myDocumentShapeFilter.IsNull())
     myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
   myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
@@ -185,24 +200,43 @@ void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
 
 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
 {
-  if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
+  if (PartSet_SketcherMgr::isSketchOperation(theOperation) ||
+      PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
     mySketchMgr->stopSketch(theOperation);
   }
+  else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
+    mySketchMgr->stopNestedSketch(theOperation);
+  }
   myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
 }
 
 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
 {
   bool aCanDisplay = false;
-  if (mySketchMgr->activeSketch()) {
+  if (!mySketchMgr->canDisplayObject(theObject))
+    return aCanDisplay;
+  CompositeFeaturePtr aSketchFeature = mySketchMgr->activeSketch();
+  if (aSketchFeature.get() != NULL) {
     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
 
-    if (aFeature.get() != NULL) {
-      if (aFeature == mySketchMgr->activeSketch()) {
+    // MPV: the second and third conditions to avoid crash on exit for application
+    if (aFeature.get() != NULL && aFeature->data().get() && aFeature->data()->isValid()) {
+      if (aFeature == aSketchFeature) {
         aCanDisplay = false;
       }
-      else {
-        aCanDisplay = mySketchMgr->sketchOperationIdList().contains(aFeature->getKind().c_str());
+      else if (aSketchFeature.get() && aSketchFeature->data().get() &&
+               aSketchFeature->data()->isValid()) {
+        for (int i = 0; i < aSketchFeature->numberOfSubs() && !aCanDisplay; i++) {
+          FeaturePtr aSubFeature = aSketchFeature->subFeature(i);
+          std::list<ResultPtr> aResults = aSubFeature->results();
+          std::list<ResultPtr>::const_iterator aIt;
+          for (aIt = aResults.begin(); aIt != aResults.end() && !aCanDisplay; ++aIt) {
+            if (theObject == (*aIt))
+              aCanDisplay = true;
+          }
+          if (aSubFeature == theObject)
+            aCanDisplay = true;
+        }
       }
     }
   }
@@ -214,7 +248,8 @@ bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
 
 void PartSet_Module::addViewerItems(QMenu* theMenu) const
 {
-  if (!isSketchOperationActive() && !isSketchFeatureOperationActive())
+  if (!PartSet_SketcherMgr::isSketchOperation(myWorkshop->currentOperation()) &&
+      !isSketchFeatureOperationActive())
     return;
   ModuleBase_ISelection* aSelection = myWorkshop->selection();
   QObjectPtrList aObjects = aSelection->selectedPresentations();
@@ -235,8 +270,7 @@ void PartSet_Module::addViewerItems(QMenu* theMenu) const
 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
 {
   ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
-  if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) && 
-    (theOperation->isEditOperation())) {
+  if (PartSet_SketcherMgr::isSketchOperation(theOperation) &&  (theOperation->isEditOperation())) {
     // we have to manually activate the sketch label in edit mode
       aPanel->activateWidget(aPanel->modelWidgets().first());
       return;
@@ -265,20 +299,17 @@ void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
     if (theOperation->isEditOperation()) {
       // TODO: #391 - to be removed
       std::string aId = theOperation->id().toStdString();
-      if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
-        if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
-            (aId == SketchPlugin_ConstraintLength::ID()) || 
-            (aId == SketchPlugin_ConstraintDistance::ID())) {
-          // Find and activate widget for management of point for dimension line position
-          QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
-          foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
-            PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
-            if (aPntWgt) {
-              aPanel->activateWidget(aPntWgt);
-              return;
-            }
+      if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation) &&
+          PartSet_SketcherMgr::isDistanceOperation(theOperation)) {
+        // Find and activate widget for management of point for dimension line position
+        QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
+        foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
+          PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
+          if (aPntWgt) {
+            aPanel->activateWidget(aPntWgt);
+            return;
           }
-        } 
+        }
       }
     }
   }
@@ -295,7 +326,7 @@ void PartSet_Module::onSelectionChanged()
   // An edit operation is enable only if the current opeation is the sketch operation
   if (mySketchMgr->activeSketch()) {
     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
-      isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
+      isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
   }
   if (isSketcherOp) {
     // Editing of constraints can be done on selection
@@ -418,14 +449,6 @@ QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget*
     return 0;
 }
 
-bool PartSet_Module::isSketchOperationActive() const
-{
-  ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
-
-  bool isSketchOp = aOperation && aOperation->id().toStdString() == SketchPlugin_Sketch::ID();
-  return isSketchOp;
-}
-
 bool PartSet_Module::isSketchFeatureOperationActive() const
 {
   bool isCurrentSketchOp = false;
@@ -473,7 +496,7 @@ void PartSet_Module::onAction(bool isChecked)
 
 void PartSet_Module::deleteObjects()
 {
-  bool isSketchOp = isSketchOperationActive();
+  bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(myWorkshop->currentOperation());
   if (!isSketchOp && !isSketchFeatureOperationActive())
     return;