0032626: Samples - add Qt::WA_NativeWindow attribute to Qt IESample
[occt.git] / samples / qt / Common / src / View.cxx
1 #if !defined _WIN32
2 #define QT_CLEAN_NAMESPACE         /* avoid definition of INT32 and INT8 */
3 #endif
4
5 #include "View.h"
6 #include "ApplicationCommon.h"
7
8 #include <Standard_WarningsDisable.hxx>
9 #include <QApplication>
10 #include <QPainter>
11 #include <QMenu>
12 #include <QColorDialog>
13 #include <QCursor>
14 #include <QFileInfo>
15 #include <QFileDialog>
16 #include <QMouseEvent>
17 #include <QRubberBand>
18 #include <QMdiSubWindow>
19 #include <QStyleFactory>
20 #if !defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)) && QT_VERSION < 0x050000
21   #include <QX11Info>
22 #endif
23 #include <Standard_WarningsRestore.hxx>
24
25
26 #include <Graphic3d_GraphicDriver.hxx>
27 #include <Graphic3d_TextureEnv.hxx>
28
29 #include <OcctWindow.h>
30 #include <Aspect_DisplayConnection.hxx>
31
32 namespace
33 {
34   //! Map Qt buttons bitmask to virtual keys.
35   Aspect_VKeyMouse qtMouseButtons2VKeys (Qt::MouseButtons theButtons)
36   {
37     Aspect_VKeyMouse aButtons = Aspect_VKeyMouse_NONE;
38     if ((theButtons & Qt::LeftButton) != 0)
39     {
40       aButtons |= Aspect_VKeyMouse_LeftButton;
41     }
42     if ((theButtons & Qt::MiddleButton) != 0)
43     {
44       aButtons |= Aspect_VKeyMouse_MiddleButton;
45     }
46     if ((theButtons & Qt::RightButton) != 0)
47     {
48       aButtons |= Aspect_VKeyMouse_RightButton;
49     }
50     return aButtons;
51   }
52
53   //! Map Qt mouse modifiers bitmask to virtual keys.
54   Aspect_VKeyFlags qtMouseModifiers2VKeys (Qt::KeyboardModifiers theModifiers)
55   {
56     Aspect_VKeyFlags aFlags = Aspect_VKeyFlags_NONE;
57     if ((theModifiers & Qt::ShiftModifier) != 0)
58     {
59       aFlags |= Aspect_VKeyFlags_SHIFT;
60     }
61     if ((theModifiers & Qt::ControlModifier) != 0)
62     {
63       aFlags |= Aspect_VKeyFlags_CTRL;
64     }
65     if ((theModifiers & Qt::AltModifier) != 0)
66     {
67       aFlags |= Aspect_VKeyFlags_ALT;
68     }
69     return aFlags;
70   }
71 }
72
73 static QCursor* defCursor     = NULL;
74 static QCursor* handCursor    = NULL;
75 static QCursor* panCursor     = NULL;
76 static QCursor* globPanCursor = NULL;
77 static QCursor* zoomCursor    = NULL;
78 static QCursor* rotCursor     = NULL;
79
80 View::View( Handle(AIS_InteractiveContext) theContext, QWidget* parent )
81 : QWidget( parent ),
82   myIsRaytracing( false ),
83   myIsShadowsEnabled (true),
84   myIsReflectionsEnabled (false),
85   myIsAntialiasingEnabled (false),
86   myViewActions( 0 ),
87   myRaytraceActions( 0 ),
88   myBackMenu( NULL )
89 {
90 #if !defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)) && QT_VERSION < 0x050000
91   XSynchronize(x11Info().display(),true);
92 #endif
93   myContext = theContext;
94
95   myCurZoom = 0;
96   setAttribute(Qt::WA_PaintOnScreen);
97   setAttribute(Qt::WA_NoSystemBackground);
98   setAttribute(Qt::WA_NativeWindow);
99
100   myDefaultGestures = myMouseGestureMap;
101   myCurrentMode = CurAction3d_Nothing;
102   setMouseTracking( true );
103
104   initViewActions();
105   initCursors();
106
107   setBackgroundRole( QPalette::NoRole );//NoBackground );
108   // set focus policy to threat QContextMenuEvent from keyboard  
109   setFocusPolicy( Qt::StrongFocus );
110   init();
111 }
112
113 View::~View()
114 {
115   delete myBackMenu;
116 }
117
118 void View::init()
119 {
120   if ( myView.IsNull() )
121     myView = myContext->CurrentViewer()->CreateView();
122
123   Handle(OcctWindow) hWnd = new OcctWindow ( this );
124   myView->SetWindow (hWnd);
125   if ( !hWnd->IsMapped() )
126   {
127     hWnd->Map();
128   }
129   myView->SetBackgroundColor (Quantity_NOC_BLACK);
130   myView->MustBeResized();
131
132   if (myIsRaytracing)
133     myView->ChangeRenderingParams().Method = Graphic3d_RM_RAYTRACING;
134 }
135
136 void View::paintEvent( QPaintEvent *  )
137 {
138 //  QApplication::syncX();
139   myView->InvalidateImmediate();
140   FlushViewEvents (myContext, myView, true);
141 }
142
143 void View::resizeEvent( QResizeEvent * )
144 {
145 //  QApplication::syncX();
146   if( !myView.IsNull() )
147   {
148     myView->MustBeResized();
149   }
150 }
151
152 void View::OnSelectionChanged (const Handle(AIS_InteractiveContext)& ,
153                                const Handle(V3d_View)& )
154 {
155   ApplicationCommonWindow::getApplication()->onSelectionChanged();
156 }
157
158 void View::fitAll()
159 {
160   myView->FitAll();
161   myView->ZFitAll();
162   myView->Redraw();
163 }
164
165 void View::fitArea()
166 {
167   setCurrentAction (CurAction3d_WindowZooming);
168 }
169
170 void View::zoom()
171 {
172   setCurrentAction (CurAction3d_DynamicZooming);
173 }
174
175 void View::pan()
176 {
177   setCurrentAction (CurAction3d_DynamicPanning);
178 }
179
180 void View::rotation()
181 {
182   setCurrentAction (CurAction3d_DynamicRotation);
183 }
184
185 void View::globalPan()
186 {
187   // save the current zoom value
188   myCurZoom = myView->Scale();
189   // Do a Global Zoom
190   myView->FitAll();
191   // Set the mode
192   setCurrentAction (CurAction3d_GlobalPanning);
193 }
194
195 void View::front()
196 {
197   myView->SetProj( V3d_Yneg );
198 }
199
200 void View::back()
201 {
202   myView->SetProj( V3d_Ypos );
203 }
204
205 void View::top()
206 {
207   myView->SetProj( V3d_Zpos );
208 }
209
210 void View::bottom()
211 {
212   myView->SetProj( V3d_Zneg );
213 }
214
215 void View::left()
216 {
217   myView->SetProj( V3d_Xneg );
218 }
219
220 void View::right()
221 {
222   myView->SetProj( V3d_Xpos );
223 }
224
225 void View::axo()
226 {
227   myView->SetProj( V3d_XposYnegZpos );
228 }
229
230 void View::reset()
231 {
232   myView->Reset();
233 }
234
235 void View::hlrOff()
236 {
237   QApplication::setOverrideCursor( Qt::WaitCursor );
238   myView->SetComputedMode (false);
239   myView->Redraw();
240   QApplication::restoreOverrideCursor();
241 }
242
243 void View::hlrOn()
244 {
245   QApplication::setOverrideCursor( Qt::WaitCursor );
246   myView->SetComputedMode (true);
247   myView->Redraw();
248   QApplication::restoreOverrideCursor();
249 }
250
251 void View::SetRaytracedShadows (bool theState)
252 {
253   myView->ChangeRenderingParams().IsShadowEnabled = theState;
254
255   myIsShadowsEnabled = theState;
256
257   myContext->UpdateCurrentViewer();
258 }
259
260 void View::SetRaytracedReflections (bool theState)
261 {
262   myView->ChangeRenderingParams().IsReflectionEnabled = theState;
263
264   myIsReflectionsEnabled = theState;
265
266   myContext->UpdateCurrentViewer();
267 }
268
269 void View::onRaytraceAction()
270 {
271   QAction* aSentBy = (QAction*)sender();
272   
273   if (aSentBy == myRaytraceActions->at (ToolRaytracingId))
274   {
275     bool aState = myRaytraceActions->at (ToolRaytracingId)->isChecked(); 
276
277     QApplication::setOverrideCursor (Qt::WaitCursor);
278     if (aState)
279       EnableRaytracing();
280     else
281       DisableRaytracing();
282     QApplication::restoreOverrideCursor();
283   }
284
285   if (aSentBy == myRaytraceActions->at (ToolShadowsId))
286   {
287     bool aState = myRaytraceActions->at (ToolShadowsId)->isChecked(); 
288     SetRaytracedShadows (aState);
289   }
290
291   if (aSentBy == myRaytraceActions->at (ToolReflectionsId))
292   {
293     bool aState = myRaytraceActions->at (ToolReflectionsId)->isChecked();
294     SetRaytracedReflections (aState);
295   }
296
297   if (aSentBy == myRaytraceActions->at (ToolAntialiasingId))
298   {
299     bool aState = myRaytraceActions->at (ToolAntialiasingId)->isChecked();
300     SetRaytracedAntialiasing (aState);
301   }
302 }
303
304 void View::SetRaytracedAntialiasing (bool theState)
305 {
306   myView->ChangeRenderingParams().IsAntialiasingEnabled = theState;
307
308   myIsAntialiasingEnabled = theState;
309
310   myContext->UpdateCurrentViewer();
311 }
312
313 void View::EnableRaytracing()
314 {
315   if (!myIsRaytracing)
316     myView->ChangeRenderingParams().Method = Graphic3d_RM_RAYTRACING;
317
318   myIsRaytracing = true;
319
320   myContext->UpdateCurrentViewer();
321 }
322
323 void View::DisableRaytracing()
324 {
325   if (myIsRaytracing)
326     myView->ChangeRenderingParams().Method = Graphic3d_RM_RASTERIZATION;
327
328   myIsRaytracing = false;
329
330   myContext->UpdateCurrentViewer();
331 }
332
333 void View::updateToggled( bool isOn )
334 {
335   QAction* sentBy = (QAction*)sender();
336
337   if( !isOn )
338     return;
339
340   for ( int i = ViewFitAllId; i < ViewHlrOffId; i++ )
341   {
342     QAction* anAction = myViewActions->at( i );
343     
344     if ( ( anAction == myViewActions->at( ViewFitAreaId ) ) ||
345          ( anAction == myViewActions->at( ViewZoomId ) ) ||
346          ( anAction == myViewActions->at( ViewPanId ) ) ||
347          ( anAction == myViewActions->at( ViewGlobalPanId ) ) ||
348          ( anAction == myViewActions->at( ViewRotationId ) ) )
349     {
350       if ( anAction && ( anAction != sentBy ) )
351       {
352         anAction->setCheckable( true );
353         anAction->setChecked( false );
354       }
355       else
356       {
357         if ( sentBy == myViewActions->at( ViewFitAreaId ) )
358           setCursor( *handCursor );
359         else if ( sentBy == myViewActions->at( ViewZoomId ) )
360           setCursor( *zoomCursor );
361         else if ( sentBy == myViewActions->at( ViewPanId ) )
362           setCursor( *panCursor );
363         else if ( sentBy == myViewActions->at( ViewGlobalPanId ) )
364           setCursor( *globPanCursor );
365         else if ( sentBy == myViewActions->at( ViewRotationId ) )
366           setCursor( *rotCursor );
367         else
368           setCursor( *defCursor );
369
370         sentBy->setCheckable( false );
371       }
372     }
373   }
374 }
375
376 void View::initCursors()
377 {
378   if ( !defCursor )
379     defCursor = new QCursor( Qt::ArrowCursor );
380   if ( !handCursor )
381     handCursor = new QCursor( Qt::PointingHandCursor );
382   if ( !panCursor )
383     panCursor = new QCursor( Qt::SizeAllCursor );
384   if ( !globPanCursor )
385     globPanCursor = new QCursor( Qt::CrossCursor );
386   if ( !zoomCursor )
387     zoomCursor = new QCursor( QPixmap( ApplicationCommonWindow::getResourceDir() + QString( "/" ) + QObject::tr( "ICON_CURSOR_ZOOM" ) ) );
388   if ( !rotCursor )
389     rotCursor = new QCursor( QPixmap( ApplicationCommonWindow::getResourceDir() + QString( "/" ) + QObject::tr( "ICON_CURSOR_ROTATE" ) ) );
390 }
391
392 QList<QAction*>* View::getViewActions()
393 {
394   initViewActions();
395   return myViewActions;
396 }
397
398 QList<QAction*>* View::getRaytraceActions()
399 {
400   initRaytraceActions();
401   return myRaytraceActions;
402 }
403
404 /*!
405   Get paint engine for the OpenGL viewer. [ virtual public ]
406 */
407 QPaintEngine* View::paintEngine() const
408 {
409   return 0;
410 }
411
412 void View::initViewActions()
413 {
414   if ( myViewActions )
415     return;
416
417   myViewActions = new QList<QAction*>();
418   QString dir = ApplicationCommonWindow::getResourceDir() + QString( "/" );
419   QAction* a;
420
421   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_FITALL") ), QObject::tr("MNU_FITALL"), this );
422   a->setToolTip( QObject::tr("TBR_FITALL") );
423   a->setStatusTip( QObject::tr("TBR_FITALL") );
424   connect( a, SIGNAL( triggered() ) , this, SLOT( fitAll() ) );
425   myViewActions->insert(ViewFitAllId, a);
426
427   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_FITAREA") ), QObject::tr("MNU_FITAREA"), this );
428   a->setToolTip( QObject::tr("TBR_FITAREA") );
429   a->setStatusTip( QObject::tr("TBR_FITAREA") );
430   connect( a, SIGNAL( triggered() ) , this, SLOT( fitArea() ) );
431
432   a->setCheckable( true );
433   connect( a, SIGNAL( toggled( bool ) ) , this, SLOT( updateToggled( bool ) ) );
434   myViewActions->insert( ViewFitAreaId, a );
435
436   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_ZOOM") ), QObject::tr("MNU_ZOOM"), this );
437   a->setToolTip( QObject::tr("TBR_ZOOM") );
438   a->setStatusTip( QObject::tr("TBR_ZOOM") );
439   connect( a, SIGNAL( triggered() ) , this, SLOT( zoom() ) );
440
441   a->setCheckable( true );
442   connect( a, SIGNAL( toggled(bool) ) , this, SLOT( updateToggled(bool) ) );
443   myViewActions->insert( ViewZoomId, a );
444
445   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_PAN") ), QObject::tr("MNU_PAN"), this );
446   a->setToolTip( QObject::tr("TBR_PAN") );
447   a->setStatusTip( QObject::tr("TBR_PAN") );
448   connect( a, SIGNAL( triggered() ) , this, SLOT( pan() ) );
449
450   a->setCheckable( true );
451   connect( a, SIGNAL( toggled(bool) ) , this, SLOT( updateToggled(bool) ) );
452   myViewActions->insert( ViewPanId, a );
453
454   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_GLOBALPAN") ), QObject::tr("MNU_GLOBALPAN"), this );
455   a->setToolTip( QObject::tr("TBR_GLOBALPAN") );
456   a->setStatusTip( QObject::tr("TBR_GLOBALPAN") );
457   connect( a, SIGNAL( triggered() ) , this, SLOT( globalPan() ) );
458
459   a->setCheckable( true );
460   connect( a, SIGNAL( toggled(bool) ) , this, SLOT( updateToggled(bool) ) );
461   myViewActions->insert( ViewGlobalPanId, a );
462
463   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_FRONT") ), QObject::tr("MNU_FRONT"), this );
464   a->setToolTip( QObject::tr("TBR_FRONT") );
465   a->setStatusTip( QObject::tr("TBR_FRONT") );
466   connect( a, SIGNAL( triggered() ) , this, SLOT( front() ) );
467   myViewActions->insert( ViewFrontId, a );
468
469   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_BACK") ), QObject::tr("MNU_BACK"), this );
470   a->setToolTip( QObject::tr("TBR_BACK") );
471   a->setStatusTip( QObject::tr("TBR_BACK") );
472   connect( a, SIGNAL( triggered() ) , this, SLOT( back() ) );
473   myViewActions->insert(ViewBackId, a);
474
475   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_TOP") ), QObject::tr("MNU_TOP"), this );
476   a->setToolTip( QObject::tr("TBR_TOP") );
477   a->setStatusTip( QObject::tr("TBR_TOP") );
478   connect( a, SIGNAL( triggered() ) , this, SLOT( top() ) );
479   myViewActions->insert( ViewTopId, a );
480
481   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_BOTTOM") ), QObject::tr("MNU_BOTTOM"), this );
482   a->setToolTip( QObject::tr("TBR_BOTTOM") );
483   a->setStatusTip( QObject::tr("TBR_BOTTOM") );
484   connect( a, SIGNAL( triggered() ) , this, SLOT( bottom() ) );
485   myViewActions->insert( ViewBottomId, a );
486
487   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_LEFT") ), QObject::tr("MNU_LEFT"), this );
488   a->setToolTip( QObject::tr("TBR_LEFT") );
489   a->setStatusTip( QObject::tr("TBR_LEFT") );
490   connect( a, SIGNAL( triggered() ) , this, SLOT( left() ) );
491   myViewActions->insert( ViewLeftId, a );
492
493   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_RIGHT") ), QObject::tr("MNU_RIGHT"), this );
494   a->setToolTip( QObject::tr("TBR_RIGHT") );
495   a->setStatusTip( QObject::tr("TBR_RIGHT") );
496   connect( a, SIGNAL( triggered() ) , this, SLOT( right() ) );
497   myViewActions->insert( ViewRightId, a );
498
499   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_AXO") ), QObject::tr("MNU_AXO"), this );
500   a->setToolTip( QObject::tr("TBR_AXO") );
501   a->setStatusTip( QObject::tr("TBR_AXO") );
502   connect( a, SIGNAL( triggered() ) , this, SLOT( axo() ) );
503   myViewActions->insert( ViewAxoId, a );
504
505   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_ROTATION") ), QObject::tr("MNU_ROTATION"), this );
506   a->setToolTip( QObject::tr("TBR_ROTATION") );
507   a->setStatusTip( QObject::tr("TBR_ROTATION") );
508   connect( a, SIGNAL( triggered() ) , this, SLOT( rotation() ) );
509   a->setCheckable( true );
510   connect( a, SIGNAL( toggled(bool) ) , this, SLOT( updateToggled(bool) ) );
511   myViewActions->insert( ViewRotationId, a );
512
513   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_RESET") ), QObject::tr("MNU_RESET"), this );
514   a->setToolTip( QObject::tr("TBR_RESET") );
515   a->setStatusTip( QObject::tr("TBR_RESET") );
516   connect( a, SIGNAL( triggered() ) , this, SLOT( reset() ) );
517   myViewActions->insert( ViewResetId, a );
518
519   QActionGroup* ag = new QActionGroup( this );
520
521   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_HLROFF") ), QObject::tr("MNU_HLROFF"), this );
522   a->setToolTip( QObject::tr("TBR_HLROFF") );
523   a->setStatusTip( QObject::tr("TBR_HLROFF") );
524   connect( a, SIGNAL( triggered() ) , this, SLOT( hlrOff() ) );
525   a->setCheckable( true );
526   ag->addAction(a);
527   myViewActions->insert(ViewHlrOffId, a);
528
529   a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_HLRON") ), QObject::tr("MNU_HLRON"), this );
530   a->setToolTip( QObject::tr("TBR_HLRON") );
531   a->setStatusTip( QObject::tr("TBR_HLRON") );
532   connect( a, SIGNAL( triggered() ) ,this, SLOT( hlrOn() ) );
533   
534   a->setCheckable( true );
535   ag->addAction(a);
536   myViewActions->insert( ViewHlrOnId, a );
537 }
538
539 void View::initRaytraceActions()
540 {
541   if ( myRaytraceActions )
542     return;
543
544   myRaytraceActions = new QList<QAction*>();
545   QString dir = ApplicationCommonWindow::getResourceDir() + QString( "/" );
546   QAction* a;
547
548   a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_RAYTRACING") ), QObject::tr("MNU_TOOL_RAYTRACING"), this );
549   a->setToolTip( QObject::tr("TBR_TOOL_RAYTRACING") );
550   a->setStatusTip( QObject::tr("TBR_TOOL_RAYTRACING") );
551   a->setCheckable( true );
552   a->setChecked( false );
553   connect( a, SIGNAL( triggered() ) , this, SLOT( onRaytraceAction() ) );
554   myRaytraceActions->insert( ToolRaytracingId, a );
555
556   a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_SHADOWS") ), QObject::tr("MNU_TOOL_SHADOWS"), this );
557   a->setToolTip( QObject::tr("TBR_TOOL_SHADOWS") );
558   a->setStatusTip( QObject::tr("TBR_TOOL_SHADOWS") );
559   a->setCheckable( true );
560   a->setChecked( true );
561   connect( a, SIGNAL( triggered() ) , this, SLOT( onRaytraceAction() ) );
562   myRaytraceActions->insert( ToolShadowsId, a );
563
564   a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_REFLECTIONS") ), QObject::tr("MNU_TOOL_REFLECTIONS"), this );
565   a->setToolTip( QObject::tr("TBR_TOOL_REFLECTIONS") );
566   a->setStatusTip( QObject::tr("TBR_TOOL_REFLECTIONS") );
567   a->setCheckable( true );
568   a->setChecked( false );
569   connect( a, SIGNAL( triggered() ) , this, SLOT( onRaytraceAction() ) );
570   myRaytraceActions->insert( ToolReflectionsId, a );
571
572   a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_ANTIALIASING") ), QObject::tr("MNU_TOOL_ANTIALIASING"), this );
573   a->setToolTip( QObject::tr("TBR_TOOL_ANTIALIASING") );
574   a->setStatusTip( QObject::tr("TBR_TOOL_ANTIALIASING") );
575   a->setCheckable( true );
576   a->setChecked( false );
577   connect( a, SIGNAL( triggered() ) , this, SLOT( onRaytraceAction() ) );
578   myRaytraceActions->insert( ToolAntialiasingId, a );
579 }
580
581 void View::activateCursor( const CurrentAction3d mode )
582 {
583   switch( mode )
584   {
585     case CurAction3d_DynamicPanning:
586       setCursor( *panCursor );
587       break;
588     case CurAction3d_DynamicZooming:
589       setCursor( *zoomCursor );
590       break;
591     case CurAction3d_DynamicRotation:
592       setCursor( *rotCursor );
593       break;
594     case CurAction3d_GlobalPanning:
595       setCursor( *globPanCursor );
596       break;
597     case CurAction3d_WindowZooming:
598       setCursor( *handCursor );
599       break;
600     case CurAction3d_Nothing:
601     default:
602       setCursor( *defCursor );
603       break;
604   }
605 }
606
607 void View::mousePressEvent (QMouseEvent* theEvent)
608 {
609   const Graphic3d_Vec2i aPnt (theEvent->pos().x(), theEvent->pos().y());
610   const Aspect_VKeyFlags aFlags = qtMouseModifiers2VKeys (theEvent->modifiers());
611   if (!myView.IsNull()
612     && UpdateMouseButtons (aPnt,
613                            qtMouseButtons2VKeys (theEvent->buttons()),
614                            aFlags,
615                            false))
616   {
617     updateView();
618   }
619   myClickPos = aPnt;
620 }
621
622 void View::mouseReleaseEvent (QMouseEvent* theEvent)
623 {
624   const Graphic3d_Vec2i aPnt (theEvent->pos().x(), theEvent->pos().y());
625   const Aspect_VKeyFlags aFlags = qtMouseModifiers2VKeys (theEvent->modifiers());
626   if (!myView.IsNull()
627     && UpdateMouseButtons (aPnt,
628                            qtMouseButtons2VKeys (theEvent->buttons()),
629                            aFlags,
630                            false))
631   {
632     updateView();
633   }
634
635   if (myCurrentMode == CurAction3d_GlobalPanning)
636   {
637     myView->Place (aPnt.x(), aPnt.y(), myCurZoom);
638   }
639   if (myCurrentMode != CurAction3d_Nothing)
640   {
641     setCurrentAction (CurAction3d_Nothing);
642   }
643   if (theEvent->button() == Qt::RightButton
644    && (aFlags & Aspect_VKeyFlags_CTRL) == 0
645    && (myClickPos - aPnt).cwiseAbs().maxComp() <= 4)
646   {
647     Popup (aPnt.x(), aPnt.y());
648   }
649 }
650
651 void View::mouseMoveEvent (QMouseEvent* theEvent)
652 {
653   const Graphic3d_Vec2i aNewPos (theEvent->pos().x(), theEvent->pos().y());
654   if (!myView.IsNull()
655     && UpdateMousePosition (aNewPos,
656                             qtMouseButtons2VKeys (theEvent->buttons()),
657                             qtMouseModifiers2VKeys (theEvent->modifiers()),
658                             false))
659   {
660     updateView();
661   }
662 }
663
664 //==============================================================================
665 //function : wheelEvent
666 //purpose  :
667 //==============================================================================
668 void View::wheelEvent (QWheelEvent* theEvent)
669 {
670   const Graphic3d_Vec2i aPos (theEvent->pos().x(), theEvent->pos().y());
671   if (!myView.IsNull()
672     && UpdateZoom (Aspect_ScrollDelta (aPos, theEvent->delta() / 8)))
673   {
674     updateView();
675   }
676 }
677
678 // =======================================================================
679 // function : updateView
680 // purpose  :
681 // =======================================================================
682 void View::updateView()
683 {
684   update();
685 }
686
687 void View::defineMouseGestures()
688 {
689   myMouseGestureMap.Clear();
690   AIS_MouseGesture aRot = AIS_MouseGesture_RotateOrbit;
691   activateCursor (myCurrentMode);
692   switch (myCurrentMode)
693   {
694     case CurAction3d_Nothing:
695     {
696       noActiveActions();
697       myMouseGestureMap = myDefaultGestures;
698       break;
699     }
700     case CurAction3d_DynamicZooming:
701     {
702       myMouseGestureMap.Bind (Aspect_VKeyMouse_LeftButton, AIS_MouseGesture_Zoom);
703       break;
704     }
705     case CurAction3d_GlobalPanning:
706     {
707       break;
708     }
709     case CurAction3d_WindowZooming:
710     {
711       myMouseGestureMap.Bind (Aspect_VKeyMouse_LeftButton, AIS_MouseGesture_ZoomWindow);
712       break;
713     }
714     case CurAction3d_DynamicPanning:
715     {
716       myMouseGestureMap.Bind (Aspect_VKeyMouse_LeftButton, AIS_MouseGesture_Pan);
717       break;
718     }
719     case CurAction3d_DynamicRotation:
720     {
721       myMouseGestureMap.Bind (Aspect_VKeyMouse_LeftButton, aRot);
722       break;
723     }
724   }
725 }
726
727 void View::Popup( const int /*x*/, const int /*y*/ )
728 {
729   ApplicationCommonWindow* stApp = ApplicationCommonWindow::getApplication();
730   QMdiArea* ws = ApplicationCommonWindow::getWorkspace();
731   QMdiSubWindow* w = ws->activeSubWindow();
732   if ( myContext->NbSelected() )
733   {
734     QList<QAction*>* aList = stApp->getToolActions();
735     QMenu* myToolMenu = new QMenu( 0 );
736     myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolWireframeId ) );
737     myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolShadingId ) );
738     myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolColorId ) );
739         
740     QMenu* myMaterMenu = new QMenu( myToolMenu );
741
742     QList<QAction*>* aMeterActions = ApplicationCommonWindow::getApplication()->getMaterialActions();
743         
744     QString dir = ApplicationCommonWindow::getResourceDir() + QString( "/" );
745     myMaterMenu = myToolMenu->addMenu( QPixmap( dir+QObject::tr("ICON_TOOL_MATER")), QObject::tr("MNU_MATER") );
746     for ( int i = 0; i < aMeterActions->size(); i++ )
747       myMaterMenu->addAction( aMeterActions->at( i ) );
748        
749     myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolTransparencyId ) );
750     myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolDeleteId ) );
751     addItemInPopup(myToolMenu);
752     myToolMenu->exec( QCursor::pos() );
753     delete myToolMenu;
754   }
755   else
756   {
757     if (!myBackMenu)
758     {
759       myBackMenu = new QMenu( 0 );
760
761       QAction* a = new QAction( QObject::tr("MNU_CH_BACK"), this );
762       a->setToolTip( QObject::tr("TBR_CH_BACK") );
763       connect( a, SIGNAL( triggered() ), this, SLOT( onBackground() ) );
764       myBackMenu->addAction( a );  
765       addItemInPopup(myBackMenu);
766
767       a = new QAction( QObject::tr("MNU_CH_ENV_MAP"), this );
768       a->setToolTip( QObject::tr("TBR_CH_ENV_MAP") );
769       connect( a, SIGNAL( triggered() ), this, SLOT( onEnvironmentMap() ) );
770       a->setCheckable( true );
771       a->setChecked( false );
772       myBackMenu->addAction( a );  
773       addItemInPopup(myBackMenu);
774     }
775
776     myBackMenu->exec( QCursor::pos() );
777   }
778   if ( w )
779     w->setFocus();
780 }
781
782 void View::addItemInPopup( QMenu* /*theMenu*/)
783 {
784 }
785
786 void View::noActiveActions()
787 {
788     for ( int i = ViewFitAllId; i < ViewHlrOffId ; i++ )
789     {
790         QAction* anAction = myViewActions->at( i );
791         if( ( anAction == myViewActions->at( ViewFitAreaId ) ) ||
792             ( anAction == myViewActions->at( ViewZoomId ) ) ||
793             ( anAction == myViewActions->at( ViewPanId ) ) ||
794             ( anAction == myViewActions->at( ViewGlobalPanId ) ) ||
795             ( anAction == myViewActions->at( ViewRotationId ) ) )
796         {
797             setCursor( *defCursor );
798             anAction->setCheckable( true );
799             anAction->setChecked( false );
800         }
801     }
802 }
803
804 void View::onBackground()
805 {
806     QColor aColor ;
807     Standard_Real R1;
808     Standard_Real G1;
809     Standard_Real B1;
810     myView->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
811     aColor.setRgb((Standard_Integer)(R1 * 255), (Standard_Integer)(G1 * 255), (Standard_Integer)(B1 * 255));
812
813     QColor aRetColor = QColorDialog::getColor(aColor);
814
815     if( aRetColor.isValid() )
816     {
817         R1 = aRetColor.red()/255.;
818         G1 = aRetColor.green()/255.;
819         B1 = aRetColor.blue()/255.;
820         myView->SetBackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
821     }
822     myView->Redraw();
823 }
824
825 void View::onEnvironmentMap()
826 {
827   if (myBackMenu->actions().at(1)->isChecked())
828   {
829     QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "",
830                            tr("All Image Files (*.bmp *.gif *.jpg *.jpeg *.png *.tga)"));
831     
832     const TCollection_AsciiString anUtf8Path (fileName.toUtf8().data());
833     
834     Handle(Graphic3d_TextureEnv) aTexture = new Graphic3d_TextureEnv( anUtf8Path );
835
836     myView->SetTextureEnv (aTexture);
837   }
838   else
839   {
840     myView->SetTextureEnv (Handle(Graphic3d_TextureEnv)());
841   }
842   
843   myView->Redraw();
844 }
845
846 bool View::dump(Standard_CString theFile)
847 {
848   return myView->Dump(theFile);
849 }
850
851 Handle(V3d_View)& View::getView()
852 {
853   return myView;
854 }
855
856 Handle(AIS_InteractiveContext)& View::getContext()
857 {
858   return myContext;
859 }
860
861 View::CurrentAction3d View::getCurrentMode()
862 {
863   return myCurrentMode;
864 }