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