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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_ImportGeomObjectOp.h"
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"
28 #include <HYDROData_Obstacle.h>
29 #include <HYDROData_PolylineXY.h>
30 #include <HYDROData_Iterator.h>
34 #include <SalomeApp_Study.h>
36 #include <LightApp_Application.h>
37 #include <LightApp_UpdateFlags.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_MessageBox.h>
44 HYDROGUI_ImportGeomObjectOp::HYDROGUI_ImportGeomObjectOp( HYDROGUI_Module* theModule,
47 : HYDROGUI_Operation( theModule ),
48 myOpType( theOpType ),
49 myGEOMOp( theGEOMOp ),
51 myIsToShowPanel( true )
53 if ( myOpType == ImportSelectedAsPolyline ) {
54 setName( tr( "IMPORT_GEOM_OBJECT_AS_POLYLINE" ) );
56 setName( tr( "IMPORT_GEOM_OBJECT_AS_OBSTACLE" ) );
60 HYDROGUI_ImportGeomObjectOp::~HYDROGUI_ImportGeomObjectOp()
64 void HYDROGUI_ImportGeomObjectOp::startOperation()
66 // Get GEOM objects to import
67 myGeomObjects.clear();
69 if ( myOpType == ImportSelectedAsObstacle ) {
71 HYDROGUI_Tool::GetSelectedGeomObjects( module(), getObstacleTypes() );
72 } else if ( myOpType == ImportSelectedAsPolyline ) {
74 HYDROGUI_Tool::GetSelectedGeomObjects( module(), getPolylineTypes() );
77 // Do not show the panel if more than one GEOM objects are selected
78 myIsToShowPanel = myIsToShowPanel && ( myGeomObjects.count() <= 1 );
80 HYDROGUI_Operation::startOperation();
82 HYDROGUI_GeomObjectDlg* aPanel = 0;
84 if ( myIsToShowPanel ) {
86 aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
89 // Reset the panel state
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 );
104 aPanel->setObjectNames( anExistingNames );
112 // Activate GEOM module operation in case of the corresponding operation type
113 if ( myOpType == ImportCreatedAsObstacle && myGEOMOp > 0 ) {
114 LightApp_Application* anApp = module()->getApp();
116 connect( anApp, SIGNAL( operationFinished( const QString&, const QString&, const QStringList& ) ),
117 this, SLOT( onExternalOperationFinished( const QString&, const QString&, const QStringList& ) ) );
119 module()->getApp()->activateOperation( "Geometry", myGEOMOp );
124 void HYDROGUI_ImportGeomObjectOp::abortOperation()
126 LightApp_Application* anApp = module()->getApp();
128 anApp->disconnect( this );
131 closeExternalOperationDlg();
133 HYDROGUI_Operation::abortOperation();
136 void HYDROGUI_ImportGeomObjectOp::commitOperation()
138 closeExternalOperationDlg();
140 HYDROGUI_Operation::commitOperation();
143 bool HYDROGUI_ImportGeomObjectOp::processApply( int& theUpdateFlags,
144 QString& theErrorMsg,
145 QStringList& theBrowseObjectsEntries )
147 // Get active SalomeApp_Study
148 SalomeApp_Study* aStudy =
149 dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
154 // Check that GEOM objects list is not empty
155 if ( myGeomObjects.isEmpty() ) {
156 theErrorMsg = tr( "NO_GEOM_OBJECT_TO_IMPORT" );
160 QString anObjectName;
161 Handle(HYDROData_Entity) anObjectToEdit;
162 ObjectKind anObjectKind =
163 myOpType == ImportSelectedAsPolyline ? KIND_POLYLINEXY : KIND_OBSTACLE;
165 if ( myGeomObjects.count() == 1 ) {
167 HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
170 anObjectName = aPanel->getObjectName().simplified();
171 if ( anObjectName.isEmpty() ) {
172 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
176 // Get object to edit
177 QString anEditedName = aPanel->getEditedObjectName().simplified();
179 if ( !anEditedName.isEmpty() ) {
180 anObjectToEdit = HYDROGUI_Tool::FindObjectByName( module(), anEditedName, anObjectKind );
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 );
196 // Get the GEOM object as SObject
197 foreach ( const QString& anEntry, myGeomObjects ) {
198 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry)) );
200 // Get the corresponding TopoDS_Shape
201 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( aSObject->GetIOR().c_str() );
202 if ( aShape.IsNull() ) {
206 // Create/edit an object
207 Handle(HYDROData_Entity) anObject;
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() );
223 anObject = anObjectToEdit;
227 if ( anObjectName.isEmpty() ) {
228 QString aName = QString::fromStdString( aSObject->GetName() );
229 anObjectName = HYDROGUI_Tool::GenerateObjectName(
230 module(), aName, QStringList(), true );
232 if ( anObject->GetName() != anObjectName ) {
233 anObject->SetName( anObjectName );
236 anObjectName.clear();
239 if ( myOpType == ImportCreatedAsObstacle || myOpType == ImportSelectedAsObstacle ) {
240 Handle(HYDROData_Obstacle) anObstacle = Handle(HYDROData_Obstacle)::DownCast( anObject );
241 anObstacle->SetShape3D( aShape );
243 } else if ( myOpType == ImportSelectedAsPolyline ) {
244 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( anObject );
245 anIsOk = aPolyline->ImportShape( aShape );
247 /* TODO: check it before start operation
248 if ( anIsOk && !aPolyline->IsEditable() )
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 );
260 // Check operation status
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;
274 HYDROGUI_InputPanel* HYDROGUI_ImportGeomObjectOp::createInputPanel() const
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 );
287 void HYDROGUI_ImportGeomObjectOp::updateDefaultName()
290 HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
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() );
300 QString anEntry = myGeomObjects.first();
301 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry) ) );
303 aPanel->setDefaultName( QString::fromStdString(aSObject->GetName()) );
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
315 void HYDROGUI_ImportGeomObjectOp::onExternalOperationFinished(
316 const QString& theModuleName, const QString& theOperationName,
317 const QStringList& theEntryList )
319 // Process "Geometry" module operations with non-empty list of created objects only
320 if ( theModuleName != "Geometry" || theEntryList.isEmpty() ) {
324 // Store the operation name
325 myGEOMOpName = theOperationName;
327 // Store the geom objects entries list
328 myGeomObjects = theEntryList;
330 // Update the default name of the HYDRO object
333 // Close the dialog corresponding to the external operation
334 closeExternalOperationDlg();
337 void HYDROGUI_ImportGeomObjectOp::closeExternalOperationDlg()
339 if ( myGEOMOpName.isEmpty() ) {
343 SUIT_Desktop* aDesktop = module()->getApp()->desktop();
345 QList<QDialog*> aDialogs = aDesktop->findChildren<QDialog*>();
346 foreach ( QDialog* aDlg, aDialogs ) {
347 if ( typeid(*aDlg).name() == myGEOMOpName ) {
355 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getObstacleTypes()
357 QList<GEOM::shape_type> aTypes;
359 aTypes << GEOM::COMPOUND << GEOM::COMPOUND << GEOM::SOLID <<
360 GEOM::SHELL << GEOM::FACE << GEOM::SHAPE;
365 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getPolylineTypes()
367 QList<GEOM::shape_type> aTypes;
369 aTypes << GEOM::WIRE << GEOM::EDGE;