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