Salome HOME
Merge branch 'Dev_1.3.0' of newgeom:newgeom into Dev_1.3.0
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
index e94be9298c944b36d7da6b72b9acda9a8557f402..cd842eedc73b8e1bb1cf974103ca9655b7af123f 100644 (file)
 #include "PartSet_WidgetMultiSelector.h"
 #include "PartSet_WidgetEditor.h"
 #include "PartSet_WidgetFileSelector.h"
+#include "PartSet_WidgetSketchCreator.h"
 #include "PartSet_SketcherMgr.h"
 #include "PartSet_MenuMgr.h"
 
+#include "PartSet_Filters.h"
+#include "PartSet_FilterInfinite.h"
+
 #include <PartSetPlugin_Remove.h>
 #include <PartSetPlugin_Part.h>
 
@@ -28,6 +32,8 @@
 
 #include <GeomValidators_Face.h>
 #include <GeomValidators_ConstructionComposite.h>
+#include <GeomValidators_ZeroOffset.h>
+#include <GeomValidators_BooleanArguments.h>
 
 
 #include <ModelAPI_Object.h>
@@ -49,6 +55,7 @@
 #include <XGUI_ContextMenuMgr.h>
 #include <XGUI_Tools.h>
 #include <XGUI_ObjectsBrowser.h>
+#include <XGUI_SelectionMgr.h>
 
 #include <SketchPlugin_Feature.h>
 #include <SketchPlugin_Sketch.h>
@@ -88,6 +95,8 @@
 #include <GeomAlgoAPI_FaceBuilder.h>
 #include <GeomDataAPI_Dir.h>
 
+#include <SelectMgr_ListIteratorOfListOfFilter.hxx>
+
 #ifdef _DEBUG
 #include <QDebug>
 #endif
@@ -125,12 +134,39 @@ PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
 
   Events_Loop* aLoop = Events_Loop::loop();
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
+
+  mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
+  mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
 }
 
 PartSet_Module::~PartSet_Module()
 {
-  if (!myDocumentShapeFilter.IsNull())
-    myDocumentShapeFilter.Nullify();
+  SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
+  for (; aIt.More(); aIt.Next()) {
+    Handle(SelectMgr_Filter) aFilter = aIt.Value();
+    if (!aFilter.IsNull())
+      aFilter.Nullify();
+  }
+}
+
+void PartSet_Module::activateSelectionFilters()
+{
+  SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
+  for (; aIt.More(); aIt.Next()) {
+    Handle(SelectMgr_Filter) aFilter = aIt.Value();
+    if (!aFilter.IsNull())
+      myWorkshop->viewer()->addSelectionFilter(aFilter);
+  }
+}
+
+void PartSet_Module::deactivateSelectionFilters()
+{
+  SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
+  for (; aIt.More(); aIt.Next()) {
+    Handle(SelectMgr_Filter) aFilter = aIt.Value();
+    if (!aFilter.IsNull())
+      myWorkshop->viewer()->removeSelectionFilter(aFilter);
+  }
 }
 
 void PartSet_Module::registerValidators()
@@ -158,6 +194,12 @@ void PartSet_Module::registerValidators()
   aFactory->registerValidator("GeomValidators_ConstructionComposite",
                               new GeomValidators_ConstructionComposite);
 
+  aFactory->registerValidator("GeomValidators_ZeroOffset",
+                              new GeomValidators_ZeroOffset);
+
+  aFactory->registerValidator("GeomValidators_BooleanArguments",
+                              new GeomValidators_BooleanArguments);
+
   aFactory->registerValidator("PartSet_SketchEntityValidator",
                               new PartSet_SketchEntityValidator);
 
@@ -180,7 +222,7 @@ void PartSet_Module::registerProperties()
                                    Config_Prop::Integer, SKETCH_WIDTH);
 }
 
