1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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, or (at your option) any later version.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ImportGeomObjectOp.h"
25 #include "HYDROGUI_GeomObjectDlg.h"
26 #include "HYDROGUI_DataObject.h"
27 #include "HYDROGUI_DataModel.h"
28 #include "HYDROGUI_Module.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
32 #include <HYDROData_Obstacle.h>
33 #include <HYDROData_PolylineXY.h>
34 #include <HYDROData_Iterator.h>
38 #include <SalomeApp_Study.h>
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
43 #include <SUIT_Desktop.h>
44 #include <SUIT_MessageBox.h>
48 HYDROGUI_ImportGeomObjectOp::HYDROGUI_ImportGeomObjectOp( HYDROGUI_Module* theModule,
51 : HYDROGUI_Operation( theModule ),
52 myOpType( theOpType ),
53 myGEOMOp( theGEOMOp ),
55 myIsToShowPanel( true )
57 if ( myOpType == ImportSelectedAsPolyline ) {
58 setName( tr( "IMPORT_GEOM_OBJECT_AS_POLYLINE" ) );
60 setName( tr( "IMPORT_GEOM_OBJECT_AS_OBSTACLE" ) );
64 HYDROGUI_ImportGeomObjectOp::~HYDROGUI_ImportGeomObjectOp()
68 void HYDROGUI_ImportGeomObjectOp::startOperation()
70 // Get GEOM objects to import
71 myGeomObjects.clear();
73 if ( myOpType == ImportSelectedAsObstacle ) {
75 HYDROGUI_Tool::GetSelectedGeomObjects( module(), getObstacleTypes() );
76 } else if ( myOpType == ImportSelectedAsPolyline ) {
78 HYDROGUI_Tool::GetSelectedGeomObjects( module(), getPolylineTypes() );
81 // Do not show the panel if more than one GEOM objects are selected
82 myIsToShowPanel = myIsToShowPanel && ( myGeomObjects.count() <= 1 );
84 HYDROGUI_Operation::startOperation();
86 HYDROGUI_GeomObjectDlg* aPanel = 0;
88 if ( myIsToShowPanel ) {
90 aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
93 // Reset the panel state
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 );
108 aPanel->setObjectNames( anExistingNames );
116 // Activate GEOM module operation in case of the corresponding operation type
117 if ( myOpType == ImportCreatedAsObstacle && myGEOMOp > 0 ) {
118 LightApp_Application* anApp = module()->getApp();
120 connect( anApp, SIGNAL( operationFinished( const QString&, const QString&, const QStringList& ) ),
121 this, SLOT( onExternalOperationFinished( const QString&, const QString&, const QStringList& ) ) );
123 module()->getApp()->activateOperation( "Geometry", myGEOMOp );
128 void HYDROGUI_ImportGeomObjectOp::abortOperation()
130 LightApp_Application* anApp = module()->getApp();
132 anApp->disconnect( this );
135 closeExternalOperationDlg();
137 HYDROGUI_Operation::abortOperation();
140 void HYDROGUI_ImportGeomObjectOp::commitOperation()
142 closeExternalOperationDlg();
144 HYDROGUI_Operation::commitOperation();
147 bool HYDROGUI_ImportGeomObjectOp::processApply( int& theUpdateFlags,
148 QString& theErrorMsg,
149 QStringList& theBrowseObjectsEntries )
151 // Get active SalomeApp_Study
152 SalomeApp_Study* aStudy =
153 dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
158 // Check that GEOM objects list is not empty
159 if ( myGeomObjects.isEmpty() ) {
160 theErrorMsg = tr( "NO_GEOM_OBJECT_TO_IMPORT" );
164 QString anObjectName;
165 Handle(HYDROData_Entity) anObjectToEdit;
166 ObjectKind anObjectKind =
167 myOpType == ImportSelectedAsPolyline ? KIND_POLYLINEXY : KIND_OBSTACLE;
169 if ( myGeomObjects.count() == 1 ) {
171 HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
174 anObjectName = aPanel->getObjectName().simplified();
175 if ( anObjectName.isEmpty() ) {
176 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
180 // Get object to edit
181 QString anEditedName = aPanel->getEditedObjectName().simplified();
183 if ( !anEditedName.isEmpty() ) {
184 anObjectToEdit = HYDROGUI_Tool::FindObjectByName( module(), anEditedName, anObjectKind );
188 if( anObjectToEdit.IsNull() || anObjectToEdit->GetName() != anObjectName ) {
189 // check that there are no other objects with the same name in the document
190 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName/*, anObjectKind*/ );
191 if( !anObject.IsNull() ) {
192 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
200 // Get the GEOM object as SObject
201 foreach ( const QString& anEntry, myGeomObjects ) {
202 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry)) );
204 // Get the corresponding TopoDS_Shape
205 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( aSObject->GetIOR().c_str() );
206 if ( aShape.IsNull() ) {
210 // Create/edit an object
211 Handle(HYDROData_Entity) anObject;
213 if ( anObjectToEdit.IsNull() ) {
214 if ( myOpType == ImportCreatedAsObstacle || myOpType == ImportSelectedAsObstacle ) {
215 anObject = doc()->CreateObject( KIND_OBSTACLE );
216 Handle(HYDROData_Obstacle) anObstacle = Handle(HYDROData_Obstacle)::DownCast( anObject );
217 anObstacle->SetFillingColor( HYDROData_Obstacle::DefaultFillingColor() );
218 anObstacle->SetBorderColor( HYDROData_Obstacle::DefaultBorderColor() );
219 anObstacle->SetGeomObjectEntry( anEntry.toLatin1().constData() );
220 } else if ( myOpType == ImportSelectedAsPolyline ) {
221 anObject = doc()->CreateObject( KIND_POLYLINEXY );
222 Handle(HYDROData_PolylineXY) aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anObject );
223 aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
224 aPolylineObj->SetGeomObjectEntry( anEntry.toLatin1().constData() );
227 anObject = anObjectToEdit;
231 if ( anObjectName.isEmpty() ) {
232 QString aName = QString::fromStdString( aSObject->GetName() );
233 anObjectName = HYDROGUI_Tool::GenerateObjectName(
234 module(), aName, QStringList(), true );
236 if ( anObject->GetName() != anObjectName ) {
237 anObject->SetName( anObjectName );
240 anObjectName.clear();
243 if ( myOpType == ImportCreatedAsObstacle || myOpType == ImportSelectedAsObstacle ) {
244 Handle(HYDROData_Obstacle) anObstacle = Handle(HYDROData_Obstacle)::DownCast( anObject );
245 anObstacle->SetShape3D( aShape );
247 } else if ( myOpType == ImportSelectedAsPolyline ) {
248 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( anObject );
249 anIsOk = aPolyline->ImportShape( aShape );
251 /* TODO: check it before start operation
252 if ( anIsOk && !aPolyline->IsEditable() )
254 anIsOk = SUIT_MessageBox::question( module()->getApp()->desktop(),
255 tr( "POLYLINE_IS_UNRECOGNIZED_TLT" ),
256 tr( "POLYLINE_IS_UNRECOGNIZED_MSG" ),
257 QMessageBox::Yes | QMessageBox::No,
258 QMessageBox::No ) == QMessageBox::Yes;
259 setPrintErrorMessage( anIsOk );
264 // Check operation status
267 module()->setIsToUpdate( anObject );
268 QString aHydroObjEntry = HYDROGUI_DataObject::dataObjectEntry( anObject );
269 theBrowseObjectsEntries.append( aHydroObjEntry );
270 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
278 HYDROGUI_InputPanel* HYDROGUI_ImportGeomObjectOp::createInputPanel() const
280 HYDROGUI_InputPanel* aPanel = 0;
281 if ( myIsToShowPanel ) {
282 QString anObjectTypeName =
283 myOpType == ImportSelectedAsPolyline ? tr("DEFAULT_POLYLINE_NAME") :
284 tr("DEFAULT_OBSTACLE_NAME");
285 aPanel = new HYDROGUI_GeomObjectDlg( module(), getName(), anObjectTypeName );
291 void HYDROGUI_ImportGeomObjectOp::updateDefaultName()
294 HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
299 // Set the current GEOM object name to the panel
300 if ( myGeomObjects.count() == 1 ) {
301 SalomeApp_Study* aStudy =
302 dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
304 QString anEntry = myGeomObjects.first();
305 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry) ) );
307 aPanel->setDefaultName( QString::fromStdString(aSObject->GetName()) );
314 * Called when the operation perfomed by another module is finished.
315 * \param theModuleName the name of the module which perfomed the operation
316 * \param theOperationName the operation name
317 * \param theEntryList the list of the created objects entries
319 void HYDROGUI_ImportGeomObjectOp::onExternalOperationFinished(
320 const QString& theModuleName, const QString& theOperationName,
321 const QStringList& theEntryList )
323 // Process "Geometry" module operations with non-empty list of created objects only
324 if ( theModuleName != "Geometry" || theEntryList.isEmpty() ) {
328 // Store the operation name
329 myGEOMOpName = theOperationName;
331 // Store the geom objects entries list
332 myGeomObjects = theEntryList;
334 // Update the default name of the HYDRO object
337 // Close the dialog corresponding to the external operation
338 closeExternalOperationDlg();
341 void HYDROGUI_ImportGeomObjectOp::closeExternalOperationDlg()
343 if ( myGEOMOpName.isEmpty() ) {
347 SUIT_Desktop* aDesktop = module()->getApp()->desktop();
349 QList<QDialog*> aDialogs = aDesktop->findChildren<QDialog*>();
350 foreach ( QDialog* aDlg, aDialogs ) {
351 if ( typeid(*aDlg).name() == myGEOMOpName ) {
359 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getObstacleTypes()
361 QList<GEOM::shape_type> aTypes;
363 aTypes << GEOM::COMPOUND << GEOM::COMPOUND << GEOM::SOLID <<
364 GEOM::SHELL << GEOM::FACE << GEOM::SHAPE;
369 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getPolylineTypes()
371 QList<GEOM::shape_type> aTypes;
373 aTypes << GEOM::WIRE << GEOM::EDGE;