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