Salome HOME
0022661: EDF GEOM: [HYDRO] Integration of the polyline editor in GEOM
[modules/geom.git] / src / EntityGUI / EntityGUI.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : EntityGUI.cxx
25 // Author : Damien COQUERET, Open CASCADE S.A.S.
26 //
27 #include "EntityGUI.h"
28
29 #include "GeometryGUI.h"
30 #include "GeometryGUI_Operations.h"
31 #include "GEOMUtils.hxx"
32
33 #include <LightApp_SelectionMgr.h>
34 #include <OCCViewer_ViewManager.h>
35 #include <OCCViewer_ViewModel.h>
36 #include <OCCViewer_ViewPort3d.h>
37 #include <OCCViewer_ViewWindow.h>
38 #include <SALOME_ListIteratorOfListIO.hxx>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_Session.h>
42 #include <SUIT_ViewWindow.h>
43 #include <SalomeApp_Application.h>
44 #include <SalomeApp_Study.h>
45
46 #include <TopoDS_Shape.hxx>
47 #include <TopoDS.hxx>
48 #include <BRep_Tool.hxx>
49 #include <ProjLib.hxx>
50 #include <ElSLib.hxx>
51
52 #include <QMouseEvent>
53 #include <QApplication>
54
55 #include "EntityGUI_SketcherDlg.h"        // Sketcher
56 #include "EntityGUI_3DSketcherDlg.h"      // Sketcher
57 #include "EntityGUI_IsolineDlg.h"         // Isoline
58 #include "EntityGUI_SubShapeDlg.h"        // Method SUBSHAPE
59 #include "EntityGUI_FeatureDetectorDlg.h" // Feature Detection
60 #include "EntityGUI_PictureImportDlg.h"   // Import Picture in viewer
61 #include "EntityGUI_FieldDlg.h"           // Create/Edit Field
62 #include "EntityGUI_PolylineDlg.h"        // Create/Edit 2d polyline
63
64 #include "GEOMImpl_Types.hxx"
65
66
67 //=======================================================================
68 // function : EntityGUI()
69 // purpose  : Constructor
70 //=======================================================================
71 EntityGUI::EntityGUI( GeometryGUI* parent ) :  GEOMGUI( parent )
72 {
73   mySimulationShape1 = new AIS_Shape( TopoDS_Shape() );
74   mySimulationShape2 = new AIS_Shape( TopoDS_Shape() );
75 }
76
77 //=======================================================================
78 // function : ~EntityGUI()
79 // purpose  : Destructor
80 //=======================================================================
81 EntityGUI::~EntityGUI()
82 {
83 }
84
85
86 //=======================================================================
87 // function : OnGUIEvent()
88 // purpose  : 
89 //=======================================================================
90 bool EntityGUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
91 {
92   SalomeApp_Application* app = getGeometryGUI()->getApp();
93   if ( !app ) return false;
94
95   getGeometryGUI()->EmitSignalDeactivateDialog();
96   QDialog* aDlg = NULL;
97
98   switch ( theCommandID ) {
99   case GEOMOp::Op2dSketcher: // 2D SKETCHER
100     getGeometryGUI()->ActiveWorkingPlane();
101     aDlg = new EntityGUI_SketcherDlg( getGeometryGUI(), parent );
102     break;
103   case GEOMOp::Op3dSketcher: // 3D SKETCHER
104     aDlg = new EntityGUI_3DSketcherDlg( getGeometryGUI(), parent );
105     break;
106   case GEOMOp::OpIsoline:    // ISOLINE
107     aDlg = new EntityGUI_IsolineDlg( getGeometryGUI(), parent );
108     break;
109   case GEOMOp::OpExplode:    // EXPLODE
110     aDlg = new EntityGUI_SubShapeDlg( getGeometryGUI(), parent );
111     break;
112 #ifdef WITH_OPENCV
113   case GEOMOp::OpFeatureDetect:    // FEATURE DETECTION
114     aDlg = new EntityGUI_FeatureDetectorDlg( getGeometryGUI(), parent );
115     break;
116 #endif
117   case GEOMOp::OpPictureImport:    // IMPORT PICTURE IN VIEWER
118     aDlg = new EntityGUI_PictureImportDlg( getGeometryGUI(), parent );
119     break;
120   case GEOMOp::OpCreateField: // CREATE FIELD
121     aDlg = new EntityGUI_FieldDlg (getGeometryGUI(), GEOM::GEOM_Field::_nil(), 0,
122                                    parent);
123     break;
124   case GEOMOp::OpEditField: // EDIT FIELD
125   case GEOMOp::OpEditFieldPopup:
126   {
127     SALOME_ListIO aList;
128     aList.Clear();
129
130     LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
131     if (aSelMgr)
132       aSelMgr->selectedObjects(aList);
133     SALOME_ListIteratorOfListIO anIter (aList);
134
135     GEOM::GEOM_Field_var     field;
136     GEOM::GEOM_FieldStep_var step;
137
138     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
139     if ( aList.Extent() > 0 && study ) {
140       for ( ; anIter.More(); anIter.Next() )
141       {
142         Handle(SALOME_InteractiveObject) anIObj = anIter.Value();
143         if ( !anIObj.IsNull() && anIObj->hasEntry() )
144           if ( _PTR(SObject) obj = study->studyDS()->FindObjectID( anIObj->getEntry() ))
145           {
146             CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( obj );
147             GEOM::GEOM_BaseObject_var bo = GEOM::GEOM_BaseObject::_narrow( corbaObj );
148             GEOM::GEOM_Field_var     f;
149             GEOM::GEOM_FieldStep_var s;
150             switch ( bo->GetType() ) {
151             case GEOM_FIELD:
152               f = GEOM::GEOM_Field::_narrow( corbaObj ); break;
153             case GEOM_FIELD_STEP:
154               step = GEOM::GEOM_FieldStep::_narrow( corbaObj );
155               if ( !step->_is_nil() )
156                 f = step->GetField();
157               break;
158             default:
159               continue;
160             }
161             if ( !f->_is_nil() )
162             {
163               if ( !field->_is_nil() && !f->_is_equivalent( field ))
164               {
165                 field = GEOM::GEOM_Field::_nil(); // several field selected
166                 break;
167               }
168               field = f;
169             }
170           }
171       }
172     }
173
174     if ( !field->_is_nil()) {
175       int stepID;
176       if ( !step->_is_nil() ) {
177         stepID = step->GetID();
178       }
179       else {
180         GEOM::ListOfLong_var stepIDs = field->GetSteps();
181         if ( stepIDs->length() > 0 )
182           stepID = stepIDs[0];
183         else
184           stepID = 0;
185       }
186       aDlg = new EntityGUI_FieldDlg (getGeometryGUI(), field, stepID, parent); 
187       break;
188     }
189     SUIT_MessageBox::warning(parent, tr("WRN_WARNING"), tr("NO_FIELD"));
190     break;
191   }
192   case GEOMOp::Op2dPolylineEditor: // POLYLINE EDITOR
193     getGeometryGUI()->ActiveWorkingPlane();
194     aDlg = new EntityGUI_PolylineDlg( getGeometryGUI(), parent );
195     break;
196   default:
197     app->putInfo( tr( "GEOM_PRP_COMMAND" ).arg( theCommandID ) );
198     break;
199   }
200   if ( aDlg )
201     aDlg->show();
202
203   return true;
204 }
205
206 //=================================================================================
207 // function : 0nMousePress()
208 // purpose  : [static] manage mouse events
209 //=================================================================================
210 bool EntityGUI::OnMousePress( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
211 {
212   QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
213
214   // Create Point dialog, OCC viewer 
215   if ( aDlg && 
216        theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
217        pe->modifiers() != Qt::ControlModifier ) {
218     
219     gp_Pnt aPnt;
220   
221     if ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_SketcherDlg" ) == 0 ) 
222     { 
223       EntityGUI_SketcherDlg* aSketcherDlg = (EntityGUI_SketcherDlg*) aDlg;
224       ((OCCViewer_ViewWindow*)theViewWindow)->setSketcherStyle(true);
225       if ( aSketcherDlg->acceptMouseEvent() ) {
226         OCCViewer_Viewer* anOCCViewer =
227           ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
228         Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
229     
230         ic->InitSelected();
231         if ( pe->modifiers() == Qt::ShiftModifier )
232           ic->ShiftSelect();  // Append selection
233         else
234           ic->Select();       // New selection
235
236         ic->InitSelected();
237         if ( ic->MoreSelected() ) {
238           TopoDS_Shape aShape = ic->SelectedShape();
239           if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
240             aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
241         }
242         else {
243           OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
244           aPnt = GEOMUtils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
245         }
246         
247         Qt::KeyboardModifiers modifiers = pe->modifiers();
248         aSketcherDlg->OnPointSelected( modifiers, aPnt );  // "feed" the point to point construction dialog
249       } // acceptMouseEvent()
250     }
251 #ifdef WITH_OPENCV
252     if (  QString( aDlg->metaObject()->className() ).compare( "EntityGUI_FeatureDetectorDlg" ) == 0 ) 
253     {
254       EntityGUI_FeatureDetectorDlg* aCornerDlg = (EntityGUI_FeatureDetectorDlg*) aDlg;
255       if ( aCornerDlg->acceptMouseEvent() ) {
256         OCCViewer_Viewer* anOCCViewer =
257           ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
258         Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();   
259
260         ic->InitSelected();
261         ic->Select();       // New selection
262
263         ic->InitSelected();
264         TopoDS_Shape aShape;
265         if ( ic->MoreSelected() ) 
266           aShape = ic->SelectedShape();
267         if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
268             aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
269         else 
270         {
271           OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
272           aPnt = GEOMUtils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
273         }
274         
275 //         aCornerDlg->OnPointSelected( aPnt );  // "feed" the point to corner detection dialog
276         
277 //         QPoint start = QPoint(pe->x(),pe->y());
278         aCornerDlg->setStartPnt( aPnt );
279       } // acceptMouseEvent()
280       
281     }
282 #endif
283   }
284
285   return false;
286 }
287
288 //=================================================================================
289 // function : 0nMouseRelease()
290 // purpose  : [static] manage mouse events
291 //=================================================================================
292 bool EntityGUI::OnMouseRelease( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
293 {
294   ((OCCViewer_ViewWindow*)theViewWindow)->setSketcherStyle(false);
295   QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
296 #ifdef WITH_OPENCV
297   if ( aDlg && ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_FeatureDetectorDlg" ) == 0 ) &&
298        theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
299        pe->modifiers() != Qt::ControlModifier ) 
300   {   
301     EntityGUI_FeatureDetectorDlg* aCornerDlg = (EntityGUI_FeatureDetectorDlg*) aDlg;
302    
303     gp_Pnt aPnt; 
304       
305     if ( aCornerDlg->acceptMouseEvent() )
306     {
307 //       QPoint end = QPoint(pe->x(),pe->y());
308       OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
309       aPnt = GEOMUtils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
310       aCornerDlg->setEndPnt( aPnt );
311     }    
312   }
313 #endif
314   return false;
315 }
316
317 //=================================================================================
318 // function : 0nMouseMove()
319 // purpose  : [static] manage mouse events
320 //=================================================================================
321 bool EntityGUI::OnMouseMove( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
322 {
323   QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
324   
325   if ( aDlg && QString( aDlg->metaObject()->className() ).compare( "EntityGUI_SketcherDlg" ) == 0 &&
326        theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) 
327   { 
328     EntityGUI_SketcherDlg* aSketcherDlg = (EntityGUI_SketcherDlg*) aDlg;
329     if ( aSketcherDlg->acceptMouseEvent() ) 
330     {    
331       OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
332       gp_Pnt aPnt = GEOMUtils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
333   
334       Qt::KeyboardModifiers modifiers = pe->modifiers();
335       if (QApplication::mouseButtons() == Qt::LeftButton )
336         aSketcherDlg->OnPointSelected( modifiers, aPnt, false );  // "feed" the point to point construction dialog
337     }
338   }
339     
340   return false;
341 }
342
343 //=====================================================================================
344 // function : DisplaySimulationShape() 
345 // purpose  : Displays 'this->mySimulationShape' a pure graphical shape from a TopoDS_Shape
346 //=====================================================================================
347 void EntityGUI::DisplaySimulationShape( const TopoDS_Shape& S1, const TopoDS_Shape& S2 ) 
348 {
349   MESSAGE("EntityGUI::DisplaySimulationShape")
350   SalomeApp_Application* app = getGeometryGUI()->getApp();
351   if ( !app ) return;
352
353   SUIT_ViewManager* aVM = app->desktop()->activeWindow()->getViewManager();
354   if ( aVM->getType() != OCCViewer_Viewer::Type() )
355     return;
356
357   OCCViewer_Viewer* v3d = ( (OCCViewer_ViewManager*)aVM )->getOCCViewer();
358   Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
359   try {
360     if ( !S1.IsNull() ) {
361       /* erase any previous */
362       ic->Erase( mySimulationShape1, Standard_True );
363       ic->ClearPrs( mySimulationShape1 );
364
365       mySimulationShape1 = new AIS_Shape( TopoDS_Shape() );
366       mySimulationShape1->Set( S1 );
367       mySimulationShape1->SetColor( Quantity_NOC_RED );
368
369       ic->Deactivate( mySimulationShape1 );
370       ic->Display( mySimulationShape1, Standard_False );
371       mySimulationShape1->UnsetColor();
372     }
373     if ( !S2.IsNull() ) {
374       ic->Erase( mySimulationShape2, Standard_True );
375       ic->ClearPrs( mySimulationShape2 );
376
377       mySimulationShape2 = new AIS_Shape( TopoDS_Shape() );
378       mySimulationShape2->Set( S2 );
379       mySimulationShape2->SetColor( Quantity_NOC_VIOLET );
380
381       ic->Deactivate( mySimulationShape2 );
382       ic->Display( mySimulationShape2, Standard_False );
383       mySimulationShape2->UnsetColor();
384     }
385     ic->UpdateCurrentViewer();
386   }
387   catch( Standard_Failure ) {
388     MESSAGE( "Exception catched in EntityGUI::DisplaySimulationShape" );
389   } 
390 }
391
392 //==================================================================================
393 // function : EraseSimulationShape()
394 // purpose  : Clears the display of 'mySimulationShape' a pure graphical shape
395 //==================================================================================
396 void EntityGUI::EraseSimulationShape()
397 {
398   MESSAGE("EntityGUI::EraseSimulationShape")
399   SalomeApp_Application* app = getGeometryGUI()->getApp();
400   if ( !app ) return;
401
402   // get all view windows at the desktop
403   QList<SUIT_ViewWindow*> aWndLst = app->desktop()->windows();
404   //get all view windows, which belong to the active study
405   QList<SUIT_ViewWindow*> aWndLstAS;
406   SUIT_ViewWindow* vw;
407
408   QListIterator<SUIT_ViewWindow*> itWL( aWndLst );
409   while ( itWL.hasNext() && ( vw = itWL.next() ) )
410     if ( vw->getViewManager()->study() == app->activeStudy() )
411       aWndLstAS.append( vw );
412
413   QListIterator<SUIT_ViewWindow*> itWLAS( aWndLstAS );
414   while ( itWLAS.hasNext() && ( vw = itWLAS.next() ) ) {
415     if ( vw->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
416       OCCViewer_Viewer* v3d = ( (OCCViewer_ViewManager*)( vw->getViewManager() ) )->getOCCViewer();
417       Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
418       ic->Erase( mySimulationShape1, Standard_True );
419       ic->Erase( mySimulationShape2, Standard_True );
420       ic->ClearPrs( mySimulationShape1 );
421       ic->ClearPrs( mySimulationShape2 );
422       ic->UpdateCurrentViewer();
423     } 
424   }
425 }
426
427 //=====================================================================================
428 // function : SObjectExist()
429 // purpose  :
430 //=====================================================================================
431 bool EntityGUI::SObjectExist( const _PTR(SObject)& theFatherObject, const char* IOR )
432 {
433   SalomeApp_Application* app = getGeometryGUI()->getApp();
434   if ( !app ) return false;
435   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
436   if ( !appStudy ) return false;
437
438   _PTR(Study) aStudy = appStudy->studyDS();
439   _PTR(ChildIterator) it ( aStudy->NewChildIterator( theFatherObject ) );
440   _PTR(SObject) RefSO;
441   _PTR(GenericAttribute) anAttr;
442   for ( ; it->More();it->Next() ) {
443     _PTR(SObject) SO ( it->Value() );
444     if ( SO->FindAttribute( anAttr, "AttributeIOR" ) ) {
445       _PTR(AttributeIOR) anIOR ( anAttr  );
446       if ( strcmp( anIOR->Value().c_str(), IOR ) == 0 )
447         return true;
448     }
449     if ( SO->ReferencedObject( RefSO ) ) {
450       if ( RefSO->FindAttribute( anAttr, "AttributeIOR" ) ) {
451         _PTR(AttributeIOR) anIOR ( anAttr );
452         if ( strcmp( anIOR->Value().c_str(), IOR ) == 0 )
453           return true;
454       }
455     }
456   }
457   return false;
458 }
459
460
461 //=====================================================================================
462 // EXPORTED METHODS
463 //=====================================================================================
464 extern "C"
465 {
466 #ifdef WIN32
467   __declspec( dllexport )
468 #endif
469   GEOMGUI* GetLibGUI( GeometryGUI* parent )
470   {
471     return new EntityGUI( parent );
472   }
473 }