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