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