1 // SALOME VTKViewer : build VTK viewer into Salome desktop
3 // Copyright (C) 2003 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : VTKViewer_RenderWindowInteractor.cxx
25 // Author : Nicolas REJNERI
30 #include "VTKViewer_RenderWindowInteractor.h"
31 #include "VTKViewer_RenderWindow.h"
32 #include "VTKViewer_InteractorStyleSALOME.h"
34 #include "QAD_Application.h"
35 #include "QAD_Desktop.h"
37 #include "SALOME_Selection.h"
38 #include "SALOME_Actor.h"
46 #include <vtkAssemblyNode.h>
48 #include <vtkInteractorStyle.h>
49 #include <vtkObjectFactory.h>
50 #include <vtkPicker.h>
51 #include <vtkCellPicker.h>
52 #include <vtkPointPicker.h>
53 #include <vtkUnstructuredGrid.h>
54 #include <vtkPolyDataMapper.h>
55 #include <vtkSphereSource.h>
56 #include <vtkDataSet.h>
57 #include <vtkMaskPoints.h>
58 #include <vtkVertex.h>
59 #include <vtkRendererCollection.h>
60 #include <vtkPolyDataWriter.h>
66 VTKViewer_RenderWindowInteractor* VTKViewer_RenderWindowInteractor::New() {
67 vtkObject *ret = vtkObjectFactory::CreateInstance("VTKViewer_RenderWindowInteractor") ;
69 return dynamic_cast<VTKViewer_RenderWindowInteractor *>(ret) ;
71 return new VTKViewer_RenderWindowInteractor;
74 VTKViewer_RenderWindowInteractor::VTKViewer_RenderWindowInteractor() {
76 this->mTimer = new QTimer( this ) ;
77 this->displaymode = 0;
79 vtkPicker* thepicker = vtkPicker::New();
80 thepicker->SetTolerance(0);
81 this->SetPicker(thepicker);
83 connect(mTimer, SIGNAL(timeout()), this, SLOT(TimerFunc())) ;
87 VTKViewer_RenderWindowInteractor::~VTKViewer_RenderWindowInteractor() {
91 void VTKViewer_RenderWindowInteractor::PrintSelf(ostream& os, vtkIndent indent) {
92 vtkRenderWindowInteractor::PrintSelf(os, indent) ;
94 // :NOTE: Fri Apr 21 21:51:05 2000 Pagey
95 // QGL specific stuff goes here. One should add output
96 // lines here if any protected members are added to
102 // We never allow the VTKViewer_RenderWindowInteractor to control
103 // the event loop. The application always has the control.
105 void VTKViewer_RenderWindowInteractor::Initialize() {
107 // We cannot do much unless there is a render window
108 // associated with this interactor.
110 if( ! RenderWindow ) {
111 vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize(): No render window attached!") ;
116 // We cannot hand a render window which is not a VTKViewer_RenderWindow.
117 // One way to force this is to use dynamic_cast and hope that
118 // it works. If the dynamic_cast does not work, we flag an error
119 // and get the hell out.
121 vtkRenderWindow *my_render_win = dynamic_cast<vtkRenderWindow *>(RenderWindow) ;
122 if( !my_render_win ) {
123 vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize() can only handle VTKViewer_RenderWindow.") ;
128 // If the render window has zero size, then set it to a default
131 int* aSize = my_render_win->GetSize();
132 this->Size[0] = ((aSize[0] > 0) ? aSize[0] : 300);
133 this->Size[1] = ((aSize[1] > 0) ? aSize[1] : 300);
136 // Enable the interactor.
141 // Start the rendering of the window.
143 my_render_win->Start() ;
146 // The interactor has been initialized.
148 this->Initialized = 1 ;
150 Cell_Actor = vtkActor::New();
151 Cell_Actor->PickableOff();
152 Cell_Actor->GetProperty()->SetColor(1,1,0);
153 Cell_Actor->GetProperty()->SetLineWidth(5);
154 Cell_Actor->GetProperty()->SetRepresentationToSurface();
156 Edge_Actor = vtkActor::New();
157 Edge_Actor->PickableOff();
158 Edge_Actor->GetProperty()->SetColor(1,0,0);
159 Edge_Actor->GetProperty()->SetLineWidth(5);
160 Edge_Actor->GetProperty()->SetRepresentationToWireframe();
162 Point_Actor = vtkActor::New();
163 Point_Actor->PickableOff();
164 Point_Actor->GetProperty()->SetColor(1,1,0);
165 Point_Actor->GetProperty()->SetPointSize(5);
166 Point_Actor->GetProperty()->SetRepresentationToPoints();
171 void VTKViewer_RenderWindowInteractor::SetSelectionMode(int mode)
173 Cell_Actor->VisibilityOff();
174 Edge_Actor->VisibilityOff();
175 Point_Actor->VisibilityOff();
177 selectionmode = mode;
179 vtkPointPicker* thepicker = vtkPointPicker::New();
180 // thepicker->SetTolerance(0.001);
181 this->SetPicker(thepicker);
182 } else if ( mode == 2 ) {
183 vtkCellPicker* thepicker = vtkCellPicker::New();
184 thepicker->SetTolerance(0.001);
185 this->SetPicker(thepicker);
186 } else if ( mode == 3 ) {
187 vtkCellPicker* thepicker = vtkCellPicker::New();
188 thepicker->SetTolerance(0.001);
189 this->SetPicker(thepicker);
190 } else if ( mode == 4 ) {
191 vtkPicker* thepicker = vtkPicker::New();
192 thepicker->SetTolerance(0.001);
193 this->SetPicker(thepicker);
198 void VTKViewer_RenderWindowInteractor::Enable() {
200 // Do not need to do anything if already enabled.
202 if( this->Enabled ) {
207 // Attach slots to every useful signal of the render window.
209 this->ConnectSlots() ;
215 void VTKViewer_RenderWindowInteractor::Disable() {
216 if( ! this->Enabled ) {
220 this->DisconnectSlots() ;
225 // ==================================
226 void VTKViewer_RenderWindowInteractor::Start() {
228 // We do not allow this interactor to control the
229 // event loop. Only the QtApplication objects are
230 // allowed to do that.
232 vtkErrorMacro(<<"VTKViewer_RenderWindowInteractor::Start() not allowed to start event loop.") ;
236 void VTKViewer_RenderWindowInteractor::UpdateSize(int w, int h) {
237 // if the size changed send this on to the RenderWindow
238 if ((w != this->Size[0])||(h != this->Size[1])) {
241 this->RenderWindow->SetSize(w,h);
245 int VTKViewer_RenderWindowInteractor::CreateTimer(int vtkNotUsed(timertype)) {
247 // Start a one-shot timer for 10ms.
249 mTimer->start(10, TRUE) ;
253 int VTKViewer_RenderWindowInteractor::DestroyTimer(void) {
255 // :TRICKY: Tue May 2 00:17:32 2000 Pagey
257 // QTimer will automatically expire after 10ms. So
258 // we do not need to do anything here. In fact, we
259 // should not even Stop() the QTimer here because doing
260 // this will skip some of the processing that the TimerFunc()
261 // does and will result in undesirable effects. For
262 // example, this will result in vtkLODActor to leave
263 // the models in low-res mode after the mouse stops
269 void VTKViewer_RenderWindowInteractor::TimerFunc() {
270 if( ! this->Enabled ) {
274 ((vtkInteractorStyle*)this->InteractorStyle)->OnTimer() ;
275 emit RenderWindowModified() ;
278 void VTKViewer_RenderWindowInteractor::ConnectSlots() {
279 ProcessSlotConnections(true) ;
282 void VTKViewer_RenderWindowInteractor::DisconnectSlots() {
283 ProcessSlotConnections(false) ;
286 void VTKViewer_RenderWindowInteractor::ProcessSlotConnections(bool conn) {
288 // We cannot do much unless there is a render window
289 // associated with this interactor.
291 if( ! myGUIWindow ) {
292 vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize(): No render window attached!") ;
297 bool (*slot_func) ( const QObject * sender,
299 const QObject * receiver,
300 const char * member ) ;
302 slot_func = &QObject::connect ;
304 slot_func = &QObject::disconnect ;
307 // We cannot hand a render window which is not a VTKViewer_RenderWindow.
308 // One way to force this is to use dynamic_cast and hope that
309 // it works. If the dynamic_cast does not work, we flag an error
310 // and get the hell out.
312 if( ! myGUIWindow ) {
313 vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize() can only handle VTKViewer_RenderWindow.") ;
317 slot_func ( myGUIWindow, SIGNAL(ButtonPressed(const QMouseEvent *)),
318 this, SLOT(ButtonPressed(const QMouseEvent *)) ) ;
319 slot_func( myGUIWindow, SIGNAL(ButtonReleased(const QMouseEvent *)),
320 this, SLOT(ButtonReleased(const QMouseEvent *)) ) ;
323 slot_func( myGUIWindow, SIGNAL(LeftButtonPressed(const QMouseEvent *)),
324 this, SLOT(LeftButtonPressed(const QMouseEvent *)) ) ;
325 slot_func( myGUIWindow, SIGNAL(LeftButtonReleased(const QMouseEvent *)),
326 this, SLOT(LeftButtonReleased(const QMouseEvent *)) ) ;
328 slot_func( myGUIWindow, SIGNAL(MiddleButtonPressed(const QMouseEvent *)),
329 this, SLOT(MiddleButtonPressed(const QMouseEvent *)) ) ;
330 slot_func( myGUIWindow, SIGNAL(MiddleButtonReleased(const QMouseEvent *)),
331 this, SLOT(MiddleButtonReleased(const QMouseEvent *)) ) ;
333 slot_func( myGUIWindow, SIGNAL(RightButtonPressed(const QMouseEvent *)),
334 this, SLOT(RightButtonPressed(const QMouseEvent *)) ) ;
335 slot_func( myGUIWindow, SIGNAL(RightButtonReleased(const QMouseEvent *)),
336 this, SLOT(RightButtonReleased(const QMouseEvent *)) ) ;
338 slot_func( myGUIWindow, SIGNAL(MouseMove(const QMouseEvent *)),
339 this, SLOT(MouseMove(const QMouseEvent *)) ) ;
341 slot_func( myGUIWindow, SIGNAL(KeyPressed(QKeyEvent *)),
342 this, SLOT(KeyPressed(QKeyEvent *)) ) ;
344 slot_func( this, SIGNAL(RenderWindowModified()),
345 myGUIWindow, SLOT(update()) ) ;
349 void VTKViewer_RenderWindowInteractor::MouseMove(const QMouseEvent *event) {
350 if( ! this->Enabled ) {
353 VTKViewer_InteractorStyleSALOME* Style = 0;
354 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
355 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
357 emit RenderWindowModified() ;
360 Style->OnMouseMove(0, 0, event->x(), event->y()/*this->Size[1] - event->y() - 1*/) ;
361 if (Style->needsRedrawing() )
362 emit RenderWindowModified() ;
365 void VTKViewer_RenderWindowInteractor::LeftButtonPressed(const QMouseEvent *event) {
366 if( ! this->Enabled ) {
369 VTKViewer_InteractorStyleSALOME* Style = 0;
370 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
371 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
374 Style->OnLeftButtonDown((event->state() & ControlButton),
375 (event->state() & ShiftButton),
376 event->x(), event->y());
379 void VTKViewer_RenderWindowInteractor::LeftButtonReleased(const QMouseEvent *event) {
380 if( ! this->Enabled ) {
383 VTKViewer_InteractorStyleSALOME* Style = 0;
384 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
385 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
388 Style->OnLeftButtonUp( (event->state() & ControlButton),
389 (event->state() & ShiftButton),
390 event->x(), event->y() ) ;
393 void VTKViewer_RenderWindowInteractor::MiddleButtonPressed(const QMouseEvent *event) {
394 if( ! this->Enabled ) {
397 VTKViewer_InteractorStyleSALOME* Style = 0;
398 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
399 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
402 Style->OnMiddleButtonDown((event->state() & ControlButton),
403 (event->state() & ShiftButton),
404 event->x(), event->y() ) ;
407 void VTKViewer_RenderWindowInteractor::MiddleButtonReleased(const QMouseEvent *event) {
408 if( ! this->Enabled ) {
411 VTKViewer_InteractorStyleSALOME* Style = 0;
412 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
413 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
416 Style->OnMiddleButtonUp( (event->state() & ControlButton),
417 (event->state() & ShiftButton),
418 event->x(), event->y() ) ;
421 void VTKViewer_RenderWindowInteractor::RightButtonPressed(const QMouseEvent *event) {
422 if( ! this->Enabled ) {
425 VTKViewer_InteractorStyleSALOME* Style = 0;
426 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
427 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
430 Style->OnRightButtonDown( (event->state() & ControlButton),
431 (event->state() & ShiftButton),
432 event->x(), event->y() ) ;
435 void VTKViewer_RenderWindowInteractor::RightButtonReleased(const QMouseEvent *event) {
436 if( ! this->Enabled ) {
439 VTKViewer_InteractorStyleSALOME* Style = 0;
440 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
441 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
444 Style->OnRightButtonUp( (event->state() & ControlButton),
445 (event->state() & ShiftButton),
446 event->x(), event->y() ) ;
449 void VTKViewer_RenderWindowInteractor::ButtonPressed(const QMouseEvent *event) {
453 void VTKViewer_RenderWindowInteractor::ButtonReleased(const QMouseEvent *event) {
457 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToWireframe(vtkActorCollection* ac)
461 for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
462 if ( anActor->IsA("GEOM_Actor") ) {
463 SALOME_Actor* SALOMEactor = SALOME_Actor::SafeDownCast(anActor);
464 // Specific for GEOM actor
465 if ( SALOMEactor->getDisplayMode() == 1 )
466 SALOMEactor->setDisplayMode(0);
468 anActor->GetProperty()->SetRepresentationToWireframe();
471 this->RenderWindow->Render();
472 emit RenderWindowModified() ;
475 int VTKViewer_RenderWindowInteractor::GetDisplayMode() {
479 void VTKViewer_RenderWindowInteractor::SetDisplayMode(int mode) {
480 if(mode==0) ChangeRepresentationToWireframe();
481 else ChangeRepresentationToSurface();
485 void VTKViewer_RenderWindowInteractor::SwitchRepresentation(const Handle(SALOME_InteractiveObject)& IObject,
488 for (this->RenderWindow->GetRenderers()->InitTraversal();
489 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
490 vtkActorCollection* theActors = aren->GetActors();
491 theActors->InitTraversal();
492 vtkActor *ac = theActors->GetNextActor();
494 if ( ac->IsA("SALOME_Actor") ) {
495 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
496 if ( anActor->hasIO() ) {
497 if ( IObject->isSame( anActor->getIO() ) ) {
498 if ( anActor->IsA("GEOM_Actor") ) {
500 if ( anActor->getDisplayMode() == 0 ) anActor->setDisplayMode(1);
501 else anActor->setDisplayMode(0);
504 if(anActor->GetProperty()->GetRepresentation() <= 1)
505 anActor->GetProperty()->SetRepresentationToSurface();
507 anActor->GetProperty()->SetRepresentationToWireframe();
512 ac = theActors->GetNextActor();
516 this->RenderWindow->Render();
517 emit RenderWindowModified() ;
523 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToWireframe()
524 // change all actors to wireframe
526 vtkActorCollection *ac;
527 vtkActor *anActor, *aPart;
529 ac = GetRenderer()->GetActors();
530 ChangeRepresentationToWireframe(ac);
535 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToSurface(vtkActorCollection* ac)
537 vtkActor *anActor, *aPart;
539 for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
540 if ( anActor->IsA("GEOM_Actor") ) {
541 SALOME_Actor* SALOMEactor = SALOME_Actor::SafeDownCast(anActor);
542 // Specific for GEOM actor
543 if ( SALOMEactor->getDisplayMode() == 0 )
544 SALOMEactor->setDisplayMode(1);
546 anActor->GetProperty()->SetRepresentationToSurface();
549 this->RenderWindow->Render();
550 emit RenderWindowModified() ;
553 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToSurface()
554 // change all actors to "surface" or solid
556 vtkActorCollection *ac;
557 vtkActor *anActor, *aPart;
559 ac = GetRenderer()->GetActors();
560 ChangeRepresentationToSurface(ac);
563 vtkRenderer* VTKViewer_RenderWindowInteractor::GetRenderer()
565 vtkRendererCollection * theRenderers = this->RenderWindow->GetRenderers();
566 theRenderers->InitTraversal();
567 return theRenderers->GetNextItem();
570 void VTKViewer_RenderWindowInteractor::EraseAll()
572 vtkActorCollection *ac;
573 vtkActor *anActor, *aPart;
575 ac = GetRenderer()->GetActors();
576 for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
577 if ( anActor->IsA("SALOME_Actor") ) {
578 SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( anActor );
579 SActor->SetVisibility(false);
582 emit RenderWindowModified() ;
585 void VTKViewer_RenderWindowInteractor::DisplayAll()
587 vtkActorCollection *ac;
588 vtkActor *anActor, *aPart;
590 ac = GetRenderer()->GetActors();
591 for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
592 if ( anActor->IsA("SALOME_Actor") ) {
593 SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( anActor );
594 SActor->SetVisibility( true );
597 emit RenderWindowModified() ;
600 void VTKViewer_RenderWindowInteractor::Erase(const Handle(SALOME_InteractiveObject)& IObject, bool update)
603 for (this->RenderWindow->GetRenderers()->InitTraversal();
604 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
605 vtkActorCollection* theActors = aren->GetActors();
606 vtkActorCollection* theChildActors = vtkActorCollection::New();
607 theActors->InitTraversal();
608 vtkActor *ac = theActors->GetNextActor();
610 if ( ac->IsA("SALOME_Actor") ) {
611 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
612 if ( anActor->hasIO() ) {
613 if ( IObject->isSame( anActor->getIO() ) ) {
614 anActor->SetVisibility( false );
615 anActor->GetChildActors(theChildActors);
619 ac = theActors->GetNextActor();
622 // Erase dependent actors
623 theChildActors->InitTraversal();
624 ac = theChildActors->GetNextActor();
626 ac->SetVisibility( false );
627 ac = theChildActors->GetNextActor();
631 emit RenderWindowModified() ;
634 void VTKViewer_RenderWindowInteractor::Remove(const Handle(SALOME_InteractiveObject)& IObject, bool update)
637 for (this->RenderWindow->GetRenderers()->InitTraversal();
638 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
639 vtkActorCollection* theActors = aren->GetActors();
640 theActors->InitTraversal();
641 vtkActor *ac = theActors->GetNextActor();
643 if ( ac->IsA("SALOME_Actor") ) {
644 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
645 if ( anActor->hasIO() ) {
646 if ( IObject->isSame( anActor->getIO() ) ) {
647 aren->RemoveActor(anActor);
651 ac = theActors->GetNextActor();
655 emit RenderWindowModified() ;
658 float VTKViewer_RenderWindowInteractor::GetTransparency(const Handle(SALOME_InteractiveObject)& IObject) {
660 for (this->RenderWindow->GetRenderers()->InitTraversal();
661 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
662 vtkActorCollection* theActors = aren->GetActors();
663 theActors->InitTraversal();
664 vtkActor *ac = theActors->GetNextActor();
666 if ( ac->IsA("SALOME_Actor") ) {
667 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
668 if ( anActor->hasIO() ) {
669 if ( IObject->isSame( anActor->getIO() ) ) {
670 if ( anActor->IsA("GEOM_Actor") ) {
672 return (1-(anActor->GetOpacity()));
674 else return (1-anActor->GetProperty()->GetOpacity());
678 ac = theActors->GetNextActor();
684 void VTKViewer_RenderWindowInteractor::SetTransparency(const Handle(SALOME_InteractiveObject)& IObject, float trans) {
686 for (this->RenderWindow->GetRenderers()->InitTraversal();
687 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
688 vtkActorCollection* theActors = aren->GetActors();
689 theActors->InitTraversal();
690 vtkActor *ac = theActors->GetNextActor();
692 if ( ac->IsA("SALOME_Actor") ) {
693 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
694 if ( anActor->hasIO() ) {
695 if ( IObject->isSame( anActor->getIO() ) ) {
696 if ( anActor->IsA("GEOM_Actor") ) {
698 anActor->SetOpacity(1-trans);
700 else anActor->GetProperty()->SetOpacity(1-trans);
704 ac = theActors->GetNextActor();
709 void VTKViewer_RenderWindowInteractor::Display( SALOME_Actor* SActor, bool update)
712 for (this->RenderWindow->GetRenderers()->InitTraversal();
713 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
714 aren->AddActor( SActor );
715 SActor->SetVisibility( true );
720 emit RenderWindowModified();
724 void VTKViewer_RenderWindowInteractor::Display(const Handle(SALOME_InteractiveObject)& IObject, bool update)
726 Standard_Boolean isalreadydisplayed = false;
729 for (this->RenderWindow->GetRenderers()->InitTraversal();
730 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
731 vtkActorCollection* theActors = aren->GetActors();
732 theActors->InitTraversal();
733 vtkActor *ac = theActors->GetNextActor();
735 if ( ac->IsA("SALOME_Actor") ) {
736 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
737 if ( anActor->hasIO() ) {
738 if ( IObject->isSame( anActor->getIO() ) ) {
739 anActor->SetVisibility( true );
743 ac = theActors->GetNextActor();
747 emit RenderWindowModified() ;
750 void VTKViewer_RenderWindowInteractor::KeyPressed(QKeyEvent *event) {
751 // MESSAGE ( " vtkQGLRenderWindowInteractor::KeyPressed " )
753 // if (!QAD_Application::getDesktop()->getActiveComponent().isEmpty()) {
754 // QAD_Application::getDesktop()->onKeyPress( event );
758 bool VTKViewer_RenderWindowInteractor::highlight( const Handle(SALOME_InteractiveObject)& IObject,
762 VTKViewer_InteractorStyleSALOME* aStyle = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
763 if (!aStyle) return false;
766 for (this->RenderWindow->GetRenderers()->InitTraversal();
767 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
768 vtkActorCollection* theActors = aren->GetActors();
769 theActors->InitTraversal();
770 vtkActor *ac = theActors->GetNextActor();
772 if ( ac->IsA("SALOME_Actor") ) {
773 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
774 if ( anActor->hasIO() ) {
775 if ( IObject->isSame( anActor->getIO() ) ) {
776 if ( anActor->GetMapper() == NULL ) {
779 //highlight or unhilight actors
780 if ( anActor->hasHighlight() ) {
781 anActor->highlight(hilight);
783 if ( anActor->GetVisibility() == 1 ) {
787 if ( (prop3D=vtkProp3D::SafeDownCast(anActor)) != NULL ) {
788 aStyle->HighlightProp3D(prop3D);
789 } else if ( (actor2D=vtkActor2D::SafeDownCast(anActor)) != NULL ) {
790 aStyle->HighlightActor2D(actor2D);
792 //this->InteractorStyle->HighlightProp(anActor);
794 aStyle->HighlightProp3D(NULL);
795 aStyle->HighlightActor2D(NULL);
796 //this->InteractorStyle->HighlightProp(NULL);
803 ac = theActors->GetNextActor();
808 emit RenderWindowModified();
814 void VTKViewer_RenderWindowInteractor::Update() {
816 for (this->RenderWindow->GetRenderers()->InitTraversal();
817 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
818 vtkActorCollection* theActors = aren->GetActors();
819 theActors->InitTraversal();
820 vtkActor *ac = theActors->GetNextActor();
823 ac->ApplyProperties();
824 //VSV: No in VTK: ac->Update();
825 ac = theActors->GetNextActor();
828 emit RenderWindowModified() ;
833 bool VTKViewer_RenderWindowInteractor::unHighlightAll()
836 for (this->RenderWindow->GetRenderers()->InitTraversal();
837 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
838 vtkActorCollection* theActors = aren->GetActors();
839 theActors->InitTraversal();
841 if ( theActors->IsItemPresent(Point_Actor) != 0 )
842 aren->RemoveActor( Point_Actor );
843 if ( theActors->IsItemPresent( Edge_Actor ) != 0 )
844 aren->RemoveActor( Edge_Actor );
845 if ( theActors->IsItemPresent( Cell_Actor ) != 0 )
846 aren->RemoveActor( Cell_Actor );
848 vtkActor *ac = theActors->GetNextActor();
851 if ( ac->IsA("SALOME_Actor") ) {
852 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
853 if ( anActor->hasIO() ) {
854 //highlight or unhilight actors
855 if ( anActor->hasHighlight() )
856 anActor->highlight(false);
859 ac = theActors->GetNextActor();
863 emit RenderWindowModified() ;
871 void VTKViewer_RenderWindowInteractor::SetColor(const Handle(SALOME_InteractiveObject)& IObject,QColor thecolor) {
873 for (this->RenderWindow->GetRenderers()->InitTraversal();
874 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
875 vtkActorCollection* theActors = aren->GetActors();
876 theActors->InitTraversal();
877 vtkActor *ac = theActors->GetNextActor();
879 if ( ac->IsA("SALOME_Actor") ) {
880 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
881 if ( anActor->hasIO() ) {
882 if ( IObject->isSame( anActor->getIO() ) ) {
883 if ( anActor->IsA("GEOM_Actor") ) {
885 anActor->SetColor(float(thecolor.red())/255,float(thecolor.green())/255,float(thecolor.blue())/255);
888 anActor->GetProperty()->SetColor(float(thecolor.red())/255,float(thecolor.green())/255,float(thecolor.blue())/255);
893 ac = theActors->GetNextActor();
898 QColor VTKViewer_RenderWindowInteractor::GetColor(const Handle(SALOME_InteractiveObject)& IObject) {
900 QColor emptycolor(0,0,0);
901 for (this->RenderWindow->GetRenderers()->InitTraversal();
902 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
903 vtkActorCollection* theActors = aren->GetActors();
904 theActors->InitTraversal();
905 vtkActor *ac = theActors->GetNextActor();
907 if ( ac->IsA("SALOME_Actor") ) {
908 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
909 if ( anActor->hasIO() ) {
910 if ( IObject->isSame( anActor->getIO() ) ) {
911 if ( anActor->IsA("GEOM_Actor") ) {
914 anActor->GetColor(r,g,b);
915 return QColor(int(r*255),int(g*255),int(b*255));
919 anActor->GetProperty()->GetColor(color);
920 return QColor(int(color[0]*255),int(color[1]*255),int(color[2]*255));
925 ac = theActors->GetNextActor();
934 bool VTKViewer_RenderWindowInteractor::isInViewer(const Handle(SALOME_InteractiveObject)& IObject)
937 for (this->RenderWindow->GetRenderers()->InitTraversal();
938 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
939 vtkActorCollection* theActors = aren->GetActors();
940 theActors->InitTraversal();
941 vtkActor *ac = theActors->GetNextActor();
943 if ( ac->IsA("SALOME_Actor") ) {
944 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
945 if ( anActor->hasIO() ) {
946 if ( IObject->isSame( anActor->getIO() ) ) {
947 //MESSAGE ( " VTKViewer_RenderWindowInteractor::isInViewer = TRUE" )
952 ac = theActors->GetNextActor();
955 //MESSAGE ( " VTKViewer_RenderWindowInteractor::isInViewer = FALSE" )
959 bool VTKViewer_RenderWindowInteractor::isVisible(const Handle(SALOME_InteractiveObject)& IObject)
962 for (this->RenderWindow->GetRenderers()->InitTraversal();
963 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
964 vtkActorCollection* theActors = aren->GetActors();
965 theActors->InitTraversal();
966 vtkActor *ac = theActors->GetNextActor();
968 if ( ac->IsA("SALOME_Actor") ) {
969 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
970 if ( anActor->hasIO() ) {
971 if ( IObject->isSame( anActor->getIO() ) ) {
972 return anActor->GetVisibility();
976 ac = theActors->GetNextActor();
982 void VTKViewer_RenderWindowInteractor::rename(const Handle(SALOME_InteractiveObject)& IObject, QString newName)
985 for (this->RenderWindow->GetRenderers()->InitTraversal();
986 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
987 vtkActorCollection* theActors = aren->GetActors();
988 theActors->InitTraversal();
989 vtkActor *ac = theActors->GetNextActor();
991 if ( ac->IsA("SALOME_Actor") ) {
992 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
993 if ( anActor->hasIO() ) {
994 if ( IObject->isSame( anActor->getIO() ) ) {
995 anActor->setName(strdup(newName));
999 ac = theActors->GetNextActor();
1003 emit RenderWindowModified() ;
1006 bool VTKViewer_RenderWindowInteractor::highlight(const Handle(SALOME_InteractiveObject)& IObject,
1007 const TColStd_MapOfInteger& MapIndex,
1008 VTKViewer_RenderWindowInteractor::TCreateMapperFun theFun,
1009 vtkActor *theActor, bool hilight, bool update )
1011 if(MapIndex.Extent() == 0) return false;
1013 this->RenderWindow->GetRenderers()->InitTraversal();
1014 for (; aRen = this->RenderWindow->GetRenderers()->GetNextItem(); ) {
1015 vtkActorCollection* anActorColl = aRen->GetActors();
1016 if ( anActorColl->IsItemPresent(theActor) != 0 )
1017 aRen->RemoveActor(theActor);
1018 anActorColl->InitTraversal();
1019 vtkActor *ac = NULL;
1020 for(; (ac = anActorColl->GetNextActor()) != NULL; ){
1021 if(ac->IsA("SALOME_Actor")){
1022 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast(ac);
1023 if(anActor->hasIO()){
1024 if(IObject->isSame(anActor->getIO())){
1025 if(vtkPolyData* aSourcePolyData = anActor->GetPolyDataInput()){
1026 vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
1027 (*theFun)(aSourcePolyData,aMapper,MapIndex);
1028 theActor->SetMapper(aMapper);
1030 if(anActorColl->IsItemPresent(theActor) == 0)
1031 aRen->AddActor(theActor);
1033 theActor->VisibilityOn();
1035 theActor->VisibilityOff();
1044 this->RenderWindow->Render();
1045 emit RenderWindowModified() ;
1050 static void CellCreateMapper(vtkPolyData *theSourcePolyData, vtkPolyDataMapper* theMapper,
1051 const TColStd_MapOfInteger& theMapIndex)
1053 vtkPolyData* aPolyData = vtkPolyData::New();
1054 aPolyData->Allocate();
1056 vtkIdList *ptIds = vtkIdList::New();
1057 ptIds->Allocate(theSourcePolyData->GetMaxCellSize());
1058 vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
1059 aPolyData->SetPoints(theSourcePolyData->GetPoints());
1060 TColStd_MapIteratorOfMapOfInteger ite(theMapIndex);
1061 int aNbOfParts = theSourcePolyData->GetNumberOfCells();
1062 for(; ite.More(); ite.Next()){
1063 int aPartId = ite.Key();
1064 if(0 > aPartId || aPartId >= aNbOfParts) break;
1065 theSourcePolyData->GetCellPoints(aPartId,ptIds);
1066 vtkCell* aCell = theSourcePolyData->GetCell(aPartId);
1067 aPolyData->InsertNextCell(aCell->GetCellType(),ptIds);
1068 for (int i = 0, iEnd = aCell->GetNumberOfEdges(); i < iEnd; i++){
1069 vtkCell* anEdgeCell = aCell->GetEdge(i);
1070 aPolyData->InsertNextCell(VTK_LINE,anEdgeCell->GetPointIds());
1074 theMapper->SetInput(aPolyData);
1075 aPolyData->Delete();
1078 bool VTKViewer_RenderWindowInteractor::highlightCell(const Handle(SALOME_InteractiveObject)& IObject,
1080 const TColStd_MapOfInteger& MapIndex,
1083 return highlight(IObject,MapIndex,&CellCreateMapper,Cell_Actor,hilight,update);
1087 static void PointCreateMapper(vtkPolyData *theSourcePolyData, vtkPolyDataMapper* theMapper,
1088 const TColStd_MapOfInteger& theMapIndex)
1090 vtkPolyData* aPolyData = vtkPolyData::New();
1091 aPolyData->Allocate();
1093 vtkPoints *aPoints = vtkPoints::New();
1094 vtkMaskPoints* aMaskPoints = vtkMaskPoints::New();
1095 TColStd_MapIteratorOfMapOfInteger ite(theMapIndex);
1096 aPoints->SetNumberOfPoints(theMapIndex.Extent());
1097 int aNbOfParts = theSourcePolyData->GetNumberOfPoints();
1098 for(int i = 0; ite.More(); ite.Next(),i++){
1099 int aPartId = ite.Key();
1100 if(0 > aPartId || aPartId >= aNbOfParts) break;
1101 aPoints->SetPoint(i,theSourcePolyData->GetPoint(aPartId));
1103 aPolyData->SetPoints(aPoints);
1105 aMaskPoints->SetInput(aPolyData);
1106 aMaskPoints->SetGenerateVertices(1);
1107 aMaskPoints->SetOnRatio(1);
1108 theMapper->SetInput(aMaskPoints->GetOutput());
1109 aMaskPoints->Delete();
1112 bool VTKViewer_RenderWindowInteractor::highlightPoint(const Handle(SALOME_InteractiveObject)& IObject,
1113 bool hilight, const TColStd_MapOfInteger& MapIndex,
1116 return highlight(IObject,MapIndex,&PointCreateMapper,Point_Actor,hilight,update);
1120 static void EdgeCreateMapper(vtkPolyData *theSourcePolyData, vtkPolyDataMapper* theMapper,
1121 const TColStd_MapOfInteger& theMapIndex)
1123 vtkPolyData* aPolyData = vtkPolyData::New();
1124 aPolyData->Allocate();
1126 vtkIdList *ptIds = vtkIdList::New();
1127 ptIds->Allocate(theSourcePolyData->GetMaxCellSize());
1128 vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
1129 aPolyData->SetPoints(theSourcePolyData->GetPoints());
1130 int aCellId = -1, aCellCounter = 0;
1131 for(TColStd_MapIteratorOfMapOfInteger ite(theMapIndex); ite.More(); ite.Next()){
1133 aCellId = ite.Key();
1137 if(aCellCounter == 1){
1138 vtkCell* aCell = theSourcePolyData->GetCell(aCellId);
1139 if(aCell->GetCellType() <= VTK_LINE){
1140 aPolyData->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
1142 TColStd_MapIteratorOfMapOfInteger ite(theMapIndex);
1143 int aNbOfParts = aCell->GetNumberOfEdges();
1144 for(; ite.More(); ite.Next()){
1146 int aPartId = -ite.Key()-1;
1147 if(0 > aPartId || aPartId >= aNbOfParts) break;
1148 vtkCell* anEdgeCell = aCell->GetEdge(aPartId);
1149 aPolyData->InsertNextCell(VTK_LINE,anEdgeCell->GetPointIds());
1154 int aNbOfParts = theSourcePolyData->GetNumberOfCells();
1155 for(TColStd_MapIteratorOfMapOfInteger ite(theMapIndex); ite.More(); ite.Next()){
1157 int aPartId = ite.Key();
1158 if(0 > aPartId || aPartId >= aNbOfParts) break;
1159 vtkCell* aCell = theSourcePolyData->GetCell(aPartId);
1160 aPolyData->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
1165 theMapper->SetInput(aPolyData);
1166 aPolyData->Delete();
1170 bool VTKViewer_RenderWindowInteractor::highlightEdge( const Handle(SALOME_InteractiveObject)& IObject,
1172 const TColStd_MapOfInteger& MapIndex,
1175 return highlight(IObject,MapIndex,&EdgeCreateMapper,Edge_Actor,hilight,update);