1 #include "VTKViewer_RenderWindowInteractor.h"
2 #include "VTKViewer_RenderWindow.h"
3 #include "VTKViewer_InteractorStyle.h"
4 #include "SUIT_ViewModel.h"
5 #include "VTKViewer_ViewWindow.h"
7 //#include "SUIT_Application.h"
8 //#include "SUIT_Desktop.h"
10 //#include "SALOME_Selection.h"
11 #include "VTKViewer_Actor.h"
12 #include "VTKViewer_Algorithm.h"
13 #include "VTKViewer_Functor.h"
21 #include <vtkAssemblyNode.h>
23 #include <vtkInteractorStyle.h>
24 #include <vtkObjectFactory.h>
25 #include <vtkPicker.h>
26 #include <vtkCellPicker.h>
27 #include <vtkPointPicker.h>
28 #include <vtkUnstructuredGrid.h>
29 #include <vtkPolyDataMapper.h>
30 #include <vtkSphereSource.h>
31 #include <vtkDataSet.h>
32 #include <vtkMaskPoints.h>
33 #include <vtkVertex.h>
34 #include <vtkRendererCollection.h>
35 #include <vtkPolyDataWriter.h>
40 //****************************************************************
41 VTKViewer_RenderWindowInteractor* VTKViewer_RenderWindowInteractor::New()
43 vtkObject *ret = vtkObjectFactory::CreateInstance("VTKViewer_RenderWindowInteractor") ;
45 return dynamic_cast<VTKViewer_RenderWindowInteractor *>(ret) ;
47 return new VTKViewer_RenderWindowInteractor;
50 //****************************************************************
51 VTKViewer_RenderWindowInteractor::VTKViewer_RenderWindowInteractor()
54 this->mTimer = new QTimer( this ) ;
57 myBasicPicker = vtkPicker::New();
58 myCellPicker = vtkCellPicker::New();
59 myPointPicker = vtkPointPicker::New();
61 myCellActor = VTKViewer_Actor::New();
62 myCellActor->PickableOff();
63 myCellActor->GetProperty()->SetColor(1,1,0);
64 myCellActor->GetProperty()->SetLineWidth(5);
65 myCellActor->GetProperty()->SetRepresentationToSurface();
67 myEdgeActor = VTKViewer_Actor::New();
68 myEdgeActor->PickableOff();
69 myEdgeActor->GetProperty()->SetColor(1,0,0);
70 myEdgeActor->GetProperty()->SetLineWidth(5);
71 myEdgeActor->GetProperty()->SetRepresentationToWireframe();
73 myPointActor = VTKViewer_Actor::New();
74 myPointActor->PickableOff();
75 myPointActor->GetProperty()->SetColor(1,1,0);
76 myPointActor->GetProperty()->SetPointSize(5);
77 myPointActor->GetProperty()->SetRepresentationToPoints();
79 connect(mTimer, SIGNAL(timeout()), this, SLOT(TimerFunc())) ;
82 //****************************************************************
83 VTKViewer_RenderWindowInteractor::~VTKViewer_RenderWindowInteractor()
87 if ( GetRenderWindow() ) {
88 myViewWnd->RemoveActor(myCellActor);
89 myViewWnd->RemoveActor(myEdgeActor);
90 myViewWnd->RemoveActor(myPointActor);
93 myCellActor->Delete();
94 myEdgeActor->Delete();
95 myPointActor->Delete();
97 myBasicPicker->Delete();
98 myCellPicker->Delete();
99 myPointPicker->Delete();
102 //****************************************************************
103 void VTKViewer_RenderWindowInteractor::PrintSelf(ostream& os, vtkIndent indent)
105 vtkRenderWindowInteractor::PrintSelf(os, indent) ;
107 // :NOTE: Fri Apr 21 21:51:05 2000 Pagey
108 // QGL specific stuff goes here. One should add output
109 // lines here if any protected members are added to
114 //****************************************************************
115 // We never allow the VTKViewer_RenderWindowInteractor to control
116 // the event loop. The application always has the control.
118 void VTKViewer_RenderWindowInteractor::Initialize()
121 // We cannot do much unless there is a render window
122 // associated with this interactor.
124 if( ! RenderWindow ) {
125 vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize(): No render window attached!") ;
130 // We cannot hand a render window which is not a VTKViewer_RenderWindow.
131 // One way to force this is to use dynamic_cast and hope that
132 // it works. If the dynamic_cast does not work, we flag an error
133 // and get the hell out.
135 vtkRenderWindow *my_render_win = dynamic_cast<vtkRenderWindow *>(RenderWindow) ;
136 if( !my_render_win ) {
137 vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize() can only handle VTKViewer_RenderWindow.") ;
142 // If the render window has zero size, then set it to a default
145 int* aSize = my_render_win->GetSize();
146 this->Size[0] = ((aSize[0] > 0) ? aSize[0] : 300);
147 this->Size[1] = ((aSize[1] > 0) ? aSize[1] : 300);
149 this->SetPicker(myBasicPicker);
151 SetSelectionTolerance();
154 // Enable the interactor.
159 // Start the rendering of the window.
161 my_render_win->Start() ;
164 // The interactor has been initialized.
166 this->Initialized = 1 ;
172 //----------------------------------------------------------------------------
173 void VTKViewer_RenderWindowInteractor::setViewWindow(VTKViewer_ViewWindow* theViewWnd){
174 myViewWnd = theViewWnd;
177 myViewWnd->InsertActor(myCellActor);
178 myViewWnd->InsertActor(myEdgeActor);
179 myViewWnd->InsertActor(myPointActor);
183 //----------------------------------------------------------------------------
184 void VTKViewer_RenderWindowInteractor::MoveInternalActors()
186 myViewWnd->MoveActor(myCellActor);
187 myViewWnd->MoveActor(myEdgeActor);
188 myViewWnd->MoveActor(myPointActor);
191 //----------------------------------------------------------------------------
192 void VTKViewer_RenderWindowInteractor::SetInteractorStyle(vtkInteractorObserver *theInteractor){
193 myInteractorStyle = dynamic_cast<VTKViewer_InteractorStyle*>(theInteractor);
194 vtkRenderWindowInteractor::SetInteractorStyle(theInteractor);
198 /*void VTKViewer_RenderWindowInteractor::SetSelectionMode(Selection_Mode theMode)
200 myCellActor->SetVisibility(false);
201 myEdgeActor->SetVisibility(false);
202 myPointActor->SetVisibility(false);
206 this->SetPicker(myBasicPicker);
209 this->SetPicker(myPointPicker);
214 case VolumeSelection:
215 case EdgeOfCellSelection:
216 this->SetPicker(myCellPicker);
220 myInteractorStyle->OnSelectionModeChanged();
223 //****************************************************************
224 void VTKViewer_RenderWindowInteractor::SetSelectionProp(const double& theRed, const double& theGreen,
225 const double& theBlue, const int& theWidth)
227 myCellActor->GetProperty()->SetColor(theRed, theGreen, theBlue);
228 myCellActor->GetProperty()->SetLineWidth(theWidth);
230 myPointActor->GetProperty()->SetColor(theRed, theGreen, theBlue);
231 myPointActor->GetProperty()->SetPointSize(theWidth);
234 //****************************************************************
235 void VTKViewer_RenderWindowInteractor::SetSelectionTolerance(const double& theTolNodes, const double& theTolItems)
237 myTolNodes = theTolNodes;
238 myTolItems = theTolItems;
240 myBasicPicker->SetTolerance(myTolItems);
241 myCellPicker->SetTolerance(myTolItems);
242 myPointPicker->SetTolerance(myTolNodes);
246 //****************************************************************
247 void VTKViewer_RenderWindowInteractor::Enable()
250 // Do not need to do anything if already enabled.
252 if( this->Enabled ) {
260 //****************************************************************
261 void VTKViewer_RenderWindowInteractor::Disable()
263 if( ! this->Enabled ) {
271 //****************************************************************
272 void VTKViewer_RenderWindowInteractor::Start()
275 // We do not allow this interactor to control the
276 // event loop. Only the QtApplication objects are
277 // allowed to do that.
279 vtkErrorMacro(<<"VTKViewer_RenderWindowInteractor::Start() not allowed to start event loop.") ;
282 //****************************************************************
283 void VTKViewer_RenderWindowInteractor::UpdateSize(int w, int h)
285 // if the size changed send this on to the RenderWindow
286 if ((w != this->Size[0])||(h != this->Size[1])) {
289 this->RenderWindow->SetSize(w,h);
293 //****************************************************************
294 int VTKViewer_RenderWindowInteractor::CreateTimer(int vtkNotUsed(timertype))
297 // Start a one-shot timer for 10ms.
299 mTimer->start(10, TRUE) ;
303 //****************************************************************
304 int VTKViewer_RenderWindowInteractor::DestroyTimer(void)
307 // :TRICKY: Tue May 2 00:17:32 2000 Pagey
309 // QTimer will automatically expire after 10ms. So
310 // we do not need to do anything here. In fact, we
311 // should not even Stop() the QTimer here because doing
312 // this will skip some of the processing that the TimerFunc()
313 // does and will result in undesirable effects. For
314 // example, this will result in vtkLODActor to leave
315 // the models in low-res mode after the mouse stops
321 //****************************************************************
322 void VTKViewer_RenderWindowInteractor::TimerFunc()
324 if( ! this->Enabled ) {
328 ((vtkInteractorStyle*)this->InteractorStyle)->OnTimer() ;
329 emit RenderWindowModified() ;
332 void VTKViewer_RenderWindowInteractor::MouseMove(QMouseEvent *event) {
333 if( ! this->Enabled ) {
336 myInteractorStyle->OnMouseMove(0, 0, event->x(), event->y()/*this->Size[1] - event->y() - 1*/) ;
337 if (myInteractorStyle->needsRedrawing() )
338 emit RenderWindowModified() ;
341 void VTKViewer_RenderWindowInteractor::LeftButtonPressed(const QMouseEvent *event) {
342 if( ! this->Enabled ) {
345 myInteractorStyle->OnLeftButtonDown((event->state() & ControlButton),
346 (event->state() & ShiftButton),
347 event->x(), event->y());
350 void VTKViewer_RenderWindowInteractor::LeftButtonReleased(const QMouseEvent *event) {
351 if( ! this->Enabled ) {
354 myInteractorStyle->OnLeftButtonUp( (event->state() & ControlButton),
355 (event->state() & ShiftButton),
356 event->x(), event->y() ) ;
359 void VTKViewer_RenderWindowInteractor::MiddleButtonPressed(const QMouseEvent *event) {
360 if( ! this->Enabled ) {
363 myInteractorStyle->OnMiddleButtonDown((event->state() & ControlButton),
364 (event->state() & ShiftButton),
365 event->x(), event->y() ) ;
368 void VTKViewer_RenderWindowInteractor::MiddleButtonReleased(const QMouseEvent *event) {
369 if( ! this->Enabled ) {
372 myInteractorStyle->OnMiddleButtonUp( (event->state() & ControlButton),
373 (event->state() & ShiftButton),
374 event->x(), event->y() ) ;
377 void VTKViewer_RenderWindowInteractor::RightButtonPressed(const QMouseEvent *event) {
378 if( ! this->Enabled ) {
381 myInteractorStyle->OnRightButtonDown( (event->state() & ControlButton),
382 (event->state() & ShiftButton),
383 event->x(), event->y() ) ;
386 void VTKViewer_RenderWindowInteractor::RightButtonReleased(const QMouseEvent *event) {
387 if( ! this->Enabled ) {
390 bool isOperation = myInteractorStyle->CurrentState() != VTK_INTERACTOR_STYLE_CAMERA_NONE;
391 myInteractorStyle->OnRightButtonUp( (event->state() & ControlButton),
392 (event->state() & ShiftButton),
393 event->x(), event->y() );
396 QContextMenuEvent aEvent( QContextMenuEvent::Mouse,
397 event->pos(), event->globalPos(),
399 emit contextMenuRequested( &aEvent );
403 void VTKViewer_RenderWindowInteractor::ButtonPressed(const QMouseEvent *event) {
407 void VTKViewer_RenderWindowInteractor::ButtonReleased(const QMouseEvent *event) {
412 int VTKViewer_RenderWindowInteractor::GetDisplayMode() {
413 return myDisplayMode;
416 void VTKViewer_RenderWindowInteractor::SetDisplayMode(int theMode) {
418 ChangeRepresentationToWireframe();
420 ChangeRepresentationToSurface();
421 myDisplayMode = theMode;
424 //****************************************************************
425 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToWireframe()
426 // change all actors to wireframe
428 ChangeRepresentationToWireframe(GetRenderer()->GetActors());
431 //****************************************************************
432 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToSurface()
434 ChangeRepresentationToSurface(GetRenderer()->GetActors());
438 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToWireframe(vtkActorCollection* theCollection)
441 ForEach<VTKViewer_Actor>(theCollection,
442 TSetFunction<VTKViewer_Actor,int>
443 (&VTKViewer_Actor::setDisplayMode,0));
444 emit RenderWindowModified();
447 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToSurface(vtkActorCollection* theCollection)
450 ForEach<VTKViewer_Actor>(theCollection,
451 TSetFunction<VTKViewer_Actor,int>
452 (&VTKViewer_Actor::setDisplayMode,1));
453 emit RenderWindowModified();
456 //****************************************************************
457 vtkRenderer* VTKViewer_RenderWindowInteractor::GetRenderer()
459 vtkRendererCollection * theRenderers = this->RenderWindow->GetRenderers();
460 theRenderers->InitTraversal();
461 return theRenderers->GetNextItem();
464 //****************************************************************
465 void VTKViewer_RenderWindowInteractor::EraseAll()
469 //****************************************************************
470 void VTKViewer_RenderWindowInteractor::DisplayAll()
473 vtkActorCollection* aCollection = GetRenderer()->GetActors();
474 ForEach<VTKViewer_Actor>(aCollection,TSetVisibility<VTKViewer_Actor>(true));
476 emit RenderWindowModified() ;
479 //****************************************************************
480 void VTKViewer_RenderWindowInteractor::Erase( VTKViewer_Actor* SActor, bool update)
484 void VTKViewer_RenderWindowInteractor::Remove( VTKViewer_Actor* SActor, bool updateViewer )
488 GetRenderer()->RemoveProp( SActor );
490 emit RenderWindowModified();
494 void VTKViewer_RenderWindowInteractor::RemoveAll( const bool updateViewer )
496 vtkRenderer* aRenderer = GetRenderer();
497 vtkActorCollection* anActors = aRenderer->GetActors();
500 anActors->InitTraversal();
501 while ( vtkActor *anAct = anActors->GetNextActor() )
503 if ( anAct->IsA( "VTKViewer_Actor" ) )
509 emit RenderWindowModified();
516 void VTKViewer_RenderWindowInteractor::Display( VTKViewer_Actor* theActor, bool update)
518 GetRenderer()->AddActor(theActor);
519 theActor->SetVisibility(true);
522 emit RenderWindowModified();
525 void VTKViewer_RenderWindowInteractor::KeyPressed(QKeyEvent *event)
531 struct TUpdateAction{
532 void operator()(vtkActor* theActor){
533 theActor->ApplyProperties();
537 void VTKViewer_RenderWindowInteractor::Update() {
539 vtkRenderer* aRen = GetRenderer();
540 ForEach<vtkActor>(aRen->GetActors(),TUpdateAction());
544 emit RenderWindowModified();
548 void VTKViewer_RenderWindowInteractor::unHighlightSubSelection(){
549 myPointActor->SetVisibility(false);
550 myEdgeActor->SetVisibility(false);
551 myCellActor->SetVisibility(false);
554 bool VTKViewer_RenderWindowInteractor::unHighlightAll(){
555 unHighlightSubSelection();
557 emit RenderWindowModified() ;
562 //----------------------------------------------------------------------------
563 bool VTKViewer_RenderWindowInteractor::highlight(const TColStd_IndexedMapOfInteger& theMapIndex,
564 VTKViewer_Actor* theMapActor, VTKViewer_Actor* theActor,
565 TUpdateActor theFun, bool hilight, bool update)
567 if(theMapIndex.Extent() == 0) return false;
570 setActorData(theMapIndex,theMapActor,theActor,theFun);
571 theActor->SetVisibility(true);
574 theActor->SetVisibility(false);
578 this->RenderWindow->Render();
579 emit RenderWindowModified() ;
585 void VTKViewer_RenderWindowInteractor::setActorData(const TColStd_IndexedMapOfInteger& theMapIndex,
586 VTKViewer_Actor* theMapActor,
587 VTKViewer_Actor *theActor,
590 (*theFun)(theMapIndex,theMapActor,theActor);