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