1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SALOME VTKViewer : build VTK viewer into Salome desktop
27 #include "SVTK_RenderWindowInteractor.h"
29 #include "SVTK_InteractorStyle.h"
30 #include "SVTK_Renderer.h"
31 #include "SVTK_Functor.h"
32 #include "SALOME_Actor.h"
35 // Put Qt includes before the X11 includes which #define the symbol None
36 // (see SVTK_SpaceMouse.h) to avoid the compilation error.
40 #include <QMouseEvent>
42 #include "SVTK_SpaceMouse.h"
43 #include "SVTK_Event.h"
45 #include "VTKViewer_Algorithm.h"
48 #include <vtkObjectFactory.h>
49 #include <vtkRendererCollection.h>
50 #include <vtkRenderWindow.h>
51 #include <vtkGenericRenderWindowInteractor.h>
52 #include <vtkCallbackCommand.h>
53 #include <vtkCommand.h>
54 #include <vtkPicker.h>
55 #include <vtkCamera.h>
57 static bool GENERATE_SUIT_EVENTS = true;
58 static bool FOCUS_UNDER_MOUSE = false;
64 QVTK_RenderWindowInteractor
65 ::QVTK_RenderWindowInteractor(QWidget* theParent,
68 myRenderWindow(vtkRenderWindow::New())
70 setAttribute( Qt::WA_PaintOnScreen );
71 setAttribute( Qt::WA_NoSystemBackground );
73 setObjectName(theName);
75 setMouseTracking(true);
77 myRenderWindow->Delete();
78 myRenderWindow->DoubleBufferOn();
81 myRenderWindow->SetDisplayId((void*)QX11Info::display());
83 myRenderWindow->SetWindowId((void*)winId());
87 To initialize by vtkGenericRenderWindowInteractor instance
90 QVTK_RenderWindowInteractor
91 ::Initialize(vtkGenericRenderWindowInteractor* theDevice)
94 myDevice->SetRenderWindow( NULL );
99 theDevice->SetRenderWindow( getRenderWindow() );
105 QVTK_RenderWindowInteractor
106 ::~QVTK_RenderWindowInteractor()
109 SVTK_SpaceMouse* aSpaceMouse = SVTK_SpaceMouse::getInstance();
110 if ( aSpaceMouse && aSpaceMouse->isSpaceMouseOn() )
111 aSpaceMouse->close( QX11Info::display() );
117 \return corresponding render window interactor
119 vtkGenericRenderWindowInteractor*
120 QVTK_RenderWindowInteractor
123 return myDevice.GetPointer();
127 \return corresponding render window
130 QVTK_RenderWindowInteractor
133 return myRenderWindow.GetPointer();
137 Just to simplify usage of its device (vtkGenericRenderWindowInteractor)
140 QVTK_RenderWindowInteractor
141 ::InvokeEvent(unsigned long theEvent, void* theCallData)
143 GetDevice()->InvokeEvent(theEvent,theCallData);
147 Get paint engine for the scene
149 QPaintEngine* QVTK_RenderWindowInteractor::paintEngine() const
155 Need for initial contents display on Win32
158 QVTK_RenderWindowInteractor
162 update(); // needed for initial contents display on Win32
166 To implement final initialization, just before the widget is displayed
169 QVTK_RenderWindowInteractor
172 // Final initialization just before the widget is displayed
173 GetDevice()->SetSize(width(),height());
174 if(!GetDevice()->GetInitialized() && GetDevice()->GetRenderWindow()){
175 GetDevice()->Initialize();
176 GetDevice()->ConfigureEvent();
181 To adjust widget and vtkRenderWindow size
184 QVTK_RenderWindowInteractor
185 ::resize(int w, int h)
187 GetDevice()->UpdateSize(w,h);
191 Custom paint event handler
194 QVTK_RenderWindowInteractor
195 ::paintEvent( QPaintEvent* theEvent )
197 GetDevice()->CreateTimer(VTKI_TIMER_FIRST);
202 Custom resize event handler
205 QVTK_RenderWindowInteractor
206 ::resizeEvent( QResizeEvent* theEvent )
208 int* aSize = getRenderWindow()->GetSize();
209 int aWidth = aSize[0];
210 int aHeight = aSize[1];
212 GetDevice()->UpdateSize(width(),height());
214 if(isVisible() && aWidth && aHeight){
215 if( aWidth != width() || aHeight != height() ) {
216 vtkRendererCollection * aRenderers = getRenderWindow()->GetRenderers();
217 aRenderers->InitTraversal();
219 if(vtkRenderer *aRenderer = aRenderers->GetNextItem()) {
220 vtkCamera *aCamera = aRenderer->GetActiveCamera();
221 double aScale = aCamera->GetParallelScale();
222 if((aWidth - width())*(aHeight - height()) > 0)
223 aCoeff = sqrt(double(aWidth)/double(width())*double(height())/double(aHeight));
225 aCoeff = double(aWidth)/double(width());
226 aCamera->SetParallelScale(aScale*aCoeff);
237 Custom context menu event handler
240 QVTK_RenderWindowInteractor
241 ::contextMenuEvent( QContextMenuEvent* event )
245 Custom mouse move event handler
248 QVTK_RenderWindowInteractor
249 ::mouseMoveEvent( QMouseEvent* event )
251 GetDevice()->SetEventInformationFlipY(event->x(),
253 event->modifiers() & Qt::ControlModifier,
254 event->modifiers() & Qt::ShiftModifier);
255 GetDevice()->MouseMoveEvent();
260 Custom mouse press event handler
263 QVTK_RenderWindowInteractor
264 ::mousePressEvent( QMouseEvent* event )
266 GetDevice()->SetEventInformationFlipY(event->x(),
268 event->modifiers() & Qt::ControlModifier,
269 event->modifiers() & Qt::ShiftModifier);
270 if( event->button() & Qt::LeftButton )
271 GetDevice()->LeftButtonPressEvent();
272 else if( event->button() & Qt::MidButton )
273 GetDevice()->MiddleButtonPressEvent();
274 else if( event->button() & Qt::RightButton )
275 GetDevice()->RightButtonPressEvent();
280 Custom mouse release event handler
283 QVTK_RenderWindowInteractor
284 ::mouseReleaseEvent( QMouseEvent *event )
286 GetDevice()->SetEventInformationFlipY(event->x(),
288 event->modifiers() & Qt::ControlModifier,
289 event->modifiers() & Qt::ShiftModifier);
291 if( event->button() & Qt::LeftButton )
292 GetDevice()->LeftButtonReleaseEvent();
293 else if( event->button() & Qt::MidButton )
294 GetDevice()->MiddleButtonReleaseEvent();
295 else if( event->button() & Qt::RightButton )
296 GetDevice()->RightButtonReleaseEvent();
301 Custom mouse double click event handler
304 QVTK_RenderWindowInteractor
305 ::mouseDoubleClickEvent( QMouseEvent* event )
310 Custom mouse wheel event handler
313 QVTK_RenderWindowInteractor
314 ::wheelEvent( QWheelEvent* event )
318 GetDevice()->SetEventInformationFlipY(event->x(),
320 event->modifiers() & Qt::ControlModifier,
321 event->modifiers() & Qt::ShiftModifier);
322 if ( event->delta()>0)
323 GetDevice()->MouseWheelForwardEvent();
325 GetDevice()->MouseWheelBackwardEvent();
330 Custom key press event handler
333 QVTK_RenderWindowInteractor
334 ::keyPressEvent( QKeyEvent* event )
336 GetDevice()->SetKeyEventInformation(event->modifiers() & Qt::ControlModifier,
337 event->modifiers() & Qt::ShiftModifier,
339 GetDevice()->KeyPressEvent();
340 GetDevice()->CharEvent();
344 Custom key release event handler
347 QVTK_RenderWindowInteractor
348 ::keyReleaseEvent( QKeyEvent * event )
350 GetDevice()->SetKeyEventInformation(event->modifiers() & Qt::ControlModifier,
351 event->modifiers() & Qt::ShiftModifier,
353 GetDevice()->KeyReleaseEvent();
358 Custom enter event handler
361 QVTK_RenderWindowInteractor
362 ::enterEvent( QEvent* event )
364 if(FOCUS_UNDER_MOUSE){
368 GetDevice()->EnterEvent();
372 Custom leave event handler
375 QVTK_RenderWindowInteractor
376 ::leaveEvent( QEvent * )
378 GetDevice()->LeaveEvent();
382 Reimplemented from QWidget in order to set window - receiver
383 of space mouse events.
386 QVTK_RenderWindowInteractor
387 ::focusInEvent( QFocusEvent* event )
389 QWidget::focusInEvent( event );
392 // register set space mouse events receiver
393 SVTK_SpaceMouse* aSpaceMouse = SVTK_SpaceMouse::getInstance();
397 if ( !aSpaceMouse->isSpaceMouseOn() )
398 // initialize 3D space mouse driver
399 aSpaceMouse->initialize( QX11Info::display(), winId() );
401 aSpaceMouse->setWindow( QX11Info::display(), winId() );
407 Reimplemented from QWidget in order to set window - receiver
408 of space mouse events.
411 QVTK_RenderWindowInteractor
412 ::focusOutEvent ( QFocusEvent* event )
414 QWidget::focusOutEvent( event );
417 // unregister set space mouse events receiver
418 SVTK_SpaceMouse* aSpaceMouse = SVTK_SpaceMouse::getInstance();
419 if ( aSpaceMouse && aSpaceMouse->isSpaceMouseOn() )
420 aSpaceMouse->setWindow( QX11Info::display(), 0 );
428 To handle native Win32 events (from such devices as SpaceMouse)
430 bool QVTK_RenderWindowInteractor::winEvent( MSG* msg, long* result )
432 // TODO: Implement event handling for SpaceMouse
433 return QWidget::winEvent( msg, result);
439 To handle native X11 events (from such devices as SpaceMouse)
442 QVTK_RenderWindowInteractor
443 ::x11Event( XEvent *xEvent )
445 // handle 3d space mouse events
446 if ( SVTK_SpaceMouse* aSpaceMouse = SVTK_SpaceMouse::getInstance() )
448 if ( aSpaceMouse->isSpaceMouseOn() && xEvent->type == ClientMessage )
450 SVTK_SpaceMouse::MoveEvent anEvent;
451 int type = aSpaceMouse->translateEvent( QX11Info::display(), xEvent, &anEvent, 1.0, 1.0 );
454 case SVTK_SpaceMouse::SpaceMouseMove:
455 GetDevice()->InvokeEvent( SVTK::SpaceMouseMoveEvent, anEvent.data );
457 case SVTK_SpaceMouse::SpaceButtonPress:
458 GetDevice()->InvokeEvent( SVTK::SpaceMouseButtonEvent, &anEvent.button );
460 case SVTK_SpaceMouse::SpaceButtonRelease:
463 return true; // stop handling the event
467 return QWidget::x11Event( xEvent );
475 SVTK_RenderWindowInteractor
476 ::SVTK_RenderWindowInteractor(QWidget* theParent,
477 const char* theName):
478 QVTK_RenderWindowInteractor(theParent,theName),
479 myEventCallbackCommand(vtkCallbackCommand::New())
481 myEventCallbackCommand->Delete();
483 myEventCallbackCommand->SetClientData(this);
486 myEventCallbackCommand->SetCallback(SVTK_RenderWindowInteractor::ProcessEvents);
490 To initialize properly the class
493 SVTK_RenderWindowInteractor
494 ::Initialize(vtkGenericRenderWindowInteractor* theDevice,
495 SVTK_Renderer* theRenderer,
496 SVTK_Selector* theSelector)
498 QVTK_RenderWindowInteractor::Initialize(theDevice);
499 SetRenderer(theRenderer);
500 SetSelector(theSelector);
506 SVTK_RenderWindowInteractor
507 ::~SVTK_RenderWindowInteractor()
509 // Sequence of the destruction call are fixed and should be changed.
510 // vtkRenderWindow instance should be destroyed after all vtkRenderer's
511 GetDevice()->SetInteractorStyle(NULL);
512 while(!myInteractorStyles.empty()){
513 const PInteractorStyle& aStyle = myInteractorStyles.top();
514 aStyle->SetInteractor(NULL);
515 myInteractorStyles.pop();
520 GetDevice()->SetRenderWindow(NULL);
524 To get corresponding SVTK_Renderer instance
527 SVTK_RenderWindowInteractor
530 return myRenderer.GetPointer();
534 To get corresponding SVTK_Renderer device (just to simplify collobaration with SVTK_Renderer)
537 SVTK_RenderWindowInteractor
540 return GetRenderer()->GetDevice();
545 \param theRenderer - new renderer
548 SVTK_RenderWindowInteractor
549 ::SetRenderer(SVTK_Renderer* theRenderer)
551 if(theRenderer == myRenderer.GetPointer())
555 myRenderWindow->RemoveRenderer(getRenderer());
557 myRenderer = theRenderer;
560 myRenderWindow->AddRenderer(getRenderer());
565 Changes interactor style
566 \param theStyle - new interactor style
569 SVTK_RenderWindowInteractor
570 ::InitInteractorStyle(vtkInteractorStyle* theStyle)
572 GetDevice()->SetInteractorStyle(theStyle);
576 To change current interactor style by pushing the new one into the container
579 SVTK_RenderWindowInteractor
580 ::PushInteractorStyle(vtkInteractorStyle* theStyle)
582 myInteractorStyles.push(PInteractorStyle(theStyle));
583 InitInteractorStyle(theStyle);
587 To restore previous interactor style
590 SVTK_RenderWindowInteractor
591 ::PopInteractorStyle()
593 if(GetInteractorStyle())
594 myInteractorStyles.pop();
596 if(GetInteractorStyle())
597 InitInteractorStyle(GetInteractorStyle());
601 To get current interactor style
604 SVTK_RenderWindowInteractor
605 ::GetInteractorStyle()
607 return myInteractorStyles.empty() ? 0 : myInteractorStyles.top().GetPointer();
612 To get current selector
615 SVTK_RenderWindowInteractor
618 return mySelector.GetPointer();
624 \param theSelector - new selector
627 SVTK_RenderWindowInteractor
628 ::SetSelector(SVTK_Selector* theSelector)
630 if(mySelector.GetPointer())
631 mySelector->RemoveObserver(myEventCallbackCommand.GetPointer());
633 mySelector = theSelector;
635 if(mySelector.GetPointer())
636 mySelector->AddObserver(vtkCommand::EndPickEvent,
637 myEventCallbackCommand.GetPointer(),
642 Main process VTK event method
645 SVTK_RenderWindowInteractor
646 ::ProcessEvents(vtkObject* vtkNotUsed(theObject),
647 unsigned long theEvent,
649 void* vtkNotUsed(theCallData))
651 SVTK_RenderWindowInteractor* self = reinterpret_cast<SVTK_RenderWindowInteractor*>(theClientData);
654 case vtkCommand::EndPickEvent:
655 self->onEmitSelectionChanged();
661 To change selection mode (just to simplify collobaration with SVTK_Selector)
664 SVTK_RenderWindowInteractor
665 ::SetSelectionMode(Selection_Mode theMode)
667 mySelector->SetSelectionMode(theMode);
671 To get current selection mode (just to simplify collobaration with SVTK_Selector)
674 SVTK_RenderWindowInteractor
675 ::SelectionMode() const
677 return mySelector->SelectionMode();
682 Emits signal selectionChanged()
685 SVTK_RenderWindowInteractor
686 ::onEmitSelectionChanged()
688 return emit selectionChanged();
693 Custom mouse move event handler
696 SVTK_RenderWindowInteractor
697 ::mouseMoveEvent( QMouseEvent* event )
699 QVTK_RenderWindowInteractor::mouseMoveEvent(event);
701 if(GENERATE_SUIT_EVENTS)
702 emit MouseMove( event );
707 Custom mouse press event handler
710 SVTK_RenderWindowInteractor
711 ::mousePressEvent( QMouseEvent* event )
713 QVTK_RenderWindowInteractor::mousePressEvent(event);
715 if(GENERATE_SUIT_EVENTS)
716 emit MouseButtonPressed( event );
721 Custom mouse release event handler
724 SVTK_RenderWindowInteractor
725 ::mouseReleaseEvent( QMouseEvent *event )
727 SVTK_InteractorStyle* style = 0;
728 bool aRightBtn = event->button() == Qt::RightButton;
729 bool isOperation = false;
730 bool isPolygonalSelection = false;
731 if( aRightBtn && GetInteractorStyle()) {
732 style = dynamic_cast<SVTK_InteractorStyle*>( GetInteractorStyle() );
734 isOperation = style->CurrentState() != VTK_INTERACTOR_STYLE_CAMERA_NONE;
737 QVTK_RenderWindowInteractor::mouseReleaseEvent(event);
740 isPolygonalSelection = style->GetPolygonState() == Finished;
741 style->SetPolygonState( Disable );
744 if ( aRightBtn && !isOperation && !isPolygonalSelection &&
745 !( event->modifiers() & Qt::ControlModifier ) &&
746 !( event->modifiers() & Qt::ShiftModifier ) ) {
747 QContextMenuEvent aEvent( QContextMenuEvent::Mouse,
748 event->pos(), event->globalPos() );
749 emit contextMenuRequested( &aEvent );
751 if(GENERATE_SUIT_EVENTS)
752 emit MouseButtonReleased( event );
757 Custom mouse double click event handler
760 SVTK_RenderWindowInteractor
761 ::mouseDoubleClickEvent( QMouseEvent* event )
763 if( GetInteractorStyle() && event->button() == Qt::LeftButton ) {
764 SVTK_InteractorStyle* style = dynamic_cast<SVTK_InteractorStyle*>( GetInteractorStyle() );
766 style->OnMouseButtonDoubleClick();
769 QVTK_RenderWindowInteractor::mouseDoubleClickEvent(event);
771 if(GENERATE_SUIT_EVENTS)
772 emit MouseDoubleClicked( event );
777 Custom mouse wheel event handler
780 SVTK_RenderWindowInteractor
781 ::wheelEvent( QWheelEvent* event )
783 QVTK_RenderWindowInteractor::wheelEvent(event);
785 if(event->delta() > 0)
786 GetDevice()->InvokeEvent(SVTK::ZoomInEvent,NULL);
788 GetDevice()->InvokeEvent(SVTK::ZoomOutEvent,NULL);
790 if(GENERATE_SUIT_EVENTS)
791 emit WheelMoved( event );
795 Custom key press event handler
798 SVTK_RenderWindowInteractor
799 ::keyPressEvent( QKeyEvent* event )
801 QVTK_RenderWindowInteractor::keyPressEvent(event);
803 if(GENERATE_SUIT_EVENTS)
804 emit KeyPressed( event );
808 Custom key release event handler
811 SVTK_RenderWindowInteractor
812 ::keyReleaseEvent( QKeyEvent * event )
814 QVTK_RenderWindowInteractor::keyReleaseEvent(event);
816 if(GENERATE_SUIT_EVENTS)
817 emit KeyReleased( event );