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_Tool2.h"
27 #include "HYDROGUI_UpdateFlags.h"
29 #include <HYDROData_Obstacle.h>
30 #include <HYDROData_PolylineXY.h>
31 #include <HYDROData_Iterator.h>
35 #include <SalomeApp_Study.h>
37 #include <LightApp_Application.h>
38 #include <LightApp_UpdateFlags.h>
40 #include <SUIT_Desktop.h>
41 #include <SUIT_MessageBox.h>
45 HYDROGUI_ImportGeomObjectOp::HYDROGUI_ImportGeomObjectOp( HYDROGUI_Module* theModule,
48 : HYDROGUI_Operation( theModule ),
49 myOpType( theOpType ),
50 myGEOMOp( theGEOMOp ),
52 myIsToShowPanel( true )
54 if ( myOpType == ImportSelectedAsPolyline ) {
55 setName( tr( "IMPORT_GEOM_OBJECT_AS_POLYLINE" ) );
57 setName( tr( "IMPORT_GEOM_OBJECT_AS_OBSTACLE" ) );
61 HYDROGUI_ImportGeomObjectOp::~HYDROGUI_ImportGeomObjectOp()
65 void HYDROGUI_ImportGeomObjectOp::startOperation()
67 // Get GEOM objects to import
68 myGeomObjects.clear();
70 if ( myOpType == ImportSelectedAsObstacle ) {
72 HYDROGUI_Tool::GetSelectedGeomObjects( module(), getObstacleTypes() );
73 } else if ( myOpType == ImportSelectedAsPolyline ) {
75 HYDROGUI_Tool::GetSelectedGeomObjects( module(), getPolylineTypes() );
78 // Do not show the panel if more than one GEOM objects are selected
79 myIsToShowPanel = myIsToShowPanel && ( myGeomObjects.count() <= 1 );
81 HYDROGUI_Operation::startOperation();
83 HYDROGUI_GeomObjectDlg* aPanel = 0;
85 if ( myIsToShowPanel ) {
87 aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
90 // Reset the panel state
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 );
105 aPanel->setObjectNames( anExistingNames );
113 // Activate GEOM module operation in case of the corresponding operation type
114 if ( myOpType == ImportCreatedAsObstacle && myGEOMOp > 0 ) {
115 LightApp_Application* anApp = module()->getApp();
117 connect( anApp, SIGNAL( operationFinished( const QString&, const QString&, const QStringList& ) ),
118 this, SLOT( onExternalOperationFinished( const QString&, const QString&, const QStringList& ) ) );
120 module()->getApp()->activateOperation( "Geometry", myGEOMOp );
125 void HYDROGUI_ImportGeomObjectOp::abortOperation()
127 LightApp_Application* anApp = module()->getApp();
129 anApp->disconnect( this );
132 closeExternalOperationDlg();
134 HYDROGUI_Operation::abortOperation();
137 void HYDROGUI_ImportGeomObjectOp::commitOperation()
139 closeExternalOperationDlg();
141 HYDROGUI_Operation::commitOperation();
144 bool HYDROGUI_ImportGeomObjectOp::processApply( int& theUpdateFlags,
145 QString& theErrorMsg,
146 QStringList& theBrowseObjectsEntries )
148 // Get active SalomeApp_Study
149 SalomeApp_Study* aStudy =
150 dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
155 // Check that GEOM objects list is not empty
156 if ( myGeomObjects.isEmpty() ) {
157 theErrorMsg = tr( "NO_GEOM_OBJECT_TO_IMPORT" );
161 QString anObjectName;
162 Handle(HYDROData_Entity) anObjectToEdit;
163 ObjectKind anObjectKind =
164 myOpType == ImportSelectedAsPolyline ? KIND_POLYLINEXY : KIND_OBSTACLE;
166 if ( myGeomObjects.count() == 1 ) {
168 HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
171 anObjectName = aPanel->getObjectName().simplified();
172 if ( anObjectName.isEmpty() ) {
173 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
177 // Get object to edit
178 QString anEditedName = aPanel->getEditedObjectName().simplified();
180 if ( !anEditedName.isEmpty() ) {
181 anObjectToEdit = HYDROGUI_Tool::FindObjectByName( module(), anEditedName, anObjectKind );
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 );
197 // Get the GEOM object as SObject
198 foreach ( const QString& anEntry, myGeomObjects ) {
199 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry)) );
201 // Get the corresponding TopoDS_Shape
202 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( aSObject->GetIOR().c_str() );
203 if ( aShape.IsNull() ) {
207 // Create/edit an object
208 Handle(HYDROData_Entity) anObject;
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() );
224 anObject = anObjectToEdit;
228 if ( anObjectName.isEmpty() ) {
229 QString aName = QString::fromStdString( aSObject->GetName() );
230 anObjectName = HYDROGUI_Tool::GenerateObjectName(
231 module(), aName, QStringList(), true );
233 if ( anObject->GetName() != anObjectName ) {
234 anObject->SetName( anObjectName );
237 anObjectName.clear();
240 if ( myOpType == ImportCreatedAsObstacle || myOpType == ImportSelectedAsObstacle ) {
241 Handle(HYDROData_Obstacle) anObstacle = Handle(HYDROData_Obstacle)::DownCast( anObject );
242 anObstacle->SetShape3D( aShape );
244 } else if ( myOpType == ImportSelectedAsPolyline ) {
245 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( anObject );
246 anIsOk = aPolyline->ImportShape( aShape );
248 /* TODO: check it before start operation
249 if ( anIsOk && !aPolyline->IsEditable() )
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 );
261 // Check operation status
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;
275 HYDROGUI_InputPanel* HYDROGUI_ImportGeomObjectOp::createInputPanel() const
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 );
288 void HYDROGUI_ImportGeomObjectOp::updateDefaultName()
291 HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
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() );
301 QString anEntry = myGeomObjects.first();
302 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry) ) );
304 aPanel->setDefaultName( QString::fromStdString(aSObject->GetName()) );
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
316 void HYDROGUI_ImportGeomObjectOp::onExternalOperationFinished(
317 const QString& theModuleName, const QString& theOperationName,
318 const QStringList& theEntryList )
320 // Process "Geometry" module operations with non-empty list of created objects only
321 if ( theModuleName != "Geometry" || theEntryList.isEmpty() ) {
325 // Store the operation name
326 myGEOMOpName = theOperationName;
328 // Store the geom objects entries list
329 myGeomObjects = theEntryList;
331 // Update the default name of the HYDRO object
334 // Close the dialog corresponding to the external operation
335 closeExternalOperationDlg();
338 void HYDROGUI_ImportGeomObjectOp::closeExternalOperationDlg()
340 if ( myGEOMOpName.isEmpty() ) {
344 SUIT_Desktop* aDesktop = module()->getApp()->desktop();
346 QList<QDialog*> aDialogs = aDesktop->findChildren<QDialog*>();
347 foreach ( QDialog* aDlg, aDialogs ) {
348 if ( typeid(*aDlg).name() == myGEOMOpName ) {
356 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getObstacleTypes()
358 QList<GEOM::shape_type> aTypes;
360 aTypes << GEOM::COMPOUND << GEOM::COMPOUND << GEOM::SOLID <<
361 GEOM::SHELL << GEOM::FACE << GEOM::SHAPE;
366 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getPolylineTypes()
368 QList<GEOM::shape_type> aTypes;
370 aTypes << GEOM::WIRE << GEOM::EDGE;