-void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation) 
+void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation) 
 {
   if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
     mySketchMgr->commitNestedSketch(theOperation);
@@ -191,9 +233,9 @@ void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
   // the selection is cleared after commit the create operation
   // in order to do not use the same selected objects in the restarted operation
   // for common behaviour, the selection is cleared even if the operation is not restarted
-  Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
-  if (!aContext.IsNull())
-    aContext->ClearSelected();
+  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
+  XGUI_Workshop* aWorkshop = aConnector->workshop();
+  aWorkshop->selector()->clearSelection();
 
   /// Restart sketcher operations automatically
   FeaturePtr aFeature = theOperation->feature();
@@ -216,22 +258,12 @@ void PartSet_Module::breakOperationSequence()
   myRestartingMode = RM_None;
 }
 
-void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
+void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
 {
   breakOperationSequence();
 }
 
-void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
-{
-  // Install the document filter before any other filter
-  if (myDocumentShapeFilter.IsNull())
-    myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
-  myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
-
-  ModuleBase_IModule::sendOperation(theOperation);
-}
-
-void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
+void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
 {
   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
     Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
@@ -243,7 +275,7 @@ void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
   }
 }
 
-void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
+void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
 {
   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
     mySketchMgr->stopSketch(theOperation);
@@ -254,7 +286,6 @@ void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
   else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
     mySketchMgr->stopNestedSketch(theOperation);
   }
-  myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
 }
 
 ModuleBase_Operation* PartSet_Module::currentOperation() const
@@ -288,12 +319,31 @@ bool PartSet_Module::canRedo() const
   return aCanRedo;
 }
 
+bool PartSet_Module::canCommitOperation() const
+{
+  return mySketchMgr->canCommitOperation();
+}
+
 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
 {
   // the sketch manager put the restriction to the objects display
   return mySketchMgr->canDisplayObject(theObject);
 }
 
+bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
+{
+  bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
+
+  ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
+  bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
+       isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
+  if (isSketchOp || isNestedOp) {
+    // in active sketch operation it is possible to activate operation object in selection
+    // in the edit operation, e.g. points of the line can be moved when the line is edited
+    aCanActivate = aCanActivate || anOperation->isEditOperation();
+  }
+  return aCanActivate;
+}
 
 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
 {
@@ -339,24 +389,6 @@ void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
         }
       }
     }
-  } else {
-    // Start editing constraint
-    if (theOperation->isEditOperation()) {
-      // TODO: #391 - to be removed
-      std::string aId = theOperation->id().toStdString();
-      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;
-          }
-        }
-      }
-    }
   }
 }
 
@@ -454,49 +486,52 @@ void PartSet_Module::onVertexSelected()
 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
                                             Config_WidgetAPI* theWidgetApi, std::string theParentId)
 {
-  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
-  XGUI_Workshop* aWorkshop = aConnector->workshop();
+  ModuleBase_IWorkshop* aWorkshop = workshop();
+  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
+  XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
   ModuleBase_ModelWidget* aWgt = NULL;
   if (theType == "sketch-start-label") {
     PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, 
       theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
-    aLabelWgt->setWorkshop(aWorkshop);
+    aLabelWgt->setWorkshop(aXUIWorkshop);
     connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
     connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
       mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
     aWgt = aLabelWgt;
   } else if (theType == "sketch-2dpoint_selector") {
-    PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
-    aPointWgt->setWorkshop(aWorkshop);
+    PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
+                                                                 theWidgetApi, theParentId);
     aPointWgt->setSketch(mySketchMgr->activeSketch());
     connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
     aWgt = aPointWgt;
   } else if (theType == "point2ddistance") {
-    PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
-    aDistanceWgt->setWorkshop(aWorkshop);
+    PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
+                                                        aWorkshop, theWidgetApi, theParentId);
     aDistanceWgt->setSketch(mySketchMgr->activeSketch());
     aWgt = aDistanceWgt;
   } else if(theType == "point2dangle") {
-    PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent, theWidgetApi, theParentId);
-    anAngleWgt->setWorkshop(aWorkshop);
+    PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
+                                                           aWorkshop, theWidgetApi, theParentId);
     anAngleWgt->setSketch(mySketchMgr->activeSketch());
     aWgt = anAngleWgt;
   } else if (theType == "sketch_shape_selector") {
     PartSet_WidgetShapeSelector* aShapeSelectorWgt =
-      new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
+      new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
     aWgt = aShapeSelectorWgt;
   } else if (theType == "sketch_multi_selector") {
     PartSet_WidgetMultiSelector* aShapeSelectorWgt =
-      new PartSet_WidgetMultiSelector(theParent, workshop(), theWidgetApi, theParentId);
+      new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
     aWgt = aShapeSelectorWgt;
   } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
