Salome HOME
Merge remote-tracking branch 'origin/BR_LAND_COVER' into BR_v14_rc
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DeleteOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_DeleteOp.h"
20
21 #include "HYDROGUI_DeleteDlg.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Tool.h"
24 #include "HYDROGUI_UpdateFlags.h"
25
26 #include <HYDROData_Object.h>
27
28 #include <LightApp_Application.h>
29
30 #include <SUIT_Desktop.h>
31 #include <SUIT_MessageBox.h>
32
33 HYDROGUI_DeleteOp::HYDROGUI_DeleteOp( HYDROGUI_Module* theModule )
34 : HYDROGUI_Operation( theModule )
35 {
36   setName( tr( "DELETE" ) );
37 }
38
39 HYDROGUI_DeleteOp::~HYDROGUI_DeleteOp()
40 {
41 }
42
43 void HYDROGUI_DeleteOp::startOperation()
44 {
45   HYDROGUI_Operation::startOperation();
46
47   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
48   if( aSeq.IsEmpty() )
49   {
50     abort();
51     return;
52   }
53
54   bool anIsCanRemove = true;
55
56   QStringList anObjNames;
57   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
58   {
59     Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
60     if( anObject.IsNull() )
61       continue;
62
63     if ( !anObject->CanRemove() )
64     {
65       anIsCanRemove = false;
66       break;
67     }
68
69     anObjNames.append( anObject->GetName() );
70   }
71
72   if ( !anIsCanRemove )
73   {
74     SUIT_MessageBox::critical( module()->getApp()->desktop(),
75                                tr( "DELETE_OBJECTS" ), tr( "DELETE_OBJECTS_IMPOSIBLE" ) );
76     abort();
77     return;
78   }
79
80   anObjNames.removeDuplicates();
81
82   // check the back-references
83   QMap<QString,HYDROData_SequenceOfObjects> aBackObjects =
84     HYDROGUI_Tool::GetObjectsBackReferences( module(), anObjNames );
85
86   QMap<QString,HYDROData_SequenceOfObjects>::const_iterator anIt = aBackObjects.begin(),
87                                                             aLast = aBackObjects.end();
88   QStringList aRefList;
89   for ( ; anIt != aLast; anIt++ ) {
90     QStringList aSeqList;
91     HYDROData_SequenceOfObjects::Iterator anIter( anIt.value() );
92     for ( ; anIter.More(); anIter.Next() )
93     {
94       Handle(HYDROData_Entity) anEntity = anIter.Value();
95       if ( anEntity.IsNull() )
96         continue;
97       aSeqList.append( QString( tr( "DELETE_OBJECT_NAME" ) ).arg( anEntity->GetName() ) );
98     }
99     aRefList.append( QString( tr( "DELETE_OBJECT_IS_USED_FOR" ) ).arg( anIt.key() )
100                                                           .arg( aSeqList.join( ", " ) ) );
101   }
102
103   //QString aNamesList = anObjNames.join( "\n" );
104   if ( !aBackObjects.isEmpty() )
105   {
106     SUIT_MessageBox::critical( module()->getApp()->desktop(),
107                                tr( "DELETE_OBJECTS" ),
108                                tr( "DELETED_OBJECTS_HAS_BACKREFERENCES" ).arg( aRefList.join( "\n" ) ) );
109     abort();
110     return;
111   }
112
113   HYDROGUI_DeleteDlg aDeleteDlg( module()->getApp()->desktop() );
114   aDeleteDlg.setObjectsToRemove( anObjNames );
115   if ( aDeleteDlg.exec() != HYDROGUI_DeleteDlg::Accepted )
116   {
117     abort();
118     return;
119   }
120
121   startDocOperation();
122
123   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
124   {
125     Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
126
127     if ( !anObject.IsNull() && !anObject->IsRemoved() ) {
128       anObject->Remove();
129       module()->setObjectRemoved( anObject );
130     }
131   }
132
133   commitDocOperation();
134
135   module()->update( UF_Model | UF_Viewer | UF_OCCViewer | UF_VTKViewer );
136   commit();
137 }