Salome HOME
Avoid crash on clearing of a document by a script
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
index 6d3684ec28fc9cbaf9be7c2d3ab156db6ae43360..aa7b4e383f47e0d4298d138e4c26fb6407b0a877 100755 (executable)
@@ -38,6 +38,7 @@
 #include "PartSet_CustomPrs.h"
 #include "PartSet_IconFactory.h"
 #include "PartSet_OverconstraintListener.h"
+#include "PartSet_TreeNodes.h"
 
 #include "PartSet_Filters.h"
 #include "PartSet_FilterInfinite.h"
@@ -46,6 +47,8 @@
 #include <PartSetPlugin_Part.h>
 #include <PartSetPlugin_Duplicate.h>
 
+#include <SketchPlugin_ConstraintCoincidence.h>
+
 #include <ModuleBase_Operation.h>
 #include <ModuleBase_OperationAction.h>
 #include <ModuleBase_IViewer.h>
@@ -71,6 +74,7 @@
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_Tools.h>
 #include <ModelAPI_ResultConstruction.h>
+#include <ModelAPI_AttributeIntArray.h>
 
 #include <GeomDataAPI_Point2D.h>
 #include <GeomDataAPI_Point.h>
 #include <SketchPlugin_Sketch.h>
 
 #include <SketcherPrs_SymbolPrs.h>
+#include <SketcherPrs_PositionMgr.h>
 #include <SketcherPrs_Coincident.h>
 #include <SketcherPrs_Tools.h>
 
@@ -148,6 +153,7 @@ extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop*
 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
 : ModuleBase_IModule(theWshop),
   myVisualLayerId(0),
+  myRoot(0),
   myIsOperationIsLaunched(false)
 {
   new PartSet_IconFactory();
@@ -201,8 +207,25 @@ PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
 
   Config_PropManager::registerProp("Visualization", "hidden_face_transparency",
                                    "Hidden faces transparency",
-                                   Config_Prop::Double,
+                                   Config_Prop::DblSpin,
                                    "0.8");
+  std::ostringstream aStream;
+  aStream << SketcherPrs_Tools::getDefaultArrowSize();
+  Config_PropManager::registerProp("Visualization", "dimension_arrow_size",
+    "Dimension arrow size", Config_Prop::IntSpin, aStream.str());
+
+  Config_PropManager::registerProp("Visualization", "dimension_font", "Dimension font",
+    Config_Prop::String, "Times-bold");
+
+  aStream.str("");
+  aStream.clear();
+  aStream << SketcherPrs_Tools::getDefaultTextHeight();
+  Config_PropManager::registerProp("Visualization", "dimension_value_size",
+    "Dimension value size", Config_Prop::IntSpin, aStream.str());
+
+  Config_PropManager::registerProp("Visualization", "sketch_dimension_color",
+    "Dimension color",
+    Config_Prop::Color, SKETCH_DIMENSION_COLOR);
 }
 
 //******************************************************
@@ -217,8 +240,18 @@ PartSet_Module::~PartSet_Module()
   }
   delete myCustomPrs;
   delete myOverconstraintListener;
+  delete myRoot;
 }
 
+//******************************************************
+void PartSet_Module::createFeatures()
+{
+  ModuleBase_IModule::createFeatures();
+  myRoot = new PartSet_RootNode();
+  myRoot->setWorkshop(workshop());
+}
+
+
 //******************************************************
 void PartSet_Module::storeSelection()
 {
@@ -264,6 +297,7 @@ void PartSet_Module::registerValidators()
     new PartSet_MultyTranslationSelection);
   aFactory->registerValidator("PartSet_SplitSelection", new PartSet_SplitSelection);
   aFactory->registerValidator("PartSet_ProjectionSelection", new PartSet_ProjectionSelection);
+  aFactory->registerValidator("PartSet_IntersectionSelection", new PartSet_IntersectionSelection);
 }
 
 //******************************************************
