Salome HOME
Fix for the bug #255: VTK viewer is not updated after modification of objects.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DeleteOp.cxx
index 83372230525666d4519d3f2fb188997e1ff0568a..da2cd3739a300c4b2c0dc1001bc9544ca57c5c91 100644 (file)
 
 #include "HYDROGUI_DeleteOp.h"
 
-#include "HYDROGUI_DataModel.h"
 #include "HYDROGUI_Module.h"
-#include "HYDROGUI_Operations.h"
+#include "HYDROGUI_Tool.h"
 #include "HYDROGUI_UpdateFlags.h"
 
-#include <HYDROData_Iterator.h>
 #include <HYDROData_Object.h>
 
 #include <LightApp_Application.h>
-#include <LightApp_DataOwner.h>
 
 #include <SUIT_Desktop.h>
 #include <SUIT_MessageBox.h>
-#include <SUIT_SelectionMgr.h>
 
 HYDROGUI_DeleteOp::HYDROGUI_DeleteOp( HYDROGUI_Module* theModule )
 : HYDROGUI_Operation( theModule )
@@ -51,36 +47,77 @@ void HYDROGUI_DeleteOp::startOperation()
 {
   HYDROGUI_Operation::startOperation();
 
-  HYDROGUI_DataModel* aModel = module()->getDataModel();
+  HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
+  if( aSeq.IsEmpty() )
+  {
+    abort();
+    return;
+  }
 
-  SUIT_SelectionMgr* aSelectionMgr = selectionMgr();
-  SUIT_DataOwnerPtrList anOwners;
-  aSelectionMgr->selected( anOwners );
+  bool anIsCanRemove = true;
 
-  if( !anOwners.isEmpty() )
+  QStringList anObjNames;
+  for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
   {
-    int anAnswer = SUIT_MessageBox::question( module()->getApp()->desktop(),
-                                              tr( "DELETE_OBJECTS" ),
-                                              tr( "CONFIRM_DELETION" ),
-                                              QMessageBox::Yes | QMessageBox::No,
-                                              QMessageBox::No );
-    if( anAnswer == QMessageBox::No )
+    Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
+    if( anObject.IsNull() )
+      continue;
+
+    if ( !anObject->CanRemove() )
     {
-      abort();
-      return;
+      anIsCanRemove = false;
+      break;
     }
+
+    anObjNames.append( anObject->GetName() );
   }
 
-  foreach( SUIT_DataOwner* aSUITOwner, anOwners )
+  if ( !anIsCanRemove )
   {
-    if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
-    {
-      Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
-      if( !anObject.IsNull() )
-        anObject->Remove();
-    }
+    SUIT_MessageBox::critical( module()->getApp()->desktop(),
+                               tr( "DELETE_OBJECTS" ), tr( "DELETE_OBJECTS_IMPOSIBLE" ) );
+    abort();
+    return;
   }
 
-  module()->update( UF_Model | UF_Viewer );
+  anObjNames.removeDuplicates();
+  anObjNames.sort();
+
+  // check the back-references
+  QMap<QString,HYDROData_SequenceOfObjects> aBackObjects =
+    HYDROGUI_Tool::GetObjectsBackReferences( module(), anObjNames );
+
+  if ( !aBackObjects.isEmpty() )
+  {
+    SUIT_MessageBox::critical( module()->getApp()->desktop(),
+                               tr( "DELETE_OBJECTS" ), tr( "DELETED_OBJECTS_HAS_BACKREFERENCES" ) );
+    abort();
+    return;
+  }
+
+  QString aNamesList = anObjNames.join( "\n" );
+  int anAnswer = SUIT_MessageBox::question( module()->getApp()->desktop(),
+                                            tr( "DELETE_OBJECTS" ),
+                                            tr( "CONFIRM_DELETION" ).arg( aNamesList ),
+                                            QMessageBox::Yes | QMessageBox::No,
+                                            QMessageBox::No );
+  if( anAnswer == QMessageBox::No )
+  {
+    abort();
+    return;
+  }
+
+  startDocOperation();
+
+  for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
+  {
+    Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
+    if ( !anObject.IsNull() && !anObject->IsRemoved() )
+      anObject->Remove();
+  }
+
+  commitDocOperation();
+
+  module()->update( UF_Model | UF_Viewer | UF_OCCViewer | UF_VTKViewer );
   commit();
 }