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