0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[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);
4e61131a 98 setAttribute(Qt::WA_NativeWindow);
7fd59977 99
0439d1cf 100 myDefaultGestures = myMouseGestureMap;
2ad228f6 101 myCurrentMode = CurAction3d_Nothing;
2ad228f6 102 setMouseTracking( true );
af4b4ba9 103
2ad228f6 104 initViewActions();
105 initCursors();
4d183a4b 106
2ad228f6 107 setBackgroundRole( QPalette::NoRole );//NoBackground );
108 // set focus policy to threat QContextMenuEvent from keyboard
109 setFocusPolicy( Qt::StrongFocus );
de6273a3 110 init();
7fd59977 111}
112
113View::~View()
114{
e276548b 115 delete myBackMenu;
7fd59977 116}
117
118void View::init()
119{
861a7b03 120 if ( myView.IsNull() )
121 myView = myContext->CurrentViewer()->CreateView();
861a7b03 122
de6273a3 123 Handle(OcctWindow) hWnd = new OcctWindow ( this );
5f9575b3 124 myView->SetWindow (hWnd);
861a7b03 125 if ( !hWnd->IsMapped() )
5f9575b3 126 {
127 hWnd->Map();
128 }
129 myView->SetBackgroundColor (Quantity_NOC_BLACK);
130 myView->MustBeResized();
e276548b 131
861a7b03 132 if (myIsRaytracing)
2ad228f6 133 myView->ChangeRenderingParams().Method = Graphic3d_RM_RAYTRACING;
7fd59977 134}
135
de75ed09 136void View::paintEvent( QPaintEvent * )
7fd59977 137{
138// QApplication::syncX();
0439d1cf 139 myView->InvalidateImmediate();
140 FlushViewEvents (myContext, myView, true);
7fd59977 141}
142
de75ed09 143void View::resizeEvent( QResizeEvent * )
7fd59977 144{
145// QApplication::syncX();
2ad228f6 146 if( !myView.IsNull() )
147 {
148 myView->MustBeResized();
149 }
7fd59977 150}
151
0439d1cf 152void View::OnSelectionChanged (const Handle(AIS_InteractiveContext)& ,
153 const Handle(V3d_View)& )
154{
155 ApplicationCommonWindow::getApplication()->onSelectionChanged();
156}
157
7fd59977 158void View::fitAll()
159{
2ad228f6 160 myView->FitAll();
161 myView->ZFitAll();
162 myView->Redraw();
7fd59977 163}
164
165void View::fitArea()
166{
0439d1cf 167 setCurrentAction (CurAction3d_WindowZooming);
7fd59977 168}
169
170void View::zoom()
171{
0439d1cf 172 setCurrentAction (CurAction3d_DynamicZooming);
7fd59977 173}
174
175void View::pan()
176{
0439d1cf 177 setCurrentAction (CurAction3d_DynamicPanning);
7fd59977 178}
179
180void View::rotation()
181{
0439d1cf 182 setCurrentAction (CurAction3d_DynamicRotation);
7fd59977 183}
184
185void View::globalPan()
186{
2ad228f6 187 // save the current zoom value
188 myCurZoom = myView->Scale();
189 // Do a Global Zoom
190 myView->FitAll();
191 // Set the mode
0439d1cf 192 setCurrentAction (CurAction3d_GlobalPanning);
7fd59977 193}
194
195void View::front()
196{
32757c6e 197 myView->SetProj( V3d_Yneg );
7fd59977 198}
199
200void View::back()
201{
32757c6e 202 myView->SetProj( V3d_Ypos );
7fd59977 203}
204
205void View::top()
206{
2ad228f6 207 myView->SetProj( V3d_Zpos );
7fd59977 208}
209
210void View::bottom()
211{
2ad228f6 212 myView->SetProj( V3d_Zneg );
7fd59977 213}
214
215void View::left()
216{
32757c6e 217 myView->SetProj( V3d_Xneg );
7fd59977 218}
219
220void View::right()
221{
32757c6e 222 myView->SetProj( V3d_Xpos );
7fd59977 223}
224
225void View::axo()
226{
2ad228f6 227 myView->SetProj( V3d_XposYnegZpos );
7fd59977 228}
229
230void View::reset()
231{
2ad228f6 232 myView->Reset();
7fd59977 233}
234
235void View::hlrOff()
236{
2ad228f6 237 QApplication::setOverrideCursor( Qt::WaitCursor );
0439d1cf 238 myView->SetComputedMode (false);
1eeef710 239 myView->Redraw();
2ad228f6 240 QApplication::restoreOverrideCursor();
7fd59977 241}
242
243void View::hlrOn()
244{
2ad228f6 245 QApplication::setOverrideCursor( Qt::WaitCursor );
0439d1cf 246 myView->SetComputedMode (true);
1eeef710 247 myView->Redraw();
2ad228f6 248 QApplication::restoreOverrideCursor();
7fd59977 249}
250
861a7b03 251void View::SetRaytracedShadows (bool theState)
e276548b 252{
2ad228f6 253 myView->ChangeRenderingParams().IsShadowEnabled = theState;
861a7b03 254
255 myIsShadowsEnabled = theState;
256
257 myContext->UpdateCurrentViewer();
e276548b 258}
259
861a7b03 260void View::SetRaytracedReflections (bool theState)
e276548b 261{
2ad228f6 262 myView->ChangeRenderingParams().IsReflectionEnabled = theState;
861a7b03 263
264 myIsReflectionsEnabled = theState;
265
266 myContext->UpdateCurrentViewer();
e276548b 267}
268
861a7b03 269void View::onRaytraceAction()
e276548b 270{
861a7b03 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
304void View::SetRaytracedAntialiasing (bool theState)
305{
2ad228f6 306 myView->ChangeRenderingParams().IsAntialiasingEnabled = theState;
861a7b03 307
308 myIsAntialiasingEnabled = theState;
309
310 myContext->UpdateCurrentViewer();
311}
312
313void View::EnableRaytracing()
314{
315 if (!myIsRaytracing)
2ad228f6 316 myView->ChangeRenderingParams().Method = Graphic3d_RM_RAYTRACING;
861a7b03 317
318 myIsRaytracing = true;
319
320 myContext->UpdateCurrentViewer();
321}
322
323void View::DisableRaytracing()
324{
325 if (myIsRaytracing)
2ad228f6 326 myView->ChangeRenderingParams().Method = Graphic3d_RM_RASTERIZATION;
861a7b03 327
328 myIsRaytracing = false;
329
330 myContext->UpdateCurrentViewer();
e276548b 331}
332
7fd59977 333void View::updateToggled( bool isOn )
334{
2ad228f6 335 QAction* sentBy = (QAction*)sender();
7fd59977 336
2ad228f6 337 if( !isOn )
338 return;
7fd59977 339
2ad228f6 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 ) ) )
7fd59977 349 {
2ad228f6 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 }
7fd59977 372 }
2ad228f6 373 }
7fd59977 374}
375
376void View::initCursors()
377{
2ad228f6 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" ) ) );
7fd59977 390}
391
392QList<QAction*>* View::getViewActions()
393{
2ad228f6 394 initViewActions();
395 return myViewActions;
7fd59977 396}
397
861a7b03 398QList<QAction*>* View::getRaytraceActions()
399{
2ad228f6 400 initRaytraceActions();
401 return myRaytraceActions;
861a7b03 402}
403
4d183a4b 404/*!
405 Get paint engine for the OpenGL viewer. [ virtual public ]
406*/
407QPaintEngine* View::paintEngine() const
408{
409 return 0;
410}
411
7fd59977 412void 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") );
3cb77da4 424 connect( a, SIGNAL( triggered() ) , this, SLOT( fitAll() ) );
7fd59977 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") );
3cb77da4 430 connect( a, SIGNAL( triggered() ) , this, SLOT( fitArea() ) );
7fd59977 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") );
3cb77da4 439 connect( a, SIGNAL( triggered() ) , this, SLOT( zoom() ) );
7fd59977 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") );
3cb77da4 448 connect( a, SIGNAL( triggered() ) , this, SLOT( pan() ) );
7fd59977 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") );
3cb77da4 457 connect( a, SIGNAL( triggered() ) , this, SLOT( globalPan() ) );
7fd59977 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") );
3cb77da4 466 connect( a, SIGNAL( triggered() ) , this, SLOT( front() ) );
7fd59977 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") );
3cb77da4 472 connect( a, SIGNAL( triggered() ) , this, SLOT( back() ) );
7fd59977 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") );
3cb77da4 478 connect( a, SIGNAL( triggered() ) , this, SLOT( top() ) );
7fd59977 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") );
3cb77da4 484 connect( a, SIGNAL( triggered() ) , this, SLOT( bottom() ) );
7fd59977 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") );
3cb77da4 490 connect( a, SIGNAL( triggered() ) , this, SLOT( left() ) );
7fd59977 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") );
3cb77da4 496 connect( a, SIGNAL( triggered() ) , this, SLOT( right() ) );
7fd59977 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") );
3cb77da4 502 connect( a, SIGNAL( triggered() ) , this, SLOT( axo() ) );
7fd59977 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") );
3cb77da4 508 connect( a, SIGNAL( triggered() ) , this, SLOT( rotation() ) );
7fd59977 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") );
3cb77da4 516 connect( a, SIGNAL( triggered() ) , this, SLOT( reset() ) );
7fd59977 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") );
3cb77da4 524 connect( a, SIGNAL( triggered() ) , this, SLOT( hlrOff() ) );
7fd59977 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") );
3cb77da4 532 connect( a, SIGNAL( triggered() ) ,this, SLOT( hlrOn() ) );
7fd59977 533
534 a->setCheckable( true );
535 ag->addAction(a);
536 myViewActions->insert( ViewHlrOnId, a );
537}
538
861a7b03 539void 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 );
a1565817 553 connect( a, SIGNAL( triggered() ) , this, SLOT( onRaytraceAction() ) );
861a7b03 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 );
a1565817 561 connect( a, SIGNAL( triggered() ) , this, SLOT( onRaytraceAction() ) );
861a7b03 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 );
2ad228f6 568 a->setChecked( false );
a1565817 569 connect( a, SIGNAL( triggered() ) , this, SLOT( onRaytraceAction() ) );
861a7b03 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 );
a1565817 577 connect( a, SIGNAL( triggered() ) , this, SLOT( onRaytraceAction() ) );
861a7b03 578 myRaytraceActions->insert( ToolAntialiasingId, a );
579}
580
7fd59977 581void View::activateCursor( const CurrentAction3d mode )
582{
583 switch( mode )
584 {
585 case CurAction3d_DynamicPanning:
2ad228f6 586 setCursor( *panCursor );
587 break;
7fd59977 588 case CurAction3d_DynamicZooming:
2ad228f6 589 setCursor( *zoomCursor );
590 break;
7fd59977 591 case CurAction3d_DynamicRotation:
2ad228f6 592 setCursor( *rotCursor );
593 break;
7fd59977 594 case CurAction3d_GlobalPanning:
2ad228f6 595 setCursor( *globPanCursor );
596 break;
7fd59977 597 case CurAction3d_WindowZooming:
2ad228f6 598 setCursor( *handCursor );
599 break;
7fd59977 600 case CurAction3d_Nothing:
601 default:
2ad228f6 602 setCursor( *defCursor );
603 break;
7fd59977 604 }
605}
606
0439d1cf 607void View::mousePressEvent (QMouseEvent* theEvent)
7fd59977 608{
0439d1cf 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))
7fd59977 616 {
0439d1cf 617 updateView();
7fd59977 618 }
0439d1cf 619 myClickPos = aPnt;
7fd59977 620}
621
0439d1cf 622void View::mouseReleaseEvent (QMouseEvent* theEvent)
7fd59977 623{
0439d1cf 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 }
7fd59977 634
0439d1cf 635 if (myCurrentMode == CurAction3d_GlobalPanning)
7fd59977 636 {
0439d1cf 637 myView->Place (aPnt.x(), aPnt.y(), myCurZoom);
7fd59977 638 }
0439d1cf 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)
7fd59977 646 {
0439d1cf 647 Popup (aPnt.x(), aPnt.y());
7fd59977 648 }
7fd59977 649}
650
0439d1cf 651void View::mouseMoveEvent (QMouseEvent* theEvent)
7fd59977 652{
0439d1cf 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 }
7fd59977 662}
663
0439d1cf 664//==============================================================================
665//function : wheelEvent
666//purpose :
667//==============================================================================
668void View::wheelEvent (QWheelEvent* theEvent)
7fd59977 669{
0439d1cf 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 }
7fd59977 676}
677
0439d1cf 678// =======================================================================
679// function : updateView
680// purpose :
681// =======================================================================
682void View::updateView()
7fd59977 683{
0439d1cf 684 update();
7fd59977 685}
686
0439d1cf 687void View::defineMouseGestures()
7fd59977 688{
0439d1cf 689 myMouseGestureMap.Clear();
690 AIS_MouseGesture aRot = AIS_MouseGesture_RotateOrbit;
691 activateCursor (myCurrentMode);
692 switch (myCurrentMode)
693 {
694 case CurAction3d_Nothing:
7fd59977 695 {
0439d1cf 696 noActiveActions();
697 myMouseGestureMap = myDefaultGestures;
698 break;
7fd59977 699 }
0439d1cf 700 case CurAction3d_DynamicZooming:
7fd59977 701 {
0439d1cf 702 myMouseGestureMap.Bind (Aspect_VKeyMouse_LeftButton, AIS_MouseGesture_Zoom);
703 break;
7fd59977 704 }
0439d1cf 705 case CurAction3d_GlobalPanning:
7fd59977 706 {
0439d1cf 707 break;
7fd59977 708 }
0439d1cf 709 case CurAction3d_WindowZooming:
7fd59977 710 {
0439d1cf 711 myMouseGestureMap.Bind (Aspect_VKeyMouse_LeftButton, AIS_MouseGesture_ZoomWindow);
712 break;
7fd59977 713 }
0439d1cf 714 case CurAction3d_DynamicPanning:
7fd59977 715 {
0439d1cf 716 myMouseGestureMap.Bind (Aspect_VKeyMouse_LeftButton, AIS_MouseGesture_Pan);
717 break;
7fd59977 718 }
0439d1cf 719 case CurAction3d_DynamicRotation:
7fd59977 720 {
0439d1cf 721 myMouseGestureMap.Bind (Aspect_VKeyMouse_LeftButton, aRot);
722 break;
7fd59977 723 }
0439d1cf 724 }
7fd59977 725}
726
de75ed09 727void View::Popup( const int /*x*/, const int /*y*/ )
7fd59977 728{
729 ApplicationCommonWindow* stApp = ApplicationCommonWindow::getApplication();
861a7b03 730 QMdiArea* ws = ApplicationCommonWindow::getWorkspace();
731 QMdiSubWindow* w = ws->activeSubWindow();
7fd59977 732 if ( myContext->NbSelected() )
733 {
734 QList<QAction*>* aList = stApp->getToolActions();
735 QMenu* myToolMenu = new QMenu( 0 );
2ad228f6 736 myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolWireframeId ) );
737 myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolShadingId ) );
738 myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolColorId ) );
7fd59977 739
740 QMenu* myMaterMenu = new QMenu( myToolMenu );
741
742 QList<QAction*>* aMeterActions = ApplicationCommonWindow::getApplication()->getMaterialActions();
743
744 QString dir = ApplicationCommonWindow::getResourceDir() + QString( "/" );
2ad228f6 745 myMaterMenu = myToolMenu->addMenu( QPixmap( dir+QObject::tr("ICON_TOOL_MATER")), QObject::tr("MNU_MATER") );
7fd59977 746 for ( int i = 0; i < aMeterActions->size(); i++ )
2ad228f6 747 myMaterMenu->addAction( aMeterActions->at( i ) );
7fd59977 748
749 myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolTransparencyId ) );
2ad228f6 750 myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolDeleteId ) );
7fd59977 751 addItemInPopup(myToolMenu);
2ad228f6 752 myToolMenu->exec( QCursor::pos() );
7fd59977 753 delete myToolMenu;
754 }
755 else
756 {
e276548b 757 if (!myBackMenu)
758 {
759 myBackMenu = new QMenu( 0 );
760
2ad228f6 761 QAction* a = new QAction( QObject::tr("MNU_CH_BACK"), this );
762 a->setToolTip( QObject::tr("TBR_CH_BACK") );
a1565817 763 connect( a, SIGNAL( triggered() ), this, SLOT( onBackground() ) );
2ad228f6 764 myBackMenu->addAction( a );
e276548b 765 addItemInPopup(myBackMenu);
766
767 a = new QAction( QObject::tr("MNU_CH_ENV_MAP"), this );
2ad228f6 768 a->setToolTip( QObject::tr("TBR_CH_ENV_MAP") );
a1565817 769 connect( a, SIGNAL( triggered() ), this, SLOT( onEnvironmentMap() ) );
e276548b 770 a->setCheckable( true );
771 a->setChecked( false );
2ad228f6 772 myBackMenu->addAction( a );
e276548b 773 addItemInPopup(myBackMenu);
774 }
775
7fd59977 776 myBackMenu->exec( QCursor::pos() );
7fd59977 777 }
778 if ( w )
779 w->setFocus();
780}
781
de75ed09 782void View::addItemInPopup( QMenu* /*theMenu*/)
7fd59977 783{
784}
785
7fd59977 786void 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 );
2ad228f6 798 anAction->setCheckable( true );
799 anAction->setChecked( false );
7fd59977 800 }
801 }
802}
803
804void 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);
55a40de8 811 aColor.setRgb((Standard_Integer)(R1 * 255), (Standard_Integer)(G1 * 255), (Standard_Integer)(B1 * 255));
7fd59977 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
e276548b 825void 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)"));
6cfc06f0
BB
831
832 const TCollection_AsciiString anUtf8Path (fileName.toUtf8().data());
833
834 Handle(Graphic3d_TextureEnv) aTexture = new Graphic3d_TextureEnv( anUtf8Path );
e276548b 835
836 myView->SetTextureEnv (aTexture);
e276548b 837 }
838 else
839 {
83da37b1 840 myView->SetTextureEnv (Handle(Graphic3d_TextureEnv)());
e276548b 841 }
842
843 myView->Redraw();
844}
845
7fd59977 846bool View::dump(Standard_CString theFile)
847{
7fd59977 848 return myView->Dump(theFile);
849}
850
851Handle(V3d_View)& View::getView()
852{
853 return myView;
854}
855
856Handle(AIS_InteractiveContext)& View::getContext()
857{
858 return myContext;
859}
860
861View::CurrentAction3d View::getCurrentMode()
862{
863 return myCurrentMode;
864}