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_RecognizeContoursOp.h"
21 #include "HYDROGUI_RecognizeContoursDlg.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Shape.h"
24 #include "HYDROGUI_Tool.h"
25 #include "HYDROGUI_UpdateFlags.h"
27 #include <HYDROData_Document.h>
28 #include <HYDROData_GeomTool.h>
29 #include <HYDROData_ShapesTool.h>
30 #include <HYDROData_PolylineXY.h>
32 #include <GeometryGUI.h>
33 #include <GeometryGUI_Operations.h>
34 #include <GEOM_Constants.h>
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewModel.h>
40 #include <SalomeApp_Study.h>
42 #include <LightApp_Application.h>
43 #include <LightApp_DataOwner.h>
44 #include <LightApp_Displayer.h>
45 #include <LightApp_SelectionMgr.h>
47 #include <SUIT_Desktop.h>
48 #include <SUIT_ViewManager.h>
51 #include <QTemporaryFile>
56 HYDROGUI_RecognizeContoursOp::HYDROGUI_RecognizeContoursOp( HYDROGUI_Module* theModule )
57 : HYDROGUI_Operation( theModule ),
60 setName( tr( "CONTOURS_RECOGNITION" ) );
66 HYDROGUI_RecognizeContoursOp::~HYDROGUI_RecognizeContoursOp()
73 void HYDROGUI_RecognizeContoursOp::startOperation()
75 HYDROGUI_Operation::startOperation();
77 // Get the selected image
78 myImage = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
79 if ( myImage.IsNull() ) {
84 // Create temporary graphics file
85 QImage aQImage = myImage->Image();
86 myTmpImageFile = new QTemporaryFile( myImage->GetName() );
87 if ( !myTmpImageFile->open() ||
88 !aQImage.save( myTmpImageFile->fileName(), "PNG", 100 ) ) {
93 // Create the input panel
94 HYDROGUI_RecognizeContoursDlg* aPanel =
95 ::qobject_cast<HYDROGUI_RecognizeContoursDlg*>( inputPanel() );
104 SalomeApp_Study* aStudy =
105 dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
107 // Get active view manager
108 SUIT_ViewManager* aViewMgr = module()->getApp()->activeViewManager();
110 // Export the selected image to GEOM module
111 if ( aStudy && aViewMgr ) {
112 SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy->studyDS() );
113 GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
115 QString aGeomPictureEntry;
117 HYDROData_GeomTool::createFaceInGEOM(
118 aGeomEngine, aDSStudy, aQImage.width(), aQImage.height(), myImage->GetName(), aGeomPictureEntry );
120 if ( !aGeomPictureEntry.isEmpty() ) {
121 aStudy->setObjectProperty( aViewMgr->getGlobalId(), aGeomPictureEntry,
122 GEOM::propertyName( GEOM::Texture ), myTmpImageFile->fileName() );
124 // update the object browser
125 module()->getApp()->updateObjectBrowser( true );
127 // select the picture
128 SUIT_DataOwnerPtrList aList( true );
129 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aGeomPictureEntry ) ) );
130 selectionMgr()->setSelected(aList );
132 // Add GEOM picture object entry to the list of temporary geom objects
133 myTmpGeomObjects << aGeomPictureEntry;
137 // Activate GEOM module operation
138 LightApp_Application* anApp = module()->getApp();
140 connect( anApp, SIGNAL( operationFinished( const QString&, const QString&, const QStringList& ) ),
141 this, SLOT( onExternalOperationFinished( const QString&, const QString&, const QStringList& ) ) );
143 module()->getApp()->activateOperation( "Geometry", GEOMOp::OpFeatureDetect );
149 void HYDROGUI_RecognizeContoursOp::abortOperation()
151 LightApp_Application* anApp = module()->getApp();
153 anApp->disconnect( this );
158 HYDROGUI_Operation::abortOperation();
163 void HYDROGUI_RecognizeContoursOp::commitOperation()
167 HYDROGUI_Operation::commitOperation();
172 bool HYDROGUI_RecognizeContoursOp::processApply( int& theUpdateFlags,
173 QString& theErrorMsg,
174 QStringList& theBrowseObjectsEntries )
177 HYDROGUI_RecognizeContoursDlg* aPanel =
178 ::qobject_cast<HYDROGUI_RecognizeContoursDlg*>( inputPanel() );
183 // Check if contour GEOM object exists
184 if ( myGeomContourEntry.isEmpty() ) {
185 theErrorMsg = tr( "NO_DETECTED_CONTOURS" );
190 foreach ( QString aName, aPanel->getSelectedtPolylineNames() ) {
191 TopoDS_Shape aShape = myPolylineShapes.value( aName )->getTopoShape();
192 if ( aShape.IsNull() ) {
196 Handle(HYDROData_PolylineXY) aPolylineObj =
197 Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
199 if( !aPolylineObj.IsNull() ) {
200 aPolylineObj->SetName( aName );
201 aPolylineObj->ImportShape( aShape );
202 aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
203 aPolylineObj->Update();
204 module()->setIsToUpdate( aPolylineObj );
208 theUpdateFlags = UF_Model;
215 HYDROGUI_InputPanel* HYDROGUI_RecognizeContoursOp::createInputPanel() const
217 HYDROGUI_InputPanel* aPanel = new HYDROGUI_RecognizeContoursDlg( module(), getName() );
219 connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
225 * Called when the operation perfomed by another module is finished.
226 * \param theModuleName the name of the module which perfomed the operation
227 * \param theOperationName the operation name
228 * \param theEntryList the list of the created objects entries
230 void HYDROGUI_RecognizeContoursOp::onExternalOperationFinished(
231 const QString& theModuleName, const QString& theOperationName,
232 const QStringList& theEntryList )
234 // Process "Geometry" module operations only
235 if ( theModuleName != "Geometry" ) {
239 // Store the operation name
240 myGEOMOpName = theOperationName;
242 // Close the dialog corresponding to the external operation
243 closeExternalOperationDlg();
245 // Erase the GEOM objects
246 LightApp_Displayer().Erase( theEntryList );
248 // Add GEOM object entries to the list of temporary GEOM objects
249 myTmpGeomObjects << theEntryList;
251 if ( theEntryList.count() == 1 ) {
252 myGeomContourEntry = theEntryList.first();
254 // Update the list of polylines
255 updateRecognizedPolylines();
260 Close the GEOM contours detection dialog.
262 void HYDROGUI_RecognizeContoursOp::closeExternalOperationDlg()
264 if ( myGEOMOpName.isEmpty() ) {
268 SUIT_Desktop* aDesktop = module()->getApp()->desktop();
270 QList<QDialog*> aDialogs = aDesktop->findChildren<QDialog*>();
271 foreach ( QDialog* aDlg, aDialogs ) {
272 if ( typeid(*aDlg).name() == myGEOMOpName ) {
281 Update the list of recognized polylines by exploding the GEOM contour object.
283 void HYDROGUI_RecognizeContoursOp::updateRecognizedPolylines()
289 SalomeApp_Study* aStudy =
290 dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
295 // Explode the compound
296 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable( myGeomContourEntry ) ) );
298 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( aSObject->GetIOR().c_str() );
300 TopTools_SequenceOfShape aSubShapes;
301 HYDROData_ShapesTool::ExploreShapeToShapes( aShape, TopAbs_WIRE, aSubShapes );
302 if ( aSubShapes.Length() < 1 ) {
303 HYDROData_ShapesTool::ExploreShapeToShapes( aShape, TopAbs_EDGE, aSubShapes );
306 Handle(AIS_InteractiveContext) aCtx = NULL;
309 if ( !getPreviewManager() ) {
310 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
311 module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
314 OCCViewer_ViewManager* aViewManager = getPreviewManager();
315 if ( aViewManager ) {
316 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
317 aCtx = aViewer->getAISContext();
318 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
322 for ( int i = 1; i <= aSubShapes.Length(); i++ ) {
323 const TopoDS_Shape& aSubShape = aSubShapes.Value( i );
325 HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
326 aShape->setShape( aSubShape, true, false );
328 QString aPrefix = QString("%1_%2_%3").arg( myImage->GetName(), "Contour", QString::number( i ) );
329 QString aName = HYDROGUI_Tool::GenerateObjectName( module(), aPrefix, QStringList(), true );
330 myPolylineShapes.insert( aName, aShape);
333 if ( !aCtx.IsNull() ) { //@MZN
334 aCtx->UpdateCurrentViewer();
338 HYDROGUI_RecognizeContoursDlg* aPanel =
339 ::qobject_cast<HYDROGUI_RecognizeContoursDlg*>( inputPanel() );
341 aPanel->setPolylineNames( myPolylineShapes.keys() );
349 void HYDROGUI_RecognizeContoursOp::erasePreview()
351 foreach ( HYDROGUI_Shape* aShape, myPolylineShapes ) {
355 myPolylineShapes.clear();
359 Called when selection of the recognized polylines is changed.
361 void HYDROGUI_RecognizeContoursOp::onSelectionChanged( const QStringList& theSelectedNames )
363 foreach ( QString aName, myPolylineShapes.keys() ) {
364 bool isSelected = theSelectedNames.contains( aName );
365 myPolylineShapes[aName]->highlight( isSelected, true );
370 Called when selection in the viewer is changed.
372 void HYDROGUI_RecognizeContoursOp::onViewerSelectionChanged()
375 HYDROGUI_RecognizeContoursDlg* aPanel =
376 ::qobject_cast<HYDROGUI_RecognizeContoursDlg*>( inputPanel() );
381 Handle(AIS_InteractiveContext) aCtx = NULL;
383 OCCViewer_ViewManager* aViewManager = getPreviewManager();
384 if ( aViewManager ) {
385 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
386 aCtx = aViewer->getAISContext();
390 if ( !aCtx.IsNull() ) {
391 QStringList aSelectedNames;
393 foreach ( QString aName, myPolylineShapes.keys() ) {
394 bool isSelected = aCtx->IsSelected( myPolylineShapes[aName]->getAISObject() );
396 aSelectedNames << aName;
400 aPanel->setSelectedPolylineNames( aSelectedNames );
405 Do the operation data cleanup.
407 void HYDROGUI_RecognizeContoursOp::cleanup()
409 // Close the external operation dialog
410 closeExternalOperationDlg();
415 // Delete temporary image file
416 if ( myTmpImageFile ) {
417 delete myTmpImageFile;
418 myTmpImageFile = NULL;
421 // Delete temporary GEOM objects
422 if ( !myTmpGeomObjects.isEmpty() ) {
423 HYDROGUI_Tool::DeleteGeomObjects( module(), myTmpGeomObjects );
424 // update the object browser
425 module()->getApp()->updateObjectBrowser( true );