Salome HOME
updated copyright message
[modules/shaper.git] / src / ModuleBase / ModuleBase_Tools.cpp
index b014d60deff7189df0f53bbd2d824ffcc4dd917c..da130c0ac9a06b74b275ef34a64404568c5811c7 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2021  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -21,6 +21,7 @@
 
 #include <ModuleBase_ParamIntSpinBox.h>
 #include <ModuleBase_ParamSpinBox.h>
+#include <ModuleBase_Preferences.h>
 #include <ModuleBase_WidgetFactory.h>
 #include <ModuleBase_IWorkshop.h>
 #include <ModuleBase_IModule.h>
@@ -36,6 +37,7 @@
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_AttributeRefAttrList.h>
+#include <ModelAPI_ResultGroup.h>
 #include <ModelAPI_ResultPart.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_Folder.h>
 
 #include <ModelGeomAlgo_Point2D.h>
+#include <SUIT_ResourceMgr.h>
+
+#include <Basics_OCCTVersion.hxx>
+
+#ifdef HAVE_SALOME
+#include <SUIT_Application.h>
+#include <SUIT_Session.h>
+#endif
 
 #include <StdSelect_BRepOwner.hxx>
 #include <TopoDS_Iterator.hxx>
@@ -84,6 +94,7 @@
 #include <QTextCodec>
 #include <QWindow>
 #include <QScreen>
+#include <QCheckBox>
 
 #include <sstream>
 #include <string>
@@ -108,6 +119,56 @@ namespace ModuleBase_Tools {
 
 //******************************************************************
 
+  //! Waits for REDISPLAY message and set the Visible flag to the entities
+  //! according to Preferences choice.
+  class ModuleBase_RedisplayListener : public Events_Listener
+  {
+  public:
+    static std::shared_ptr<ModuleBase_RedisplayListener> instance()
+    {
+      static std::shared_ptr<ModuleBase_RedisplayListener>
+          anInstance(new ModuleBase_RedisplayListener);
+      return anInstance;
+    }
+
+    void processEvent(const std::shared_ptr<Events_Message>& theMessage)
+    {
+      if (theMessage->eventID() == Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY))
+      {
+#if HAVE_SALOME
+        // If the python script is being loaded now, the preferences should be used
+        // to display the required object
+        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());
+          if (aSize > 0) {
+            ObjectPtr anPartObject = aRootDoc->object(ModelAPI_ResultPart::group(), aSize - 1);
+            ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anPartObject);
+            ModuleBase_Tools::setDisplaying(aPart, true);
+          }
+        }
+#endif
+      }
+    }
+
+  private:
+    ModuleBase_RedisplayListener()
+    {
+      Events_Loop::loop()->registerListener(this,
+          Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
+    }
+  };
+
+  static std::shared_ptr<ModuleBase_RedisplayListener>
+      RL = ModuleBase_RedisplayListener::instance();
+
 //******************************************************************
 
 void adjustMargins(QWidget* theWidget)
@@ -387,7 +448,7 @@ int shapeType(const QString& theType)
 
 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
                   bool& hasParameter, bool& hasCompositeOwner, bool& hasResultInHistory,
-                  bool& hasFolder)
+                  bool& hasFolder, bool &hasGroupsOnly)
 {
   hasResult = false;
   hasFeature = false;
@@ -395,9 +456,11 @@ void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFe
   hasCompositeOwner = false;
   hasResultInHistory = false;
   hasFolder = false;
+  bool hasNonGroup = false;
   foreach(ObjectPtr aObj, theObjects) {
     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
+    ResultGroupPtr aGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(aObj);
     FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aObj);
     ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
     FieldStepPtr aStep = std::dynamic_pointer_cast<ModelAPI_ResultField::ModelAPI_FieldStep>(aObj);
@@ -406,6 +469,7 @@ void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFe
     hasFeature |= (aFeature.get() != NULL);
     hasFolder |= (aFolder.get() != NULL);
     hasParameter |= (aConstruction.get() != NULL);
+    hasNonGroup |= (aGroup.get() == NULL);
     if (hasFeature)
       hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
     else if (aResult.get())
@@ -416,41 +480,12 @@ void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFe
       hasResultInHistory = aFeature.get() && aFeature->isInHistory();
     }
 
-    if (hasFeature && hasResult  && hasParameter && hasCompositeOwner)
+    if (hasFeature && hasResult && hasParameter && hasCompositeOwner && hasNonGroup)
       break;
   }
+  hasGroupsOnly = !hasNonGroup;
 }
 
