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 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()){
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);
99 //===========================================================================
100 ViewManager_i::ViewManager_i(SALOMEDS::Study_ptr theStudy)
102 if(MYDEBUG) MESSAGE("ViewManager_i::ViewManager_i - "<<this);
104 CORBA::String_var aStudyName = theStudy->Name();
105 myApplication = ProcessEvent(new TNewViewManagerEvent(aStudyName.in()));
109 ViewManager_i::~ViewManager_i()
111 if(MYDEBUG) MESSAGE("ViewManager_i::~ViewManager_i - "<<this);
115 struct TCurrentViewEvent: public SALOME_Event
117 const SalomeApp_Application* myApplication;
118 typedef VISU::View_ptr TResult;
121 TCurrentViewEvent(const SalomeApp_Application* theApplication):
122 myApplication(theApplication),
123 myResult(VISU::View::_nil())
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();
143 VISU::View_ptr ViewManager_i::GetCurrentView(){
144 return ProcessEvent(new TCurrentViewEvent(myApplication));
148 template<class TViewFrame>
149 struct TCreateViewFrameEvent: public SALOME_Event
151 //const SUIT_Application* myApplication;
152 SalomeApp_Application* myApplication;
153 typedef typename TViewFrame::TInterface TInterface;
154 typedef typename TInterface::_ptr_type TResult;
157 //TCreateViewFrameEvent (const SUIT_Application* theApplication):
158 TCreateViewFrameEvent (SalomeApp_Application* theApplication):
159 myApplication(theApplication),
160 myResult(TInterface::_nil())
167 //if (CheckStudy(myStudyDocument)){
168 TViewFrame* pView = new TViewFrame (myApplication);
169 if (pView->Create(1))
170 myResult = pView->_this();
176 VISU::View3D_ptr ViewManager_i::Create3DView()
178 if (MYDEBUG) MESSAGE("ViewManager_i::Create3DView");
179 return ProcessEvent(new TCreateViewFrameEvent<View3D_i>(myApplication));
182 VISU::XYPlot_ptr ViewManager_i::CreateXYPlot()
184 if (MYDEBUG) MESSAGE("ViewManager_i::CreateXYPlot");
185 return ProcessEvent(new TCreateViewFrameEvent<XYPlot_i>(myApplication));
188 class TCreateViewEvent: public SALOME_Event
191 TCreateViewEvent (SalomeApp_Application* theApplication)
192 : myApplication(theApplication)
195 SalomeApp_Application* myApplication;
198 class TCreateTableViewFrameEvent: public TCreateViewEvent
202 TCreateTableViewFrameEvent (SalomeApp_Application* theApplication,
204 TCreateViewEvent(theApplication),
206 myResult(VISU::TableView::_nil())
209 virtual void Execute()
211 //if (CheckStudy(myStudyDocument)) {
212 VISU::TableView_i* pView = new TableView_i (myApplication);
213 if (pView->Create(myTable) != NULL)
214 myResult = pView->_this();
217 typedef VISU::TableView_ptr TResult;
221 VISU::TableView_ptr ViewManager_i::CreateTableView (VISU::Table_ptr theTable)
223 if (MYDEBUG) MESSAGE("ViewManager_i::CreateTableView");
224 //return ProcessEvent(new TCreateViewFrameEvent<TableView_i>(myApplication));
225 return ProcessEvent(new TCreateTableViewFrameEvent (myApplication, theTable));
228 void ViewManager_i::Destroy (View_ptr theView)
230 class TEvent: public SALOME_Event {
233 TEvent(View_ptr theView):
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())) {
240 pView->_remove_ref();
246 if (MYDEBUG) MESSAGE("ViewManager_i::Destroy - " << theView->_is_nil());
247 ProcessVoidEvent(new TEvent(theView));
250 //===========================================================================
251 // VISU namespace functions
252 //===========================================================================
253 SVTK_ViewWindow* GetViewWindow (SUIT_ViewWindow* theStudyFrame)
255 return dynamic_cast<SVTK_ViewWindow*>(theStudyFrame);
258 vtkRenderer* GetRenderer (SUIT_ViewWindow* theStudyFrame)
260 return GetViewWindow(theStudyFrame)->getRenderer();
263 vtkCamera* GetCamera (SUIT_ViewWindow* theStudyFrame)
265 return GetRenderer(theStudyFrame)->GetActiveCamera();
268 void RepaintView (SUIT_ViewWindow* theViewWindow)
270 SVTK_ViewWindow* vf = GetViewWindow(theViewWindow);
271 vf->getRenderer()->ResetCameraClippingRange();
272 vf->getRenderWindow()->getRenderWindow()->Render();
275 VISU_Actor* UpdateViewer (SUIT_ViewWindow* theStudyFrame, int theDisplaing, Prs3d_i* thePrs)
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();
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();
292 aResActor->VisibilityOff();
294 if(theDisplaing > eDisplay)
295 anVISUActor->VisibilityOff();
297 anVISUActor->VisibilityOn();
302 RepaintView(theStudyFrame);
305 if(thePrs != NULL && theDisplaing < eErase){
307 anVISUActor = thePrs->CreateActor();
308 vf->AddActor(anVISUActor);
309 }catch(std::exception& exc){
310 if(MYDEBUG) INFOS(exc.what());
313 if(MYDEBUG) INFOS("Unknown exception was occured!!!");
317 RepaintView(theStudyFrame);
321 void UpdatePlot2d (Plot2d_ViewFrame *theView,int theDisplaying, Curve_i* theCurve)
323 if(MYDEBUG) MESSAGE("UpdatePlot2d - theDisplaying = " << theDisplaying);
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));
333 } else if (theDisplaying == eErase) {
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);
344 } else if (theDisplaying == eDisplay) {
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() );
358 int nbPoints = theCurve->GetData( xList, yList );
359 if (nbPoints > 0 && xList && yList) {
360 aSPlot2dC->setData( xList, yList, nbPoints );
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.),
369 (int)(color.B*255.)));
371 aSPlot2dC->setAutoAssign(theCurve->IsAuto());
372 theView->displayCurve(aSPlot2dC);
377 Plot2d_Curve* crv = theCurve->CreatePresentation();
378 if(MYDEBUG) MESSAGE("UpdatePlot2d - displaying : curve (new) - "<<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() );
392 } else if (theDisplaying == eDisplayOnly) {
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() );
406 int nbPoints = theCurve->GetData( xList, yList );
407 if ( nbPoints > 0 && xList && yList ) {
408 aSPlot2dC->setData( xList, yList, nbPoints );
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.)));
416 aSPlot2dC->setAutoAssign(theCurve->IsAuto());
417 theView->displayCurve(aSPlot2dC);
420 theView->eraseCurve(aSPlot2dC);
424 Plot2d_Curve* crv = theCurve->CreatePresentation();
425 if(MYDEBUG) MESSAGE("UpdatePlot2d - displaying only : curve (new) - " << 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());
442 VISU_Actor* GetActor (VISU::Prs3d_i* thePrs3d, SVTK_ViewWindow* theVTKFrame)
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();