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