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