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