Salome HOME
Merge 'abn/V8_1_fix' branch into V8_1_BR.
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationManager_i.txx
index 4c66cfc719180ee00e429e0cb41b9bb7d9403de7..e4b6cf8e9b9897f70d90ed3dfbf883c5bc806610 100644 (file)
 #ifndef _MED_PRESENTATION_MANAGER_I_TXX_
 #define _MED_PRESENTATION_MANAGER_I_TXX_
 
+#include <Basics_Utils.hxx>
+
 template<typename PresentationType, typename PresentationParameters>
 MEDPresentation::TypeID
-MEDPresentationManager_i::_makePresentation(PresentationParameters params)
+MEDPresentationManager_i::_makePresentation(const PresentationParameters params, const MEDCALC::ViewModeType viewMode)
 {
+  int activeViewId = getActiveViewPythonId();
+
   // Replace = Remove then add
-  if (params.viewMode == MEDCALC::VIEW_MODE_REPLACE) {
-    MEDPresentation::TypeID currentPresentationId = _getActivePresentationId();
-    if (currentPresentationId > -1)
-      removePresentation(currentPresentationId);
+  std::vector<int> to_del;
+  if (viewMode == MEDCALC::VIEW_MODE_REPLACE)
+    {
+      // Prepare all presentations to be removed from this view:
+      std::map<MEDPresentation::TypeID, MEDPresentation*>::const_iterator it;
+      for (it = _presentations.begin(); it != _presentations.end(); ++it)
+        {
+          int viewId2 = (*it).second->getPyViewID();
+          if (viewId2 == activeViewId)
+            to_del.push_back((*it).first);
+        }
   }
 
   // Create a new presentation instance
   PresentationType* presentation = NULL;
+  MEDPresentation::TypeID newID = MEDPresentationManager_i::GenerateID();
+  STDLOG("Generated presentation ID: " << newID);
   try {
-    presentation = new PresentationType(params);  // on stack or on heap?? stack for now
+    presentation = new PresentationType(params, viewMode);  // on stack or on heap?? heap for now
+    presentation->initFieldMeshInfos();
+    // In replace or overlap mode we force the display in the active view:
+    if(activeViewId != -1 && (viewMode == MEDCALC::VIEW_MODE_REPLACE || viewMode == MEDCALC::VIEW_MODE_OVERLAP))
+      presentation->setPyViewID(activeViewId);
+    else
+      presentation->setPyViewID(newID);  // ensures a new ID for the view
   }
   catch (const std::exception& e) {
     std::cerr << e.what() << std::endl;
+    STDLOG("Error: " << e.what());
     return -1;
   }
 
-  MEDPresentation::TypeID newID = MEDPresentationManager_i::GenerateID();
   _presentations.insert( std::pair<MEDPresentation::TypeID, MEDPresentation *>(newID, presentation) );
   presentation->generatePipeline();
+
+  // If generatePipeline didn't throw, we can actually remove presentations to be deleted:
+  STDLOG("About to remove all presentations from view " << activeViewId);
+  for (std::vector<int>::const_iterator it2 = to_del.begin(); it2 != to_del.end(); ++it2)
+    removePresentation(*it2);
+
+  // Make the view holding the newly created presentation the active one:
+  activateView(newID);
   return newID;
 }
 
 template<typename PresentationType, typename PresentationParameters>
 void
-MEDPresentationManager_i::_updatePresentation(MEDPresentation::TypeID presentationID, PresentationParameters params)
+MEDPresentationManager_i::_updatePresentation(MEDPresentation::TypeID presentationID, const PresentationParameters params)
 {
   MEDPresentation* presentation = _getPresentation(presentationID);
   if (!presentation) {
@@ -60,4 +87,18 @@ MEDPresentationManager_i::_updatePresentation(MEDPresentation::TypeID presentati
   presentation->updatePipeline<PresentationType>(params);
 }
 
+template<typename PresentationType, typename PresentationParameters>
+void
+MEDPresentationManager_i::_getParameters(MEDPresentation::TypeID presentationID, PresentationParameters & params) const
+{
+  MEDPresentation* presentation = _getPresentation(presentationID);
+  if (!presentation) {
+    std::cerr << "_getParameters(): presentation not found!!" << std::endl;
+    return;
+  }
+
+  presentation->getParameters<PresentationType>(params);
+}
+
+
 #endif // _MED_PRESENTATION_MANAGER_I_TXX_