0029643: Samples: create .bat files for building AndroidQt sample on Windows platform
[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
8#include <QApplication>
9#include <QPainter>
10#include <QMenu>
11#include <QColorDialog>
12#include <QCursor>
13#include <QFileInfo>
e276548b 14#include <QFileDialog>
7fd59977 15#include <QMouseEvent>
16#include <QRubberBand>
861a7b03 17#include <QMdiSubWindow>
18#include <QStyleFactory>
a722bd55 19#if !defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)) && QT_VERSION < 0x050000
20 #include <QX11Info>
21#endif
22
7fd59977 23
7fd59977 24#include <Graphic3d_ExportFormat.hxx>
dc3fe572 25#include <Graphic3d_GraphicDriver.hxx>
e276548b 26#include <Graphic3d_TextureEnv.hxx>
7fd59977 27
de6273a3 28#include <OcctWindow.h>
dc3fe572 29#include <Aspect_DisplayConnection.hxx>
30
7fd59977 31// the key for multi selection :
32#define MULTISELECTIONKEY Qt::ShiftModifier
33
34// the key for shortcut ( use to activate dynamic rotation, panning )
35#define CASCADESHORTCUTKEY Qt::ControlModifier
36
37// for elastic bean selection
38#define ValZWMin 1
39
40static QCursor* defCursor = NULL;
41static QCursor* handCursor = NULL;
42static QCursor* panCursor = NULL;
43static QCursor* globPanCursor = NULL;
44static QCursor* zoomCursor = NULL;
45static QCursor* rotCursor = NULL;
46
861a7b03 47View::View( Handle(AIS_InteractiveContext) theContext, QWidget* parent )
7fd59977 48: QWidget( parent ),
2ad228f6 49 myIsRaytracing( false ),
50 myIsShadowsEnabled (true),
51 myIsReflectionsEnabled (false),
52 myIsAntialiasingEnabled (false),
53 myViewActions( 0 ),
54 myRaytraceActions( 0 ),
55 myBackMenu( NULL )
7fd59977 56{
a722bd55 57#if !defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)) && QT_VERSION < 0x050000
58 XSynchronize(x11Info().display(),true);
59#endif
2ad228f6 60 myContext = theContext;
e276548b 61
2ad228f6 62 myXmin = 0;
63 myYmin = 0;
64 myXmax = 0;
65 myYmax = 0;
66 myCurZoom = 0;
67 myRectBand = 0;
7fd59977 68
2ad228f6 69 setAttribute(Qt::WA_PaintOnScreen);
70 setAttribute(Qt::WA_NoSystemBackground);
7fd59977 71
2ad228f6 72 myCurrentMode = CurAction3d_Nothing;
73 myHlrModeIsOn = Standard_False;
74 setMouseTracking( true );
af4b4ba9 75
2ad228f6 76 initViewActions();
77 initCursors();
4d183a4b 78
2ad228f6 79 setBackgroundRole( QPalette::NoRole );//NoBackground );
80 // set focus policy to threat QContextMenuEvent from keyboard
81 setFocusPolicy( Qt::StrongFocus );
82 setAttribute( Qt::WA_PaintOnScreen );
83 setAttribute( Qt::WA_NoSystemBackground );
de6273a3 84 init();
7fd59977 85}
86
87View::~View()
88{
e276548b 89 delete myBackMenu;
7fd59977 90}
91
92void View::init()
93{
861a7b03 94 if ( myView.IsNull() )
95 myView = myContext->CurrentViewer()->CreateView();
861a7b03 96
de6273a3 97 Handle(OcctWindow) hWnd = new OcctWindow ( this );
5f9575b3 98 myView->SetWindow (hWnd);
861a7b03 99 if ( !hWnd->IsMapped() )
5f9575b3 100 {
101 hWnd->Map();
102 }
103 myView->SetBackgroundColor (Quantity_NOC_BLACK);
104 myView->MustBeResized();
e276548b 105
861a7b03 106 if (myIsRaytracing)
2ad228f6 107 myView->ChangeRenderingParams().Method = Graphic3d_RM_RAYTRACING;
7fd59977 108}
109
de75ed09 110void View::paintEvent( QPaintEvent * )
7fd59977 111{
112// QApplication::syncX();
2ad228f6 113 myView->Redraw();
7fd59977 114}
115
de75ed09 116void View::resizeEvent( QResizeEvent * )
7fd59977 117{
118// QApplication::syncX();
2ad228f6 119 if( !myView.IsNull() )
120 {
121 myView->MustBeResized();
122 }
7fd59977 123}
124
125void View::fitAll()
126{
2ad228f6 127 myView->FitAll();
128 myView->ZFitAll();
129 myView->Redraw();
7fd59977 130}
131
132void View::fitArea()
133{
2ad228f6 134 myCurrentMode = CurAction3d_WindowZooming;
7fd59977 135}
136
137void View::zoom()
138{
2ad228f6 139 myCurrentMode = CurAction3d_DynamicZooming;
7fd59977 140}
141
142void View::pan()
143{
2ad228f6 144 myCurrentMode = CurAction3d_DynamicPanning;
7fd59977 145}
146
147void View::rotation()
148{
2ad228f6 149 myCurrentMode = CurAction3d_DynamicRotation;
7fd59977 150}
151
152void View::globalPan()
153{
2ad228f6 154 // save the current zoom value
155 myCurZoom = myView->Scale();
156 // Do a Global Zoom
157 myView->FitAll();
158 // Set the mode
159 myCurrentMode = CurAction3d_GlobalPanning;
7fd59977 160}
161
162void View::front()
163{
32757c6e 164 myView->SetProj( V3d_Yneg );
7fd59977 165}
166
167void View::back()
168{
32757c6e 169 myView->SetProj( V3d_Ypos );
7fd59977 170}
171
172void View::top()
173{
2ad228f6 174 myView->SetProj( V3d_Zpos );
7fd59977 175}
176
177void View::bottom()
178{
2ad228f6 179 myView->SetProj( V3d_Zneg );
7fd59977 180}
181
182void View::left()
183{
32757c6e 184 myView->SetProj( V3d_Xneg );
7fd59977 185}
186
187void View::right()
188{
32757c6e 189 myView->SetProj( V3d_Xpos );
7fd59977 190}
191
192void View::axo()
193{
2ad228f6 194 myView->SetProj( V3d_XposYnegZpos );
7fd59977 195}
196
197void View::reset()
198{
2ad228f6 199 myView->Reset();
7fd59977 200}
201
202void View::hlrOff()
203{
2ad228f6 204 QApplication::setOverrideCursor( Qt::WaitCursor );
205 myHlrModeIsOn = Standard_False;
206 myView->SetComputedMode (myHlrModeIsOn);
1eeef710 207 myView->Redraw();
2ad228f6 208 QApplication::restoreOverrideCursor();
7fd59977 209}
210
211void View::hlrOn()
212{
2ad228f6 213 QApplication::setOverrideCursor( Qt::WaitCursor );
214 myHlrModeIsOn = Standard_True;
215 myView->SetComputedMode (myHlrModeIsOn);
1eeef710 216 myView->Redraw();
2ad228f6 217 QApplication::restoreOverrideCursor();
7fd59977 218}
219
861a7b03 220void View::SetRaytracedShadows (bool theState)
e276548b 221{
2ad228f6 222 myView->ChangeRenderingParams().IsShadowEnabled = theState;
861a7b03 223
224 myIsShadowsEnabled = theState;
225
226 myContext->UpdateCurrentViewer();
e276548b 227}
228
861a7b03 229void View::SetRaytracedReflections (bool theState)
e276548b 230{
2ad228f6 231 myView->ChangeRenderingParams().IsReflectionEnabled = theState;
861a7b03 232
233 myIsReflectionsEnabled = theState;
234
235 myContext->UpdateCurrentViewer();
e276548b 236}
237
861a7b03 238void View::onRaytraceAction()
e276548b 239{
861a7b03 240 QAction* aSentBy = (QAction*)sender();
241
242 if (aSentBy == myRaytraceActions->at (ToolRaytracingId))
243 {
244 bool aState = myRaytraceActions->at (ToolRaytracingId)->isChecked();
245
246 QApplication::setOverrideCursor (Qt::WaitCursor);
247 if (aState)
248 EnableRaytracing();
249 else
250 DisableRaytracing();
251 QApplication::restoreOverrideCursor();
252 }
253
254 if (aSentBy == myRaytraceActions->at (ToolShadowsId))
255 {
256 bool aState = myRaytraceActions->at (ToolShadowsId)->isChecked();
257 SetRaytracedShadows (aState);
258 }
259
260 if (aSentBy == myRaytraceActions->at (ToolReflectionsId))
261 {
262 bool aState = myRaytraceActions->at (ToolReflectionsId)->isChecked();
263 SetRaytracedReflections (aState);
264 }
265
266 if (aSentBy == myRaytraceActions->at (ToolAntialiasingId))
267 {
268 bool aState = myRaytraceActions->at (ToolAntialiasingId)->isChecked();
269 SetRaytracedAntialiasing (aState);
270 }
271}
272
273void View::SetRaytracedAntialiasing (bool theState)
274{
2ad228f6 275 myView->ChangeRenderingParams().IsAntialiasingEnabled = theState;
861a7b03 276
277 myIsAntialiasingEnabled = theState;
278
279 myContext->UpdateCurrentViewer();
280}
281
282void View::EnableRaytracing()
283{
284 if (!myIsRaytracing)
2ad228f6 285 myView->ChangeRenderingParams().Method = Graphic3d_RM_RAYTRACING;
861a7b03 286
287 myIsRaytracing = true;
288
289 myContext->UpdateCurrentViewer();
290}
291
292void View::DisableRaytracing()
293{
294 if (myIsRaytracing)
2ad228f6 295 myView->ChangeRenderingParams().Method = Graphic3d_RM_RASTERIZATION;
861a7b03 296
297 myIsRaytracing = false;
298
299 myContext->UpdateCurrentViewer();
e276548b 300}
301
7fd59977 302void View::updateToggled( bool isOn )
303{
2ad228f6 304 QAction* sentBy = (QAction*)sender();
7fd59977 305
2ad228f6 306 if( !isOn )
307 return;
7fd59977 308
2ad228f6 309 for ( int i = ViewFitAllId; i < ViewHlrOffId; i++ )
310 {
311 QAction* anAction = myViewActions->at( i );
312
313 if ( ( anAction == myViewActions->at( ViewFitAreaId ) ) ||
314 ( anAction == myViewActions->at( ViewZoomId ) ) ||
315 ( anAction == myViewActions->at( ViewPanId ) ) ||
316 ( anAction == myViewActions->at( ViewGlobalPanId ) ) ||
317 ( anAction == myViewActions->at( ViewRotationId ) ) )
7fd59977 318 {
2ad228f6 319 if ( anAction && ( anAction != sentBy ) )
320 {
321 anAction->setCheckable( true );
322 anAction->setChecked( false );
323 }
324 else
325 {
326 if ( sentBy == myViewActions->at( ViewFitAreaId ) )
327 setCursor( *handCursor );
328 else if ( sentBy == myViewActions->at( ViewZoomId ) )
329 setCursor( *zoomCursor );
330 else if ( sentBy == myViewActions->at( ViewPanId ) )
331 setCursor( *panCursor );
332 else if ( sentBy == myViewActions->at( ViewGlobalPanId ) )
333 setCursor( *globPanCursor );
334 else if ( sentBy == myViewActions->at( ViewRotationId ) )
335 setCursor( *rotCursor );
336 else
337 setCursor( *defCursor );
338
339 sentBy->setCheckable( false );
340 }
7fd59977 341 }
2ad228f6 342 }
7fd59977 343}
344
345void View::initCursors()
346{
2ad228f6 347 if ( !defCursor )
348 defCursor = new QCursor( Qt::ArrowCursor );
349 if ( !handCursor )
350 handCursor = new QCursor( Qt::PointingHandCursor );
351 if ( !panCursor )
352 panCursor = new QCursor( Qt::SizeAllCursor );
353 if ( !globPanCursor )
354 globPanCursor = new QCursor( Qt::CrossCursor );
355 if ( !zoomCursor )
356 zoomCursor = new QCursor( QPixmap( ApplicationCommonWindow::getResourceDir() + QString( "/" ) + QObject::tr( "ICON_CURSOR_ZOOM" ) ) );
357 if ( !rotCursor )
358 rotCursor = new QCursor( QPixmap( ApplicationCommonWindow::getResourceDir() + QString( "/" ) + QObject::tr( "ICON_CURSOR_ROTATE" ) ) );
7fd59977 359}
360
361QList<QAction*>* View::getViewActions()
362{
2ad228f6 363 initViewActions();
364 return myViewActions;
7fd59977 365}
366
861a7b03 367QList<QAction*>* View::getRaytraceActions()
368{
2ad228f6 369 initRaytraceActions();
370 return myRaytraceActions;
861a7b03 371}
372
4d183a4b 373/*!
374 Get paint engine for the OpenGL viewer. [ virtual public ]
375*/
376QPaintEngine* View::paintEngine() const
377{
378 return 0;
379}
380
7fd59977 381void View::initViewActions()
382{
383 if ( myViewActions )
384 return;
385
386 myViewActions = new QList<QAction*>();
387 QString dir = ApplicationCommonWindow::getResourceDir() + QString( "/" );
388 QAction* a;
389
390 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_FITALL") ), QObject::tr("MNU_FITALL"), this );
391 a->setToolTip( QObject::tr("TBR_FITALL") );
392 a->setStatusTip( QObject::tr("TBR_FITALL") );
3cb77da4 393 connect( a, SIGNAL( triggered() ) , this, SLOT( fitAll() ) );
7fd59977 394 myViewActions->insert(ViewFitAllId, a);
395
396 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_FITAREA") ), QObject::tr("MNU_FITAREA"), this );
397 a->setToolTip( QObject::tr("TBR_FITAREA") );
398 a->setStatusTip( QObject::tr("TBR_FITAREA") );
3cb77da4 399 connect( a, SIGNAL( triggered() ) , this, SLOT( fitArea() ) );
7fd59977 400
401 a->setCheckable( true );
402 connect( a, SIGNAL( toggled( bool ) ) , this, SLOT( updateToggled( bool ) ) );
403 myViewActions->insert( ViewFitAreaId, a );
404
405 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_ZOOM") ), QObject::tr("MNU_ZOOM"), this );
406 a->setToolTip( QObject::tr("TBR_ZOOM") );
407 a->setStatusTip( QObject::tr("TBR_ZOOM") );
3cb77da4 408 connect( a, SIGNAL( triggered() ) , this, SLOT( zoom() ) );
7fd59977 409
410 a->setCheckable( true );
411 connect( a, SIGNAL( toggled(bool) ) , this, SLOT( updateToggled(bool) ) );
412 myViewActions->insert( ViewZoomId, a );
413
414 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_PAN") ), QObject::tr("MNU_PAN"), this );
415 a->setToolTip( QObject::tr("TBR_PAN") );
416 a->setStatusTip( QObject::tr("TBR_PAN") );
3cb77da4 417 connect( a, SIGNAL( triggered() ) , this, SLOT( pan() ) );
7fd59977 418
419 a->setCheckable( true );
420 connect( a, SIGNAL( toggled(bool) ) , this, SLOT( updateToggled(bool) ) );
421 myViewActions->insert( ViewPanId, a );
422
423 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_GLOBALPAN") ), QObject::tr("MNU_GLOBALPAN"), this );
424 a->setToolTip( QObject::tr("TBR_GLOBALPAN") );
425 a->setStatusTip( QObject::tr("TBR_GLOBALPAN") );
3cb77da4 426 connect( a, SIGNAL( triggered() ) , this, SLOT( globalPan() ) );
7fd59977 427
428 a->setCheckable( true );
429 connect( a, SIGNAL( toggled(bool) ) , this, SLOT( updateToggled(bool) ) );
430 myViewActions->insert( ViewGlobalPanId, a );
431
432 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_FRONT") ), QObject::tr("MNU_FRONT"), this );
433 a->setToolTip( QObject::tr("TBR_FRONT") );
434 a->setStatusTip( QObject::tr("TBR_FRONT") );
3cb77da4 435 connect( a, SIGNAL( triggered() ) , this, SLOT( front() ) );
7fd59977 436 myViewActions->insert( ViewFrontId, a );
437
438 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_BACK") ), QObject::tr("MNU_BACK"), this );
439 a->setToolTip( QObject::tr("TBR_BACK") );
440 a->setStatusTip( QObject::tr("TBR_BACK") );
3cb77da4 441 connect( a, SIGNAL( triggered() ) , this, SLOT( back() ) );
7fd59977 442 myViewActions->insert(ViewBackId, a);
443
444 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_TOP") ), QObject::tr("MNU_TOP"), this );
445 a->setToolTip( QObject::tr("TBR_TOP") );
446 a->setStatusTip( QObject::tr("TBR_TOP") );
3cb77da4 447 connect( a, SIGNAL( triggered() ) , this, SLOT( top() ) );
7fd59977 448 myViewActions->insert( ViewTopId, a );
449
450 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_BOTTOM") ), QObject::tr("MNU_BOTTOM"), this );
451 a->setToolTip( QObject::tr("TBR_BOTTOM") );
452 a->setStatusTip( QObject::tr("TBR_BOTTOM") );
3cb77da4 453 connect( a, SIGNAL( triggered() ) , this, SLOT( bottom() ) );
7fd59977 454 myViewActions->insert( ViewBottomId, a );
455
456 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_LEFT") ), QObject::tr("MNU_LEFT"), this );
457 a->setToolTip( QObject::tr("TBR_LEFT") );
458 a->setStatusTip( QObject::tr("TBR_LEFT") );
3cb77da4 459 connect( a, SIGNAL( triggered() ) , this, SLOT( left() ) );
7fd59977 460 myViewActions->insert( ViewLeftId, a );
461
462 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_RIGHT") ), QObject::tr("MNU_RIGHT"), this );
463 a->setToolTip( QObject::tr("TBR_RIGHT") );
464 a->setStatusTip( QObject::tr("TBR_RIGHT") );
3cb77da4 465 connect( a, SIGNAL( triggered() ) , this, SLOT( right() ) );
7fd59977 466 myViewActions->insert( ViewRightId, a );
467
468 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_AXO") ), QObject::tr("MNU_AXO"), this );
469 a->setToolTip( QObject::tr("TBR_AXO") );
470 a->setStatusTip( QObject::tr("TBR_AXO") );
3cb77da4 471 connect( a, SIGNAL( triggered() ) , this, SLOT( axo() ) );
7fd59977 472 myViewActions->insert( ViewAxoId, a );
473
474 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_ROTATION") ), QObject::tr("MNU_ROTATION"), this );
475 a->setToolTip( QObject::tr("TBR_ROTATION") );
476 a->setStatusTip( QObject::tr("TBR_ROTATION") );
3cb77da4 477 connect( a, SIGNAL( triggered() ) , this, SLOT( rotation() ) );
7fd59977 478 a->setCheckable( true );
479 connect( a, SIGNAL( toggled(bool) ) , this, SLOT( updateToggled(bool) ) );
480 myViewActions->insert( ViewRotationId, a );
481
482 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_RESET") ), QObject::tr("MNU_RESET"), this );
483 a->setToolTip( QObject::tr("TBR_RESET") );
484 a->setStatusTip( QObject::tr("TBR_RESET") );
3cb77da4 485 connect( a, SIGNAL( triggered() ) , this, SLOT( reset() ) );
7fd59977 486 myViewActions->insert( ViewResetId, a );
487
488 QActionGroup* ag = new QActionGroup( this );
489
490 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_HLROFF") ), QObject::tr("MNU_HLROFF"), this );
491 a->setToolTip( QObject::tr("TBR_HLROFF") );
492 a->setStatusTip( QObject::tr("TBR_HLROFF") );
3cb77da4 493 connect( a, SIGNAL( triggered() ) , this, SLOT( hlrOff() ) );
7fd59977 494 a->setCheckable( true );
495 ag->addAction(a);
496 myViewActions->insert(ViewHlrOffId, a);
497
498 a = new QAction( QPixmap( dir+QObject::tr("ICON_VIEW_HLRON") ), QObject::tr("MNU_HLRON"), this );
499 a->setToolTip( QObject::tr("TBR_HLRON") );
500 a->setStatusTip( QObject::tr("TBR_HLRON") );
3cb77da4 501 connect( a, SIGNAL( triggered() ) ,this, SLOT( hlrOn() ) );
7fd59977 502
503 a->setCheckable( true );
504 ag->addAction(a);
505 myViewActions->insert( ViewHlrOnId, a );
506}
507
861a7b03 508void View::initRaytraceActions()
509{
510 if ( myRaytraceActions )
511 return;
512
513 myRaytraceActions = new QList<QAction*>();
514 QString dir = ApplicationCommonWindow::getResourceDir() + QString( "/" );
515 QAction* a;
516
517 a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_RAYTRACING") ), QObject::tr("MNU_TOOL_RAYTRACING"), this );
518 a->setToolTip( QObject::tr("TBR_TOOL_RAYTRACING") );
519 a->setStatusTip( QObject::tr("TBR_TOOL_RAYTRACING") );
520 a->setCheckable( true );
521 a->setChecked( false );
a1565817 522 connect( a, SIGNAL( triggered() ) , this, SLOT( onRaytraceAction() ) );
861a7b03 523 myRaytraceActions->insert( ToolRaytracingId, a );
524
525 a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_SHADOWS") ), QObject::tr("MNU_TOOL_SHADOWS"), this );
526 a->setToolTip( QObject::tr("TBR_TOOL_SHADOWS") );
527 a->setStatusTip( QObject::tr("TBR_TOOL_SHADOWS") );
528 a->setCheckable( true );
529 a->setChecked( true );
a1565817 530 connect( a, SIGNAL( triggered() ) , this, SLOT( onRaytraceAction() ) );
861a7b03 531 myRaytraceActions->insert( ToolShadowsId, a );
532
533 a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_REFLECTIONS") ), QObject::tr("MNU_TOOL_REFLECTIONS"), this );
534 a->setToolTip( QObject::tr("TBR_TOOL_REFLECTIONS") );
535 a->setStatusTip( QObject::tr("TBR_TOOL_REFLECTIONS") );
536 a->setCheckable( true );
2ad228f6 537 a->setChecked( false );
a1565817 538 connect( a, SIGNAL( triggered() ) , this, SLOT( onRaytraceAction() ) );
861a7b03 539 myRaytraceActions->insert( ToolReflectionsId, a );
540
541 a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_ANTIALIASING") ), QObject::tr("MNU_TOOL_ANTIALIASING"), this );
542 a->setToolTip( QObject::tr("TBR_TOOL_ANTIALIASING") );
543 a->setStatusTip( QObject::tr("TBR_TOOL_ANTIALIASING") );
544 a->setCheckable( true );
545 a->setChecked( false );
a1565817 546 connect( a, SIGNAL( triggered() ) , this, SLOT( onRaytraceAction() ) );
861a7b03 547 myRaytraceActions->insert( ToolAntialiasingId, a );
548}
549
7fd59977 550void View::mousePressEvent( QMouseEvent* e )
551{
552 if ( e->button() == Qt::LeftButton )
553 onLButtonDown( ( e->buttons() | e->modifiers() ), e->pos() );
554 else if ( e->button() == Qt::MidButton )
555 onMButtonDown( e->buttons() | e->modifiers(), e->pos() );
556 else if ( e->button() == Qt::RightButton )
557 onRButtonDown( e->buttons() | e->modifiers(), e->pos() );
558}
559
560void View::mouseReleaseEvent(QMouseEvent* e)
561{
562 if ( e->button() == Qt::LeftButton )
563 onLButtonUp( e->buttons(), e->pos() );
564 else if ( e->button() == Qt::MidButton )
565 onMButtonUp( e->buttons(), e->pos() );
566 else if( e->button() == Qt::RightButton )
567 onRButtonUp( e->buttons(), e->pos() );
568}
569
570void View::mouseMoveEvent(QMouseEvent* e)
571{
572 onMouseMove( e->buttons(), e->pos() );
573}
574
575void View::activateCursor( const CurrentAction3d mode )
576{
577 switch( mode )
578 {
579 case CurAction3d_DynamicPanning:
2ad228f6 580 setCursor( *panCursor );
581 break;
7fd59977 582 case CurAction3d_DynamicZooming:
2ad228f6 583 setCursor( *zoomCursor );
584 break;
7fd59977 585 case CurAction3d_DynamicRotation:
2ad228f6 586 setCursor( *rotCursor );
587 break;
7fd59977 588 case CurAction3d_GlobalPanning:
2ad228f6 589 setCursor( *globPanCursor );
590 break;
7fd59977 591 case CurAction3d_WindowZooming:
2ad228f6 592 setCursor( *handCursor );
593 break;
7fd59977 594 case CurAction3d_Nothing:
595 default:
2ad228f6 596 setCursor( *defCursor );
597 break;
7fd59977 598 }
599}
600
601void View::onLButtonDown( const int/*Qt::MouseButtons*/ nFlags, const QPoint point )
602{
603 // save the current mouse coordinate in min
604 myXmin = point.x();
605 myYmin = point.y();
606 myXmax = point.x();
607 myYmax = point.y();
608
609 if ( nFlags & CASCADESHORTCUTKEY )
610 {
611 myCurrentMode = CurAction3d_DynamicZooming;
612 }
613 else
614 {
615 switch ( myCurrentMode )
616 {
617 case CurAction3d_Nothing:
618 if ( nFlags & MULTISELECTIONKEY )
2ad228f6 619 MultiDragEvent( myXmax, myYmax, -1 );
7fd59977 620 else
2ad228f6 621 DragEvent( myXmax, myYmax, -1 );
7fd59977 622 break;
623 case CurAction3d_DynamicZooming:
624 break;
625 case CurAction3d_WindowZooming:
626 break;
627 case CurAction3d_DynamicPanning:
628 break;
629 case CurAction3d_GlobalPanning:
630 break;
631 case CurAction3d_DynamicRotation:
de75ed09 632 if (myHlrModeIsOn)
633 {
2ad228f6 634 myView->SetComputedMode (Standard_False);
de75ed09 635 }
7fd59977 636 myView->StartRotation( point.x(), point.y() );
637 break;
638 default:
9775fa61 639 throw Standard_Failure( "incompatible Current Mode" );
7fd59977 640 break;
641 }
642 }
643 activateCursor( myCurrentMode );
644}
645
de75ed09 646void View::onMButtonDown( const int/*Qt::MouseButtons*/ nFlags, const QPoint /*point*/ )
7fd59977 647{
648 if ( nFlags & CASCADESHORTCUTKEY )
649 myCurrentMode = CurAction3d_DynamicPanning;
650 activateCursor( myCurrentMode );
651}
652
653void View::onRButtonDown( const int/*Qt::MouseButtons*/ nFlags, const QPoint point )
654{
655 if ( nFlags & CASCADESHORTCUTKEY )
656 {
de75ed09 657 if (myHlrModeIsOn)
658 {
659 myView->SetComputedMode (Standard_False);
660 }
7fd59977 661 myCurrentMode = CurAction3d_DynamicRotation;
662 myView->StartRotation( point.x(), point.y() );
663 }
664 else
665 {
666 Popup( point.x(), point.y() );
667 }
668 activateCursor( myCurrentMode );
669}
670
671void View::onLButtonUp( Qt::MouseButtons nFlags, const QPoint point )
672{
673 switch( myCurrentMode )
674 {
675 case CurAction3d_Nothing:
676 if ( point.x() == myXmin && point.y() == myYmin )
677 {
2ad228f6 678 // no offset between down and up --> selectEvent
679 myXmax = point.x();
680 myYmax = point.y();
681 if ( nFlags & MULTISELECTIONKEY )
682 MultiInputEvent( point.x(), point.y() );
7fd59977 683 else
2ad228f6 684 InputEvent( point.x(), point.y() );
7fd59977 685 }
686 else
687 {
2ad228f6 688 DrawRectangle( myXmin, myYmin, myXmax, myYmax, Standard_False );
689 myXmax = point.x();
690 myYmax = point.y();
691 if ( nFlags & MULTISELECTIONKEY )
692 MultiDragEvent( point.x(), point.y(), 1 );
7fd59977 693 else
2ad228f6 694 DragEvent( point.x(), point.y(), 1 );
7fd59977 695 }
696 break;
697 case CurAction3d_DynamicZooming:
698 myCurrentMode = CurAction3d_Nothing;
699 noActiveActions();
700 break;
701 case CurAction3d_WindowZooming:
702 DrawRectangle( myXmin, myYmin, myXmax, myYmax, Standard_False );//,LongDash);
703 myXmax = point.x();
704 myYmax = point.y();
705 if ( (abs( myXmin - myXmax ) > ValZWMin ) ||
706 (abs( myYmin - myYmax ) > ValZWMin ) )
2ad228f6 707 myView->WindowFitAll( myXmin, myYmin, myXmax, myYmax );
7fd59977 708 myCurrentMode = CurAction3d_Nothing;
709 noActiveActions();
710 break;
711 case CurAction3d_DynamicPanning:
712 myCurrentMode = CurAction3d_Nothing;
713 noActiveActions();
714 break;
715 case CurAction3d_GlobalPanning :
716 myView->Place( point.x(), point.y(), myCurZoom );
717 myCurrentMode = CurAction3d_Nothing;
718 noActiveActions();
719 break;
720 case CurAction3d_DynamicRotation:
721 myCurrentMode = CurAction3d_Nothing;
722 noActiveActions();
723 break;
724 default:
9775fa61 725 throw Standard_Failure(" incompatible Current Mode ");
7fd59977 726 break;
727 }
728 activateCursor( myCurrentMode );
729 ApplicationCommonWindow::getApplication()->onSelectionChanged();
730}
731
de75ed09 732void View::onMButtonUp( Qt::MouseButtons /*nFlags*/, const QPoint /*point*/ )
7fd59977 733{
734 myCurrentMode = CurAction3d_Nothing;
735 activateCursor( myCurrentMode );
736}
737
de75ed09 738void View::onRButtonUp( Qt::MouseButtons /*nFlags*/, const QPoint point )
7fd59977 739{
740 if ( myCurrentMode == CurAction3d_Nothing )
741 Popup( point.x(), point.y() );
742 else
743 {
744 QApplication::setOverrideCursor( Qt::WaitCursor );
745 // reset tyhe good Degenerated mode according to the strored one
746 // --> dynamic rotation may have change it
1eeef710 747 if (myHlrModeIsOn)
748 {
749 myView->SetComputedMode (myHlrModeIsOn);
750 myView->Redraw();
751 }
7fd59977 752 QApplication::restoreOverrideCursor();
753 myCurrentMode = CurAction3d_Nothing;
754 }
755 activateCursor( myCurrentMode );
756}
757
758void View::onMouseMove( Qt::MouseButtons nFlags, const QPoint point )
759{
760 if ( nFlags & Qt::LeftButton || nFlags & Qt::RightButton || nFlags & Qt::MidButton )
761 {
762 switch ( myCurrentMode )
763 {
764 case CurAction3d_Nothing:
2ad228f6 765 myXmax = point.x();
766 myYmax = point.y();
767 DrawRectangle( myXmin, myYmin, myXmax, myYmax, Standard_False );
768 if ( nFlags & MULTISELECTIONKEY )
769 MultiDragEvent( myXmax, myYmax, 0 );
7fd59977 770 else
2ad228f6 771 DragEvent( myXmax, myYmax, 0 );
772 DrawRectangle( myXmin, myYmin, myXmax, myYmax, Standard_True );
7fd59977 773 break;
774 case CurAction3d_DynamicZooming:
2ad228f6 775 myView->Zoom( myXmax, myYmax, point.x(), point.y() );
7fd59977 776 myXmax = point.x();
777 myYmax = point.y();
2ad228f6 778 break;
7fd59977 779 case CurAction3d_WindowZooming:
2ad228f6 780 myXmax = point.x();
7fd59977 781 myYmax = point.y();
2ad228f6 782 DrawRectangle( myXmin, myYmin, myXmax, myYmax, Standard_False );
783 DrawRectangle( myXmin, myYmin, myXmax, myYmax, Standard_True );
784 break;
7fd59977 785 case CurAction3d_DynamicPanning:
2ad228f6 786 myView->Pan( point.x() - myXmax, myYmax - point.y() );
787 myXmax = point.x();
788 myYmax = point.y();
789 break;
7fd59977 790 case CurAction3d_GlobalPanning:
791 break;
792 case CurAction3d_DynamicRotation:
2ad228f6 793 myView->Rotation( point.x(), point.y() );
794 myView->Redraw();
7fd59977 795 break;
796 default:
9775fa61 797 throw Standard_Failure( "incompatible Current Mode" );
7fd59977 798 break;
799 }
800 }
801 else
802 {
803 myXmax = point.x();
804 myYmax = point.y();
2ad228f6 805 if ( nFlags & MULTISELECTIONKEY )
806 MultiMoveEvent( point.x(), point.y() );
7fd59977 807 else
2ad228f6 808 MoveEvent( point.x(), point.y() );
7fd59977 809 }
810}
811
812void View::DragEvent( const int x, const int y, const int TheState )
813{
814 // TheState == -1 button down
815 // TheState == 0 move
816 // TheState == 1 button up
817
818 static Standard_Integer theButtonDownX = 0;
819 static Standard_Integer theButtonDownY = 0;
820
821 if ( TheState == -1 )
822 {
823 theButtonDownX = x;
824 theButtonDownY = y;
825 }
826
4d183a4b 827 if ( TheState == 1 )
7fd59977 828 {
0577ae8c 829 myContext->Select( theButtonDownX, theButtonDownY, x, y, myView, Standard_True );
7fd59977 830 emit selectionChanged();
831 }
832}
833
de75ed09 834void View::InputEvent( const int /*x*/, const int /*y*/ )
7fd59977 835{
0577ae8c 836 myContext->Select (Standard_True);
7fd59977 837 emit selectionChanged();
838}
839
840void View::MoveEvent( const int x, const int y )
841{
0577ae8c 842 myContext->MoveTo( x, y, myView, Standard_True );
7fd59977 843}
844
845void View::MultiMoveEvent( const int x, const int y )
846{
0577ae8c 847 myContext->MoveTo( x, y, myView, Standard_True );
7fd59977 848}
849
850void View::MultiDragEvent( const int x, const int y, const int TheState )
851{
852 static Standard_Integer theButtonDownX = 0;
853 static Standard_Integer theButtonDownY = 0;
854
855 if ( TheState == -1 )
856 {
857 theButtonDownX = x;
858 theButtonDownY = y;
859 }
860 if ( TheState == 0 )
861 {
0577ae8c 862 myContext->ShiftSelect( theButtonDownX, theButtonDownY, x, y, myView, Standard_True );
7fd59977 863 emit selectionChanged();
864 }
865}
866
de75ed09 867void View::MultiInputEvent( const int /*x*/, const int /*y*/ )
7fd59977 868{
0577ae8c 869 myContext->ShiftSelect (Standard_True);
7fd59977 870 emit selectionChanged();
871}
872
de75ed09 873void View::Popup( const int /*x*/, const int /*y*/ )
7fd59977 874{
875 ApplicationCommonWindow* stApp = ApplicationCommonWindow::getApplication();
861a7b03 876 QMdiArea* ws = ApplicationCommonWindow::getWorkspace();
877 QMdiSubWindow* w = ws->activeSubWindow();
7fd59977 878 if ( myContext->NbSelected() )
879 {
880 QList<QAction*>* aList = stApp->getToolActions();
881 QMenu* myToolMenu = new QMenu( 0 );
2ad228f6 882 myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolWireframeId ) );
883 myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolShadingId ) );
884 myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolColorId ) );
7fd59977 885
886 QMenu* myMaterMenu = new QMenu( myToolMenu );
887
888 QList<QAction*>* aMeterActions = ApplicationCommonWindow::getApplication()->getMaterialActions();
889
890 QString dir = ApplicationCommonWindow::getResourceDir() + QString( "/" );
2ad228f6 891 myMaterMenu = myToolMenu->addMenu( QPixmap( dir+QObject::tr("ICON_TOOL_MATER")), QObject::tr("MNU_MATER") );
7fd59977 892 for ( int i = 0; i < aMeterActions->size(); i++ )
2ad228f6 893 myMaterMenu->addAction( aMeterActions->at( i ) );
7fd59977 894
895 myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolTransparencyId ) );
2ad228f6 896 myToolMenu->addAction( aList->at( ApplicationCommonWindow::ToolDeleteId ) );
7fd59977 897 addItemInPopup(myToolMenu);
2ad228f6 898 myToolMenu->exec( QCursor::pos() );
7fd59977 899 delete myToolMenu;
900 }
901 else
902 {
e276548b 903 if (!myBackMenu)
904 {
905 myBackMenu = new QMenu( 0 );
906
2ad228f6 907 QAction* a = new QAction( QObject::tr("MNU_CH_BACK"), this );
908 a->setToolTip( QObject::tr("TBR_CH_BACK") );
a1565817 909 connect( a, SIGNAL( triggered() ), this, SLOT( onBackground() ) );
2ad228f6 910 myBackMenu->addAction( a );
e276548b 911 addItemInPopup(myBackMenu);
912
913 a = new QAction( QObject::tr("MNU_CH_ENV_MAP"), this );
2ad228f6 914 a->setToolTip( QObject::tr("TBR_CH_ENV_MAP") );
a1565817 915 connect( a, SIGNAL( triggered() ), this, SLOT( onEnvironmentMap() ) );
e276548b 916 a->setCheckable( true );
917 a->setChecked( false );
2ad228f6 918 myBackMenu->addAction( a );
e276548b 919 addItemInPopup(myBackMenu);
920 }
921
7fd59977 922 myBackMenu->exec( QCursor::pos() );
7fd59977 923 }
924 if ( w )
925 w->setFocus();
926}
927
de75ed09 928void View::addItemInPopup( QMenu* /*theMenu*/)
7fd59977 929{
930}
931
932void View::DrawRectangle(const int MinX, const int MinY,
2ad228f6 933 const int MaxX, const int MaxY, const bool Draw)
7fd59977 934{
935 static Standard_Integer StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
936 static Standard_Boolean m_IsVisible;
937
938 StoredMinX = (MinX < MaxX) ? MinX: MaxX ;
939 StoredMinY = (MinY < MaxY) ? MinY: MaxY ;
940 StoredMaxX = (MinX > MaxX) ? MinX: MaxX ;
941 StoredMaxY = (MinY > MaxY) ? MinY: MaxY ;
942
943 QRect aRect;
944 aRect.setRect( StoredMinX, StoredMinY, abs(StoredMaxX-StoredMinX), abs(StoredMaxY-StoredMinY));
945
946 if ( !myRectBand )
947 {
948 myRectBand = new QRubberBand( QRubberBand::Rectangle, this );
861a7b03 949 myRectBand->setStyle( QStyleFactory::create("windows") );
7fd59977 950 myRectBand->setGeometry( aRect );
951 myRectBand->show();
952
953 /*QPalette palette;
954 palette.setColor(myRectBand->foregroundRole(), Qt::white);
955 myRectBand->setPalette(palette);*/
956 }
957
958 if ( m_IsVisible && !Draw ) // move or up : erase at the old position
959 {
960 myRectBand->hide();
961 delete myRectBand;
962 myRectBand = 0;
963 m_IsVisible = false;
964 }
965
966 if (Draw) // move : draw
967 {
968 //aRect.setRect( StoredMinX, StoredMinY, abs(StoredMaxX-StoredMinX), abs(StoredMaxY-StoredMinY));
969 m_IsVisible = true;
970 myRectBand->setGeometry( aRect );
971 //myRectBand->show();
972 }
973}
974
975void View::noActiveActions()
976{
977 for ( int i = ViewFitAllId; i < ViewHlrOffId ; i++ )
978 {
979 QAction* anAction = myViewActions->at( i );
980 if( ( anAction == myViewActions->at( ViewFitAreaId ) ) ||
981 ( anAction == myViewActions->at( ViewZoomId ) ) ||
982 ( anAction == myViewActions->at( ViewPanId ) ) ||
983 ( anAction == myViewActions->at( ViewGlobalPanId ) ) ||
984 ( anAction == myViewActions->at( ViewRotationId ) ) )
985 {
986 setCursor( *defCursor );
2ad228f6 987 anAction->setCheckable( true );
988 anAction->setChecked( false );
7fd59977 989 }
990 }
991}
992
993void View::onBackground()
994{
995 QColor aColor ;
996 Standard_Real R1;
997 Standard_Real G1;
998 Standard_Real B1;
999 myView->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
1000 aColor.setRgb(R1*255,G1*255,B1*255);
1001
1002 QColor aRetColor = QColorDialog::getColor(aColor);
1003
1004 if( aRetColor.isValid() )
1005 {
1006 R1 = aRetColor.red()/255.;
1007 G1 = aRetColor.green()/255.;
1008 B1 = aRetColor.blue()/255.;
1009 myView->SetBackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
1010 }
1011 myView->Redraw();
1012}
1013
e276548b 1014void View::onEnvironmentMap()
1015{
1016 if (myBackMenu->actions().at(1)->isChecked())
1017 {
1018 QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "",
1019 tr("All Image Files (*.bmp *.gif *.jpg *.jpeg *.png *.tga)"));
6cfc06f0
BB
1020
1021 const TCollection_AsciiString anUtf8Path (fileName.toUtf8().data());
1022
1023 Handle(Graphic3d_TextureEnv) aTexture = new Graphic3d_TextureEnv( anUtf8Path );
e276548b 1024
1025 myView->SetTextureEnv (aTexture);
e276548b 1026 }
1027 else
1028 {
83da37b1 1029 myView->SetTextureEnv (Handle(Graphic3d_TextureEnv)());
e276548b 1030 }
1031
1032 myView->Redraw();
1033}
1034
7fd59977 1035bool View::dump(Standard_CString theFile)
1036{
7fd59977 1037 return myView->Dump(theFile);
1038}
1039
1040Handle(V3d_View)& View::getView()
1041{
1042 return myView;
1043}
1044
1045Handle(AIS_InteractiveContext)& View::getContext()
1046{
1047 return myContext;
1048}
1049
1050View::CurrentAction3d View::getCurrentMode()
1051{
1052 return myCurrentMode;
1053}
1054
1055
1056