Salome HOME
Fix on:
[modules/kernel.git] / src / VTKViewer / VTKViewer_ViewFrame.cxx
1 //  SALOME VTKViewer : build VTK viewer into Salome desktop
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : VTKViewer_ViewFrame.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 #include "VTKViewer_ViewFrame.h"
30 #include "VTKViewer_Utilities.h"
31 #include "VTKViewer_Trihedron.h"
32 #include "VTKViewer_RenderWindow.h"
33 #include "VTKViewer_RenderWindowInteractor.h"
34 #include "VTKViewer_InteractorStyleSALOME.h"
35 #include "VTKViewer_Algorithm.h"
36 #include "VTKViewer_Functor.h"
37 #include "VTKViewer_Prs.h"
38
39 #include "SALOME_Actor.h"
40 #include "SALOME_Transform.h"
41 #include "SALOME_TransformFilter.h"
42 #include "SALOME_GeometryFilter.h"
43 #include "SALOMEGUI.h"
44
45 #include "QAD_Settings.h"
46 #include "QAD_Config.h"
47 #include "QAD_Application.h"
48 #include "QAD_Desktop.h"
49 #include "SALOME_Selection.h"
50 #include "SALOME_InteractiveObject.hxx"
51 #include "ToolsGUI.h"
52 #include "SALOMEDS_Tool.hxx"
53
54 #include "utilities.h"
55
56 //QT Include
57 #include <qlayout.h>
58 #include <qcolordialog.h>
59 #include <qfiledialog.h>
60 #include <qapplication.h>
61
62 // VTK Includes
63 #include <vtkActor.h>
64 #include <vtkCamera.h>
65 #include <vtkRenderer.h>
66 #include <vtkTransform.h>
67 #include <vtkActorCollection.h>
68
69 #include <TColStd_IndexedMapOfInteger.hxx>
70
71 using namespace std;
72
73 /*!
74     Constructor
75 */
76 VTKViewer_ViewFrame::VTKViewer_ViewFrame(QWidget* parent, const char* name) 
77   :  QAD_ViewFrame(parent, name)
78 {
79   m_ViewUp[0] = 0; m_ViewUp[1] = 0; m_ViewUp[2] = -1;
80   m_ViewNormal[0] = 0; m_ViewNormal[1] = 0; m_ViewNormal[2] = 1;
81   m_Triedron = VTKViewer_Trihedron::New();
82   m_Transform = SALOME_Transform::New();
83   //m_Renderer = VTKViewer_Renderer::New() ;
84   m_Renderer = vtkRenderer::New() ;
85
86   m_Triedron->AddToRender(m_Renderer);
87   InitialSetup();
88 }  
89
90 void VTKViewer_ViewFrame::InitialSetup() {
91   m_RW = new VTKViewer_RenderWindow(this, "RenderWindow");
92   m_RW->getRenderWindow()->AddRenderer(m_Renderer);
93
94   m_Renderer->GetActiveCamera()->ParallelProjectionOn();
95   m_Renderer->LightFollowCameraOn();
96   m_Renderer->TwoSidedLightingOn();
97
98   // Set BackgroundColor
99   QString BgrColorRed   = QAD_CONFIG->getSetting("VTKViewer:BackgroundColorRed");
100   QString BgrColorGreen = QAD_CONFIG->getSetting("VTKViewer:BackgroundColorGreen");
101   QString BgrColorBlue  = QAD_CONFIG->getSetting("VTKViewer:BackgroundColorBlue");
102
103   if( !BgrColorRed.isEmpty() && !BgrColorGreen.isEmpty() && !BgrColorBlue.isEmpty() ) 
104     m_Renderer->SetBackground( BgrColorRed.toInt()/255., BgrColorGreen.toInt()/255., BgrColorBlue.toInt()/255. );
105   else
106     m_Renderer->SetBackground( 0, 0, 0 );
107   
108   // Create an interactor.
109   m_RWInteractor = VTKViewer_RenderWindowInteractor::New();
110   m_RWInteractor->SetRenderWindow(m_RW->getRenderWindow());
111
112   VTKViewer_InteractorStyleSALOME* RWS = VTKViewer_InteractorStyleSALOME::New();
113   m_RWInteractor->SetInteractorStyle(RWS); 
114   RWS->Delete();
115
116   m_RWInteractor->setGUIWindow(m_RW);
117   RWS->setGUIWindow(m_RW);
118
119   m_RWInteractor->Initialize();
120   m_RWInteractor->setViewFrame(this);
121   RWS->setTriedron(m_Triedron);
122   RWS->setViewFrame(this);
123   //SRN: additional initialization, to init CurrentRenderer of vtkInteractorStyle 
124   RWS->FindPokedRenderer(0, 0);
125
126   setCentralWidget( m_RW );
127   onViewReset();
128 }
129
130 VTKViewer_ViewFrame::~VTKViewer_ViewFrame() {
131   // In order to ensure that the interactor unregisters
132   // this RenderWindow, we assign a NULL RenderWindow to 
133   // it before deleting it.
134   m_RWInteractor->SetRenderWindow(NULL) ;
135   m_RWInteractor->Delete() ;
136   
137   m_Transform->Delete() ;
138   //m_RW->Delete() ;
139   m_Renderer->RemoveAllProps();
140   // NRI : BugID 1137:  m_Renderer->Delete() ;
141   m_Triedron->Delete();
142   INFOS("VTKViewer_ViewFrame::~VTKViewer_ViewFrame()");
143 }
144
145 /*!
146   Returns widget containing 3D-Viewer
147 */
148 QWidget* VTKViewer_ViewFrame::getViewWidget(){
149   return m_RW;
150 }
151
152 bool VTKViewer_ViewFrame::isTrihedronDisplayed(){
153   return m_Triedron->GetVisibility() == VTKViewer_Trihedron::eOn;
154 }
155
156 bool VTKViewer_ViewFrame::ComputeTrihedronSize( double& theNewSize, double& theSize )
157 {
158   // calculating diagonal of visible props of the renderer
159   float bnd[ 6 ];
160   m_Triedron->VisibilityOff();
161   if ( ::ComputeVisiblePropBounds( m_Renderer, bnd ) == 0 )
162   {
163     bnd[ 1 ] = bnd[ 3 ] = bnd[ 5 ] = 100;
164     bnd[ 0 ] = bnd[ 2 ] = bnd[ 100 ] = 0;
165   }
166   m_Triedron->VisibilityOn();
167   float aLength = 0;
168   static bool CalcByDiag = false;
169   if ( CalcByDiag )
170   {
171     aLength = sqrt( ( bnd[1]-bnd[0])*(bnd[1]-bnd[0] )+
172                     ( bnd[3]-bnd[2])*(bnd[3]-bnd[2] )+
173                     ( bnd[5]-bnd[4])*(bnd[5]-bnd[4] ) );
174   }
175   else
176   {
177     aLength = bnd[ 1 ]-bnd[ 0 ];
178     aLength = max( ( bnd[ 3 ] - bnd[ 2 ] ),aLength );
179     aLength = max( ( bnd[ 5 ] - bnd[ 4 ] ),aLength );
180   }
181
182   static float aSizeInPercents = 105;
183   QString aSetting = QAD_CONFIG->getSetting( "Viewer:TrihedronSize" );
184   if ( !aSetting.isEmpty() )
185     aSizeInPercents = aSetting.toFloat();
186
187   static float EPS_SIZE = 5.0E-3;
188   theSize = m_Triedron->GetSize();
189   theNewSize = aLength * aSizeInPercents / 100.0;
190
191   // if the new trihedron size have sufficient difference, then apply the value
192   return fabs( theNewSize - theSize) > theSize * EPS_SIZE ||
193          fabs( theNewSize-theSize ) > theNewSize * EPS_SIZE;
194 }
195
196 double VTKViewer_ViewFrame::GetTrihedronSize() const
197 {
198   return m_Triedron->GetSize();
199 }
200
201 void VTKViewer_ViewFrame::AdjustTrihedrons( const bool forcedUpdate )
202 {
203   if ( !isTrihedronDisplayed() && !forcedUpdate )
204     return;
205
206   int aVisibleNum = m_Triedron->GetVisibleActorCount( m_Renderer );
207   if ( aVisibleNum || forcedUpdate )
208   {
209     // if the new trihedron size have sufficient difference, then apply the value
210     double aNewSize = 100, anOldSize;
211     if ( ComputeTrihedronSize( aNewSize, anOldSize ) || forcedUpdate )
212     {
213       m_Triedron->SetSize( aNewSize );
214       // itearte throuh displayed objects and set size if necessary
215
216       vtkActorCollection* anActors = getRenderer()->GetActors();
217       anActors->InitTraversal();
218       while( vtkActor* anActor = anActors->GetNextActor() )
219       {
220         if( SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast( anActor ) )
221         {
222           if ( aSActor->IsResizable() )
223             aSActor->SetSize( 0.5 * aNewSize );
224         }
225       }
226     }
227   }
228
229   ::ResetCameraClippingRange(m_Renderer);
230 }
231
232 void VTKViewer_ViewFrame::onAdjustTrihedron()
233 {   
234   AdjustTrihedrons( false );
235 }
236
237 /*!
238   Display/hide Trihedron
239 */
240 void VTKViewer_ViewFrame::onViewTrihedron(){
241   if(!m_Triedron) return;
242   if(isTrihedronDisplayed())
243     m_Triedron->VisibilityOff();
244   else{
245     m_Triedron->VisibilityOn();
246   }
247   Repaint();
248 }
249
250 /*!
251   Provides top projection of the active view
252 */
253 void VTKViewer_ViewFrame::onViewTop(){
254   vtkCamera* camera = m_Renderer->GetActiveCamera();
255   camera->SetPosition(0,0,1);
256   camera->SetViewUp(0,1,0);
257   camera->SetFocalPoint(0,0,0);
258   onViewFitAll();
259 }
260
261 /*!
262   Provides bottom projection of the active view
263 */
264 void VTKViewer_ViewFrame::onViewBottom(){
265   vtkCamera* camera = m_Renderer->GetActiveCamera();
266   camera->SetPosition(0,0,-1);
267   camera->SetViewUp(0,1,0);
268   camera->SetFocalPoint(0,0,0);
269   onViewFitAll();
270 }
271
272 /*!
273   Provides left projection of the active view
274 */
275 void VTKViewer_ViewFrame::onViewLeft(){
276   vtkCamera* camera = m_Renderer->GetActiveCamera(); 
277   camera->SetPosition(0,-1,0);
278   camera->SetViewUp(0,0,1);
279   camera->SetFocalPoint(0,0,0);
280   onViewFitAll();
281 }
282
283 /*!
284   Provides right projection of the active view
285 */
286 void VTKViewer_ViewFrame::onViewRight(){
287   vtkCamera* camera = m_Renderer->GetActiveCamera();
288   camera->SetPosition(0,1,0);
289   camera->SetViewUp(0,0,1);
290   camera->SetFocalPoint(0,0,0);
291   onViewFitAll();
292 }
293
294 /*!
295   Provides back projection of the active view
296 */
297 void VTKViewer_ViewFrame::onViewBack(){
298   vtkCamera* camera = m_Renderer->GetActiveCamera();
299   camera->SetPosition(-1,0,0);
300   camera->SetViewUp(0,0,1);
301   camera->SetFocalPoint(0,0,0);
302   onViewFitAll();
303 }
304
305 /*!
306   Provides front projection of the active view
307 */
308 void VTKViewer_ViewFrame::onViewFront(){
309   vtkCamera* camera = m_Renderer->GetActiveCamera();
310   camera->SetPosition(1,0,0);
311   camera->SetViewUp(0,0,1);
312   camera->SetFocalPoint(0,0,0);
313   onViewFitAll();
314 }
315
316 /*!
317   Fits all objects in the active view
318 */
319 void VTKViewer_ViewFrame::onViewFitAll(){
320   m_RWInteractor->GetInteractorStyleSALOME()->ViewFitAll();
321   Repaint();
322 }
323
324 /*!
325   Reset the active view
326 */
327 void VTKViewer_ViewFrame::onViewReset(){
328   int aTriedronIsVisible = isTrihedronDisplayed();
329   m_Triedron->SetVisibility(VTKViewer_Trihedron::eOnlyLineOn);
330   ::ResetCamera(m_Renderer,true);  
331   vtkCamera* aCamera = m_Renderer->GetActiveCamera();
332   aCamera->SetPosition(1,-1,1);
333   aCamera->SetViewUp(0,0,1);
334   ::ResetCamera(m_Renderer,true);  
335   if(aTriedronIsVisible) m_Triedron->VisibilityOn();
336   else m_Triedron->VisibilityOff();
337   static float aCoeff = 3.0;
338   aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
339   Repaint();
340 }
341
342 /*!
343   Rotates the active view
344 */
345 void VTKViewer_ViewFrame::onViewRotate(){
346   m_RWInteractor->GetInteractorStyleSALOME()->startRotate();
347 }
348
349 /*!
350   Sets a new center of the active view
351 */
352 void VTKViewer_ViewFrame::onViewGlobalPan(){
353   if(m_Triedron->GetVisibleActorCount(m_Renderer))
354     m_RWInteractor->GetInteractorStyleSALOME()->startGlobalPan();
355 }
356
357 /*!
358   Zooms the active view
359 */
360 void VTKViewer_ViewFrame::onViewZoom(){
361   m_RWInteractor->GetInteractorStyleSALOME()->startZoom();
362 }
363
364 /*!
365   Moves the active view
366 */
367 void VTKViewer_ViewFrame::onViewPan(){
368   m_RWInteractor->GetInteractorStyleSALOME()->startPan();
369 }
370
371 /*!
372   Fits all obejcts within a rectangular area of the active view
373 */
374 void VTKViewer_ViewFrame::onViewFitArea(){
375   m_RWInteractor->GetInteractorStyleSALOME()->startFitArea();
376 }
377
378 /*!
379     Set background of the viewport
380 */
381 void VTKViewer_ViewFrame::setBackgroundColor( const QColor& color)
382 {
383   if ( m_Renderer )
384     m_Renderer->SetBackground( color.red()/255., color.green()/255., color.blue()/255. );
385 }
386
387 /*!
388     Returns background of the viewport
389 */
390 QColor VTKViewer_ViewFrame::backgroundColor() const
391 {
392   float backint[3];
393   if ( m_Renderer ) {
394     m_Renderer->GetBackground(backint);
395     return QColorDialog::getColor ( QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255)), NULL );
396   }
397   return QMainWindow::backgroundColor();
398 }
399
400
401 void VTKViewer_ViewFrame::SetSelectionMode( Selection_Mode mode )
402 {
403   m_RWInteractor->SetSelectionMode( mode );
404 }
405
406 void VTKViewer_ViewFrame::rename( const Handle(SALOME_InteractiveObject)& IObject, QString newName )
407 {
408   m_RWInteractor->rename(IObject, newName);
409 }
410
411 void VTKViewer_ViewFrame::unHighlightAll() 
412 {
413   m_RWInteractor->unHighlightAll();
414 }
415
416 void VTKViewer_ViewFrame::highlight( const Handle(SALOME_InteractiveObject)& IObject, 
417                                      bool highlight, 
418                                      bool update ) 
419 {
420   QAD_Study* ActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
421   SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
422   m_RWInteractor->highlight(IObject, highlight, update);
423
424   if(Sel->HasIndex(IObject) && IObject->hasEntry()){
425     TColStd_IndexedMapOfInteger MapIndex;
426     Sel->GetIndex(IObject,MapIndex);
427     using namespace SALOME::VTK;
428     const char* anEntry = IObject->getEntry();
429     vtkActorCollection* aCollection = getRenderer()->GetActors();
430     if(SALOME_Actor* anActor = Find<SALOME_Actor>(aCollection,TIsSameEntry<SALOME_Actor>(anEntry))){
431       switch (Sel->SelectionMode()) {
432       case NodeSelection:
433         m_RWInteractor->highlightPoint(MapIndex,anActor,highlight,update);
434         break;
435       case EdgeOfCellSelection:
436         m_RWInteractor->highlightEdge(MapIndex,anActor,highlight,update);
437         break;
438       case CellSelection:
439       case EdgeSelection:
440       case FaceSelection:
441       case VolumeSelection:
442         m_RWInteractor->highlightCell(MapIndex,anActor,highlight,update);
443         break;
444       }
445     }
446   }else{
447     m_RWInteractor->unHighlightSubSelection();
448   }
449 }
450
451 bool VTKViewer_ViewFrame::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
452 {
453   return m_RWInteractor->isInViewer( IObject );
454 }
455
456 bool VTKViewer_ViewFrame::isVisible( const Handle(SALOME_InteractiveObject)& IObject ) 
457 {
458   return m_RWInteractor->isVisible( IObject );
459 }
460
461 void VTKViewer_ViewFrame::setPopupServer( QAD_Application* App )
462 {
463   m_RW->setPopupServer( App );
464 }
465
466 /* selection */
467 Handle(SALOME_InteractiveObject) VTKViewer_ViewFrame::FindIObject(const char* theEntry)
468 {
469   using namespace SALOME::VTK;
470   SALOME_Actor* anActor = 
471     Find<SALOME_Actor>(getRenderer()->GetActors(),
472                        TIsSameEntry<SALOME_Actor>(theEntry));
473   if(anActor)
474     return anActor->getIO();
475
476   return Handle(SALOME_InteractiveObject)();
477 }
478
479 /* display */           
480 void VTKViewer_ViewFrame::Display(const Handle(SALOME_InteractiveObject)& theIObject, bool update)
481 {
482   QAD_Study*          aQADStudy = QAD_Application::getDesktop()->getActiveStudy();
483   SALOME_Selection*   aSel      = SALOME_Selection::Selection( aQADStudy->getSelection() );
484   SALOMEDS::Study_var aStudy    = aQADStudy->getStudyDocument();
485
486   m_RWInteractor->Display(theIObject,false);
487   ToolsGUI::SetVisibility( aStudy, theIObject->getEntry(), true, this );
488   aSel->AddIObject(theIObject,false);
489
490   if(update)
491     Repaint();
492 }
493
494
495 struct TDisplayAction{
496   SALOME_Selection* mySel;
497   Handle(SALOME_InteractiveObject) myIO;
498   TDisplayAction(SALOME_Selection* theSel,
499                  Handle(SALOME_InteractiveObject) theIO): 
500     mySel(theSel), myIO(theIO)
501   {}
502   void operator()(SALOME_Actor* theActor){
503     theActor->SetVisibility(true);
504     mySel->AddIObject(myIO,false);
505   }
506 };
507
508 void VTKViewer_ViewFrame::DisplayOnly(const Handle(SALOME_InteractiveObject)& theIObject)
509 {
510   QAD_Study* aStudy = QAD_Application::getDesktop()->getActiveStudy();
511   SALOME_Selection* aSel = SALOME_Selection::Selection(aStudy->getSelection());
512
513   aSel->ClearIObjects();
514   //m_RWInteractor->EraseAll();
515   EraseAll();
516
517   using namespace SALOME::VTK;
518   ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
519                           TIsSameIObject<SALOME_Actor>(theIObject),
520                           TDisplayAction(aSel,theIObject));
521
522   ToolsGUI::SetVisibility(
523     aStudy->getStudyDocument(), theIObject->getEntry(), true, this );
524
525   Repaint();
526 }
527
528
529 struct TEraseAction: TDisplayAction{
530   VTKViewer_RenderWindowInteractor* myRWInteractor;
531   TEraseAction(SALOME_Selection* theSel,
532                Handle(SALOME_InteractiveObject) theIO,
533                VTKViewer_RenderWindowInteractor* theRWInteractor): 
534     TDisplayAction(theSel,theIO),
535     myRWInteractor(theRWInteractor)
536   {}
537   void operator()(SALOME_Actor* theActor){
538     myRWInteractor->Erase(myIO,false);
539     mySel->RemoveIObject(myIO,false);
540   }
541 };
542
543 void VTKViewer_ViewFrame::Erase(const Handle(SALOME_InteractiveObject)& theIObject, bool update)
544 {
545   QAD_Study* aStudy = QAD_Application::getDesktop()->getActiveStudy();
546   SALOME_Selection* aSel = SALOME_Selection::Selection(aStudy->getSelection());
547
548   using namespace SALOME::VTK;
549   ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
550                           TIsSameIObject<SALOME_Actor>(theIObject),
551                           TEraseAction(aSel,theIObject,m_RWInteractor));
552
553   ToolsGUI::SetVisibility(
554     aStudy->getStudyDocument(), theIObject->getEntry(), false, this );
555
556   if(update)
557     Repaint();
558 }
559
560
561 void VTKViewer_ViewFrame::DisplayAll()
562 {
563   m_RWInteractor->DisplayAll();
564
565   // update flag of visibility
566   QAD_Study*               aQADStudy  = QAD_Application::getDesktop()->getActiveStudy();
567   SALOMEDS::Study_var      aStudy     = aQADStudy->getStudyDocument();
568   QAD_Desktop*             aDesktop   = QAD_Application::getDesktop();
569   const QString&           aCompName  = aDesktop->getComponentDataType();
570   SALOMEDS::SObject_var    aComponent =
571     SALOMEDS::SObject::_narrow( aStudy->FindComponent ( aCompName.latin1() ) );
572     
573   std::list<SALOMEDS::SObject_var> aList;
574   SALOMEDS_Tool::GetAllChildren( aStudy, aComponent, aList );
575
576   std::list<SALOMEDS::SObject_var>::iterator anIter = aList.begin();
577   for ( ; anIter != aList.end(); ++anIter )
578     ToolsGUI::SetVisibility( aStudy, (*anIter)->GetID(), true, this );
579 }
580
581
582 void VTKViewer_ViewFrame::EraseAll()
583 {
584   m_RWInteractor->EraseAll();
585
586   // update flag of visibility
587   QAD_Study*               aQADStudy  = QAD_Application::getDesktop()->getActiveStudy();
588   SALOMEDS::Study_var      aStudy     = aQADStudy->getStudyDocument();
589   QAD_Desktop*             aDesktop   = QAD_Application::getDesktop();
590   const QString&           aCompName  = aDesktop->getComponentDataType();
591   SALOMEDS::SObject_var    aComponent =
592     SALOMEDS::SObject::_narrow( aStudy->FindComponent ( aCompName.latin1() ) );
593
594   std::list<SALOMEDS::SObject_var> aList;
595   SALOMEDS_Tool::GetAllChildren( aStudy, aComponent, aList );
596
597   std::list<SALOMEDS::SObject_var>::iterator anIter = aList.begin();
598   for ( ; anIter != aList.end(); ++anIter )
599     ToolsGUI::SetVisibility( aStudy, (*anIter)->GetID(), false, this );
600 }
601
602
603 void VTKViewer_ViewFrame::Repaint(bool theUpdateTrihedron)
604 {
605   if (theUpdateTrihedron) onAdjustTrihedron();
606   m_RW->update();
607 }
608
609 void VTKViewer_ViewFrame::GetScale(double theScale[3]){
610   m_Transform->GetScale(theScale);
611 }
612
613 void VTKViewer_ViewFrame::SetScale(double theScale[3]){
614   m_Transform->SetScale(theScale[0], theScale[1], theScale[2]);
615   m_RWInteractor->Render();
616   Repaint();
617 }
618
619 void VTKViewer_ViewFrame::InsertActor( SALOME_Actor* theActor, bool theMoveInternalActors ){
620   theActor->AddToRender(m_Renderer);
621   theActor->SetTransform(m_Transform);
622   if(theMoveInternalActors) 
623     m_RWInteractor->MoveInternalActors();
624 }
625
626 void VTKViewer_ViewFrame::AddActor( SALOME_Actor* theActor, bool theUpdate /*=false*/ ){
627   InsertActor(theActor);
628   if(theUpdate) 
629     Repaint();
630 }
631
632 void VTKViewer_ViewFrame::RemoveActor( SALOME_Actor* theActor, bool theUpdate /*=false*/ ){
633   theActor->RemoveFromRender(m_Renderer);
634   if(theUpdate) 
635     Repaint();
636 }
637
638 void VTKViewer_ViewFrame::MoveActor(SALOME_Actor* theActor)
639 {
640   RemoveActor(theActor);
641   InsertActor(theActor,true);
642 }
643
644 //==========================================================
645 /*!
646  *  VTKViewer_ViewFrame::Display
647  *  Display presentation
648  */
649 //==========================================================
650 void VTKViewer_ViewFrame::Display( const SALOME_VTKPrs* prs )
651 {
652   // try do downcast object
653   const VTKViewer_Prs* aVTKPrs = dynamic_cast<const VTKViewer_Prs*>( prs );
654   if ( !aVTKPrs || aVTKPrs->IsNull() )
655     return;
656
657   vtkActorCollection* actors = aVTKPrs->GetObjects();
658   if ( !actors )
659     return;
660
661   actors->InitTraversal();
662   vtkActor* actor;
663   while( ( actor = actors->GetNextActor() ) )
664   {
665     SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor );
666     if ( salomeActor )                      
667     {
668       // just display the object
669       m_RWInteractor->Display( salomeActor, false );
670       
671       // Set visibility flag
672       Handle(SALOME_InteractiveObject) anObj = salomeActor->getIO();
673       if ( !anObj.IsNull() && anObj->hasEntry() )
674       {
675         SALOMEDS::Study_var aStudy =
676           QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
677         ToolsGUI::SetVisibility( aStudy, anObj->getEntry(), true, this );
678       }
679
680       if ( salomeActor->IsSetCamera() )
681         salomeActor->SetCamera( getRenderer()->GetActiveCamera() );
682     }
683   }
684 }
685
686 //==========================================================
687 /*!
688  *  VTKViewer_ViewFrame::Erase
689  *  Erase presentation
690  */
691 //==========================================================
692 void VTKViewer_ViewFrame::Erase( const SALOME_VTKPrs* prs, const bool forced )
693 {
694   // try do downcast object
695   const VTKViewer_Prs* aVTKPrs = dynamic_cast<const VTKViewer_Prs*>( prs );
696   if ( !aVTKPrs || aVTKPrs->IsNull() )
697     return;
698
699   vtkActorCollection* actors = aVTKPrs->GetObjects();
700   if ( !actors )
701     return;
702
703   SALOMEDS::Study_var aStudy =
704     QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
705
706   actors->InitTraversal();
707   vtkActor* actor;
708   while( ( actor = actors->GetNextActor() ) ) {
709     SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor );
710     if ( salomeActor ) {
711       // just erase the object
712       if ( forced )
713         m_RWInteractor->Remove( salomeActor, false );
714       else
715         m_RWInteractor->Erase( salomeActor, forced );
716
717       // Set visibility flag if necessary
718       if ( !forced )
719       {
720         Handle(SALOME_InteractiveObject) anObj = salomeActor->getIO();
721         if ( !anObj.IsNull() && anObj->hasEntry() )
722           ToolsGUI::SetVisibility( aStudy, anObj->getEntry(), true, this );
723       }
724     }
725   }
726 }
727   
728 //==========================================================
729 /*!
730  *  VTKViewer_ViewFrame::CreatePrs
731  *  Create presentation by entry
732  */
733 //==========================================================
734 SALOME_Prs* VTKViewer_ViewFrame::CreatePrs( const char* entry )
735 {
736   VTKViewer_Prs* prs = new VTKViewer_Prs();
737   if ( entry ) {
738     vtkActorCollection* theActors = m_Renderer->GetActors();
739     theActors->InitTraversal();
740     vtkActor* ac;
741     while( ( ac = theActors->GetNextActor() ) ) {
742       SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
743       if ( anActor && anActor->hasIO() && !strcmp( anActor->getIO()->getEntry(), entry ) ) {
744         prs->AddObject( ac );
745       }
746     }
747   }
748   return prs;
749 }
750
751 //==========================================================
752 /*!
753  *  VTKViewer_ViewFrame::BeforeDisplay
754  *  Axiluary method called before displaying of objects
755  */
756 //==========================================================
757 void  VTKViewer_ViewFrame::BeforeDisplay( SALOME_Displayer* d )
758 {
759   d->BeforeDisplay( this, SALOME_VTKViewType() );
760 }
761
762 //==========================================================
763 /*!
764  *  VTKViewer_ViewFrame::AfterDisplay
765  *  Axiluary method called after displaying of objects
766  */
767 //==========================================================
768 void  VTKViewer_ViewFrame::AfterDisplay( SALOME_Displayer* d )
769 {
770   d->AfterDisplay( this, SALOME_VTKViewType() );
771 }
772
773 //==========================================================
774 /*!
775  *  VTKViewer_ViewFrame::undo
776  *  Redisplay all objects in viewer
777  */
778 //==========================================================
779 void VTKViewer_ViewFrame::undo( QAD_Study* theQADStudy, const char* /*StudyFrameEntry*/ )
780 {
781   redisplayAll( theQADStudy, true );
782 }
783
784 //==========================================================
785 /*!
786  *  VTKViewer_ViewFrame::redo
787  *  Redisplay all objects in viewer
788  */
789 //==========================================================
790 void VTKViewer_ViewFrame::redo( QAD_Study* theQADStudy, const char* /*StudyFrameEntry*/ )
791 {
792   redisplayAll( theQADStudy, true );
793 }
794
795 //==========================================================
796 /*!
797  *  VTKViewer_ViewFrame::redisplayAll
798  *  Redisplay all objects in viewer
799  */
800 //==========================================================
801 void VTKViewer_ViewFrame::redisplayAll( QAD_Study* theQADStudy, const bool theToUpdate )
802 {
803   SALOMEDS::Study_var      aStudy     = theQADStudy->getStudyDocument();
804   SALOME_Selection*        aSel       = SALOME_Selection::Selection( theQADStudy->getSelection() );
805   QAD_Desktop*             aDesktop   = QAD_Application::getDesktop();
806   SALOMEGUI*               aGUI       = aDesktop->getActiveGUI();
807   const QString&           aCompName  = aDesktop->getComponentDataType();
808   SALOMEDS::SObject_var    aComponent =
809     SALOMEDS::SObject::_narrow( aStudy->FindComponent ( aCompName.latin1() ) );
810
811   if ( aComponent->_is_nil() )
812     return;
813
814   bool isTrhDisplayed = isTrihedronDisplayed();
815
816   m_RWInteractor->RemoveAll( false );
817   //m_RWInteractor->EraseAll();
818
819   aSel->ClearIObjects();
820   
821   if ( isTrhDisplayed )
822     m_Triedron->AddToRender( m_Renderer );
823
824   std::list<SALOMEDS::SObject_var> aList;
825   SALOMEDS_Tool::GetAllChildren( aStudy, aComponent, aList );
826
827   std::list<SALOMEDS::SObject_var>::iterator anIter = aList.begin();
828   for ( ; anIter != aList.end(); ++anIter )
829   {
830     SALOMEDS::SObject_var anObj = (*anIter);
831     if ( ToolsGUI::GetVisibility( aStudy, anObj, this ) )
832     {
833       Handle(SALOME_InteractiveObject) anIObj = new SALOME_InteractiveObject();
834       anIObj->setEntry( anObj->GetID() );
835       aGUI->BuildPresentation( anIObj, this );
836     }
837     
838   }
839
840   if ( theToUpdate )
841     Repaint();
842 }
843
844 #define INCREMENT_FOR_OP 10
845
846 //=======================================================================
847 // name    : onPanLeft
848 // Purpose : Performs incremental panning to the left
849 //=======================================================================
850 void VTKViewer_ViewFrame::onPanLeft()
851 {
852   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalPan( -INCREMENT_FOR_OP, 0 );
853 }
854
855 //=======================================================================
856 // name    : onPanRight
857 // Purpose : Performs incremental panning to the right
858 //=======================================================================
859 void VTKViewer_ViewFrame::onPanRight()
860 {
861   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalPan( INCREMENT_FOR_OP, 0 );
862 }
863
864 //=======================================================================
865 // name    : onPanUp
866 // Purpose : Performs incremental panning to the top
867 //=======================================================================
868 void VTKViewer_ViewFrame::onPanUp()
869 {
870   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalPan( 0, INCREMENT_FOR_OP );
871 }
872
873 //=======================================================================
874 // name    : onPanDown
875 // Purpose : Performs incremental panning to the bottom
876 //=======================================================================
877 void VTKViewer_ViewFrame::onPanDown()
878 {
879   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalPan( 0, -INCREMENT_FOR_OP );
880 }
881
882 //=======================================================================
883 // name    : onZoomIn
884 // Purpose : Performs incremental zooming in
885 //=======================================================================
886 void VTKViewer_ViewFrame::onZoomIn()
887 {
888   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalZoom( INCREMENT_FOR_OP );
889 }
890
891 //=======================================================================
892 // name    : onZoomOut
893 // Purpose : Performs incremental zooming out
894 //=======================================================================
895 void VTKViewer_ViewFrame::onZoomOut()
896 {
897   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalZoom( -INCREMENT_FOR_OP );
898 }
899
900 //=======================================================================
901 // name    : onRotateLeft
902 // Purpose : Performs incremental rotating to the left
903 //=======================================================================
904 void VTKViewer_ViewFrame::onRotateLeft()
905 {
906   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalRotate( -INCREMENT_FOR_OP, 0 );
907 }
908
909 //=======================================================================
910 // name    : onRotateRight
911 // Purpose : Performs incremental rotating to the right
912 //=======================================================================
913 void VTKViewer_ViewFrame::onRotateRight()
914 {
915   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalRotate( INCREMENT_FOR_OP, 0 );
916 }
917
918 //=======================================================================
919 // name    : onRotateUp
920 // Purpose : Performs incremental rotating to the top
921 //=======================================================================
922 void VTKViewer_ViewFrame::onRotateUp()
923 {
924   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalRotate( 0, -INCREMENT_FOR_OP );
925 }
926
927 //=======================================================================
928 // name    : onRotateDown
929 // Purpose : Performs incremental rotating to the bottom
930 //=======================================================================
931 void VTKViewer_ViewFrame::onRotateDown()
932 {
933   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalRotate( 0, INCREMENT_FOR_OP );
934 }
935
936
937
938
939
940
941
942
943
944
945
946
947
948