-    aWgt = new PartSet_WidgetEditor(theParent, workshop(), theWidgetApi, theParentId);
+    aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
   } else if (theType == "export_file_selector") {
-    aWgt = new PartSet_WidgetFileSelector(theParent, workshop(), theWidgetApi, theParentId);
-  } 
+    aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
+  } else if (theType == "sketch_launcher") {
+    aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
+  }
   return aWgt;
 }
 
@@ -508,7 +543,7 @@ bool PartSet_Module::deleteObjects()
   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
        isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
-  if (isSketchOp && isNestedOp) {
+  if (isSketchOp || isNestedOp) {
     // 2. find selected presentations
     // selected objects should be collected before the current operation abort because
     // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
@@ -528,8 +563,12 @@ bool PartSet_Module::deleteObjects()
     QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
     for ( ; anIt != aLast; anIt++) {
       ObjectPtr anObject = *anIt;
-      if (mySketchMgr->isObjectOfSketch(anObject))
-        aSketchObjects.append(anObject);
+      if (mySketchMgr->isObjectOfSketch(anObject)) {
+        // sketch feature should be used in this list because workshop deletes features only
+        // results are skipped
+        FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
+        aSketchObjects.append(aSketchFeature);
+      }
     }
     // if the selection contains only local selected presentations from other sketches,
     // the Delete operation should not be done at all
@@ -555,6 +594,7 @@ bool PartSet_Module::deleteObjects()
     aWorkshop->displayer()->updateViewer();
     aMgr->finishOperation();
   } else {
+    bool isPartRemoved = false;
     // Delete part with help of PartSet plugin
     // TODO: the deleted objects has to be processed by multiselection
     QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
@@ -562,19 +602,51 @@ bool PartSet_Module::deleteObjects()
       ObjectPtr aObj = aObjects.first();
       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
       if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
-        std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
-        aMgr->startOperation(PartSetPlugin_Remove::ID());
-        FeaturePtr aFeature = aDoc->addFeature(PartSetPlugin_Remove::ID());
-        aFeature->execute();
-        aMgr->finishOperation();
-      } else
-        return false;
-    } else
-      return false;
+        // Remove feature should be created in the document of the part results
+        ResultPtr aPartResult = aFeature->firstResult();
+        if (aPartResult.get()) {
+          std::shared_ptr<ModelAPI_ResultPart> aPart =
+                       std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
+          DocumentPtr aPartDoc = aPart->partDoc();
+          if (aPartDoc.get()) {
+            aMgr->startOperation(PartSetPlugin_Remove::ID());
+            FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
+            aFeature->execute();
+            aMgr->finishOperation();
+            isPartRemoved = true;
+          }
+        }
+      }
+    }
+    return isPartRemoved;
   }
   return true;
 }
 