@@ -621,7 +655,7 @@ void PartSet_Module::moduleSelectionModes(int theModesType, QIntList& theModes)
 void PartSet_Module::moduleSelectionFilters(const QIntList& theFilterTypes,
                                             SelectMgr_ListOfFilter& theSelectionFilters)
 {
-  bool isSketchActive = mySketchMgr->activeSketch();
+  bool isSketchActive = mySketchMgr->activeSketch().get();
 
   std::map<XGUI_SelectionFilterType, Handle(SelectMgr_Filter)>::const_iterator aFiltersIt =
     mySelectionFilters.begin();
@@ -721,6 +755,7 @@ bool PartSet_Module::isSketchNeutralPointActivated() const
 void PartSet_Module::closeDocument()
 {
   myActivePartIndex = QModelIndex();
+  SketcherPrs_PositionMgr::get()->clearAll();
 }
 
 //******************************************************
@@ -906,15 +941,15 @@ ModuleBase_ModelWidget* PartSet_Module::activeWidget() const
 {
   ModuleBase_ModelWidget* anActiveWidget = 0;
 
-  anActiveWidget = mySketchReentrantMgr->internalActiveWidget();
-  if (!anActiveWidget) {
-    ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
-    if (aOperation) {
-      ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
-      anActiveWidget = aPanel ? aPanel->activeWidget() : 0;
-    }
-  }
-  return anActiveWidget;
+  ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
+  if (!aOperation)
+    return anActiveWidget;
+
+  ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
+  if (!aPanel)
+    return anActiveWidget;
+
+  return aPanel->activeWidget(true);
 }
 
 //******************************************************
@@ -1045,22 +1080,23 @@ void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
 {
   Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
   if (!anAIS.IsNull()) {
-    bool aToUseZLayer = false;
     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
-    if (aFeature.get() && PartSet_Tools::findRefsToMeFeature(aFeature,
-                                                        SketchPlugin_Projection::ID()))
-      aToUseZLayer = true;
-    Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
-    Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
-    if (!aDim.IsNull()) {
-      aToUseZLayer = true;
-    } else {
-      Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
-      if (!aCons.IsNull())
-      aToUseZLayer = true;
+    if (aFeature.get()) {
+      bool aToUseZLayer = false;
+      if (PartSet_Tools::findRefsToMeFeature(aFeature,SketchPlugin_Projection::ID()))
+        aToUseZLayer = true;
+      else {
+        CompositeFeaturePtr aParent = ModelAPI_Tools::compositeOwner(aFeature);
+        aToUseZLayer = (aParent.get() && (aParent->getKind() == SketchPlugin_Sketch::ID()));
+      }
+      if (aToUseZLayer) {
+        Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
+        if (aFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID())
+          aCtx->SetZLayer(anAIS, Graphic3d_ZLayerId_Top);
+        else
+          aCtx->SetZLayer(anAIS, myVisualLayerId);
+      }
     }
-    if (aToUseZLayer)
-      aCtx->SetZLayer(anAIS, myVisualLayerId);
   }
 }
 
@@ -1106,10 +1142,12 @@ void PartSet_Module::onViewTransformed(int theTrsfType)
   if (aView.IsNull())
     return;
 
+  bool isModified = false;
   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
   if (aCurrentOperation &&
     (PartSet_SketcherMgr::isSketchOperation(aCurrentOperation) ||
-     sketchMgr()->isNestedSketchOperation(aCurrentOperation)))
+     sketchMgr()->isNestedSketchOperation(aCurrentOperation) ||
+     (aCurrentOperation->id() == "Measurement")))
   {
     double aLen = aView->Convert(SketcherPrs_Tools::getConfigArrowSize());
 
@@ -1117,11 +1155,14 @@ void PartSet_Module::onViewTransformed(int theTrsfType)
     SketcherPrs_Tools::setArrowSize(aLen);
     const double aCurScale = aViewer->activeView()->Camera()->Scale();
     aViewer->SetScale(aViewer->activeView(), aCurScale);
-    bool isModified = false;
+#ifdef OPTIMIZE_PRS
+    QList<Handle(AIS_InteractiveObject)> aPrsList = aDisplayer->displayedPresentations();
+    foreach(Handle(AIS_InteractiveObject) aAisObj, aPrsList) {
+#else
     QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
-    foreach (AISObjectPtr aAIS, aPrsList) {
+    foreach(AISObjectPtr aAIS, aPrsList) {
       Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
-
+#endif
       Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
       if (!aDim.IsNull()) {
         aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
@@ -1132,6 +1173,7 @@ void PartSet_Module::onViewTransformed(int theTrsfType)
     if (isModified)
       aDisplayer->updateViewer();
   }
+
 }
 
 //******************************************************
@@ -1400,10 +1442,15 @@ void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMess
 
     SessionPtr aMgr = ModelAPI_Session::get();
     DocumentPtr aActiveDoc = aMgr->activeDocument();
-    if (myActivePartIndex.isValid())
-      aTreeView->setExpanded(myActivePartIndex, false);
+
+    // Clear active part index if there is no Part documents
+    // It could be not null if document was closed and opened a new
+    // without closeDocument call
+    if (aMgr->allOpenedDocuments().size() <= 1)
+      myActivePartIndex = QModelIndex();
 
     XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
+    QModelIndex aOldActive = myActivePartIndex;
     myActivePartIndex = aDataModel->documentRootIndex(aActiveDoc, 0);
     bool needUpdate = false;
     if (myActivePartIndex.isValid()) {
@@ -1411,6 +1458,8 @@ void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMess
       if (!needUpdate)
         aTreeView->setExpanded(myActivePartIndex, true);
     }
+    if ((aOldActive != myActivePartIndex) && (aOldActive.isValid()))
+      aTreeView->setExpanded(aOldActive, false);
 
     aLabel->setPalette(aPalet);
     aWorkshop->updateCommandStatus();
@@ -1628,10 +1677,15 @@ XGUI_Workshop* PartSet_Module::getWorkshop() const
   return aConnector->workshop();
 }
 
-//******************************************************
 void PartSet_Module::setDefaultConstraintShown()
 {
   myHasConstraintShown[PartSet_Tools::Geometrical] = true;
   myHasConstraintShown[PartSet_Tools::Dimensional] = true;
   myHasConstraintShown[PartSet_Tools::Expressions] = false;
 }
+
+//******************************************************
+ModuleBase_ITreeNode* PartSet_Module::rootNode() const
+{
+  return myRoot;
+}