1 // VISU OBJECT : interactive object for VISU entities implementation
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
23 // File : VISU_ViewManager_i.cc
24 // Author : Alexey PETROV
27 #include "VISU_ViewManager_i.hh"
28 #include "VISU_View_i.hh"
29 #include "VISU_Prs3d_i.hh"
30 #include "VISU_Table_i.hh"
32 #include "VISU_Actor.h"
34 #include "SUIT_Tools.h"
35 #include "SUIT_Session.h"
36 #include "SUIT_ViewWindow.h"
37 #include "SUIT_ViewManager.h"
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"
46 #include "SalomeApp_Study.h"
47 #include "SalomeApp_Application.h"
49 #include "SALOME_Event.hxx"
51 #include <vtkCamera.h>
52 #include <vtkRenderer.h>
53 #include <vtkRenderWindow.h>
58 static int MYDEBUG = 1;
60 static int MYDEBUG = 0;
65 struct TNewViewManagerEvent: public SALOME_Event
67 std::string myStudyName;
68 typedef SalomeApp_Application* TResult;
71 TNewViewManagerEvent(const char* theStudyName):
72 myStudyName(theStudyName),
80 MESSAGE("Find application for study : " << myStudyName);
81 QString aStudyName (myStudyName);
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()) {
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);
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
111 //===========================================================================
112 ViewManager_i::ViewManager_i(SALOMEDS::Study_ptr theStudy)
114 if(MYDEBUG) MESSAGE("ViewManager_i::ViewManager_i - "<<this);
116 CORBA::String_var aStudyName = theStudy->Name();
117 myApplication = ProcessEvent(new TNewViewManagerEvent(aStudyName.in()));
121 ViewManager_i::~ViewManager_i()
123 if(MYDEBUG) MESSAGE("ViewManager_i::~ViewManager_i - "<<this);
127 struct TCurrentViewEvent: public SALOME_Event
129 const SalomeApp_Application* myApplication;
130 typedef VISU::View_ptr TResult;
133 TCurrentViewEvent(const SalomeApp_Application* theApplication):
134 myApplication(theApplication),
135 myResult(VISU::View::_nil())
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();
155 VISU::View_ptr ViewManager_i::GetCurrentView(){
156 return ProcessEvent(new TCurrentViewEvent(myApplication));
160 template<class TViewFrame>
161 struct TCreateViewFrameEvent: public SALOME_Event
163 SalomeApp_Application* myApplication;
164 typedef typename TViewFrame::TInterface TInterface;
165 typedef typename TInterface::_ptr_type TResult;
168 TCreateViewFrameEvent (SalomeApp_Application* theApplication):
169 myApplication(theApplication),
170 myResult(TInterface::_nil())
177 //if (CheckStudy(myStudyDocument)) {
179 TViewFrame* pView = new TViewFrame (myApplication);
180 if (pView->Create(1))
181 myResult = pView->_this();
187 VISU::View3D_ptr ViewManager_i::Create3DView()
189 if (MYDEBUG) MESSAGE("ViewManager_i::Create3DView");
190 return ProcessEvent(new TCreateViewFrameEvent<View3D_i>(myApplication));
193 VISU::XYPlot_ptr ViewManager_i::CreateXYPlot()
195 if (MYDEBUG) MESSAGE("ViewManager_i::CreateXYPlot");
196 return ProcessEvent(new TCreateViewFrameEvent<XYPlot_i>(myApplication));
199 class TCreateViewEvent: public SALOME_Event
202 TCreateViewEvent (SalomeApp_Application* theApplication)
203 : myApplication(theApplication)
206 SalomeApp_Application* myApplication;
209 class TCreateTableViewFrameEvent: public TCreateViewEvent
213 TCreateTableViewFrameEvent (SalomeApp_Application* theApplication,
215 TCreateViewEvent(theApplication),
217 myResult(VISU::TableView::_nil())
220 virtual void Execute()
222 //if (CheckStudy(myStudyDocument)) {
223 VISU::TableView_i* pView = new TableView_i (myApplication);
224 if (pView->Create(myTable) != NULL)
225 myResult = pView->_this();
228 typedef VISU::TableView_ptr TResult;
232 VISU::TableView_ptr ViewManager_i::CreateTableView (VISU::Table_ptr theTable)
234 if (MYDEBUG) MESSAGE("ViewManager_i::CreateTableView");
235 //return ProcessEvent(new TCreateViewFrameEvent<TableView_i>(myApplication));
236 return ProcessEvent(new TCreateTableViewFrameEvent (myApplication, theTable));
239 void ViewManager_i::Destroy (View_ptr theView)
241 class TEvent: public SALOME_Event {
244 TEvent(View_ptr theView):
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())) {
251 pView->_remove_ref();
257 if (MYDEBUG) MESSAGE("ViewManager_i::Destroy - " << theView->_is_nil());
258 ProcessVoidEvent(new TEvent(theView));
261 //===========================================================================
262 // VISU namespace functions
263 //===========================================================================
264 SVTK_ViewWindow* GetViewWindow (SUIT_ViewWindow* theStudyFrame)
266 return dynamic_cast<SVTK_ViewWindow*>(theStudyFrame);
269 vtkRenderer* GetRenderer (SUIT_ViewWindow* theStudyFrame)
271 return GetViewWindow(theStudyFrame)->getRenderer();
274 vtkCamera* GetCamera (SUIT_ViewWindow* theStudyFrame)
276 return GetRenderer(theStudyFrame)->GetActiveCamera();
279 void RepaintView (SUIT_ViewWindow* theViewWindow)
281 SVTK_ViewWindow* vf = GetViewWindow(theViewWindow);
282 vf->getRenderer()->ResetCameraClippingRange();
283 vf->getRenderWindow()->getRenderWindow()->Render();
286 VISU_Actor* UpdateViewer (SUIT_ViewWindow* theStudyFrame, int theDisplaing, Prs3d_i* thePrs)
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();
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();
303 aResActor->VisibilityOff();
305 if(theDisplaing > eDisplay)
306 anVISUActor->VisibilityOff();
308 anVISUActor->VisibilityOn();
313 RepaintView(theStudyFrame);
316 if(thePrs != NULL && theDisplaing < eErase){
318 anVISUActor = thePrs->CreateActor();
319 vf->AddActor(anVISUActor);
320 }catch(std::exception& exc){
321 if(MYDEBUG) INFOS(exc.what());
324 if(MYDEBUG) INFOS("Unknown exception was occured!!!");
328 RepaintView(theStudyFrame);
332 void UpdatePlot2d (Plot2d_ViewFrame *theView,int theDisplaying, Curve_i* theCurve)
334 if(MYDEBUG) MESSAGE("UpdatePlot2d - theDisplaying = " << theDisplaying);
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));
344 } else if (theDisplaying == eErase) {
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);
355 } else if (theDisplaying == eDisplay) {
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() );
369 int nbPoints = theCurve->GetData( xList, yList );
370 if (nbPoints > 0 && xList && yList) {
371 aSPlot2dC->setData( xList, yList, nbPoints );
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.),
380 (int)(color.B*255.)));
382 aSPlot2dC->setAutoAssign(theCurve->IsAuto());
383 theView->displayCurve(aSPlot2dC);
388 Plot2d_Curve* crv = theCurve->CreatePresentation();
389 if(MYDEBUG) MESSAGE("UpdatePlot2d - displaying : curve (new) - "<<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() );
403 } else if (theDisplaying == eDisplayOnly) {
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() );
417 int nbPoints = theCurve->GetData( xList, yList );
418 if ( nbPoints > 0 && xList && yList ) {
419 aSPlot2dC->setData( xList, yList, nbPoints );
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.)));
427 aSPlot2dC->setAutoAssign(theCurve->IsAuto());
428 theView->displayCurve(aSPlot2dC);
431 theView->eraseCurve(aSPlot2dC);
435 Plot2d_Curve* crv = theCurve->CreatePresentation();
436 if(MYDEBUG) MESSAGE("UpdatePlot2d - displaying only : curve (new) - " << 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());
453 VISU_Actor* GetActor (VISU::Prs3d_i* thePrs3d, SVTK_ViewWindow* theVTKFrame)
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();