+void PartSet_Module::onFeatureTriggered()
+{
+  SessionPtr aMgr = ModelAPI_Session::get();
+  // 1. check whether the delete should be processed in the module
+  ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
+  bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
+  if (isNestedOp) {
+    // in case if in the viewer nothing is displayed, the create operation should not be
+    // comitted even if all values of the feature are initialized
+    if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
+      QAction* aCmd = dynamic_cast<QAction*>(sender());
+      //Do nothing on uncheck
+      if (aCmd->isCheckable() && !aCmd->isChecked())
+        return;
+
+      // the action information should be saved before the operation is aborted
+      // because this abort leads to update command status, which unchecks this action
+      anOperation->abort();
+
+      launchOperation(aCmd->data().toString());
+    }
+  }
+  ModuleBase_IModule::onFeatureTriggered();
+}
 
 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS) 
 {
@@ -624,7 +696,12 @@ void PartSet_Module::onViewTransformed(int theTrsfType)
     return;
   double aLen = aView->Convert(20);
 
+  double aPrevLen = SketcherPrs_Tools::getArrowSize();
   SketcherPrs_Tools::setArrowSize(aLen);
+  const double aPrevScale = aViewer->Scale(aViewer->activeView());
+  const double aCurScale = aViewer->activeView()->Camera()->Scale();
+  aViewer->SetScale(aViewer->activeView(), aCurScale);
+  SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
   bool isModified = false;
   QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
   foreach (AISObjectPtr aAIS, aPrsList) {
@@ -650,6 +727,11 @@ void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
     QPalette aPalet = aLabel->palette();
     aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
     aLabel->setPalette(aPalet);
+    aOB->treeView()->setExpandsOnDoubleClick(false);
+    connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)), 
+      SLOT(onTreeViewDoubleClick(const QModelIndex&)));
+    connect(aOB, SIGNAL(headerMouseDblClicked(const QModelIndex&)), 
+      SLOT(onTreeViewDoubleClick(const QModelIndex&)));
     connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)), 
       myDataModel, SLOT(onMouseDoubleClick(const QModelIndex&)));
   }
@@ -679,7 +761,7 @@ void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
           aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
         }
         if (aPart.get()) // this may be null is Part feature is disabled
-          aPartDoc;
+          aPartDoc = aPart->partDoc();
         if (aMgr->activeDocument() == aPartDoc)
           theMenu->addAction(myMenuMgr->action("DEACTIVATE_PART_CMD"));
         else
@@ -726,7 +808,8 @@ void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMess
         if (aPart->partDoc() == aActiveDoc) {
           QModelIndex aIndex = myDataModel->partIndex(aPart);
           if (myDataModel->activatePart(aIndex)) {
-            aTreeView->setExpanded(aOldIndex, false);
+            if (aOldIndex.isValid())
+              aTreeView->setExpanded(aOldIndex, false);
             aTreeView->setExpanded(myDataModel->activePartTree(), true);
             aPalet.setColor(QPalette::Text, Qt::black);
           }
@@ -735,5 +818,39 @@ void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMess
       }
     }
     aLabel->setPalette(aPalet);
+    aWorkshop->updateCommandStatus();
+
+    // Update displayed objects in order to update active color
+    XGUI_Displayer* aDisplayer = aWorkshop->displayer();
+    QObjectPtrList aObjects = aDisplayer->displayedObjects();
+    foreach(ObjectPtr aObj, aObjects)
+      aDisplayer->redisplay(aObj, false);
+    aDisplayer->updateViewer();
+  }
+}
+
+void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
+{
+  SessionPtr aMgr = ModelAPI_Session::get();
+  if (!theIndex.isValid()) {
+    aMgr->setActiveDocument(aMgr->moduleDocument());
+    return;
+  }
+  if (theIndex.column() != 0) // Use only first column
+    return;
+  ObjectPtr aObj = myDataModel->object(theIndex);
+  ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
+  if (!aPart.get()) { // Probably this is Feature
+    FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
+    if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
+      aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
+    }
+  }
+  if (aPart.get()) { // if this is a part
+    if (aPart->partDoc() == aMgr->activeDocument()) {
+      aMgr->setActiveDocument(aMgr->moduleDocument());
+    } else {
+      aPart->activate();
+    }
   }
 }