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