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