-/*bool setDefaultDeviationCoefficient(std::shared_ptr<GeomAPI_Shape> theGeomShape)
-{
-  if (!theGeomShape.get())
-    return false;
-  // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
-  // correction of deviation for them should not influence to the application performance
-  GeomAPI_ShapeExplorer anExp(theGeomShape, GeomAPI_Shape::FACE);
-  bool anEmpty = anExp.empty();
-  return !anExp.more();
-}*/
-
-/*void setDefaultDeviationCoefficient(const std::shared_ptr<ModelAPI_Result>& theResult,
-                                    const Handle(Prs3d_Drawer)& theDrawer)
-{
-  if (!theResult.get())
-    return;
-  bool aUseDeviation = false;
-
-  std::string aResultGroup = theResult->groupName();
-  if (aResultGroup == ModelAPI_ResultConstruction::group())
-    aUseDeviation = true;
-  else if (aResultGroup == ModelAPI_ResultBody::group()) {
-    GeomShapePtr aGeomShape = theResult->shape();
-    if (aGeomShape.get())
-      aUseDeviation = setDefaultDeviationCoefficient(aGeomShape);
-  }
-  if (aUseDeviation)
-    theDrawer->SetDeviationCoefficient(DEFAULT_DEVIATION_COEFFICIENT);
-}
-*/
 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
                                     const Handle(Prs3d_Drawer)& theDrawer)
 {
@@ -1045,10 +1080,7 @@ bool askToDelete(const std::set<FeaturePtr> theFeatures,
     aText += QString(QObject::tr(aMsg));
     aDetailedText += QString(QObject::tr(aMsgDetails))
                      .arg(aParamFeatureNames.join(aSep));
-#ifdef _DEBUG
-    QPushButton *aReplaceButton =
-#endif
-      aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole);
+    aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole);
   }
 
   if (!aText.isEmpty()) {
@@ -1073,6 +1105,33 @@ bool askToDelete(const std::set<FeaturePtr> theFeatures,
   return true;
 }
 
+//**************************************************************
+bool warningAboutConflict(QWidget* theParent, const std::string& theWarningText)
+{
+  QMessageBox aMessageBox(theParent);
+  aMessageBox.setWindowTitle(QObject::tr("Conflicts in constraint"));
+  aMessageBox.setIcon(QMessageBox::Warning);
+  aMessageBox.setText((theWarningText + "\nConstraints will be removed or substituted").c_str());
+
+  QCheckBox* aCheckBox = new QCheckBox;
+
+  aCheckBox->setTristate(false);
+  aCheckBox->setText("switch off the notifications.");
+
+  aMessageBox.setCheckBox(aCheckBox);
+  aMessageBox.setStandardButtons(QMessageBox::Ok);
+
+  aMessageBox.exec();
+
+  if (aCheckBox->isChecked())
+  {
+    ModuleBase_Preferences::resourceMgr()->setValue(SKETCH_TAB_NAME,
+                                                    "notify_change_constraint", false);
+  }
+
+  return true;
+}
+
 //**************************************************************
 void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
 {
@@ -1178,11 +1237,19 @@ void setPointBallHighlighting(AIS_InteractiveObject* theAIS)
       anAspect = aPntAspect->Aspect();
       anAspect->SetType(Aspect_TOM_BALL);
     } else {
+#if OCC_VERSION_LARGE < 0x07070000
       if(aPixMap->Format() == Image_PixMap::ImgGray) {
         aPixMap->SetFormat (Image_PixMap::ImgAlpha);
       } else if(aPixMap->Format() == Image_PixMap::ImgGrayF) {
         aPixMap->SetFormat (Image_PixMap::ImgAlphaF);
       }
+#else
+      if(aPixMap->Format() == Image_Format_Gray) {
+        aPixMap->SetFormat (Image_Format_Alpha);
+      } else if(aPixMap->Format() == Image_Format_GrayF) {
+        aPixMap->SetFormat (Image_Format_AlphaF);
+      }
+#endif
       anAspect = new Graphic3d_AspectMarker3d(aPixMap);
       aPntAspect->SetAspect(anAspect);
     }
@@ -1342,6 +1409,58 @@ qreal currentPixelRatio()
 }
 
 
+// Set displaying status to every element on group
+static void setDisplayingByLoop(DocumentPtr theDoc, int theSize,
+  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",
+      "part_visualization_script", -1);
+    // Increase ID to prevert using "As stored in HDF"
+    ++aDisplayingId;
+  }
+  else {
+    aDisplayingId = ModuleBase_Preferences::resourceMgr()->integerValue("General",
+      "part_visualization_study", -1);
+
+    // if chosen "As stored in HDF" then don't change displaying
+    if (aDisplayingId == 0)
+      return;
+  }
+
+  setDisplayingByLoop(aDoc, aConstructionSize, 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