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