0023821: Improve qmake project files for Qt samples
[occt.git] / samples / mfc / standard / 01_Geometry / src / GeometryView.cpp
CommitLineData
7fd59977 1// GeometryView.cpp : implementation of the CGeometryView class
2//
3
4#include "stdafx.h"
5#include "GeometryApp.h"
6
7#include <GeometryApp.h>
8#include "GeometryDoc.h"
9#include "GeometryView.h"
10
11#define ValZWMin 1
12
13#ifdef _DEBUG
14#undef THIS_FILE
15static char THIS_FILE[] = __FILE__;
16#endif
17
18/////////////////////////////////////////////////////////////////////////////
19// CGeometryView
20
21IMPLEMENT_DYNCREATE(CGeometryView, OCC_3dView)
22
23BEGIN_MESSAGE_MAP(CGeometryView, OCC_3dView)
24 //{{AFX_MSG_MAP(CGeometryView)
25 ON_COMMAND(ID_FILE_EXPORT_IMAGE, OnFileExportImage)
26 ON_WM_SIZE()
27 ON_WM_LBUTTONDOWN()
28 ON_WM_LBUTTONUP()
29 ON_WM_MBUTTONDOWN()
30 ON_WM_MBUTTONUP()
31 ON_WM_MOUSEMOVE()
32 ON_WM_RBUTTONDOWN()
33 ON_WM_RBUTTONUP()
34 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOff, OnUpdateBUTTONHlrOff)
35 ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOn, OnUpdateBUTTONHlrOn)
36 ON_UPDATE_COMMAND_UI(ID_BUTTONPanGlo, OnUpdateBUTTONPanGlo)
37 ON_UPDATE_COMMAND_UI(ID_BUTTONPan, OnUpdateBUTTONPan)
38 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomProg, OnUpdateBUTTONZoomProg)
39 ON_UPDATE_COMMAND_UI(ID_BUTTONZoomWin, OnUpdateBUTTONZoomWin)
40 ON_UPDATE_COMMAND_UI(ID_BUTTONRot, OnUpdateBUTTONRot)
41 //}}AFX_MSG_MAP
42END_MESSAGE_MAP()
43
44/////////////////////////////////////////////////////////////////////////////
45// CGeometryView construction/destruction
46
47CGeometryView::CGeometryView()
48{
49}
50
51CGeometryView::~CGeometryView()
52{
53}
54
55/////////////////////////////////////////////////////////////////////////////
56// CGeometryView diagnostics
57
58#ifdef _DEBUG
59void CGeometryView::AssertValid() const
60{
61 CView::AssertValid();
62}
63
64void CGeometryView::Dump(CDumpContext& dc) const
65{
66 CView::Dump(dc);
67}
68
69CGeometryDoc* CGeometryView::GetDocument() // non-debug version is inline
70{
71 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGeometryDoc)));
72 return (CGeometryDoc*)m_pDocument;
73}
74#endif //_DEBUG
75
76/////////////////////////////////////////////////////////////////////////////
77// CGeometryView message handlers
78
79
80void CGeometryView::OnFileExportImage()
81{
82 CFileDialog dlg(FALSE,_T("*.BMP"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
3994ec41 83 _T("BMP Files (*.BMP)|*.bmp |GIF Files (*.GIF)|*.gif | PNG Files (*.PNG)|*.png"
84 "|JPEG Files (*.JPEG)|*.jpeg | PPM Files (*.PPM)|*.ppm | TIFF Files (*.TIFF)"
85 "|*.tiff | TGA Files (*.TGA)|*.tga | EXR Files (*.EXR)|*.exr||"),
7fd59977 86 NULL );
87
88 if (dlg.DoModal() == IDOK)
89 {
90 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
3994ec41 91 CString aFileName = dlg.GetPathName();
92 myView->Dump(aFileName);
7fd59977 93 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
94 }
95}
96
97void CGeometryView::OnSize(UINT nType, int cx, int cy)
98{
99 if (!myView.IsNull())
100 myView->MustBeResized();
101}
102
103void CGeometryView::OnLButtonDown(UINT nFlags, CPoint point)
104{
105 // save the current mouse coordinate in min
106 myXmin=point.x; myYmin=point.y;
107 myXmax=point.x; myYmax=point.y;
108
109 if ( nFlags & MK_CONTROL )
110 {
111 // Button MB1 down Control :start zomming
112 // SetCursor(AfxGetApp()->LoadStandardCursor());
113 }
114 else // if ( Ctrl )
115 {
116 switch (myCurrentMode)
117 {
118 case CurAction3d_Nothing : // start a drag
119 if (nFlags & MK_SHIFT)
120 ShiftDragEvent(myXmax,myYmax,-1);
121 else
122 DragEvent(myXmax,myYmax,-1);
123 break;
124 break;
125 case CurAction3d_DynamicZooming : // noting
126 // SetCursor(AfxGetApp()->LoadStandardCursor());
127 break;
128 case CurAction3d_WindowZooming :
129 break;
130 case CurAction3d_DynamicPanning :// noting
131 // SetCursor(AfxGetApp()->LoadStandardCursor());
132 break;
133 case CurAction3d_GlobalPanning :// noting
134 // SetCursor(AfxGetApp()->LoadStandardCursor());
135 break;
136 case CurAction3d_DynamicRotation :
137 // SetCursor(AfxGetApp()->LoadStandardCursor());
138 if (!myDegenerateModeIsOn)
139 myView->SetDegenerateModeOn();
140 myView->StartRotation(point.x,point.y);
141 break;
142 default :
143 Standard_Failure::Raise(" incompatible Current Mode ");
144 break;
145 }
146 }
147}
148
149void CGeometryView::OnLButtonUp(UINT nFlags, CPoint point)
150{
151 if ( nFlags & MK_CONTROL )
152 {
153 return;
154 }
155 else // if ( Ctrl )
156 {
157 switch (myCurrentMode)
158 {
159 case CurAction3d_Nothing :
160 if (point.x == myXmin && point.y == myYmin)
161 { // no offset between down and up --> selectEvent
162 myXmax=point.x;
163 myYmax=point.y;
164 if (nFlags & MK_SHIFT )
165 ShiftInputEvent(point.x,point.y);
166 else
167 InputEvent (point.x,point.y);
168 } else
169 {
170 myXmax=point.x; myYmax=point.y;
171 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
172 if (nFlags & MK_SHIFT)
173 ShiftDragEvent(point.x,point.y,1);
174 else
175 DragEvent(point.x,point.y,1);
176 }
177 break;
178 case CurAction3d_DynamicZooming :
179 // SetCursor(AfxGetApp()->LoadStandardCursor());
180 myCurrentMode = CurAction3d_Nothing;
181 break;
182 case CurAction3d_WindowZooming :
183 myXmax=point.x; myYmax=point.y;
184 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
185 if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
186 // Test if the zoom window is greater than a minimale window.
187 {
188 // Do the zoom window between Pmin and Pmax
189 myView->WindowFitAll(myXmin,myYmin,myXmax,myYmax);
190 }
191 myCurrentMode = CurAction3d_Nothing;
192 break;
193 case CurAction3d_DynamicPanning :
194 myCurrentMode = CurAction3d_Nothing;
195 break;
196 case CurAction3d_GlobalPanning :
197 myView->Place(point.x,point.y,myCurZoom);
198 myCurrentMode = CurAction3d_Nothing;
199 break;
200 case CurAction3d_DynamicRotation :
201 myCurrentMode = CurAction3d_Nothing;
202 break;
203 default :
204 Standard_Failure::Raise(" incompatible Current Mode ");
205 break;
206 } //switch (myCurrentMode)
207 } // else // if ( Ctrl )
208}
209
210void CGeometryView::OnMButtonDown(UINT nFlags, CPoint point)
211{
212 if ( nFlags & MK_CONTROL )
213 {
214 // Button MB2 down Control : panning init
215 // SetCursor(AfxGetApp()->LoadStandardCursor());
216 }
217}
218
219void CGeometryView::OnMButtonUp(UINT nFlags, CPoint point)
220{
221 if ( nFlags & MK_CONTROL )
222 {
223 // Button MB2 down Control : panning init
224 // SetCursor(AfxGetApp()->LoadStandardCursor());
225 }
226}
227
228void CGeometryView::OnRButtonDown(UINT nFlags, CPoint point)
229{
230 if ( nFlags & MK_CONTROL )
231 {
232 // SetCursor(AfxGetApp()->LoadStandardCursor());
233 if (!myDegenerateModeIsOn)
234 myView->SetDegenerateModeOn();
235 myView->StartRotation(point.x,point.y);
236 }
237 else // if ( Ctrl )
238 {
239 Popup(point.x,point.y);
240 }
241}
242
243void CGeometryView::OnRButtonUp(UINT nFlags, CPoint point)
244{
245 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
246 if (!myDegenerateModeIsOn)
247 {
248 myView->SetDegenerateModeOff();
249 myDegenerateModeIsOn = Standard_False;
250 } else
251 {
252 myView->SetDegenerateModeOn();
253 myDegenerateModeIsOn = Standard_True;
254 }
255 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
256}
257
258void CGeometryView::OnMouseMove(UINT nFlags, CPoint point)
259{
260 // ============================ LEFT BUTTON =======================
261 if ( nFlags & MK_LBUTTON)
262 {
263 if ( nFlags & MK_CONTROL )
264 {
265 // move with MB1 and Control : on the dynamic zooming
266 // Do the zoom in function of mouse's coordinates
267 myView->Zoom(myXmax,myYmax,point.x,point.y);
268 // save the current mouse coordinate in min
269 myXmax = point.x;
270 myYmax = point.y;
271 }
272 else // if ( Ctrl )
273 {
274 switch (myCurrentMode)
275 {
276 case CurAction3d_Nothing :
277 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
278 myXmax = point.x;
279 myYmax = point.y;
280 if (nFlags & MK_SHIFT)
281 ShiftDragEvent(myXmax,myYmax,0);
282 else
283 DragEvent(myXmax,myYmax,0);
284 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True);
285 break;
286 case CurAction3d_DynamicZooming :
287 myView->Zoom(myXmax,myYmax,point.x,point.y);
288 // save the current mouse coordinate in min \n";
289 myXmax=point.x; myYmax=point.y;
290 break;
291 case CurAction3d_WindowZooming :
292 myXmax = point.x; myYmax = point.y;
293 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
294 DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True,LongDash);
295 break;
296 case CurAction3d_DynamicPanning :
297 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
298 myXmax = point.x; myYmax = point.y;
299 break;
300 case CurAction3d_GlobalPanning : // nothing
301 break;
302 case CurAction3d_DynamicRotation :
303 myView->Rotation(point.x,point.y);
304 myView->Redraw();
305 break;
306 default :
307 Standard_Failure::Raise(" incompatible Current Mode ");
308 break;
309 }// switch (myCurrentMode)
310 }// if ( nFlags & MK_CONTROL ) else
311 } else // if ( nFlags & MK_LBUTTON)
312 // ============================ MIDDLE BUTTON =======================
313 if ( nFlags & MK_MBUTTON)
314 {
315 if ( nFlags & MK_CONTROL )
316 {
317 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
318 myXmax = point.x; myYmax = point.y;
319
320 }
321 } else // if ( nFlags & MK_MBUTTON)
322 // ============================ RIGHT BUTTON =======================
323 if ( nFlags & MK_RBUTTON)
324 {
325 if ( nFlags & MK_CONTROL )
326 {
327 myView->Rotation(point.x,point.y);
328 }
329 }else //if ( nFlags & MK_RBUTTON)
330 // ============================ NO BUTTON =======================
331 { // No buttons
332 myXmax = point.x; myYmax = point.y;
333 if (nFlags & MK_SHIFT)
334 ShiftMoveEvent(point.x,point.y);
335 else
336 MoveEvent(point.x,point.y);
337 }
338}
339
340void CGeometryView::OnUpdateBUTTONHlrOff(CCmdUI* pCmdUI)
341{
342 pCmdUI->SetCheck (myDegenerateModeIsOn);
343 pCmdUI->Enable (!myDegenerateModeIsOn);
344}
345
346void CGeometryView::OnUpdateBUTTONHlrOn(CCmdUI* pCmdUI)
347{
348 pCmdUI->SetCheck (!myDegenerateModeIsOn);
349 pCmdUI->Enable (myDegenerateModeIsOn);
350}
351
352void CGeometryView::OnUpdateBUTTONPanGlo(CCmdUI* pCmdUI)
353{
354 pCmdUI->SetCheck (myCurrentMode == CurAction3d_GlobalPanning);
355 pCmdUI->Enable (myCurrentMode != CurAction3d_GlobalPanning);
356
357}
358
359void CGeometryView::OnUpdateBUTTONPan(CCmdUI* pCmdUI)
360{
361 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicPanning);
362 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicPanning );
363}
364
365void CGeometryView::OnUpdateBUTTONZoomProg(CCmdUI* pCmdUI)
366{
367 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicZooming );
368 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicZooming);
369}
370
371void CGeometryView::OnUpdateBUTTONZoomWin(CCmdUI* pCmdUI)
372{
373 pCmdUI->SetCheck (myCurrentMode == CurAction3d_WindowZooming);
374 pCmdUI->Enable (myCurrentMode != CurAction3d_WindowZooming);
375}
376
377void CGeometryView::OnUpdateBUTTONRot(CCmdUI* pCmdUI)
378{
379 pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicRotation);
380 pCmdUI->Enable (myCurrentMode != CurAction3d_DynamicRotation);
381}
382
383//==========================================================================================
384//==========================================================================================
385//==========================================================================================
386
387//-----------------------------------------------------------------------------------------
388//
389//-----------------------------------------------------------------------------------------
390void CGeometryView::DragEvent(const Standard_Integer x ,
391 const Standard_Integer y ,
392 const Standard_Integer TheState )
393{
394
395 // TheState == -1 button down
396 // TheState == 0 move
397 // TheState == 1 button up
398
399 static Standard_Integer theButtonDownX=0;
400 static Standard_Integer theButtonDownY=0;
401
402 if (TheState == -1)
403 {
404 theButtonDownX=x;
405 theButtonDownY=y;
406 }
407
408 if (TheState == 0)
409 GetDocument()->GetAISContext()->Select(theButtonDownX,theButtonDownY,x,y,myView);
410}
411
412//-----------------------------------------------------------------------------------------
413//
414//-----------------------------------------------------------------------------------------
415void CGeometryView::InputEvent(const Standard_Integer x ,
416 const Standard_Integer y )
417{
418 GetDocument()->GetAISContext()->Select();
419}
420
421//-----------------------------------------------------------------------------------------
422//
423//-----------------------------------------------------------------------------------------
424void CGeometryView::MoveEvent(const Standard_Integer x ,
425 const Standard_Integer y )
426{
427 GetDocument()->GetAISContext()->MoveTo(x,y,myView);
428}
429
430//-----------------------------------------------------------------------------------------
431//
432//-----------------------------------------------------------------------------------------
433void CGeometryView::ShiftMoveEvent(const Standard_Integer x ,
434 const Standard_Integer y )
435{
436 GetDocument()->GetAISContext()->MoveTo(x,y,myView);
437}
438
439//-----------------------------------------------------------------------------------------
440//
441//-----------------------------------------------------------------------------------------
442void CGeometryView::ShiftDragEvent(const Standard_Integer x ,
443 const Standard_Integer y ,
444 const Standard_Integer TheState )
445{
446 static Standard_Integer theButtonDownX=0;
447 static Standard_Integer theButtonDownY=0;
448
449 if (TheState == -1)
450 {
451 theButtonDownX=x;
452 theButtonDownY=y;
453 }
454
455 if (TheState == 0)
456 GetDocument()->GetAISContext()->ShiftSelect(theButtonDownX,theButtonDownY,x,y,myView);
457}
458
459
460//-----------------------------------------------------------------------------------------
461//
462//-----------------------------------------------------------------------------------------
463void CGeometryView::ShiftInputEvent(const Standard_Integer x ,
464 const Standard_Integer y )
465{
466 GetDocument()->GetAISContext()->ShiftSelect();
467}
468
469//-----------------------------------------------------------------------------------------
470//
471//-----------------------------------------------------------------------------------------
472void CGeometryView::Popup(const Standard_Integer x,
473 const Standard_Integer y )
474{
475 Standard_Integer PopupMenuNumber=0;
476 GetDocument()->GetAISContext()->InitCurrent();
477 if (GetDocument()->GetAISContext()->MoreCurrent())
478 PopupMenuNumber=1;
479
480 CMenu menu;
481 VERIFY(menu.LoadMenu(IDR_Popup3D));
482 CMenu* pPopup = menu.GetSubMenu(PopupMenuNumber);
483 ASSERT(pPopup != NULL);
484
485 POINT winCoord = { x , y };
486 Handle(WNT_Window) aWNTWindow=
487 Handle(WNT_Window)::DownCast(myView->Window());
488 ClientToScreen ( &winCoord);
489 pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON , winCoord.x, winCoord.y ,
490 AfxGetMainWnd());
491}
492
493