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