Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / BasicGUI / BasicGUI.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   : BasicGUI.cxx
25 // Author : Damien COQUERET, Open CASCADE S.A.S.
26 //
27 #include "BasicGUI.h"
28
29 #include "BasicGUI_PointDlg.h"        // Method POINT
30 #include "BasicGUI_LineDlg.h"         // Method LINE
31 #include "BasicGUI_CircleDlg.h"       // Method CIRCLE
32 #include "BasicGUI_EllipseDlg.h"      // Method ELLIPSE
33 #include "BasicGUI_ArcDlg.h"          // Method ARC
34 #include "BasicGUI_CurveDlg.h"        // Method CURVE
35 #include "BasicGUI_VectorDlg.h"       // Method VECTOR
36 #include "BasicGUI_PlaneDlg.h"        // Method PLANE
37 #include "BasicGUI_MarkerDlg.h"       // Method LOCAL COORDINATE SYSTEM
38
39 #include <GeometryGUI.h>
40 #include "GeometryGUI_Operations.h"
41
42 #include <SUIT_Session.h>
43 #include <SUIT_Desktop.h>
44 #include <SUIT_ViewWindow.h>
45 #include <OCCViewer_ViewWindow.h>
46 #include <OCCViewer_ViewModel.h>
47 #include <OCCViewer_ViewManager.h>
48 #include <OCCViewer_ViewPort3d.h>
49
50 #include <BRep_Tool.hxx>
51 #include <ProjLib.hxx>
52 #include <ElSLib.hxx>
53 #include <TopoDS.hxx>
54
55 #include <QMouseEvent>
56
57 //=======================================================================
58 // function : BasicGUI()
59 // purpose  : Constructor
60 //=======================================================================
61 BasicGUI::BasicGUI( GeometryGUI* parent )
62   : GEOMGUI( parent )
63 {
64 }
65
66 //=======================================================================
67 // function : ~BasicGUI()
68 // purpose  : Destructor
69 //=======================================================================
70 BasicGUI::~BasicGUI()
71 {
72 }
73
74
75 //=======================================================================
76 // function : OnGUIEvent()
77 // purpose  : 
78 //=======================================================================
79 bool BasicGUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
80 {
81   getGeometryGUI()->EmitSignalDeactivateDialog();
82   QDialog* aDlg = NULL;
83
84   switch ( theCommandID ) {
85   case GEOMOp::OpPoint:    // POINT
86     aDlg = new BasicGUI_PointDlg( getGeometryGUI(), parent ); 
87     break;
88   case GEOMOp::OpLine:     // LINE
89     aDlg = new BasicGUI_LineDlg( getGeometryGUI(), parent );
90     break;
91   case GEOMOp::OpCircle:   // CIRCLE
92     aDlg = new BasicGUI_CircleDlg( getGeometryGUI(), parent );
93     break;
94   case GEOMOp::OpEllipse:  // ELLIPSE
95     aDlg = new BasicGUI_EllipseDlg( getGeometryGUI(), parent );
96     break;
97   case GEOMOp::OpArc:      // ARC
98     aDlg = new BasicGUI_ArcDlg( getGeometryGUI(), parent );
99     break ;
100   case GEOMOp::OpVector:   // VECTOR
101     aDlg = new BasicGUI_VectorDlg( getGeometryGUI(), parent );
102     break;
103   case GEOMOp::OpPlane:    // PLANE
104     aDlg = new BasicGUI_PlaneDlg( getGeometryGUI(), parent );
105     break;
106   case GEOMOp::OpCurve:    // CURVE
107     aDlg = new BasicGUI_CurveDlg( getGeometryGUI(), parent );
108     break;
109   case GEOMOp::OpLCS:      // LOCAL COORDINATE SYSTEM
110     aDlg = new BasicGUI_MarkerDlg( getGeometryGUI(), parent );
111     break;      
112   default:
113     SUIT_Session::session()->activeApplication()->putInfo( tr( "GEOM_PRP_COMMAND" ).arg( theCommandID ) );
114     break;
115   }
116   
117   if ( aDlg != NULL )
118     aDlg->show();
119   
120   return true;
121 }
122
123
124 //=================================================================================
125 // function : 0nMousePress()
126 // purpose  : [static] manage mouse events
127 //=================================================================================
128 bool BasicGUI::OnMousePress( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
129 {
130   QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
131
132   // Create Point dialog, OCC viewer 
133   if ( aDlg && ( QString( aDlg->metaObject()->className() ).compare( "BasicGUI_PointDlg" ) == 0 ) &&
134        theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
135        pe->modifiers() != Qt::ControlModifier ) {
136     BasicGUI_PointDlg* aPntDlg = (BasicGUI_PointDlg*) aDlg;
137     if ( aPntDlg->acceptMouseEvent() ) {
138       OCCViewer_Viewer* anOCCViewer =
139         ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
140       Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
141
142       gp_Pnt aPnt;    
143
144       ic->InitSelected();
145       if ( pe->modifiers() == Qt::ShiftModifier )
146         ic->ShiftSelect();  // Append selection
147       else
148         ic->Select();       // New selection
149
150       ic->InitSelected();
151       if ( ic->MoreSelected() ) {
152         TopoDS_Shape aShape = ic->SelectedShape();
153         if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
154           aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
155       }
156       else {
157         OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
158         aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
159       }
160
161       aPntDlg->OnPointSelected( aPnt );  // "feed" the point to point construction dialog
162     } // acceptMouseEvent()
163   }
164   return false;
165 }
166
167
168 //=======================================================================
169 // function : ConvertClickToPoint()
170 // purpose  : Returns the point clicked in 3D view
171 //=======================================================================
172 gp_Pnt BasicGUI::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
173 {
174   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
175   aView->Eye( XEye, YEye, ZEye );
176
177   aView->At( XAt, YAt, ZAt );
178   gp_Pnt EyePoint( XEye, YEye, ZEye );
179   gp_Pnt AtPoint( XAt, YAt, ZAt );
180
181   gp_Vec EyeVector( EyePoint, AtPoint );
182   gp_Dir EyeDir( EyeVector );
183
184   gp_Pln PlaneOfTheView = gp_Pln( AtPoint, EyeDir );
185   Standard_Real X, Y, Z;
186   aView->Convert( x, y, X, Y, Z );
187   gp_Pnt ConvertedPoint( X, Y, Z );
188
189   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project( PlaneOfTheView, ConvertedPoint );
190   gp_Pnt ResultPoint = ElSLib::Value( ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView );
191   return ResultPoint;
192 }
193
194 //=====================================================================================
195 // EXPORTED METHODS
196 //=====================================================================================
197 extern "C"
198 {
199 #ifdef WIN32
200   __declspec( dllexport )
201 #endif
202   GEOMGUI* GetLibGUI( GeometryGUI* parent )
203   {
204     return new BasicGUI( parent );
205   }
206 }