Salome HOME
Fix for PAL8309(now, if the ctrl key is pressed, the input coordinates function(by...
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : BasicGUI.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "BasicGUI.h"
31 #include "GeometryGUI.h"
32
33 #include "QAD_RightFrame.h"
34 #include "QAD_Desktop.h"
35 #include "OCCViewer_Viewer3d.h"
36 #include "OCCViewer_ViewFrame.h"
37 #include "OCCViewer_AISSelector.h"
38 #include "OCCViewer_ViewPort3d.h"
39 #include "SALOMEGUI_QtCatchCorbaException.hxx"
40
41 #include "utilities.h"
42
43 #include <Precision.hxx>
44 #include <BRep_Tool.hxx>
45 #include <ProjLib.hxx>
46 #include <ElSLib.hxx>
47 #include <TopoDS.hxx>
48
49 #include "BasicGUI_PointDlg.h"        // Method POINT
50 #include "BasicGUI_LineDlg.h"         // Method LINE
51 #include "BasicGUI_CircleDlg.h"       // Method CIRCLE
52 #include "BasicGUI_EllipseDlg.h"      // Method ELLIPSE
53 #include "BasicGUI_ArcDlg.h"          // Method ARC
54 #include "BasicGUI_CurveDlg.h"        // Method CURVE
55 #include "BasicGUI_VectorDlg.h"       // Method VECTOR
56 #include "BasicGUI_PlaneDlg.h"        // Method PLANE
57 #include "BasicGUI_WorkingPlaneDlg.h" // Method WORKING PLANE
58 #include "BasicGUI_MarkerDlg.h"       // Method REPAIR
59
60 BasicGUI* BasicGUI::myGUIObject = 0;
61
62 //=======================================================================
63 // function : GetBasicGUI()
64 // purpose  : Get the only BasicGUI object [ static ]
65 //=======================================================================
66 BasicGUI* BasicGUI::GetBasicGUI()
67 {
68   if ( myGUIObject == 0 ) {
69     myGUIObject = new BasicGUI();
70   }
71   return myGUIObject;
72 }
73
74 //=======================================================================
75 // function : BasicGUI()
76 // purpose  : Constructor
77 //=======================================================================
78 BasicGUI::BasicGUI() :
79   GEOMGUI()
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, QAD_Desktop* parent)
98 {
99   GeometryGUI::GetGeomGUI()->EmitSignalDeactivateDialog();
100   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
101   QDialog* aDlg = NULL;
102
103   switch ( theCommandID )
104     {
105     case 4011: // POINT
106       {
107         Handle(AIS_InteractiveContext) ic;
108         QAD_StudyFrame* aFrame = QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame();
109         if ( aFrame->getTypeView() == VIEW_OCC) 
110         {
111           OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)aFrame->getRightFrame()->getViewFrame())->getViewer();
112           ic = v3d->getAISContext();
113         }
114         aDlg = new BasicGUI_PointDlg( parent, "", Sel ); 
115         break;
116       }
117     case 4012:  // LINE
118       aDlg = new BasicGUI_LineDlg(parent, "",  Sel);
119       break;
120     case 4013:  // CIRCLE
121       aDlg = new BasicGUI_CircleDlg(parent, "", Sel);
122       break;
123     case 4014:  // ELLIPSE
124       aDlg = new BasicGUI_EllipseDlg(parent, "", Sel);
125       break;
126     case 4015:  // ARC
127       aDlg = new BasicGUI_ArcDlg(parent, "", Sel);
128       break ;
129     case 4016: // VECTOR
130       aDlg = new BasicGUI_VectorDlg(parent, "", Sel);
131       break;
132     case 4017: // PLANE
133       aDlg = new BasicGUI_PlaneDlg(parent, "", Sel);
134       break;
135     case 4018: // WORKING PLANE
136       aDlg = new BasicGUI_WorkingPlaneDlg(parent, "", Sel);
137       break;
138     case 4019: // CURVE
139       aDlg = new BasicGUI_CurveDlg( parent, "", Sel );
140       break;
141     case 4020: // REPAIR
142       aDlg = new BasicGUI_MarkerDlg( parent, Sel );
143       break;      
144     default:
145       parent->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
146       break;
147     }
148
149   if ( aDlg != NULL )
150     aDlg->show();
151
152   return true;
153 }
154
155
156 //=================================================================================
157 // function : 0nMousePress()
158 // purpose  : [static] manage mouse events
159 //=================================================================================
160 bool BasicGUI::OnMousePress( QMouseEvent* pe, QAD_Desktop* parent, QAD_StudyFrame* theFrame )
161 {
162   QDialog* aDlg = GeometryGUI::GetGeomGUI()->GetActiveDialogBox();
163
164   // Create Point dialog, OCC viewer 
165   if ( aDlg && aDlg->isA( "BasicGUI_PointDlg" ) && theFrame->getTypeView() == VIEW_OCC &&  pe->state() != Qt::ControlButton )
166   {
167     BasicGUI_PointDlg* aPntDlg = (BasicGUI_PointDlg*) aDlg;
168     if ( aPntDlg->acceptMouseEvent() )
169                 {
170         OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)theFrame->getRightFrame()->getViewFrame())->getViewer();
171         Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
172       
173         gp_Pnt aPnt;    
174
175         ic->InitSelected();
176         if( pe->state() == Qt::ShiftButton )
177         v3d->getAISSelector()->shiftSelect();  // Append selection
178         else
179         v3d->getAISSelector()->select();       // New selection
180
181         ic->InitSelected();
182         if( ic->MoreSelected() )
183         {
184         TopoDS_Shape aShape = ic->SelectedShape();
185         if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
186                 aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
187         }
188         else
189         {
190                 OCCViewer_ViewPort* vp = ((OCCViewer_ViewFrame*)theFrame->getRightFrame()->getViewFrame())->getViewPort();
191         aPnt = ConvertClickToPoint( pe->x(), pe->y(), ((OCCViewer_ViewPort3d*)vp)->getView() );
192         }
193
194         aPntDlg->OnPointSelected( aPnt );  // "feed" the point to point construction dialog
195     } // acceptMouseEvent()
196   } 
197   return false;
198 }
199
200
201 //=======================================================================
202 // function : ConvertClickToPoint()
203 // purpose  : Returns the point clicked in 3D view
204 //=======================================================================
205 gp_Pnt BasicGUI::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView)
206 {
207   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
208   aView->Eye(XEye, YEye, ZEye);
209
210   aView->At(XAt, YAt, ZAt);
211   gp_Pnt EyePoint(XEye, YEye, ZEye);
212   gp_Pnt AtPoint(XAt, YAt, ZAt);
213
214   gp_Vec EyeVector(EyePoint, AtPoint);
215   gp_Dir EyeDir(EyeVector);
216
217   gp_Pln PlaneOfTheView = gp_Pln(AtPoint,EyeDir);
218   Standard_Real X, Y, Z;
219   aView->Convert(x, y, X, Y, Z);
220   gp_Pnt ConvertedPoint(X, Y, Z);
221
222   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
223   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
224   return ResultPoint;
225 }
226
227 //=====================================================================================
228 // EXPORTED METHODS
229 //=====================================================================================
230 extern "C"
231 {
232   GEOMGUI* GetLibGUI()
233   {
234     return BasicGUI::GetBasicGUI();
235   }
236 }