Salome HOME
size of image is limited by 7000 pixels
[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 #include "HYDROGUI_DataModel.h"
21 #include "HYDROGUI_DeleteDlg.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Tool.h"
24 #include "HYDROGUI_Tool2.h"
25 #include "HYDROGUI_UpdateFlags.h"
26 #include "HYDROGUI_ProfileOp.h"
27
28 #include <HYDROData_Object.h>
29 #include <HYDROData_Document.h>
30
31 #include <LightApp_Application.h>
32
33 #include <SUIT_Desktop.h>
34 #include <SUIT_MessageBox.h>
35 #include <SUIT_Study.h>
36
37 #include <QSet>
38
39 HYDROGUI_DeleteOp::HYDROGUI_DeleteOp( HYDROGUI_Module* theModule )
40 : HYDROGUI_Operation( theModule )
41 {
42   setName( tr( "DELETE" ) );
43 }
44
45 HYDROGUI_DeleteOp::~HYDROGUI_DeleteOp()
46 {
47 }
48
49 void HYDROGUI_DeleteOp::startOperation()
50 {
51   QList<SUIT_Operation*> operations = study()->operations();
52   int n = operations.size();
53   if( n>=2 )
54   {
55     SUIT_Operation* anOtherOp = operations[n-2];
56     HYDROGUI_ProfileOp* aProfileOp = dynamic_cast<HYDROGUI_ProfileOp*>( anOtherOp );
57     if( aProfileOp )
58     {
59       aProfileOp->deleteSelected();
60       abort();
61       return;
62     }
63   }
64
65   HYDROGUI_Operation::startOperation();
66
67   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
68   if( aSeq.IsEmpty() )
69   {
70     abort();
71     return;
72   }
73
74   bool anIsCanRemove = true;
75
76   QStringList anObjNames;
77   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
78   {
79     Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
80     if( anObject.IsNull() )
81       continue;
82
83     if ( !anObject->CanRemove() )
84     {
85       anIsCanRemove = false;
86       break;
87     }
88
89     anObjNames.append( anObject->GetName() );
90   }
91
92   if ( !anIsCanRemove )
93   {
94     SUIT_MessageBox::critical( module()->getApp()->desktop(),
95                                tr( "DELETE_OBJECTS" ), tr( "DELETE_OBJECTS_IMPOSIBLE" ) );
96     abort();
97     return;
98   }
99
100   anObjNames.removeDuplicates();
101
102   // check the back-references
103   QMap<QString,HYDROData_SequenceOfObjects> aBackObjects =
104     HYDROGUI_Tool::GetObjectsBackReferences( doc(), anObjNames );
105
106   QMap<QString,HYDROData_SequenceOfObjects>::const_iterator anIt = aBackObjects.begin(),
107                                                             aLast = aBackObjects.end();
108   QStringList aRefList;
109   for ( ; anIt != aLast; anIt++ ) {
110     QStringList aSeqList;
111     HYDROData_SequenceOfObjects::Iterator anIter( anIt.value() );
112     for ( ; anIter.More(); anIter.Next() )
113     {
114       Handle(HYDROData_Entity) anEntity = anIter.Value();
115       if ( anEntity.IsNull() )
116         continue;
117       aSeqList.append( QString( tr( "DELETE_OBJECT_NAME" ) ).arg( anEntity->GetName() ) );
118     }
119     aRefList.append( QString( tr( "DELETE_OBJECT_IS_USED_FOR" ) ).arg( anIt.key() )
120                                                           .arg( aSeqList.join( ", " ) ) );
121   }
122
123   //QString aNamesList = anObjNames.join( "\n" );
124   if ( !aBackObjects.isEmpty() )
125   {
126     SUIT_MessageBox::critical( module()->getApp()->desktop(),
127                                tr( "DELETE_OBJECTS" ),
128                                tr( "DELETED_OBJECTS_HAS_BACKREFERENCES" ).arg( aRefList.join( "\n" ) ) );
129     abort();
130     return;
131   }
132
133   QString aWarningMsg;
134
135   HYDROGUI_DeleteDlg aDeleteDlg( module()->getApp()->desktop() );
136   aDeleteDlg.setObjectsToRemove( anObjNames );
137   bool isLastStricklerRemoved = false;
138   if ( aDeleteDlg.exec() != HYDROGUI_DeleteDlg::Accepted )
139   {
140     abort();
141     return;
142   }
143   else
144   {
145     QStringList aTableNames = 
146       HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_STRICKLER_TABLE, false );
147     aTableNames += anObjNames;
148     if ( aTableNames.toSet().size() == anObjNames.size() )
149     {
150       aWarningMsg = tr("DELETE_LAST_TABLE_WRN");
151       isLastStricklerRemoved = true;
152     }
153   }
154
155   if ( !aWarningMsg.isEmpty() ) {
156     int anAnswer = SUIT_MessageBox::warning( module()->getApp()->desktop(),
157                                              tr("WARNING"), aWarningMsg,
158                                               QMessageBox::Yes | QMessageBox::No,
159                                               QMessageBox::No );
160     if ( anAnswer != QMessageBox::Yes ) {
161       abort();
162       return;
163     }
164   }
165
166   startDocOperation();
167
168   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
169   {
170     Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
171
172     if ( !anObject.IsNull() && !anObject->IsRemoved() ) {
173       anObject->Remove();
174       module()->setObjectRemoved( anObject );
175     }
176   }
177
178   if( isLastStricklerRemoved )
179   {
180     HYDROGUI_DataModel* aModel = dynamic_cast<HYDROGUI_DataModel*>( module()->dataModel() );
181     aModel->createDefaultStricklerTable( doc(), 0 );
182   }
183
184   commitDocOperation();
185
186   module()->update( UF_Model | UF_Viewer | UF_OCCViewer | UF_VTKViewer );
187   commit();
188
189   module()->enableLCMActions();
190 }