Salome HOME
[bos #26446] Add fix to prevent endless cycle of calling function after redisplay.
[modules/shaper.git] / src / ModuleBase / ModuleBase_Tools.cpp
index 1b5bfc9a30f6b00541daaff55073eff1d0cce123..851ae64a43ec9cf0411bcfffab04bd04239a3f45 100644 (file)
@@ -135,8 +135,13 @@ namespace ModuleBase_Tools {
 #if HAVE_SALOME
         // If the python script is being loaded now, the preferences should be used
         // to display the required object
-        SUIT_Application * app = SUIT_Session::session()->activeApplication();
-        QVariant aVar = app->property("IsLoadedScript");
+        SUIT_Session* aSession = SUIT_Session::session();
+        if (!aSession)
+          return;
+        SUIT_Application * anApp = aSession->activeApplication();
+        if (!anApp)
+          return;
+        QVariant aVar = anApp->property("IsLoadedScript");
         if (!aVar.isNull() && aVar.toBool()) {
           DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
           int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
@@ -1397,8 +1402,28 @@ qreal currentPixelRatio()
 
 // Set displaying status to every element on group
 static void setDisplayingByLoop(DocumentPtr theDoc, int theSize,
-  std::string theGroup, bool theDisplayFromScript)
+  std::string theGroup, bool theDisplayFromScript, int theDisplayingId)
 {
+  for (int anIndex = theSize - 1; anIndex >= 0; --anIndex) {
+    ObjectPtr anObject = theDoc->object(theGroup, anIndex);
+    anObject->setDisplayed((theDisplayingId == 1 && anIndex == theSize - 1) || theDisplayingId == 2);
+  }
+}
+
+void setDisplaying(ResultPartPtr thePart, bool theDisplayFromScript)
+{
+  static bool isDoingDisplay = false;
+
+  if (isDoingDisplay)
+    return;
+
+  isDoingDisplay = true;
+  DocumentPtr aDoc = thePart->partDoc();
+  int aConstructionSize = aDoc->size(ModelAPI_ResultConstruction::group());
+  int aGroupSize = aDoc->size(ModelAPI_ResultGroup::group());
+  int aFieldSize = aDoc->size(ModelAPI_ResultField::group());
+  int aResultSize = aDoc->size(ModelAPI_ResultBody::group());
+
   int aDisplayingId = -1;
   if (theDisplayFromScript) {
     aDisplayingId = ModuleBase_Preferences::resourceMgr()->integerValue("General",
@@ -1415,24 +1440,12 @@ static void setDisplayingByLoop(DocumentPtr theDoc, int theSize,
       return;
   }
 
-  for (int anIndex = theSize - 1; anIndex >= 0; --anIndex) {
-    ObjectPtr anObject = theDoc->object(theGroup, anIndex);
-    anObject->setDisplayed((aDisplayingId == 1 && anIndex == theSize - 1) || aDisplayingId == 2);
-  }
-}
-
-void setDisplaying(ResultPartPtr thePart, bool theDisplayFromScript)
-{
-  DocumentPtr aDoc = thePart->partDoc();
-  int aConstructionSize = aDoc->size(ModelAPI_ResultConstruction::group());
-  int aGroupSize = aDoc->size(ModelAPI_ResultGroup::group());
-  int aFieldSize = aDoc->size(ModelAPI_ResultField::group());
-  int aResultSize = aDoc->size(ModelAPI_ResultBody::group());
   setDisplayingByLoop(aDoc, aConstructionSize,
-    ModelAPI_ResultConstruction::group(), theDisplayFromScript);
-  setDisplayingByLoop(aDoc, aGroupSize, ModelAPI_ResultGroup::group(), theDisplayFromScript);
-  setDisplayingByLoop(aDoc, aFieldSize, ModelAPI_ResultField::group(), theDisplayFromScript);
-  setDisplayingByLoop(aDoc, aResultSize, ModelAPI_ResultBody::group(), theDisplayFromScript);
+    ModelAPI_ResultConstruction::group(), theDisplayFromScript, aDisplayingId);
+  setDisplayingByLoop(aDoc, aGroupSize, ModelAPI_ResultGroup::group(), theDisplayFromScript, aDisplayingId);
+  setDisplayingByLoop(aDoc, aFieldSize, ModelAPI_ResultField::group(), theDisplayFromScript, aDisplayingId);
+  setDisplayingByLoop(aDoc, aResultSize, ModelAPI_ResultBody::group(), theDisplayFromScript, aDisplayingId);
+  isDoingDisplay = false;
 }
 
 } // namespace ModuleBase_Tools