Salome HOME
Merge remote-tracking branch 'origin/BR_IMPROVEMENTS' into BR_v14_rc
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportGeomObjectOp.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_ImportGeomObjectOp.h"
20
21 #include "HYDROGUI_GeomObjectDlg.h"
22 #include "HYDROGUI_DataObject.h"
23 #include "HYDROGUI_DataModel.h"
24 #include "HYDROGUI_Module.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
27
28 #include <HYDROData_Obstacle.h>
29 #include <HYDROData_PolylineXY.h>
30 #include <HYDROData_Iterator.h>
31
32 #include <GEOMBase.h>
33
34 #include <SalomeApp_Study.h>
35
36 #include <LightApp_Application.h>
37 #include <LightApp_UpdateFlags.h>
38
39 #include <SUIT_Desktop.h>
40 #include <SUIT_MessageBox.h>
41
42 #include <QDialog>
43
44 HYDROGUI_ImportGeomObjectOp::HYDROGUI_ImportGeomObjectOp( HYDROGUI_Module* theModule,  
45                                                           const int theOpType, 
46                                                           const int theGEOMOp )
47 : HYDROGUI_Operation( theModule ),
48   myOpType( theOpType ),
49   myGEOMOp( theGEOMOp ),
50   myGEOMOpName( "" ),
51   myIsToShowPanel( true )
52 {
53   if ( myOpType == ImportSelectedAsPolyline ) {
54     setName( tr( "IMPORT_GEOM_OBJECT_AS_POLYLINE" ) );
55   } else {
56     setName( tr( "IMPORT_GEOM_OBJECT_AS_OBSTACLE" ) );
57   }
58 }
59
60 HYDROGUI_ImportGeomObjectOp::~HYDROGUI_ImportGeomObjectOp()
61 {
62 }
63
64 void HYDROGUI_ImportGeomObjectOp::startOperation()
65 {
66   // Get GEOM objects to import
67   myGeomObjects.clear();
68
69   if ( myOpType == ImportSelectedAsObstacle ) {
70     myGeomObjects = 
71       HYDROGUI_Tool::GetSelectedGeomObjects( module(), getObstacleTypes() );
72   } else if ( myOpType == ImportSelectedAsPolyline ) {
73     myGeomObjects = 
74       HYDROGUI_Tool::GetSelectedGeomObjects( module(), getPolylineTypes() );
75   }
76
77   // Do not show the panel if more than one GEOM objects are selected
78   myIsToShowPanel = myIsToShowPanel && ( myGeomObjects.count() <= 1 );
79
80   HYDROGUI_Operation::startOperation();
81
82   HYDROGUI_GeomObjectDlg* aPanel = 0;
83
84   if ( myIsToShowPanel ) {
85     // Get panel
86     aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
87
88     if ( aPanel ) {
89       // Reset the panel state
90       aPanel->reset();
91
92       // Set default name
93       updateDefaultName();
94
95       // Pass the existing object names to the panel
96       QStringList anExistingNames;
97       if ( myOpType == ImportCreatedAsObstacle || 
98            myOpType == ImportSelectedAsObstacle ) {
99         anExistingNames = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_OBSTACLE );
100       } else if ( myOpType == ImportSelectedAsPolyline ) {
101         anExistingNames = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY );
102       }
103
104       aPanel->setObjectNames( anExistingNames );
105     }
106   }
107
108   if ( !aPanel ) {
109     onApply();
110   }
111
112   // Activate GEOM module operation in case of the corresponding operation type
113   if ( myOpType == ImportCreatedAsObstacle && myGEOMOp > 0 ) {
114     LightApp_Application* anApp = module()->getApp();
115     if ( anApp ) {
116       connect( anApp, SIGNAL( operationFinished( const QString&, const QString&, const QStringList& ) ), 
117         this, SLOT( onExternalOperationFinished( const QString&, const QString&, const QStringList& ) ) );
118
119       module()->getApp()->activateOperation( "Geometry", myGEOMOp );
120     }
121   }
122 }
123
124 void HYDROGUI_ImportGeomObjectOp::abortOperation()
125 {
126   LightApp_Application* anApp = module()->getApp();
127   if ( anApp ) {
128     anApp->disconnect( this );
129   }
130
131   closeExternalOperationDlg();
132
133   HYDROGUI_Operation::abortOperation();
134 }
135
136 void HYDROGUI_ImportGeomObjectOp::commitOperation()
137 {
138   closeExternalOperationDlg();
139
140   HYDROGUI_Operation::commitOperation();
141 }
142
143 bool HYDROGUI_ImportGeomObjectOp::processApply( int& theUpdateFlags,
144                                                 QString& theErrorMsg,
145                                                 QStringList& theBrowseObjectsEntries )
146 {
147   // Get active SalomeApp_Study
148   SalomeApp_Study* aStudy = 
149     dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
150   if ( !aStudy ) {
151     return false;
152   }
153
154   // Check that GEOM objects list is not empty
155   if ( myGeomObjects.isEmpty() ) {
156     theErrorMsg = tr( "NO_GEOM_OBJECT_TO_IMPORT" );
157     return false;
158   }
159
160   QString anObjectName;
161   Handle(HYDROData_Entity) anObjectToEdit;  
162   ObjectKind anObjectKind = 
163     myOpType == ImportSelectedAsPolyline ? KIND_POLYLINEXY : KIND_OBSTACLE;
164
165   if ( myGeomObjects.count() == 1 ) {
166     // Get panel
167     HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
168     if ( aPanel ) {
169       // Check object name
170       anObjectName = aPanel->getObjectName().simplified();
171       if ( anObjectName.isEmpty() ) {
172         theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
173         return false;
174       }
175
176       // Get object to edit
177       QString anEditedName = aPanel->getEditedObjectName().simplified();
178
179       if ( !anEditedName.isEmpty() ) {
180         anObjectToEdit = HYDROGUI_Tool::FindObjectByName( module(), anEditedName, anObjectKind );
181       }
182     }
183
184     if( anObjectToEdit.IsNull() || anObjectToEdit->GetName() != anObjectName ) {
185       // check that there are no other objects with the same name in the document
186       Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName/*, anObjectKind*/ );
187       if( !anObject.IsNull() ) {
188         theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
189         return false;
190       }
191     }
192   }
193
194   bool anIsOk = false;
195
196   // Get the GEOM object as SObject
197   foreach ( const QString& anEntry, myGeomObjects ) {
198     _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry)) );
199     if ( aSObject ) {
200       // Get the corresponding TopoDS_Shape
201       TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( aSObject->GetIOR().c_str() );
202       if ( aShape.IsNull() ) {
203         continue;
204       }
205       
206       // Create/edit an object
207       Handle(HYDROData_Entity) anObject; 
208
209       if ( anObjectToEdit.IsNull() ) {
210         if ( myOpType == ImportCreatedAsObstacle || myOpType == ImportSelectedAsObstacle ) {
211           anObject = doc()->CreateObject( KIND_OBSTACLE );
212           Handle(HYDROData_Obstacle) anObstacle = Handle(HYDROData_Obstacle)::DownCast( anObject );
213           anObstacle->SetFillingColor( HYDROData_Obstacle::DefaultFillingColor() );
214           anObstacle->SetBorderColor( HYDROData_Obstacle::DefaultBorderColor() );
215           anObstacle->SetGeomObjectEntry( anEntry.toLatin1().constData() );
216         } else if ( myOpType == ImportSelectedAsPolyline ) {
217           anObject = doc()->CreateObject( KIND_POLYLINEXY );
218           Handle(HYDROData_PolylineXY) aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anObject );
219           aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
220           aPolylineObj->SetGeomObjectEntry( anEntry.toLatin1().constData() );
221         }
222       } else {
223         anObject = anObjectToEdit;
224       }
225
226       // Set name
227       if ( anObjectName.isEmpty() ) {
228         QString aName = QString::fromStdString( aSObject->GetName() );
229         anObjectName = HYDROGUI_Tool::GenerateObjectName( 
230           module(), aName, QStringList(), true );
231       }
232       if ( anObject->GetName() != anObjectName ) {
233         anObject->SetName( anObjectName );
234       }
235
236       anObjectName.clear();
237
238       // Set shape
239       if ( myOpType == ImportCreatedAsObstacle || myOpType == ImportSelectedAsObstacle ) {
240         Handle(HYDROData_Obstacle) anObstacle = Handle(HYDROData_Obstacle)::DownCast( anObject );
241         anObstacle->SetShape3D( aShape );
242         anIsOk = true;
243       } else if ( myOpType == ImportSelectedAsPolyline ) {
244         Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( anObject );
245         anIsOk = aPolyline->ImportShape( aShape );
246
247         /* TODO: check it before start operation
248         if ( anIsOk && !aPolyline->IsEditable() )
249         {
250           anIsOk = SUIT_MessageBox::question( module()->getApp()->desktop(),
251                                               tr( "POLYLINE_IS_UNRECOGNIZED_TLT" ),
252                                               tr( "POLYLINE_IS_UNRECOGNIZED_MSG" ),
253                                               QMessageBox::Yes | QMessageBox::No,
254                                               QMessageBox::No ) == QMessageBox::Yes;
255           setPrintErrorMessage( anIsOk );
256         }
257         */
258       }
259
260       // Check operation status
261       if ( anIsOk ) {
262         anObject->Update();
263         module()->setIsToUpdate( anObject );
264         QString aHydroObjEntry = HYDROGUI_DataObject::dataObjectEntry( anObject );
265         theBrowseObjectsEntries.append( aHydroObjEntry );
266         theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
267       }
268     }
269   }
270
271   return anIsOk;
272 }
273
274 HYDROGUI_InputPanel* HYDROGUI_ImportGeomObjectOp::createInputPanel() const
275 {
276   HYDROGUI_InputPanel* aPanel = 0;
277   if ( myIsToShowPanel ) {
278     QString anObjectTypeName = 
279       myOpType == ImportSelectedAsPolyline ? tr("DEFAULT_POLYLINE_NAME") :
280                   tr("DEFAULT_OBSTACLE_NAME");
281     aPanel = new HYDROGUI_GeomObjectDlg( module(), getName(), anObjectTypeName );
282   }
283
284   return aPanel;
285 }
286
287 void HYDROGUI_ImportGeomObjectOp::updateDefaultName()
288 {
289   // Get panel
290   HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
291   if ( !aPanel ) {
292     return;
293   }
294
295   // Set the current GEOM object name to the panel
296   if ( myGeomObjects.count() == 1 ) {
297     SalomeApp_Study* aStudy = 
298       dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
299     if ( aStudy ) {
300       QString anEntry = myGeomObjects.first();
301       _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry) ) );
302       if ( aSObject ) {
303         aPanel->setDefaultName( QString::fromStdString(aSObject->GetName()) );
304       }
305     }
306   }
307 }
308
309 /**
310  * Called when the operation perfomed by another module is finished.
311  * \param theModuleName the name of the module which perfomed the operation
312  * \param theOperationName the operation name
313  * \param theEntryList the list of the created objects entries
314  */
315 void HYDROGUI_ImportGeomObjectOp::onExternalOperationFinished( 
316   const QString& theModuleName, const QString& theOperationName,
317   const QStringList& theEntryList )
318 {
319   // Process "Geometry" module operations with non-empty list of created objects only
320   if ( theModuleName != "Geometry" || theEntryList.isEmpty() ) {
321     return;
322   }
323   
324   // Store the operation name
325   myGEOMOpName = theOperationName;
326
327   // Store the geom objects entries list
328   myGeomObjects = theEntryList;
329
330   // Update the default name of the HYDRO object
331   updateDefaultName();
332
333   // Close the dialog corresponding to the external operation
334   closeExternalOperationDlg();
335 }
336
337 void HYDROGUI_ImportGeomObjectOp::closeExternalOperationDlg()
338 {
339   if ( myGEOMOpName.isEmpty() ) {
340     return;
341   }
342
343   SUIT_Desktop* aDesktop = module()->getApp()->desktop();
344   if ( aDesktop ) {
345     QList<QDialog*> aDialogs = aDesktop->findChildren<QDialog*>();
346     foreach ( QDialog* aDlg, aDialogs ) {
347       if ( typeid(*aDlg).name() == myGEOMOpName ) {
348         aDlg->close();
349         break;
350       }
351     }
352   }
353 }
354
355 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getObstacleTypes()
356 {
357   QList<GEOM::shape_type> aTypes;
358
359   aTypes << GEOM::COMPOUND << GEOM::COMPOUND << GEOM::SOLID << 
360             GEOM::SHELL << GEOM::FACE << GEOM::SHAPE;
361
362   return aTypes;
363 }
364
365 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getPolylineTypes()
366 {
367   QList<GEOM::shape_type> aTypes;
368
369   aTypes << GEOM::WIRE << GEOM::EDGE;
370
371   return aTypes;
372 }