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