Salome HOME
SMH: 3.0.0 preparation - merged and adopted version (POLYWORK+HEAD)
[modules/visu.git] / src / VISU_I / VISU_ViewManager_i.cc
1 //  VISU OBJECT : interactive object for VISU entities implementation
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 //  File   : VISU_ViewManager_i.cc
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #include "VISU_ViewManager_i.hh"
28 #include "VISU_View_i.hh"
29 #include "VISU_Prs3d_i.hh"
30 #include "VISU_Table_i.hh"
31
32 #include "VISU_Actor.h"
33
34 #include "SUIT_Tools.h"
35 #include "SUIT_Session.h"
36 #include "SUIT_ViewWindow.h"
37 #include "SUIT_ViewManager.h"
38
39 #include "SVTK_RenderWindow.h"
40 #include "SVTK_ViewWindow.h"
41 #include "SVTK_ViewModel.h"
42 #include "SPlot2d_Curve.h"
43 #include "Plot2d_ViewFrame.h"
44 #include "Plot2d_ViewModel.h"
45
46 #include "SalomeApp_Study.h"
47 #include "SalomeApp_Application.h"
48
49 #include "SALOME_Event.hxx"
50
51 #include <vtkCamera.h>
52 #include <vtkRenderer.h>
53 #include <vtkRenderWindow.h>
54
55 using namespace std;
56
57 #ifdef _DEBUG_
58 static int MYDEBUG = 1;
59 #else
60 static int MYDEBUG = 0;
61 #endif
62
63 namespace VISU {
64
65   struct TNewViewManagerEvent: public SALOME_Event
66   {
67     std::string myStudyName;
68     typedef SalomeApp_Application* TResult;
69     TResult myResult;
70
71     TNewViewManagerEvent(const char* theStudyName):
72       myStudyName(theStudyName),
73       myResult(NULL)
74     {}
75
76     virtual
77     void
78     Execute()
79     {
80       SUIT_Session* aSession = SUIT_Session::session();
81       QPtrList<SUIT_Application> anApplications = aSession->applications();
82       QPtrListIterator<SUIT_Application> anIter(anApplications);
83       while(SUIT_Application* anApp = anIter.current()){
84         ++anIter;
85         if(SUIT_Study* aSStudy = anApp->activeStudy()){
86           if(SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(aSStudy)){
87             if(_PTR(Study) aCStudy = aStudy->studyDS()){
88               if(myStudyName == aCStudy->Name()){
89                 myResult = dynamic_cast<SalomeApp_Application*>(anApp);
90                 break;
91               }
92             }
93           }
94         }
95       }
96     }
97   };
98
99   //===========================================================================
100   ViewManager_i::ViewManager_i(SALOMEDS::Study_ptr theStudy)
101   {
102     if(MYDEBUG) MESSAGE("ViewManager_i::ViewManager_i - "<<this);
103
104     CORBA::String_var aStudyName = theStudy->Name();
105     myApplication = ProcessEvent(new TNewViewManagerEvent(aStudyName.in()));
106   }
107
108
109   ViewManager_i::~ViewManager_i()
110   {
111     if(MYDEBUG) MESSAGE("ViewManager_i::~ViewManager_i - "<<this);
112   }
113
114
115   struct TCurrentViewEvent: public SALOME_Event
116   {
117     const SalomeApp_Application* myApplication;
118     typedef VISU::View_ptr TResult;
119     TResult myResult;
120
121     TCurrentViewEvent(const SalomeApp_Application* theApplication):
122       myApplication(theApplication),
123       myResult(VISU::View::_nil())
124     {}
125
126     virtual
127     void
128     Execute()
129     {
130       //if (SUIT_ViewManager *aViewManager = myApplication->activeViewManager()) {
131       //  if (aViewManager->getType() == "VTKViewer") {
132       //    if (SUIT_ViewWindow *aViewWindow = aViewManager->getActiveView()) {
133             VISU::View3D_i* pView =
134               new View3D_i ((SalomeApp_Application*)myApplication);
135             if (pView->Create(0))
136               myResult = pView->_this();
137       //    }
138       //  }
139       //}
140     }
141   };
142
143   VISU::View_ptr ViewManager_i::GetCurrentView(){
144     return ProcessEvent(new TCurrentViewEvent(myApplication));
145   }
146
147
148   template<class TViewFrame>
149   struct TCreateViewFrameEvent: public SALOME_Event
150   {
151     //const SUIT_Application* myApplication;
152     SalomeApp_Application* myApplication;
153     typedef typename TViewFrame::TInterface TInterface;
154     typedef typename TInterface::_ptr_type TResult;
155     TResult myResult;
156
157     //TCreateViewFrameEvent (const SUIT_Application* theApplication):
158     TCreateViewFrameEvent (SalomeApp_Application* theApplication):
159       myApplication(theApplication),
160       myResult(TInterface::_nil())
161     {}
162
163     virtual
164     void
165     Execute()
166     {
167       //if (CheckStudy(myStudyDocument)){
168         TViewFrame* pView = new TViewFrame (myApplication);
169         if (pView->Create(1))
170           myResult = pView->_this();
171       //}
172     }
173   };
174
175
176   VISU::View3D_ptr ViewManager_i::Create3DView()
177   {
178     if (MYDEBUG) MESSAGE("ViewManager_i::Create3DView");
179     return ProcessEvent(new TCreateViewFrameEvent<View3D_i>(myApplication));
180   }
181
182   VISU::XYPlot_ptr ViewManager_i::CreateXYPlot()
183   {
184     if (MYDEBUG) MESSAGE("ViewManager_i::CreateXYPlot");
185     return ProcessEvent(new TCreateViewFrameEvent<XYPlot_i>(myApplication));
186   }
187
188   class TCreateViewEvent: public SALOME_Event
189   {
190   public:
191     TCreateViewEvent (SalomeApp_Application* theApplication)
192       : myApplication(theApplication)
193     {}
194   protected:
195     SalomeApp_Application* myApplication;
196   };
197
198   class TCreateTableViewFrameEvent: public TCreateViewEvent
199   {
200     Table_ptr myTable;
201   public:
202     TCreateTableViewFrameEvent (SalomeApp_Application* theApplication,
203                                 Table_ptr theTable):
204       TCreateViewEvent(theApplication),
205       myTable(theTable),
206       myResult(VISU::TableView::_nil())
207     {}
208
209     virtual void Execute()
210     {
211       //if (CheckStudy(myStudyDocument)) {
212         VISU::TableView_i* pView = new TableView_i (myApplication);
213         if (pView->Create(myTable) != NULL)
214           myResult = pView->_this();
215       //}
216     }
217     typedef VISU::TableView_ptr TResult;
218     TResult myResult;
219   };
220
221   VISU::TableView_ptr ViewManager_i::CreateTableView (VISU::Table_ptr theTable)
222   {
223     if (MYDEBUG) MESSAGE("ViewManager_i::CreateTableView");
224     //return ProcessEvent(new TCreateViewFrameEvent<TableView_i>(myApplication));
225     return ProcessEvent(new TCreateTableViewFrameEvent (myApplication, theTable));
226   }
227
228   void ViewManager_i::Destroy (View_ptr theView)
229   {
230     class TEvent: public SALOME_Event {
231       View_ptr myView;
232     public:
233       TEvent(View_ptr theView):
234         myView(theView)
235       {}
236       virtual void Execute(){
237         if (!CORBA::is_nil(myView)) {
238           if (VISU::View_i* pView = dynamic_cast<VISU::View_i*>(VISU::GetServant(myView).in())) {
239             pView->Close();
240             pView->_remove_ref();
241           }
242         }
243       }
244     };
245
246     if (MYDEBUG) MESSAGE("ViewManager_i::Destroy - " << theView->_is_nil());
247     ProcessVoidEvent(new TEvent(theView));
248   }
249
250   //===========================================================================
251   // VISU namespace functions
252   //===========================================================================
253   SVTK_ViewWindow* GetViewWindow (SUIT_ViewWindow* theStudyFrame)
254   {
255     return dynamic_cast<SVTK_ViewWindow*>(theStudyFrame);
256   }
257
258   vtkRenderer* GetRenderer (SUIT_ViewWindow* theStudyFrame)
259   {
260     return GetViewWindow(theStudyFrame)->getRenderer();
261   }
262
263   vtkCamera* GetCamera (SUIT_ViewWindow* theStudyFrame)
264   {
265     return GetRenderer(theStudyFrame)->GetActiveCamera();
266   }
267
268   void RepaintView (SUIT_ViewWindow* theViewWindow)
269   {
270     SVTK_ViewWindow* vf = GetViewWindow(theViewWindow);
271     vf->getRenderer()->ResetCameraClippingRange();
272     vf->getRenderWindow()->getRenderWindow()->Render();
273   }
274
275   VISU_Actor* UpdateViewer (SUIT_ViewWindow* theStudyFrame, int theDisplaing, Prs3d_i* thePrs)
276   {
277     SVTK_ViewWindow* vf = GetViewWindow(theStudyFrame);
278     if (!vf) return NULL;
279     if(MYDEBUG) MESSAGE("UpdateViewer - theDisplaing = "<<theDisplaing<<"; thePrs = "<<thePrs);
280     vtkRenderer *aRen = vf->getRenderer();
281     vtkActorCollection *anActColl = aRen->GetActors();
282     vtkActor *anActor;
283     VISU_Actor *anVISUActor = NULL, *aResActor = NULL;
284     for(anActColl->InitTraversal(); (anActor = anActColl->GetNextActor()) != NULL;){
285       if(anActor->IsA("VISU_Actor")){
286         anVISUActor = VISU_Actor::SafeDownCast(anActor);
287         if (thePrs == anVISUActor->GetPrs3d()) {
288           aResActor = anVISUActor->GetParent();
289           if(theDisplaing < eErase)
290             aResActor->VisibilityOn();
291           else
292             aResActor->VisibilityOff();
293         } else {
294           if(theDisplaing > eDisplay)
295             anVISUActor->VisibilityOff();
296           else
297             anVISUActor->VisibilityOn();
298         }
299       }
300     }
301     if (aResActor) {
302       RepaintView(theStudyFrame);
303       return aResActor;
304     }
305     if(thePrs != NULL && theDisplaing < eErase){
306       try{
307         anVISUActor = thePrs->CreateActor();
308         vf->AddActor(anVISUActor);
309       }catch(std::exception& exc){
310         if(MYDEBUG) INFOS(exc.what());
311         return NULL;
312       }catch(...){
313         if(MYDEBUG) INFOS("Unknown exception was occured!!!");
314         return NULL;
315       }
316     }
317     RepaintView(theStudyFrame);
318     return anVISUActor;
319   }
320
321   void UpdatePlot2d (Plot2d_ViewFrame *theView,int theDisplaying, Curve_i* theCurve)
322   {
323     if(MYDEBUG) MESSAGE("UpdatePlot2d - theDisplaying = " << theDisplaying);
324     if (!theView)
325       return;
326     QList<Plot2d_Curve> clist;
327     theView->getCurves(clist);
328     if (theDisplaying == eEraseAll) {
329       for (int i = 0; i < clist.count(); i++) {
330         if(MYDEBUG) MESSAGE("UpdatePlot2d - erasing all : curve - " << clist.at(i));
331         theView->eraseCurve(clist.at(i));
332       }
333     } else if (theDisplaying == eErase) {
334       if (theCurve) {
335         for (int i = 0; i < clist.count(); i++) {
336           SPlot2d_Curve* aSPlot2dC = dynamic_cast<SPlot2d_Curve*>(clist.at(i));
337           if (aSPlot2dC->hasIO() &&
338               !strcmp(aSPlot2dC->getIO()->getEntry(), theCurve->GetEntry())) {
339             if(MYDEBUG) MESSAGE("UpdatePlot2d - erasing : curve - " << aSPlot2dC);
340             theView->eraseCurve(aSPlot2dC);
341           }
342         }
343       }
344     } else if (theDisplaying == eDisplay) {
345       if (theCurve) {
346         bool bFound = false;
347         for (int i = 0; i < clist.count(); i++) {
348           SPlot2d_Curve* aSPlot2dC = dynamic_cast<SPlot2d_Curve*>(clist.at(i));
349           if (aSPlot2dC->hasIO() &&
350               !strcmp(aSPlot2dC->getIO()->getEntry(), theCurve->GetEntry())) {
351             if(MYDEBUG) MESSAGE("UpdatePlot2d - displaying : curve - " << aSPlot2dC);
352             aSPlot2dC->setHorTitle( theCurve->GetHorTitle().c_str() );
353             aSPlot2dC->setVerTitle( theCurve->GetVerTitle().c_str() );
354             aSPlot2dC->setHorUnits( theCurve->GetHorUnits().c_str() );
355             aSPlot2dC->setVerUnits( theCurve->GetVerUnits().c_str() );
356             double* xList = 0;
357             double* yList = 0;
358             int     nbPoints = theCurve->GetData( xList, yList );
359             if (nbPoints > 0 && xList && yList) {
360               aSPlot2dC->setData( xList, yList, nbPoints );
361             }
362             if (!theCurve->IsAuto()) {
363               aSPlot2dC->setLine((Plot2d_Curve::LineType)theCurve->GetLine(),
364                                  theCurve->GetLineWidth());
365               aSPlot2dC->setMarker((Plot2d_Curve::MarkerType)theCurve->GetMarker());
366               SALOMEDS::Color color = theCurve->GetColor();
367               aSPlot2dC->setColor(QColor((int)(color.R*255.),
368                                          (int)(color.G*255.),
369                                          (int)(color.B*255.)));
370             }
371             aSPlot2dC->setAutoAssign(theCurve->IsAuto());
372             theView->displayCurve(aSPlot2dC);
373             bFound = true;
374           }
375         }
376         if (!bFound) {
377           Plot2d_Curve* crv = theCurve->CreatePresentation();
378           if(MYDEBUG) MESSAGE("UpdatePlot2d - displaying : curve (new) - "<<crv );
379           if (crv) {
380             theView->displayCurve( crv );
381             theCurve->SetLine( (VISU::Curve::LineType)crv->getLine(), crv->getLineWidth() );
382             theCurve->SetMarker( (VISU::Curve::MarkerType)crv->getMarker());
383             SALOMEDS::Color newColor;
384             newColor.R = crv->getColor().red()/255.;
385             newColor.G = crv->getColor().green()/255.;
386             newColor.B = crv->getColor().blue()/255.;
387             theCurve->SetColor( newColor );
388             crv->setAutoAssign( theCurve->IsAuto() );
389           }
390         }
391       }
392     } else if (theDisplaying == eDisplayOnly) {
393       if (theCurve) {
394         bool bFound = false;
395         for (int i = 0; i < clist.count(); i++) {
396           SPlot2d_Curve* aSPlot2dC = dynamic_cast<SPlot2d_Curve*>(clist.at(i));
397           if (aSPlot2dC->hasIO() &&
398               !strcmp(aSPlot2dC->getIO()->getEntry(), theCurve->GetEntry())) {
399             if(MYDEBUG) MESSAGE("UpdatePlot2d - displaying only : curve - " << aSPlot2dC);
400             aSPlot2dC->setHorTitle( theCurve->GetHorTitle().c_str() );
401             aSPlot2dC->setVerTitle( theCurve->GetVerTitle().c_str() );
402             aSPlot2dC->setHorUnits( theCurve->GetHorUnits().c_str() );
403             aSPlot2dC->setVerUnits( theCurve->GetVerUnits().c_str() );
404             double* xList = 0;
405             double* yList = 0;
406             int     nbPoints = theCurve->GetData( xList, yList );
407             if ( nbPoints > 0 && xList && yList ) {
408               aSPlot2dC->setData( xList, yList, nbPoints );
409             }
410             if ( !theCurve->IsAuto() ) {
411               aSPlot2dC->setLine((Plot2d_Curve::LineType)theCurve->GetLine(), theCurve->GetLineWidth());
412               aSPlot2dC->setMarker((Plot2d_Curve::MarkerType)theCurve->GetMarker());
413               SALOMEDS::Color color = theCurve->GetColor();
414               aSPlot2dC->setColor(QColor((int)(color.R*255.), (int)(color.G*255.), (int)(color.B*255.)));
415             }
416             aSPlot2dC->setAutoAssign(theCurve->IsAuto());
417             theView->displayCurve(aSPlot2dC);
418             bFound = true;
419           } else {
420             theView->eraseCurve(aSPlot2dC);
421           }
422         }
423         if (!bFound) {
424           Plot2d_Curve* crv = theCurve->CreatePresentation();
425           if(MYDEBUG) MESSAGE("UpdatePlot2d - displaying only : curve (new) - " << crv);
426           if (crv) {
427             theView->displayCurve(crv);
428             theCurve->SetLine((VISU::Curve::LineType)crv->getLine(), crv->getLineWidth());
429             theCurve->SetMarker((VISU::Curve::MarkerType)crv->getMarker());
430             SALOMEDS::Color newColor;
431             newColor.R = crv->getColor().red()/255.;
432             newColor.G = crv->getColor().green()/255.;
433             newColor.B = crv->getColor().blue()/255.;
434             theCurve->SetColor(newColor);
435             crv->setAutoAssign(theCurve->IsAuto());
436           }
437         }
438       }
439     }
440   }
441
442   VISU_Actor* GetActor (VISU::Prs3d_i* thePrs3d, SVTK_ViewWindow* theVTKFrame)
443   {
444     vtkActorCollection *anActColl = theVTKFrame->getRenderer()->GetActors();
445     anActColl->InitTraversal();
446     while (vtkActor *anActor = anActColl->GetNextActor())
447       if (VISU_Actor* anVISUActor = dynamic_cast<VISU_Actor*>(anActor))
448         if (thePrs3d == anVISUActor->GetPrs3d())
449           return anVISUActor->GetParent();
450     return NULL;
451   }
452 }