0028832: MMgt_TShared can be replaced by Standard_Transient
[occt.git] / src / ViewerTest / ViewerTest_EventManager.cxx
1 // Created on: 1998-08-27
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <AIS_InteractiveContext.hxx>
19 #include <Aspect_Grid.hxx>
20 #include <Standard_Type.hxx>
21 #include <V3d_View.hxx>
22 #include <ViewerTest_EventManager.hxx>
23
24 IMPLEMENT_STANDARD_RTTIEXT(ViewerTest_EventManager,Standard_Transient)
25
26 //=======================================================================
27 //function : ViewerTest_EventManager
28 //purpose  :
29 //=======================================================================
30 ViewerTest_EventManager::ViewerTest_EventManager (const Handle(V3d_View)&               theView,
31                                                   const Handle(AIS_InteractiveContext)& theCtx)
32 : myCtx  (theCtx),
33   myView (theView),
34   myX    (-1),
35   myY    (-1)
36 {}
37
38 //=======================================================================
39 //function : MoveTo
40 //purpose  :
41 //=======================================================================
42
43 void ViewerTest_EventManager::MoveTo (const Standard_Integer theXPix,
44                                       const Standard_Integer theYPix)
45 {
46   Standard_Real aPnt3d[3] = {0.0, 0.0, 0.0};
47   if (!myCtx.IsNull()
48    && !myView.IsNull())
49   {
50     const Standard_Boolean toEchoGrid = myView->Viewer()->Grid()->IsActive()
51                                      && myView->Viewer()->GridEcho();
52     switch (myCtx->MoveTo (theXPix, theYPix, myView, !toEchoGrid))
53     {
54       case AIS_SOD_Nothing:
55       {
56         if (toEchoGrid)
57         {
58           myView->ConvertToGrid (theXPix, theYPix, aPnt3d[0], aPnt3d[1], aPnt3d[2]);
59           myView->Viewer()->ShowGridEcho (myView, Graphic3d_Vertex (aPnt3d[0], aPnt3d[1], aPnt3d[2]));
60           myView->RedrawImmediate();
61         }
62         break;
63       }
64       default:
65       {
66         if (toEchoGrid)
67         {
68           myView->Viewer()->HideGridEcho (myView);
69           myView->RedrawImmediate();
70         }
71         break;
72       }
73     }
74   }
75
76   myX = theXPix;
77   myY = theYPix;
78 }
79
80 //=======================================================================
81 //function : Select
82 //purpose  :
83 //=======================================================================
84
85 void ViewerTest_EventManager::Select (const Standard_Integer theXPressed,
86                                       const Standard_Integer theYPressed,
87                                       const Standard_Integer theXMotion,
88                                       const Standard_Integer theYMotion,
89                                       const Standard_Boolean theIsAutoAllowOverlap)
90 {
91   #define IS_FULL_INCLUSION Standard_True
92   if (myView.IsNull()
93    || Abs (theXPressed - theXMotion) < 2
94    || Abs (theYPressed - theYMotion) < 2)
95   {
96     return;
97   }
98   else if (!myCtx.IsNull())
99   {
100     if (theIsAutoAllowOverlap)
101     {
102       if (theYPressed == Min (theYPressed, theYMotion))
103       {
104         myCtx->MainSelector()->AllowOverlapDetection (Standard_False);
105       }
106       else
107       {
108         myCtx->MainSelector()->AllowOverlapDetection (Standard_True);
109       }
110     }
111     myCtx->Select (Min (theXPressed, theXMotion),
112                    Min (theYPressed, theYMotion),
113                    Max (theXPressed, theXMotion),
114                    Max (theYPressed, theYMotion),
115                    myView,
116                    Standard_False);
117
118     // to restore default state of viewer selector
119     if (theIsAutoAllowOverlap)
120     {
121       myCtx->MainSelector()->AllowOverlapDetection (Standard_False);
122     }
123   }
124
125   myView->Redraw();
126 }
127
128 //=======================================================================
129 //function : ShiftSelect
130 //purpose  :
131 //=======================================================================
132
133 void ViewerTest_EventManager::ShiftSelect (const Standard_Integer theXPressed,
134                                            const Standard_Integer theYPressed,
135                                            const Standard_Integer theXMotion,
136                                            const Standard_Integer theYMotion,
137                                            const Standard_Boolean theIsAutoAllowOverlap)
138 {
139   if (myView.IsNull()
140    || Abs (theXPressed - theXMotion) < 2
141    || Abs (theYPressed - theYMotion) < 2)
142   {
143     return;
144   }
145   else if (!myCtx.IsNull())
146   {
147     if (theIsAutoAllowOverlap)
148     {
149       if (theYPressed == Min (theYPressed, theYMotion))
150       {
151         myCtx->MainSelector()->AllowOverlapDetection (Standard_False);
152       }
153       else
154       {
155         myCtx->MainSelector()->AllowOverlapDetection (Standard_True);
156       }
157     }
158     myCtx->ShiftSelect (Min (theXPressed, theXMotion),
159                         Min (theYPressed, theYMotion),
160                         Max (theXPressed, theXMotion),
161                         Max (theYPressed, theYMotion),
162                         myView,
163                         Standard_False);
164
165     // to restore default state of viewer selector
166     if (theIsAutoAllowOverlap)
167     {
168       myCtx->MainSelector()->AllowOverlapDetection (Standard_False);
169     }
170   }
171   myView->Redraw();
172 }
173
174 //=======================================================================
175 //function : Select
176 //purpose  :
177 //=======================================================================
178
179 void ViewerTest_EventManager::Select()
180 {
181   if (myView.IsNull())
182   {
183     return;
184   }
185   else if (!myCtx.IsNull())
186   {
187     myCtx->Select (Standard_False);
188   }
189
190   myView->Redraw();
191 }
192
193 //=======================================================================
194 //function : ShiftSelect
195 //purpose  :
196 //=======================================================================
197
198 void ViewerTest_EventManager::ShiftSelect()
199 {
200   if (myView.IsNull())
201   {
202     return;
203   }
204   else if (!myCtx.IsNull())
205   {
206     myCtx->ShiftSelect (Standard_False);
207   }
208
209   myView->Redraw();
210 }
211
212 //=======================================================================
213 //function : Select
214 //purpose  : Selection with polyline
215 //=======================================================================
216
217 void ViewerTest_EventManager::Select (const TColgp_Array1OfPnt2d& thePolyline)
218 {
219   if (myView.IsNull())
220   {
221     return;
222   }
223   else if (!myCtx.IsNull())
224   {
225     myCtx->Select (thePolyline, myView, Standard_False);
226   }
227
228   myView->Redraw();
229 }
230
231 //=======================================================================
232 //function : ShiftSelect
233 //purpose  : Selection with polyline without erasing of current selection
234 //=======================================================================
235
236 void ViewerTest_EventManager::ShiftSelect (const TColgp_Array1OfPnt2d& thePolyline)
237 {
238   if (myView.IsNull())
239   {
240     return;
241   }
242   else if (!myCtx.IsNull())
243   {
244     myCtx->ShiftSelect (thePolyline, myView, Standard_False);
245   }
246
247   myView->Redraw();
248 }
249
250 void ViewerTest_EventManager::GetCurrentPosition (Standard_Integer& theXPix, Standard_Integer& theYPix) const
251 {
252   theXPix = myX;
253   theYPix = myY;
254 }