Salome HOME
Import of geom curve.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportGeomObjectOp.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_ImportGeomObjectOp.h"
24
25 #include "HYDROGUI_GeomObjectDlg.h"
26
27 #include "HYDROGUI_DataModel.h"
28 #include "HYDROGUI_Module.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
31
32 #include <HYDROData_Obstacle.h>
33 #include <HYDROData_PolylineXY.h>
34 #include <HYDROData_Iterator.h>
35
36 #include <GEOMBase.h>
37
38 #include <SalomeApp_Study.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
42
43 #include <SUIT_Desktop.h>
44 #include <SUIT_MessageBox.h>
45
46 #include <QDialog>
47
48 HYDROGUI_ImportGeomObjectOp::HYDROGUI_ImportGeomObjectOp( HYDROGUI_Module* theModule,  
49                                                           const int theOpType, 
50                                                           const int theGEOMOp )
51 : HYDROGUI_Operation( theModule ),
52   myOpType( theOpType ),
53   myGEOMOp( theGEOMOp ),
54   myGEOMOpName( "" ),
55   myIsToShowPanel( true )
56 {
57   if ( myOpType == ImportSelectedAsPolyline ) {
58     setName( tr( "IMPORT_GEOM_OBJECT_AS_POLYLINE" ) );
59   } else {
60     setName( tr( "IMPORT_GEOM_OBJECT_AS_OBSTACLE" ) );
61   }
62 }
63
64 HYDROGUI_ImportGeomObjectOp::~HYDROGUI_ImportGeomObjectOp()
65 {
66 }
67
68 void HYDROGUI_ImportGeomObjectOp::startOperation()
69 {
70   // Get GEOM objects to import
71   myGeomObjects.clear();
72
73   if ( myOpType == ImportSelectedAsObstacle ) {
74     myGeomObjects = 
75       HYDROGUI_Tool::GetSelectedGeomObjects( module(), getObstacleTypes() );
76   } else if ( myOpType == ImportSelectedAsPolyline ) {
77     myGeomObjects = 
78       HYDROGUI_Tool::GetSelectedGeomObjects( module(), getPolylineTypes() );
79   }
80
81   // Do not show the panel if more than one GEOM objects are selected
82   myIsToShowPanel = myIsToShowPanel && ( myGeomObjects.count() <= 1 );
83
84   HYDROGUI_Operation::startOperation();
85
86   HYDROGUI_GeomObjectDlg* aPanel = 0;
87
88   if ( myIsToShowPanel ) {
89     // Get panel
90     aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
91
92     if ( aPanel ) {
93       // Reset the panel state
94       aPanel->reset();
95
96       // Set default name
97       updateDefaultName();
98
99       // Pass the existing object names to the panel
100       QStringList anExistingNames;
101       if ( myOpType == ImportCreatedAsObstacle || 
102            myOpType == ImportSelectedAsObstacle ) {
103         anExistingNames = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_OBSTACLE );
104       } else if ( myOpType == ImportSelectedAsPolyline ) {
105         anExistingNames = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY );
106       }
107
108       aPanel->setObjectNames( anExistingNames );
109     }
110   }
111
112   if ( !aPanel ) {
113     onApply();
114   }
115
116   // Activate GEOM module operation in case of the corresponding operation type
117   if ( myOpType == ImportCreatedAsObstacle && myGEOMOp > 0 ) {
118     LightApp_Application* anApp = module()->getApp();
119     if ( anApp ) {
120       connect( anApp, SIGNAL( operationFinished( const QString&, const QString&, const QStringList& ) ), 
121         this, SLOT( onExternalOperationFinished( const QString&, const QString&, const QStringList& ) ) );
122
123       module()->getApp()->activateOperation( "Geometry", myGEOMOp );
124     }
125   }
126 }
127
128 void HYDROGUI_ImportGeomObjectOp::abortOperation()
129 {
130   LightApp_Application* anApp = module()->getApp();
131   if ( anApp ) {
132     anApp->disconnect( this );
133   }
134
135   closeExternalOperationDlg();
136
137   HYDROGUI_Operation::abortOperation();
138 }
139
140 void HYDROGUI_ImportGeomObjectOp::commitOperation()
141 {
142   closeExternalOperationDlg();
143
144   HYDROGUI_Operation::commitOperation();
145 }
146
147 bool HYDROGUI_ImportGeomObjectOp::processApply( int& theUpdateFlags,
148                                                 QString& theErrorMsg )
149 {
150   // Get active SalomeApp_Study
151   SalomeApp_Study* aStudy = 
152     dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
153   if ( !aStudy ) {
154     return false;
155   }
156
157   // Check that GEOM objects list is not empty
158   if ( myGeomObjects.isEmpty() ) {
159     theErrorMsg = tr( "NO_GEOM_OBJECT_TO_IMPORT" );
160     return false;
161   }
162
163   QString anObjectName;
164   Handle(HYDROData_Entity) anObjectToEdit;  
165   ObjectKind anObjectKind = 
166     myOpType == ImportSelectedAsPolyline ? KIND_POLYLINEXY : KIND_OBSTACLE;
167
168   if ( myGeomObjects.count() == 1 ) {
169     // Get panel
170     HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
171     if ( aPanel ) {
172       // Check object name
173       anObjectName = aPanel->getObjectName().simplified();
174       if ( anObjectName.isEmpty() ) {
175         theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
176         return false;
177       }
178
179       // Get object to edit
180       QString anEditedName = aPanel->getEditedObjectName().simplified();
181
182       if ( !anEditedName.isEmpty() ) {
183         anObjectToEdit = HYDROGUI_Tool::FindObjectByName( module(), anEditedName, anObjectKind );
184       }
185     }
186
187     if( anObjectToEdit.IsNull() || anObjectToEdit->GetName() != anObjectName ) {
188       // check that there are no other objects with the same name in the document
189       Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName/*, anObjectKind*/ );
190       if( !anObject.IsNull() ) {
191         theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
192         return false;
193       }
194     }
195   }
196
197   bool anIsOk = false;
198
199   // Get the GEOM object as SObject
200   foreach ( const QString& anEntry, myGeomObjects ) {
201     _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry)) );
202     if ( aSObject ) {
203       // Get the corresponding TopoDS_Shape
204       TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( aSObject->GetIOR().c_str() );
205       if ( aShape.IsNull() ) {
206         continue;
207       }
208       
209       // Create/edit an object
210       Handle(HYDROData_Entity) anObject; 
211
212       if ( anObjectToEdit.IsNull() ) {
213         if ( myOpType == ImportCreatedAsObstacle || myOpType == ImportSelectedAsObstacle ) {
214           anObject = doc()->CreateObject( KIND_OBSTACLE );
215           Handle(HYDROData_Obstacle) anObstacle = Handle(HYDROData_Obstacle)::DownCast( anObject );
216           anObstacle->SetFillingColor( HYDROData_Obstacle::DefaultFillingColor() );
217           anObstacle->SetBorderColor( HYDROData_Obstacle::DefaultBorderColor() );
218           anObstacle->SetGeomObjectEntry( anEntry.toLatin1().constData() );
219         } else if ( myOpType == ImportSelectedAsPolyline ) {
220           anObject = doc()->CreateObject( KIND_POLYLINEXY );
221           Handle(HYDROData_PolylineXY) aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anObject );
222           aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
223           aPolylineObj->SetGeomObjectEntry( anEntry.toLatin1().constData() );
224         }
225       } else {
226         anObject = anObjectToEdit;
227       }
228
229       // Set name
230       if ( anObjectName.isEmpty() ) {
231         QString aName = QString::fromStdString( aSObject->GetName() );
232         anObjectName = HYDROGUI_Tool::GenerateObjectName( 
233           module(), aName, QStringList(), true );
234       }
235       if ( anObject->GetName() != anObjectName ) {
236         anObject->SetName( anObjectName );
237       }
238
239       anObjectName.clear();
240
241       // Set shape
242       if ( myOpType == ImportCreatedAsObstacle || myOpType == ImportSelectedAsObstacle ) {
243         Handle(HYDROData_Obstacle) anObstacle = Handle(HYDROData_Obstacle)::DownCast( anObject );
244         anObstacle->SetShape3D( aShape );
245         anIsOk = true;
246       } else if ( myOpType == ImportSelectedAsPolyline ) {
247         Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( anObject );
248         anIsOk = aPolyline->ImportShape( aShape );
249
250         /* TODO: check it before start operation
251         if ( anIsOk && !aPolyline->IsEditable() )
252         {
253           anIsOk = SUIT_MessageBox::question( module()->getApp()->desktop(),
254                                               tr( "POLYLINE_IS_UNRECOGNIZED_TLT" ),
255                                               tr( "POLYLINE_IS_UNRECOGNIZED_MSG" ),
256                                               QMessageBox::Yes | QMessageBox::No,
257                                               QMessageBox::No ) == QMessageBox::Yes;
258           setPrintErrorMessage( anIsOk );
259         }
260         */
261       }
262
263       // Check operation status
264       if ( anIsOk ) {
265         anObject->Update();
266         module()->setIsToUpdate( anObject );
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
335 void HYDROGUI_ImportGeomObjectOp::closeExternalOperationDlg()
336 {
337   if ( myGEOMOpName.isEmpty() ) {
338     return;
339   }
340
341   SUIT_Desktop* aDesktop = module()->getApp()->desktop();
342   if ( aDesktop ) {
343     QList<QDialog*> aDialogs = aDesktop->findChildren<QDialog*>();
344     foreach ( QDialog* aDlg, aDialogs ) {
345       if ( typeid(*aDlg).name() == myGEOMOpName ) {
346         aDlg->close();
347         break;
348       }
349     }
350   }
351 }
352
353 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getObstacleTypes()
354 {
355   QList<GEOM::shape_type> aTypes;
356
357   aTypes << GEOM::COMPOUND << GEOM::COMPOUND << GEOM::SOLID << 
358             GEOM::SHELL << GEOM::FACE << GEOM::SHAPE;
359
360   return aTypes;
361 }
362
363 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getPolylineTypes()
364 {
365   QList<GEOM::shape_type> aTypes;
366
367   aTypes << GEOM::WIRE << GEOM::EDGE;
368
369   return aTypes;
370 }