Salome HOME
Update mail address
[modules/geom.git] / src / BasicGUI / BasicGUI.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : BasicGUI.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header$
28
29 #include "BasicGUI.h"
30 #include "GeometryGUI.h"
31
32 #include "SUIT_Session.h"
33 #include "SUIT_Desktop.h"
34 #include "SUIT_ViewWindow.h"
35 #include "OCCViewer_ViewWindow.h"
36 #include "OCCViewer_ViewModel.h"
37 #include "OCCViewer_ViewManager.h"
38 #include "OCCViewer_ViewPort3d.h"
39 #include "utilities.h"
40
41 #include <Precision.hxx>
42 #include <BRep_Tool.hxx>
43 #include <ProjLib.hxx>
44 #include <ElSLib.hxx>
45 #include <TopoDS.hxx>
46
47 #include "BasicGUI_PointDlg.h"        // Method POINT
48 #include "BasicGUI_LineDlg.h"         // Method LINE
49 #include "BasicGUI_CircleDlg.h"       // Method CIRCLE
50 #include "BasicGUI_EllipseDlg.h"      // Method ELLIPSE
51 #include "BasicGUI_ArcDlg.h"          // Method ARC
52 #include "BasicGUI_CurveDlg.h"        // Method CURVE
53 #include "BasicGUI_VectorDlg.h"       // Method VECTOR
54 #include "BasicGUI_PlaneDlg.h"        // Method PLANE
55 #include "BasicGUI_WorkingPlaneDlg.h" // Method WORKING PLANE
56 #include "BasicGUI_MarkerDlg.h"       // Method REPAIR
57
58 using namespace std;
59
60 BasicGUI* BasicGUI::myGUIObject = 0;
61
62 //=======================================================================
63 // function : GetBasicGUI()
64 // purpose  : Get the only BasicGUI object [ static ]
65 //=======================================================================
66 BasicGUI* BasicGUI::GetBasicGUI( GeometryGUI* parent )
67 {
68   if ( myGUIObject == 0 ) {
69     myGUIObject = new BasicGUI( parent );
70   }
71   return myGUIObject;
72 }
73
74 //=======================================================================
75 // function : BasicGUI()
76 // purpose  : Constructor
77 //=======================================================================
78 BasicGUI::BasicGUI( GeometryGUI* parent ) :
79   GEOMGUI(parent)
80 {
81 }
82
83
84 //=======================================================================
85 // function : ~BasicGUI()
86 // purpose  : Destructor
87 //=======================================================================
88 BasicGUI::~BasicGUI()
89 {
90 }
91
92
93 //=======================================================================
94 // function : OnGUIEvent()
95 // purpose  : 
96 //=======================================================================
97 bool BasicGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
98 {
99   getGeometryGUI()->EmitSignalDeactivateDialog();
100   QDialog* aDlg = NULL;
101
102   switch ( theCommandID )
103     {
104     case 4011: // POINT
105       aDlg = new BasicGUI_PointDlg( getGeometryGUI(), parent, "" ); 
106       break;
107     case 4012:  // LINE
108       aDlg = new BasicGUI_LineDlg( getGeometryGUI(), parent, "" );
109       break;
110     case 4013:  // CIRCLE
111       aDlg = new BasicGUI_CircleDlg( getGeometryGUI(), parent, "");
112       break;
113     case 4014:  // ELLIPSE
114       aDlg = new BasicGUI_EllipseDlg( getGeometryGUI(), parent, "" );
115       break;
116     case 4015:  // ARC
117       aDlg = new BasicGUI_ArcDlg( getGeometryGUI(), parent, "" );
118       break ;
119     case 4016: // VECTOR
120       aDlg = new BasicGUI_VectorDlg( getGeometryGUI(), parent, "" );
121       break;
122     case 4017: // PLANE
123       aDlg = new BasicGUI_PlaneDlg( getGeometryGUI(), parent, "");
124       break;
125     case 4018: // WORKING PLANE
126       aDlg = new BasicGUI_WorkingPlaneDlg( getGeometryGUI(), parent, "" );
127       break;
128     case 4019: // CURVE
129       aDlg = new BasicGUI_CurveDlg( getGeometryGUI(), parent, "" );
130       break;
131     case 4020: // REPAIR
132       aDlg = new BasicGUI_MarkerDlg( getGeometryGUI(), parent );
133       break;      
134     default:
135       SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
136       break;
137     }
138
139   if ( aDlg != NULL )
140     aDlg->show();
141
142   return true;
143 }
144
145
146 //=================================================================================
147 // function : 0nMousePress()
148 // purpose  : [static] manage mouse events
149 //=================================================================================
150 bool BasicGUI::OnMousePress( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
151 {
152   QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
153
154   // Create Point dialog, OCC viewer 
155   if ( aDlg && aDlg->isA( "BasicGUI_PointDlg" ) && theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&  pe->state() != Qt::ControlButton )
156   {
157     BasicGUI_PointDlg* aPntDlg = (BasicGUI_PointDlg*) aDlg;
158     if ( aPntDlg->acceptMouseEvent() )
159       {
160         OCCViewer_Viewer* anOCCViewer = ((OCCViewer_ViewManager*)(theViewWindow->getViewManager()))->getOCCViewer();
161         Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
162         
163         gp_Pnt aPnt;    
164
165         ic->InitSelected();
166         if( pe->state() == Qt::ShiftButton )
167           ic->ShiftSelect();  // Append selection
168         else
169           ic->Select();       // New selection
170
171         ic->InitSelected();
172         if( ic->MoreSelected() )
173           {
174             TopoDS_Shape aShape = ic->SelectedShape();
175             if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
176               aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
177           }
178         else
179           {
180             OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
181             aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
182           }
183
184         aPntDlg->OnPointSelected( aPnt );  // "feed" the point to point construction dialog
185       } // acceptMouseEvent()
186   } 
187   return false;
188 }
189
190
191 //=======================================================================
192 // function : ConvertClickToPoint()
193 // purpose  : Returns the point clicked in 3D view
194 //=======================================================================
195 gp_Pnt BasicGUI::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView)
196 {
197   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
198   aView->Eye(XEye, YEye, ZEye);
199
200   aView->At(XAt, YAt, ZAt);
201   gp_Pnt EyePoint(XEye, YEye, ZEye);
202   gp_Pnt AtPoint(XAt, YAt, ZAt);
203
204   gp_Vec EyeVector(EyePoint, AtPoint);
205   gp_Dir EyeDir(EyeVector);
206
207   gp_Pln PlaneOfTheView = gp_Pln(AtPoint,EyeDir);
208   Standard_Real X, Y, Z;
209   aView->Convert(x, y, X, Y, Z);
210   gp_Pnt ConvertedPoint(X, Y, Z);
211
212   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
213   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
214   return ResultPoint;
215 }
216
217 //=====================================================================================
218 // EXPORTED METHODS
219 //=====================================================================================
220 extern "C"
221 {
222 #ifdef WNT
223         __declspec( dllexport )
224 #endif
225   GEOMGUI* GetLibGUI( GeometryGUI* parent )
226   {
227     return BasicGUI::GetBasicGUI( parent );
228   }
229 }