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_InteractorStyleSALOME.cxx
25 // Author : Christophe ATTANASIO
30 #include "VTKViewer_InteractorStyleSALOME.h"
31 #include "VTKViewer_RenderWindow.h"
33 #include <qapplication.h>
34 #include "QAD_Application.h"
35 #include "QAD_Desktop.h"
37 #include "SALOME_Selection.h"
38 #include "SALOME_Actor.h"
39 #include "SALOME_ListIteratorOfListIO.hxx"
41 #include <vtkObjectFactory.h>
43 #include <vtkCommand.h>
44 #include <vtkAssemblyNode.h>
45 #include <vtkPicker.h>
46 #include <vtkPointPicker.h>
47 #include <vtkCellPicker.h>
49 #include <vtkUnstructuredGrid.h>
50 #include <vtkExtractEdges.h>
51 #include <vtkPolyDataMapper.h>
52 #include <vtkDataSetCollection.h>
53 #include <vtkImageData.h>
56 //VRV: porting on Qt 3.0.5
57 #if QT_VERSION >= 0x030005
60 //VRV: porting on Qt 3.0.5
62 //----------------------------------------------------------------------------
63 VTKViewer_InteractorStyleSALOME *VTKViewer_InteractorStyleSALOME::New()
65 // First try to create the object from the vtkObjectFactory
66 vtkObject* ret = vtkObjectFactory::CreateInstance("VTKViewer_InteractorStyleSALOME");
69 return (VTKViewer_InteractorStyleSALOME*)ret;
71 // If the factory was unable to create the object, then create it here.
72 return new VTKViewer_InteractorStyleSALOME;
76 //----------------------------------------------------------------------------
77 VTKViewer_InteractorStyleSALOME::VTKViewer_InteractorStyleSALOME()
80 this->MotionFactor = 10.0;
81 this->State = VTK_INTERACTOR_STYLE_CAMERA_NONE;
82 this->RadianToDegree = 180.0 / vtkMath::Pi();
83 this->ForcedState = VTK_INTERACTOR_STYLE_CAMERA_NONE;
87 //----------------------------------------------------------------------------
88 VTKViewer_InteractorStyleSALOME::~VTKViewer_InteractorStyleSALOME()
92 //----------------------------------------------------------------------------
93 void VTKViewer_InteractorStyleSALOME::setTriedron( vtkActorCollection* triedron )
95 m_Triedron = triedron;
98 //----------------------------------------------------------------------------
99 void VTKViewer_InteractorStyleSALOME::RotateXY(int dx, int dy)
105 if (this->CurrentRenderer == NULL)
110 int *size = this->CurrentRenderer->GetRenderWindow()->GetSize();
111 this->DeltaElevation = -20.0 / size[1];
112 this->DeltaAzimuth = -20.0 / size[0];
114 rxf = (double)dx * this->DeltaAzimuth * this->MotionFactor;
115 ryf = (double)dy * this->DeltaElevation * this->MotionFactor;
117 cam = this->CurrentRenderer->GetActiveCamera();
120 cam->OrthogonalizeViewUp();
121 this->CurrentRenderer->ResetCameraClippingRange();
122 vtkRenderWindowInteractor *rwi = this->Interactor;
123 /* VSV Light follows camera: if (this->CurrentLight)
125 // get the first light
126 this->CurrentLight->SetPosition(cam->GetPosition());
127 this->CurrentLight->SetFocalPoint(cam->GetFocalPoint());
132 //----------------------------------------------------------------------------
133 void VTKViewer_InteractorStyleSALOME::PanXY(int x, int y, int oldX, int oldY)
135 TranslateView(x, y, oldX, oldY);
136 //vtkRenderWindowInteractor *rwi = this->Interactor;
137 /* VSV Light follows camera: if (this->CurrentLight)
139 vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
140 this->CurrentLight->SetPosition(cam->GetPosition());
141 this->CurrentLight->SetFocalPoint(cam->GetFocalPoint());
144 this->Interactor->Render();
147 //----------------------------------------------------------------------------
148 void VTKViewer_InteractorStyleSALOME::DollyXY(int dx, int dy)
151 double dxf = this->MotionFactor * (double)(dx) / (double)(this->CurrentRenderer->GetCenter()[1]);
152 double dyf = this->MotionFactor * (double)(dy) / (double)(this->CurrentRenderer->GetCenter()[1]);
154 double zoomFactor = pow((double)1.1, dxf + dyf);
156 if (this->CurrentRenderer == NULL)
161 cam = this->CurrentRenderer->GetActiveCamera();
162 if (cam->GetParallelProjection())
164 cam->SetParallelScale(cam->GetParallelScale()/zoomFactor);
168 cam->Dolly(zoomFactor);
169 this->CurrentRenderer->ResetCameraClippingRange();
172 /* VSV Light follows camera: if (this->CurrentLight)
174 this->CurrentLight->SetPosition(cam->GetPosition());
175 this->CurrentLight->SetFocalPoint(cam->GetFocalPoint());
178 this->Interactor->Render();
181 //----------------------------------------------------------------------------
182 void VTKViewer_InteractorStyleSALOME::SpinXY(int x, int y, int oldX, int oldY)
184 vtkRenderWindowInteractor *rwi = this->Interactor;
187 if (this->CurrentRenderer == NULL)
192 double newAngle = atan2((double)(y - this->CurrentRenderer->GetCenter()[1]),
193 (double)(x - this->CurrentRenderer->GetCenter()[0]));
194 double oldAngle = atan2((double)(oldY -this->CurrentRenderer->GetCenter()[1]),
195 (double)(oldX - this->CurrentRenderer->GetCenter()[0]));
197 newAngle *= this->RadianToDegree;
198 oldAngle *= this->RadianToDegree;
200 cam = this->CurrentRenderer->GetActiveCamera();
201 cam->Roll(newAngle - oldAngle);
202 cam->OrthogonalizeViewUp();
208 //----------------------------------------------------------------------------
209 void VTKViewer_InteractorStyleSALOME::OnMouseMove(int vtkNotUsed(ctrl),
213 myShiftState = shift;
214 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
215 onOperation(QPoint(x, y));
216 else if (ForcedState == VTK_INTERACTOR_STYLE_CAMERA_NONE)
217 onCursorMove(QPoint(x, y));
221 //----------------------------------------------------------------------------
222 void VTKViewer_InteractorStyleSALOME::OnLeftButtonDown(int ctrl, int shift,
225 if (this->HasObserver(vtkCommand::LeftButtonPressEvent)) {
226 this->InvokeEvent(vtkCommand::LeftButtonPressEvent,NULL);
229 this->FindPokedRenderer(x, y);
230 if (this->CurrentRenderer == NULL) {
233 myShiftState = shift;
234 // finishing current viewer operation
235 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
237 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
239 myOtherPoint = myPoint = QPoint(x, y);
240 if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
241 startOperation(ForcedState);
244 startOperation(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
246 startOperation(VTK_INTERACTOR_STYLE_CAMERA_SELECT);
250 //----------------------------------------------------------------------------
251 void VTKViewer_InteractorStyleSALOME::OnLeftButtonUp(int vtkNotUsed(ctrl),
256 myShiftState = shift;
257 // finishing current viewer operation
258 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
260 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
264 //----------------------------------------------------------------------------
265 void VTKViewer_InteractorStyleSALOME::OnMiddleButtonDown(int ctrl,
269 if (this->HasObserver(vtkCommand::MiddleButtonPressEvent))
271 this->InvokeEvent(vtkCommand::MiddleButtonPressEvent,NULL);
274 this->FindPokedRenderer(x, y);
275 if (this->CurrentRenderer == NULL)
279 myShiftState = shift;
280 // finishing current viewer operation
281 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
283 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
285 myOtherPoint = myPoint = QPoint(x, y);
286 if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
287 startOperation(ForcedState);
291 startOperation(VTK_INTERACTOR_STYLE_CAMERA_PAN);
294 //----------------------------------------------------------------------------
295 void VTKViewer_InteractorStyleSALOME::OnMiddleButtonUp(int vtkNotUsed(ctrl),
300 myShiftState = shift;
301 // finishing current viewer operation
302 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
304 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
308 //----------------------------------------------------------------------------
309 void VTKViewer_InteractorStyleSALOME::OnRightButtonDown(int ctrl,
313 if (this->HasObserver(vtkCommand::RightButtonPressEvent))
315 this->InvokeEvent(vtkCommand::RightButtonPressEvent,NULL);
318 this->FindPokedRenderer(x, y);
319 if (this->CurrentRenderer == NULL)
323 myShiftState = shift;
324 // finishing current viewer operation
325 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
327 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
329 myOtherPoint = myPoint = QPoint(x, y);
330 if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
331 startOperation(ForcedState);
335 startOperation(VTK_INTERACTOR_STYLE_CAMERA_ROTATE);
339 //----------------------------------------------------------------------------
340 void VTKViewer_InteractorStyleSALOME::OnRightButtonUp(int vtkNotUsed(ctrl),
345 myShiftState = shift;
346 // finishing current viewer operation
347 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
349 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
353 //----------------------------------------------------------------------------
354 void VTKViewer_InteractorStyleSALOME::PrintSelf(ostream& os, vtkIndent indent)
356 vtkInteractorStyle::PrintSelf(os,indent);
361 const char* imageZoomCursor[] = {
366 "................................",
367 "................................",
368 ".#######........................",
369 "..aaaaaaa.......................",
370 "................................",
371 ".............#####..............",
372 "...........##.aaaa##............",
373 "..........#.aa.....a#...........",
374 ".........#.a.........#..........",
375 ".........#a..........#a.........",
376 "........#.a...........#.........",
377 "........#a............#a........",
378 "........#a............#a........",
379 "........#a............#a........",
380 "........#a............#a........",
381 ".........#...........#.a........",
382 ".........#a..........#a.........",
383 ".........##.........#.a.........",
384 "........#####.....##.a..........",
385 ".......###aaa#####.aa...........",
386 "......###aa...aaaaa.......#.....",
387 ".....###aa................#a....",
388 "....###aa.................#a....",
389 "...###aa...............#######..",
390 "....#aa.................aa#aaaa.",
391 ".....a....................#a....",
392 "..........................#a....",
393 "...........................a....",
394 "................................",
395 "................................",
396 "................................",
397 "................................"};
399 const char* imageRotateCursor[] = {
404 "................................",
405 "................................",
406 "................................",
407 "................................",
408 "........#.......................",
409 ".......#.a......................",
410 "......#######...................",
411 ".......#aaaaa#####..............",
412 "........#..##.a#aa##........##..",
413 ".........a#.aa..#..a#.....##.aa.",
414 ".........#.a.....#...#..##.aa...",
415 ".........#a.......#..###.aa.....",
416 "........#.a.......#a..#aa.......",
417 "........#a.........#..#a........",
418 "........#a.........#a.#a........",
419 "........#a.........#a.#a........",
420 "........#a.........#a.#a........",
421 ".........#.........#a#.a........",
422 "........##a........#a#a.........",
423 "......##.a#.......#.#.a.........",
424 "....##.aa..##.....##.a..........",
425 "..##.aa.....a#####.aa...........",
426 "...aa.........aaa#a.............",
427 "................#.a.............",
428 "...............#.a..............",
429 "..............#.a...............",
430 "...............a................",
431 "................................",
432 "................................",
433 "................................",
434 "................................",
435 "................................"};
437 // loads cursors for viewer operations - zoom, pan, etc...
438 void VTKViewer_InteractorStyleSALOME::loadCursors()
440 myDefCursor = QCursor(ArrowCursor);
441 myHandCursor = QCursor(PointingHandCursor);
442 myPanCursor = QCursor(SizeAllCursor);
443 myZoomCursor = QCursor(QPixmap(imageZoomCursor));
444 myRotateCursor = QCursor(QPixmap(imageRotateCursor));
445 mySpinCursor = QCursor(QPixmap(imageRotateCursor)); // temporarly !!!!!!
446 myGlobalPanCursor = QCursor(CrossCursor);
447 myCursorState = false;
450 // event filter - controls mouse and keyboard events during viewer operations
451 bool VTKViewer_InteractorStyleSALOME::eventFilter(QObject* object, QEvent* event)
453 if (!myGUIWindow) return false;
454 if ( (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::KeyPress) && object != myGUIWindow)
456 qApp->removeEventFilter(this);
457 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
459 return QObject::eventFilter(object, event);
462 // starts Zoom operation (e.g. through menu command)
463 void VTKViewer_InteractorStyleSALOME::startZoom()
465 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
468 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
470 setCursor(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
471 ForcedState = VTK_INTERACTOR_STYLE_CAMERA_ZOOM;
472 qApp->installEventFilter(this);
475 // starts Pan operation (e.g. through menu command)
476 void VTKViewer_InteractorStyleSALOME::startPan()
478 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
481 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
483 setCursor(VTK_INTERACTOR_STYLE_CAMERA_PAN);
484 ForcedState = VTK_INTERACTOR_STYLE_CAMERA_PAN;
485 qApp->installEventFilter(this);
488 // starts Rotate operation (e.g. through menu command)
489 void VTKViewer_InteractorStyleSALOME::startRotate()
491 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
494 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
496 setCursor(VTK_INTERACTOR_STYLE_CAMERA_ROTATE);
497 ForcedState = VTK_INTERACTOR_STYLE_CAMERA_ROTATE;
498 qApp->installEventFilter(this);
501 // starts Spin operation (e.g. through menu command)
502 void VTKViewer_InteractorStyleSALOME::startSpin()
504 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
507 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
509 setCursor(VTK_INTERACTOR_STYLE_CAMERA_SPIN);
510 ForcedState = VTK_INTERACTOR_STYLE_CAMERA_SPIN;
511 qApp->installEventFilter(this);
515 // starts Fit Area operation (e.g. through menu command)
516 void VTKViewer_InteractorStyleSALOME::startFitArea()
518 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
521 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
523 setCursor(VTK_INTERACTOR_STYLE_CAMERA_FIT);
524 ForcedState = VTK_INTERACTOR_STYLE_CAMERA_FIT;
525 qApp->installEventFilter(this);
528 // starts Global Panning operation (e.g. through menu command)
529 void VTKViewer_InteractorStyleSALOME::startGlobalPan()
531 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
534 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
536 setCursor(VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN);
537 ForcedState = VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN;
539 // store current zoom scale
540 vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
541 myScale = cam->GetParallelScale();
544 Standard_Boolean TriedronWasVisible = false;
546 m_Triedron->InitTraversal();
547 vtkActor *ac = m_Triedron->GetNextActor();
549 if(ac->GetVisibility()) {
550 TriedronWasVisible = true;
553 ac = m_Triedron->GetNextActor();
556 this->CurrentRenderer->ResetCamera();
557 this->CurrentRenderer->ResetCameraClippingRange();
558 if( m_Triedron && TriedronWasVisible ) {
559 m_Triedron->InitTraversal();
560 vtkActor *ac = m_Triedron->GetNextActor();
563 ac = m_Triedron->GetNextActor();
566 //VTKViewer_RenderWindow* aRW = dynamic_cast<VTKViewer_RenderWindow*>(this->Interactor->GetRenderWindow());
567 if (myGUIWindow) myGUIWindow->update();
569 qApp->installEventFilter(this);
572 // returns TRUE if needs redrawing
573 bool VTKViewer_InteractorStyleSALOME::needsRedrawing()
575 return State == VTK_INTERACTOR_STYLE_CAMERA_ZOOM ||
576 State == VTK_INTERACTOR_STYLE_CAMERA_PAN ||
577 State == VTK_INTERACTOR_STYLE_CAMERA_ROTATE ||
578 State == VTK_INTERACTOR_STYLE_CAMERA_SPIN ||
579 State == VTK_INTERACTOR_STYLE_CAMERA_NONE;
582 // fits viewer contents to rect
583 void VTKViewer_InteractorStyleSALOME::fitRect(const int left,
588 if (this->CurrentRenderer == NULL) {
591 vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
594 int x = (left + right)/2;
595 int y = (top + bottom)/2;
596 int *aSize = this->CurrentRenderer->GetRenderWindow()->GetSize();
597 int oldX = aSize[0]/2;
598 int oldY = aSize[1]/2;
599 TranslateView(oldX, oldY, x, y);
603 double dxf = (double)(aSize[0]) / (double)(abs(right - left));
604 double dyf = (double)(aSize[1]) / (double)(abs(bottom - top));
605 double zoomFactor = (dxf + dyf)/2 ;
607 if (cam->GetParallelProjection()) {
608 cam->SetParallelScale(cam->GetParallelScale()/zoomFactor);
610 cam->Dolly(zoomFactor);
611 this->CurrentRenderer->ResetCameraClippingRange();
614 //vtkRenderWindowInteractor *rwi = this->Interactor;
615 /* VSV Light follows camera: if (this->CurrentLight) {
616 this->CurrentLight->SetPosition(cam->GetPosition());
617 this->CurrentLight->SetFocalPoint(cam->GetFocalPoint());
620 //VTKViewer_RenderWindow* aRW = dynamic_cast<VTKViewer_RenderWindow*>(rwi->GetRenderWindow());
621 myGUIWindow->update();
626 // starts viewer operation (!internal usage!)
627 void VTKViewer_InteractorStyleSALOME::startOperation(int operation)
631 case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
632 case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
633 case VTK_INTERACTOR_STYLE_CAMERA_PAN:
634 case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
635 case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
636 case VTK_INTERACTOR_STYLE_CAMERA_FIT:
637 case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
638 if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
639 startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
641 if (State != VTK_INTERACTOR_STYLE_CAMERA_SELECT)
642 setCursor(operation);
645 case VTK_INTERACTOR_STYLE_CAMERA_NONE:
647 setCursor(VTK_INTERACTOR_STYLE_CAMERA_NONE);
648 State = ForcedState = VTK_INTERACTOR_STYLE_CAMERA_NONE;
653 // sets proper cursor for window when viewer operation is activated
654 void VTKViewer_InteractorStyleSALOME::setCursor(const int operation)
656 if (!myGUIWindow) return;
659 case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
660 myGUIWindow->setCursor(myZoomCursor);
661 myCursorState = true;
663 case VTK_INTERACTOR_STYLE_CAMERA_PAN:
664 myGUIWindow->setCursor(myPanCursor);
665 myCursorState = true;
667 case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
668 myGUIWindow->setCursor(myRotateCursor);
669 myCursorState = true;
671 case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
672 myGUIWindow->setCursor(mySpinCursor);
673 myCursorState = true;
675 case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
676 myGUIWindow->setCursor(myGlobalPanCursor);
677 myCursorState = true;
679 case VTK_INTERACTOR_STYLE_CAMERA_FIT:
680 case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
681 myGUIWindow->setCursor(myHandCursor);
682 myCursorState = true;
684 case VTK_INTERACTOR_STYLE_CAMERA_NONE:
686 myGUIWindow->setCursor(myDefCursor);
687 myCursorState = false;
692 // called when viewer operation started (!put necessary initialization here!)
693 void VTKViewer_InteractorStyleSALOME::onStartOperation()
695 if (!myGUIWindow) return;
697 case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
698 case VTK_INTERACTOR_STYLE_CAMERA_FIT:
700 QPainter p(myGUIWindow);
701 p.setPen(Qt::lightGray);
702 p.setRasterOp(Qt::XorROP);
703 p.drawRect(QRect(myPoint, myOtherPoint));
706 case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
707 case VTK_INTERACTOR_STYLE_CAMERA_PAN:
708 case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
709 case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
710 case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
715 // called when viewer operation finished (!put necessary post-processing here!)
716 void VTKViewer_InteractorStyleSALOME::onFinishOperation()
718 if (!myGUIWindow) return;
720 QAD_Study* aActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
721 SALOME_Selection* aSel = SALOME_Selection::Selection( aActiveStudy->getSelection() );
722 vtkRenderWindowInteractor *rwi = this->Interactor;
724 int aSelectionMode = aSel->SelectionMode();
725 bool aSelActiveCompOnly = aSel->IsSelectActiveCompOnly();
726 SALOMEDS::SComponent_var aActiveComponent = SALOMEDS::SComponent::_narrow(
727 aActiveStudy->getStudyDocument()->FindObject(QAD_Application::getDesktop()->getActiveComponent()));
730 case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
731 case VTK_INTERACTOR_STYLE_CAMERA_FIT:
733 QPainter p(myGUIWindow);
734 p.setPen(Qt::lightGray);
735 p.setRasterOp(Qt::XorROP);
736 QRect rect(myPoint, myOtherPoint);
738 rect = rect.normalize();
739 if (State == VTK_INTERACTOR_STYLE_CAMERA_FIT) {
740 // making fit rect opeation
745 y1 = h - rect.top() - 1;
747 y2 = h - rect.bottom() - 1;
748 fitRect(x1, y1, x2, y2);
751 if (myPoint == myOtherPoint) {
752 // process point selection
756 y = h - myPoint.y() - 1;
757 vtkActorCollection* listactors = NULL;
758 this->FindPokedRenderer(x, y);
759 rwi->StartPickCallback();
760 rwi->GetPicker()->Pick(x, y, 0.0, this->CurrentRenderer);
762 if ( rwi->GetPicker()->IsA("vtkCellPicker") ) {
763 vtkCellPicker* picker;
764 if ( (picker = vtkCellPicker::SafeDownCast(rwi->GetPicker())) ) {
765 MESSAGE ( " CellId : " << picker->GetCellId() );
766 if ( picker->GetCellId() >= 0 ) {
767 vtkActor* ac = picker->GetActor();
768 if ( ac->IsA("SALOME_Actor") ) {
769 SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( ac );
770 MESSAGE ( " NAME Actor : " << SActor->getName() );
772 //Cell selection //////////////////////////////////// NB
773 if ( aSelectionMode == 3 ) {
774 if ( SActor->hasIO() ) {
775 Handle(SALOME_InteractiveObject) IO = SActor->getIO();
776 // Look in the current selection
777 SALOME_ListIteratorOfListIO It(aSel->StoredIObjects());
778 Standard_Boolean IsSelected = false;
779 for(;It.More();It.Next()) {
780 Handle(SALOME_InteractiveObject) IOS = It.Value();
781 if(IO->isSame(IOS)) {
783 IO = IOS; //Added by SRN, fix SAL1307
788 // This IO is already in the selection
790 bool add = aSel->AddOrRemoveIndex( IO, picker->GetCellId(), myShiftState, false );
791 //Sel->RemoveIObject(IO);
795 this->HighlightProp( NULL );
796 aSel->ClearIObjects();
798 bool add = aSel->AddOrRemoveIndex( IO, picker->GetCellId(), myShiftState, false );
799 aSel->AddIObject( IO, false );
803 //Edge selection ////////////////////////// NB
804 else if ( aSelectionMode == 2 ) {
806 Handle(SALOME_InteractiveObject) IO = SActor->getIO();
807 float pickPosition[3],pcoords[3],closestPoint[3],weights[3],dist1=1000000.0,dist2=0;
808 int subId,edgeId=-10,pickedID,result;
809 pickedID = picker->GetCellId();
810 picker->GetPickPosition(pickPosition);
811 if (vtkDataSet* UGrid = SActor->GetMapper()->GetInput()){
812 if (vtkCell* pickedCell = UGrid->GetCell(pickedID)){
814 for (int i = 0, iEnd = pickedCell->GetNumberOfEdges(); i < iEnd; i++){
815 vtkCell* edge = pickedCell->GetEdge(i);
816 if(vtkLine* line = vtkLine::SafeDownCast(edge)){
817 result = line->EvaluatePosition(pickPosition,closestPoint,subId,pcoords,dist2,weights);
824 MESSAGE("edgeID transformed = "<<edgeId);
825 // Look in the current selection
826 SALOME_ListIteratorOfListIO It(aSel->StoredIObjects());
827 Standard_Boolean IsSelected = false;
828 for(;It.More();It.Next()) {
829 Handle(SALOME_InteractiveObject) IOS = It.Value();
830 if(IO->isSame(IOS)) {
831 IO = IOS; //Added by SRN, fix SAL1307
837 this->HighlightProp( NULL );
838 aSel->ClearIObjects();
840 aSel->SetSelectionMode(2, true);
841 bool add = aSel->AddOrRemoveIndex( IO, pickedID, true, false);
843 add = aSel->AddOrRemoveIndex( IO, -edgeId-1, true, true );
844 aSel->AddIObject( IO, false );
850 this->HighlightProp( NULL );
851 aSel->ClearIObjects();
855 } else if ( rwi->GetPicker()->IsA("vtkPointPicker") ) {
856 vtkPointPicker* picker;
857 if ( (picker = vtkPointPicker::SafeDownCast(rwi->GetPicker())) ) {
858 MESSAGE ( " PointId : " << picker->GetPointId() );
859 if ( picker->GetPointId() >= 0 ) {
860 vtkActor* ac = picker->GetActor();
861 if ( ac->IsA("SALOME_Actor") ) {
862 SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( ac );
863 MESSAGE ( " NAME Actor : " << SActor->getName() );
864 if ( SActor->hasIO() ) {
865 Handle(SALOME_InteractiveObject) IO = SActor->getIO();
868 if (aSelActiveCompOnly &&
869 strcmp(aActiveComponent->ComponentDataType(), IO->getComponentDataType()) != 0) {
872 // Look in the current selection
873 SALOME_ListIteratorOfListIO It(aSel->StoredIObjects());
874 Standard_Boolean IsSelected = false;
875 for(;It.More();It.Next()) {
876 Handle(SALOME_InteractiveObject) IOS = It.Value();
877 if(IO->isSame(IOS)) {
878 IO = IOS; //Added by SRN, fix SAL1307
884 // This IO is already in the selection
885 bool add = aSel->AddOrRemoveIndex( IO, picker->GetPointId(), myShiftState, false );
888 this->HighlightProp( NULL );
889 aSel->ClearIObjects();
891 bool add = aSel->AddOrRemoveIndex( IO, picker->GetPointId(), myShiftState, false );
892 aSel->AddIObject( IO, false );
897 this->HighlightProp( NULL );
898 aSel->ClearIObjects();
903 if ( (picker = vtkPicker::SafeDownCast(rwi->GetPicker())) ) {
904 listactors = picker->GetActors();
906 if ( listactors->GetNumberOfItems() == 0 ) {
907 // No selection clear all
908 this->PropPicked = 0;
909 this->HighlightProp( NULL );
910 aSel->ClearIObjects();
913 listactors->InitTraversal();
914 ac = listactors->GetNextActor();
915 if ( ac->IsA("SALOME_Actor") ) {
916 SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( ac );
917 if ( SActor->hasIO() ) {
919 Handle(SALOME_InteractiveObject) IO = SActor->getIO();
920 // Look in the current selection
921 SALOME_ListIteratorOfListIO It(aSel->StoredIObjects());
922 Standard_Boolean IsSelected = false;
923 for(;It.More();It.Next()) {
924 Handle(SALOME_InteractiveObject) IOS = It.Value();
925 if( IO->isSame(IOS) ) {
926 IO = IOS; //Added by SRN, fix SAL1307
932 // This IO is already in the selection
934 aSel->RemoveIObject(IO);
939 this->HighlightProp( NULL );
940 aSel->ClearIObjects();
942 aSel->AddIObject( IO, false );
947 rwi->EndPickCallback();
950 //processing rectangle selection
951 rwi->StartPickCallback();
954 this->PropPicked = 0;
955 this->HighlightProp( NULL );
956 aSel->ClearIObjects();
960 vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
961 QRect rect(myPoint, myOtherPoint);
962 rect = rect.normalize();
967 y1 = h - rect.top() - 1;
969 y2 = h - rect.bottom() - 1;
971 switch (aSelectionMode) {
972 case 1: // Nodes selection
974 if (! rwi->GetPicker()->IsA("vtkPointPicker") ) break;
975 vtkPointPicker* aPointPicker = vtkPointPicker::SafeDownCast(rwi->GetPicker());
976 vtkActorCollection* aListActors = this->CurrentRenderer->GetActors();
977 aListActors->InitTraversal();
979 for (int k = 0; k < aListActors->GetNumberOfItems(); k++) {
980 aActor = aListActors->GetNextActor();
981 if (aActor != NULL) {
982 if (aActor->GetVisibility() == 0)
984 vtkAbstractMapper3D* aMapper3D = aActor->GetMapper();
985 if ((aMapper3D != NULL) && (aActor->IsA("SALOME_Actor"))) {
986 SALOME_Actor* SActor = SALOME_Actor::SafeDownCast(aActor);
988 if ((SActor != NULL) && (SActor->hasIO())) {
989 Handle(SALOME_InteractiveObject) IO = SActor->getIO();
992 if (aSelActiveCompOnly &&
993 strcmp(aActiveComponent->ComponentDataType(), IO->getComponentDataType()) != 0) {
998 vtkVolumeMapper* aVolumeMapper;
999 vtkDataSet* aDataSet;
1001 if ( (aMapper = vtkMapper::SafeDownCast(aMapper3D)) != NULL ) {
1002 aDataSet = aMapper->GetInput();
1003 } else if ((aVolumeMapper = vtkVolumeMapper::SafeDownCast(aMapper3D)) != NULL ){
1004 aDataSet = aVolumeMapper->GetInput();
1009 for (int i=0; i < aDataSet->GetNumberOfPoints(); i++) {
1011 aPoint = aDataSet->GetPoint(i);
1012 if (IsInRect(aPoint, x1, y1, x2, y2)) {
1014 ComputeWorldToDisplay(aPoint[0],
1017 aPointPicker->Pick(aDisp[0], aDisp[1], 0.0, CurrentRenderer);
1018 if ( aPointPicker->GetPointId() >= 0) { // && (!aSel->IsIndexSelected(IO, aPointPicker->GetPointId()))) {
1019 aSel->AddOrRemoveIndex(IO, aPointPicker->GetPointId(), true, false);
1020 aSel->AddIObject(IO, false);
1031 case 2: // edges selection
1032 case 3: // triangles selection
1034 aSel->SetSelectionMode(aSelectionMode,true);
1035 if (!rwi->GetPicker()->IsA("vtkCellPicker") ) break;
1036 vtkCellPicker* aCellPicker = vtkCellPicker::SafeDownCast(rwi->GetPicker());
1037 vtkActorCollection* aListActors = this->CurrentRenderer->GetActors();
1038 aListActors->InitTraversal();
1040 for (int k = 0, kEnd = aListActors->GetNumberOfItems(); k < kEnd; k++){
1041 vtkActor* aActor = aListActors->GetNextActor();
1042 if (vtkActor* aActor = aListActors->GetNextActor()){
1043 if (aActor->GetVisibility() == 0) continue;
1044 if(SALOME_Actor* SActor = SALOME_Actor::SafeDownCast(aActor)) {
1045 if(SActor->hasIO()) {
1046 Handle(SALOME_InteractiveObject) IO = SActor->getIO();
1047 if(IO.IsNull()) continue;
1048 if(aSelActiveCompOnly)
1049 if(strcmp(aActiveComponent->ComponentDataType(),IO->getComponentDataType()) != 0)
1051 if(vtkDataSet* aDataSet = SActor->GetMapper()->GetInput()){
1052 for(int i = 0, iEnd = aDataSet->GetNumberOfCells(); i < iEnd; i++){
1053 if(vtkCell* aCell = aDataSet->GetCell(i)){
1054 if(IsInRect(aCell, x1, y1, x2, y2)){
1055 float* aBounds = aCell->GetBounds();
1057 aCenter[0] =(aBounds[0] + aBounds[1])/2; // Center X
1058 aCenter[1] =(aBounds[2] + aBounds[3])/2; // Center Y
1059 aCenter[2] =(aBounds[4] + aBounds[5])/2; // Center Z
1061 ComputeWorldToDisplay(aCenter[0],aCenter[1],aCenter[2],aDisp);
1062 aCellPicker->Pick(aDisp[0], aDisp[1], 0.0, CurrentRenderer);
1063 if(aCellPicker->GetCellId() >= 0 && !aSel->IsIndexSelected(IO,aCellPicker->GetCellId())){
1064 aSel->AddOrRemoveIndex( IO, aCellPicker->GetCellId(), true, false);
1065 aSel->AddIObject( IO, false );
1077 case 4: // objects selection
1079 vtkActorCollection* aListActors = this->CurrentRenderer->GetActors();
1080 aListActors->InitTraversal();
1082 SALOME_ListIO aListIO;
1083 for (int k = 0; k < aListActors->GetNumberOfItems(); k++) {
1084 aActor = aListActors->GetNextActor();
1086 if (aActor->GetVisibility() == 0)
1088 if ( aActor->IsA("SALOME_Actor") ) {
1089 SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast(aActor);
1090 if ( aSActor->hasIO() && IsInRect(aSActor, x1, y1, x2, y2)) {
1091 Handle(SALOME_InteractiveObject) aIO = aSActor->getIO();
1094 if (aSelActiveCompOnly &&
1095 strcmp(aActiveComponent->ComponentDataType(), aIO->getComponentDataType()) != 0) {
1098 if (aListIO.IsEmpty()) {
1099 aListIO.Append( aIO );
1101 SALOME_ListIteratorOfListIO It(aListIO);
1102 bool isStored = false;
1103 for(;It.More();It.Next()) {
1104 Handle(SALOME_InteractiveObject) IOS = It.Value();
1105 if( aIO->isSame(IOS) ) {
1106 aIO = IOS; //Added by SRN, fix SAL1307
1112 aListIO.Append( aIO );
1118 if (!aListIO.IsEmpty()) {
1119 SALOME_ListIteratorOfListIO It(aListIO);
1120 for(;It.More();It.Next()) {
1121 Handle(SALOME_InteractiveObject) IOS = It.Value();
1123 aSel->AddIObject( IOS, false );
1128 rwi->EndPickCallback();
1130 aActiveStudy->update3dViewers();
1134 case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
1135 case VTK_INTERACTOR_STYLE_CAMERA_PAN:
1136 case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
1137 case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
1139 case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
1144 y = h - myPoint.y() - 1;
1151 // called during viewer operation when user moves mouse (!put necessary processing here!)
1152 void VTKViewer_InteractorStyleSALOME::onOperation(QPoint mousePos)
1154 if (!myGUIWindow) return;
1156 GetInteractor()->GetSize(w, h);
1158 case VTK_INTERACTOR_STYLE_CAMERA_PAN:
1160 // processing panning
1161 //this->FindPokedCamera(mousePos.x(), mousePos.y());
1162 this->PanXY(mousePos.x(), myPoint.y(), myPoint.x(), mousePos.y());
1166 case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
1168 // processing zooming
1169 //this->FindPokedCamera(mousePos.x(), mousePos.y());
1170 this->DollyXY(mousePos.x() - myPoint.x(), mousePos.y() - myPoint.y());
1174 case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
1176 // processing rotation
1177 //this->FindPokedCamera(mousePos.x(), mousePos.y());
1178 this->RotateXY(mousePos.x() - myPoint.x(), myPoint.y() - mousePos.y());
1182 case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
1184 // processing spinning
1185 //this->FindPokedCamera(mousePos.x(), mousePos.y());
1186 this->SpinXY(mousePos.x(), mousePos.y(), myPoint.x(), myPoint.y());
1190 case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
1194 case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
1197 setCursor(VTK_INTERACTOR_STYLE_CAMERA_SELECT);
1199 case VTK_INTERACTOR_STYLE_CAMERA_FIT:
1201 QPainter p(myGUIWindow);
1202 p.setPen(Qt::lightGray);
1203 p.setRasterOp(Qt::XorROP);
1204 p.drawRect(QRect(myPoint, myOtherPoint));
1205 myOtherPoint = mousePos;
1206 p.drawRect(QRect(myPoint, myOtherPoint));
1210 this->LastPos[0] = mousePos.x();
1211 this->LastPos[1] = h - mousePos.y() - 1;
1214 // called when user moves mouse inside viewer window and there is no active viewer operation
1215 // (!put necessary processing here!)
1216 void VTKViewer_InteractorStyleSALOME::onCursorMove(QPoint mousePos) {
1217 // processing highlighting
1218 QAD_Study* myActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
1219 SALOME_Selection* Sel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
1221 vtkRenderWindowInteractor *rwi = this->Interactor;
1224 x = mousePos.x(); y = h - mousePos.y() - 1;
1226 this->FindPokedRenderer(x,y);
1227 rwi->StartPickCallback();
1228 rwi->GetPicker()->Pick(x, y, 0.0, this->CurrentRenderer);
1230 if ( rwi->GetPicker()->IsA("vtkPicker") ) {
1231 vtkPicker* picker = vtkPicker::SafeDownCast(rwi->GetPicker());
1232 vtkActor* ac = picker->GetActor();
1235 if ( ac->IsA("SALOME_Actor") ) {
1236 SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( ac );
1237 if ( preview != SActor ) {
1238 if ( preview != NULL ) {
1239 preview->SetPreSelected( false );
1243 if ( SActor->hasIO() ) {
1244 Handle( SALOME_InteractiveObject) IO = SActor->getIO();
1246 SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
1247 Standard_Boolean IsSelected = false;
1248 for(;It.More();It.Next()) {
1249 Handle(SALOME_InteractiveObject) IOS = It.Value();
1250 if(IO->isSame(IOS)) {
1256 if ( !IsSelected ) {
1257 // Find All actors with same IO
1258 vtkActorCollection* theActors = this->CurrentRenderer->GetActors();
1259 theActors->InitTraversal();
1260 vtkActor *ac = theActors->GetNextActor();
1262 if ( ac->IsA("SALOME_Actor") ) {
1263 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
1264 if ( anActor->hasIO() ) {
1265 Handle(SALOME_InteractiveObject) IOS = anActor->getIO();
1266 if(IO->isSame(IOS)) {
1267 anActor->SetPreSelected( true );
1271 ac = theActors->GetNextActor();
1273 // MESSAGE ( " NAME PREVIEW " << SActor->getName() );
1280 vtkActorCollection* theActors = this->CurrentRenderer->GetActors();
1281 theActors->InitTraversal();
1282 vtkActor *ac = theActors->GetNextActor();
1284 if ( ac->IsA("SALOME_Actor") ) {
1285 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
1286 anActor->SetPreSelected( false );
1288 ac = theActors->GetNextActor();
1292 this->LastPos[0] = x;
1293 this->LastPos[1] = y;
1296 // called on finsh GlobalPan operation
1297 void VTKViewer_InteractorStyleSALOME::Place(const int theX, const int theY)
1299 if (this->CurrentRenderer == NULL) {
1304 int *aSize = this->CurrentRenderer->GetRenderWindow()->GetSize();
1305 int centerX = aSize[0]/2;
1306 int centerY = aSize[1]/2;
1308 TranslateView(centerX, centerY, theX, theY);
1310 // restore zoom scale
1311 vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
1312 cam->SetParallelScale(myScale);
1313 this->CurrentRenderer->ResetCameraClippingRange();
1315 /* VSV Light follows camera: if (this->CurrentLight) {
1316 this->CurrentLight->SetPosition(cam->GetPosition());
1317 this->CurrentLight->SetFocalPoint(cam->GetFocalPoint());
1319 //VTKViewer_RenderWindow* aRW = dynamic_cast<VTKViewer_RenderWindow*>(this->Interactor->GetRenderWindow());
1320 if (myGUIWindow) myGUIWindow->update();
1325 // Translates view from Point to Point
1326 void VTKViewer_InteractorStyleSALOME::TranslateView(int toX, int toY, int fromX, int fromY)
1328 vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
1329 double viewFocus[4], focalDepth, viewPoint[3];
1330 float newPickPoint[4], oldPickPoint[4], motionVector[3];
1331 cam->GetFocalPoint(viewFocus);
1333 this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1],
1334 viewFocus[2], viewFocus);
1335 focalDepth = viewFocus[2];
1337 this->ComputeDisplayToWorld(double(toX), double(toY),
1338 focalDepth, newPickPoint);
1339 this->ComputeDisplayToWorld(double(fromX),double(fromY),
1340 focalDepth, oldPickPoint);
1342 // camera motion is reversed
1343 motionVector[0] = oldPickPoint[0] - newPickPoint[0];
1344 motionVector[1] = oldPickPoint[1] - newPickPoint[1];
1345 motionVector[2] = oldPickPoint[2] - newPickPoint[2];
1347 cam->GetFocalPoint(viewFocus);
1348 cam->GetPosition(viewPoint);
1349 cam->SetFocalPoint(motionVector[0] + viewFocus[0],
1350 motionVector[1] + viewFocus[1],
1351 motionVector[2] + viewFocus[2]);
1352 cam->SetPosition(motionVector[0] + viewPoint[0],
1353 motionVector[1] + viewPoint[1],
1354 motionVector[2] + viewPoint[2]);
1358 /// Checks: is the given Actor within display coordinates?
1359 bool VTKViewer_InteractorStyleSALOME::IsInRect(vtkActor* theActor,
1360 const int left, const int top,
1361 const int right, const int bottom)
1363 float* aBounds = theActor->GetBounds();
1364 float aMin[3], aMax[3];
1365 ComputeWorldToDisplay(aBounds[0], aBounds[2], aBounds[4], aMin);
1366 ComputeWorldToDisplay(aBounds[1], aBounds[3], aBounds[5], aMax);
1367 if (aMin[0] > aMax[0]) {
1368 float aBuf = aMin[0];
1372 if (aMin[1] > aMax[1]) {
1373 float aBuf = aMin[1];
1378 return ((aMin[0]>left) && (aMax[0]<right) && (aMin[1]>bottom) && (aMax[1]<top));
1382 /// Checks: is the given Cell within display coordinates?
1383 bool VTKViewer_InteractorStyleSALOME::IsInRect(vtkCell* theCell,
1384 const int left, const int top,
1385 const int right, const int bottom)
1387 float* aBounds = theCell->GetBounds();
1388 float aMin[3], aMax[3];
1389 ComputeWorldToDisplay(aBounds[0], aBounds[2], aBounds[4], aMin);
1390 ComputeWorldToDisplay(aBounds[1], aBounds[3], aBounds[5], aMax);
1391 if (aMin[0] > aMax[0]) {
1392 float aBuf = aMin[0];
1396 if (aMin[1] > aMax[1]) {
1397 float aBuf = aMin[1];
1402 return ((aMin[0]>left) && (aMax[0]<right) && (aMin[1]>bottom) && (aMax[1]<top));
1406 bool VTKViewer_InteractorStyleSALOME::IsInRect(float* thePoint,
1407 const int left, const int top,
1408 const int right, const int bottom)
1411 ComputeWorldToDisplay(thePoint[0], thePoint[1], thePoint[2], aPnt);
1413 return ((aPnt[0]>left) && (aPnt[0]<right) && (aPnt[1]>bottom) && (aPnt[1]<top));