]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_Tools.cxx
Salome HOME
0021206: CEA] example script fails
[modules/visu.git] / src / VISU_I / VISU_Tools.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  File   : VISU_Tools.cxx
21 //  Author : Oleg UVAROV
22 //  Module : VISU
23 //
24 #include "VISU_Tools.h"
25
26 #include "VISU_Gen_i.hh"
27 #include "VISU_Table_i.hh"
28 #include "VISU_ViewManager_i.hh"
29
30 #include <VISU_ActorBase.h>
31
32 #include <LightApp_Displayer.h>
33
34 #include <SalomeApp_Study.h>
35 #include <SalomeApp_Application.h>
36 #include <SalomeApp_Module.h>
37
38 #include <SPlot2d_ViewModel.h>
39 #include <Plot2d_ViewFrame.h>
40 #include <Plot2d_ViewManager.h>
41
42 #include <SUIT_ResourceMgr.h>
43 #include <SUIT_Session.h>
44
45 //=============================================================================
46 namespace VISU
47 {
48   //------------------------------------------------------------
49   // Internal function used by several public functions below
50   void
51   UpdateCurve(VISU::Curve_i* theCurve,
52               Plot2d_ViewFrame* thePlot,
53               SPlot2d_Curve* plotCurve,
54               int theDisplaying)
55   {
56     if ( theDisplaying == VISU::eErase ) {
57       if ( plotCurve && thePlot )
58         thePlot->eraseCurve( plotCurve, false );
59     }
60     else if ( theDisplaying == VISU::eDisplay || theDisplaying == VISU::eDisplayOnly ) {
61       if ( plotCurve ) {
62         plotCurve->setHorTitle( theCurve->GetHorTitle().c_str() );
63         //plotCurve->setVerTitle( ( theCurve->GetVerTitle().c_str() ) );
64         plotCurve->setVerTitle( theCurve->GetName().c_str() );
65         plotCurve->setHorUnits( theCurve->GetHorUnits().c_str() );
66         plotCurve->setVerUnits( theCurve->GetVerUnits().c_str() );
67         double* xList = 0;
68         double* yList = 0;
69         QStringList zList;
70         int     nbPoints = theCurve->GetData( xList, yList, zList );
71         if ( nbPoints > 0 && xList && yList ) {
72           plotCurve->setData( xList, yList, nbPoints, zList );
73         }
74         if ( !theCurve->IsAuto() ) {
75           plotCurve->setLine( (Plot2d::LineType)theCurve->GetLine(), theCurve->GetLineWidth() );
76           plotCurve->setMarker( (Plot2d::MarkerType)theCurve->GetMarker() );
77           SALOMEDS::Color color = theCurve->GetColor();
78           plotCurve->setColor( QColor( (int)(color.R*255.), (int)(color.G*255.), (int)(color.B*255.) ) );
79         }
80         plotCurve->setAutoAssign( theCurve->IsAuto() );
81         if( thePlot )
82           thePlot->displayCurve( plotCurve, false );
83       }
84       else {
85         Plot2d_Curve* crv = theCurve->CreatePresentation();
86         if ( crv ) {
87           if( thePlot )
88             thePlot->displayCurve( crv, false );
89           theCurve->SetLine( (VISU::Curve::LineType)crv->getLine(), crv->getLineWidth() );
90           theCurve->SetMarker( (VISU::Curve::MarkerType)crv->getMarker());
91           SALOMEDS::Color newColor;
92           newColor.R = crv->getColor().red()/255.;
93           newColor.G = crv->getColor().green()/255.;
94           newColor.B = crv->getColor().blue()/255.;
95           theCurve->SetColor( newColor );
96           crv->setAutoAssign( theCurve->IsAuto() );
97         }
98       }
99     }
100   }
101
102   //------------------------------------------------------------
103   void
104   PlotTable(SalomeApp_Study* theStudy,
105             Plot2d_ViewFrame* thePlot,
106             VISU::Table_i* table,
107             int theDisplaying)
108   {
109     if ( !thePlot )
110       return;
111
112     if ( theDisplaying == VISU::eDisplayOnly )
113       thePlot->EraseAll();
114     QList<Plot2d_Curve*> clist;
115     thePlot->getCurves( clist );
116     _PTR(Study) aStudy = theStudy->studyDS();
117     _PTR(SObject) TableSO = aStudy->FindObjectID( table->GetEntry() );
118     if ( TableSO ) {
119       _PTR(ChildIterator) Iter = aStudy->NewChildIterator( TableSO );
120       for ( ; Iter->More(); Iter->Next() ) {
121         CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
122         if( !CORBA::is_nil( childObject ) ) {
123           CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
124           if( !CORBA::is_nil( aCurve ) ) {
125             VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
126             SPlot2d_Curve* plotCurve = 0;
127             SPlot2d_Curve* tmpCurve;
128             for ( int i = 0; i < clist.count(); i++ ) {
129               tmpCurve = dynamic_cast<SPlot2d_Curve*>( clist.at( i ) );
130               if (tmpCurve && tmpCurve->hasIO() &&
131                   theCurve->GetEntry() == tmpCurve->getIO()->getEntry()) {
132                 plotCurve = tmpCurve;
133                 break;
134               }
135             }
136
137             UpdateCurve( theCurve, thePlot, plotCurve, theDisplaying );
138             CurveVisibilityChanged(theCurve, theDisplaying, true, false, true);
139
140             if ( theDisplaying == VISU::eErase && plotCurve ) {
141               clist.removeAll(plotCurve );
142             }
143           }
144         }
145       }
146       thePlot->Repaint();
147       SetVisibilityState(table->GetEntry(),GetStateByDisplaying(theDisplaying));
148     }
149     
150   }
151
152   //------------------------------------------------------------
153   void
154   PlotCurve(Plot2d_ViewFrame* thePlot,
155             VISU::Curve_i* theCurve,
156             int theDisplaying)
157   {
158     if ( !thePlot )
159       return;
160
161 //  if ( theDisplaying == VISU::eDisplayOnly )
162 //    thePlot->EraseAll();
163     QList<Plot2d_Curve*> clist;
164     thePlot->getCurves( clist );
165     SPlot2d_Curve* plotCurve = 0;
166     SPlot2d_Curve* tmpCurve;
167     if(theDisplaying == VISU::eErase) {
168       // 23.06.2008 skl for IPAL17672
169       for (int i = 0; i < clist.count(); i++) {
170         tmpCurve = dynamic_cast<SPlot2d_Curve*>(clist.at(i));
171         if (tmpCurve && tmpCurve->hasIO() &&
172             theCurve->GetEntry() == tmpCurve->getIO()->getEntry()) {
173           plotCurve = tmpCurve;
174           thePlot->eraseCurve(clist.at(i));
175           break;
176         }
177       }
178       UpdateCurve(theCurve, thePlot, plotCurve, theDisplaying);
179     }
180     else {
181       for (int i = 0; i < clist.count(); i++) {
182         tmpCurve = dynamic_cast<SPlot2d_Curve*>(clist.at(i));
183         if (tmpCurve && tmpCurve->hasIO() &&
184             theCurve->GetEntry() == tmpCurve->getIO()->getEntry()) {
185           plotCurve = tmpCurve;
186         }
187         else if (theDisplaying == VISU::eDisplayOnly) {
188           thePlot->eraseCurve(clist.at(i));
189         }
190       }
191       UpdateCurve(theCurve, thePlot, plotCurve, theDisplaying);
192     }
193
194     thePlot->Repaint();
195
196     SetVisibilityState(theCurve->GetEntry(),GetStateByDisplaying(theDisplaying));
197     CurveVisibilityChanged(theCurve,theDisplaying,false, true, true);
198   }
199
200   //------------------------------------------------------------
201   void
202   PlotRemoveCurve(SalomeApp_Application* theApp,
203                   VISU::Curve_i* pCrv)
204   {
205     QString anEntry = pCrv->GetEntry().c_str();
206     ViewManagerList pvm_list;
207     theApp->viewManagers( SPlot2d_Viewer::Type(), pvm_list );
208     ViewManagerList::Iterator pvm_it = pvm_list.begin();
209     for( ; pvm_it != pvm_list.end(); pvm_it++ ){
210       Plot2d_ViewManager* pvm = dynamic_cast<Plot2d_ViewManager*>( *pvm_it );
211       if( pvm ){
212         SPlot2d_Viewer* aSPlot2d = dynamic_cast<SPlot2d_Viewer*>( pvm->getViewModel() );
213         if( aSPlot2d ){
214           Plot2d_ViewFrame* thePlot = aSPlot2d->getActiveViewFrame();
215           if(thePlot){
216             QList<Plot2d_Curve*> clist;
217             thePlot->getCurves( clist );
218             for (int i = 0; i < clist.count(); i++) {
219               if(SPlot2d_Curve* plotCurve = dynamic_cast<SPlot2d_Curve*>(clist[i]))
220                 if(plotCurve->hasIO() && (plotCurve->getIO()->getEntry() == anEntry))
221                   thePlot->eraseCurve(clist[i]);
222             }
223           }
224         }
225       }
226     }
227   }
228   
229   //------------------------------------------------------------
230   // Internal function used by the function below
231   SPlot2d_Curve* GetCurveByIO( const Handle(SALOME_InteractiveObject)& theIObject,
232                                Plot2d_ViewFrame* thePlot )
233   {
234     if ( !theIObject.IsNull() && thePlot ) {
235       CurveDict aCurves = thePlot->getCurves();
236       CurveDict::Iterator it = aCurves.begin();
237       for( ; it != aCurves.end(); ++it ) {
238         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
239         if(aCurve) {
240           if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
241             return aCurve;
242         }
243       }
244     }
245     return NULL;
246   }
247
248   //------------------------------------------------------------
249   void
250   PlotContainer(Plot2d_ViewFrame* thePlot,
251                 VISU::Container_i* container,
252                 int theDisplaying)
253   {
254     if ( !thePlot )
255       return;
256
257     if ( theDisplaying == VISU::eDisplayOnly )
258       thePlot->EraseAll();
259     QList<Plot2d_Curve*> clist;
260     thePlot->getCurves( clist );
261     if ( container->GetNbCurves() > 0 ) {
262       int nbCurves = container->GetNbCurves();
263       for ( int k = 1; k <= nbCurves; k++ ) {
264         VISU::Curve_i* theCurve = container->GetCurve( k );
265         if ( theCurve && theCurve->IsValid() ) {
266           SPlot2d_Curve* plotCurve = GetCurveByIO(new SALOME_InteractiveObject(theCurve->GetEntry().c_str(), "", ""), thePlot);
267
268           UpdateCurve( theCurve, thePlot, plotCurve, theDisplaying );
269
270           if ( plotCurve && theDisplaying == VISU::eErase ) {
271             clist.removeAll( plotCurve );
272           }
273           CurveVisibilityChanged(theCurve, theDisplaying, true, true, true);
274         }
275       }
276     }
277     
278     thePlot->Repaint();
279     if(GetResourceMgr()->booleanValue("VISU","automatic_fit_all",false)){
280       thePlot->fitAll();
281     }
282     
283     qApp->processEvents();
284   }
285
286   //------------------------------------------------------------
287   void
288   CreatePlot(VISU_Gen_i* theVisuGen,
289              Plot2d_ViewFrame* thePlot,
290              _PTR(SObject) theTableSO)
291   {
292     _PTR(GenericAttribute) anAttr;
293     if ( theTableSO &&
294          ( theTableSO->FindAttribute( anAttr, "AttributeTableOfInteger" ) ||
295            theTableSO->FindAttribute( anAttr, "AttributeTableOfReal" ) ) ) {
296       CORBA::Object_var aTable = VISU::ClientSObjectToObject(theTableSO);
297       CORBA::Object_var aContainer = theVisuGen->CreateContainer();
298
299       if ( !CORBA::is_nil( aTable ) && !CORBA::is_nil( aContainer ) ) {
300         VISU::Table_i*     pTable     = dynamic_cast<VISU::Table_i*>(VISU::GetServant(aTable).in());
301         VISU::Container_i* pContainer = dynamic_cast<VISU::Container_i*>(VISU::GetServant(aContainer).in());
302
303         if ( pContainer && pTable ) {
304           for ( int i = 2; i <= pTable->GetNbRows(); i++ ) {
305             CORBA::Object_var aNewCurve = theVisuGen->CreateCurve( pTable->_this(), 1, i );
306             if( !CORBA::is_nil( aNewCurve ) ) {
307               VISU::Curve_i* pCrv = dynamic_cast<VISU::Curve_i*>( VISU::GetServant(aNewCurve).in() );
308               if ( pCrv ) {
309                 pContainer->AddCurve( pCrv->_this() );
310               }
311             }
312           }
313           PlotContainer( thePlot, pContainer, VISU::eDisplay );
314
315           QString anEntry = pContainer->GetEntry().c_str();
316           _PTR(Study) aStudy = theTableSO->GetStudy();
317           _PTR(SObject) aContainerSO = aStudy->FindObjectID(anEntry.toLatin1().data());
318           _PTR(SObject) aParentSO = aContainerSO->GetFather();
319         }
320       }
321     }
322   }
323
324   //------------------------------------------------------------
325   void SetVisibilityState(std::string entry, Qtx::VisibilityState state) {
326     if(entry.empty())
327       return;
328
329     if( SUIT_Session* aSession = SUIT_Session::session() )
330       if( SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(aSession->activeApplication()) )
331         if( SalomeApp_Study* aStudy =  dynamic_cast<SalomeApp_Study*>(anApp->activeStudy()) )
332           aStudy->setVisibilityState(entry.c_str(), state);
333
334   }
335
336   //------------------------------------------------------------
337   void SetVisibilityState(VISU_ActorBase *theActor, Qtx::VisibilityState state) {
338     if(!theActor || theActor->hasIO() || !theActor->getIO()->hasEntry())
339       return;
340     SetVisibilityState(theActor->getIO()->getEntry(), state);
341   }
342
343   void CurveVisibilityChanged(VISU::Curve_i* theCurve, 
344                               int theDisplaying,
345                               bool updateCurve,
346                               bool updateTable,
347                               bool updateContainers) {
348     
349     SUIT_Session* aSession = SUIT_Session::session();
350     if (!aSession) return;
351     
352     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(aSession->activeApplication());
353     if ( !anApp ) return;
354
355     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
356     if ( !aStudy ) return;
357
358     SalomeApp_Module* aModule = dynamic_cast<SalomeApp_Module*>( anApp->module( anApp->moduleTitle( "VISU" ) ) );
359     if ( !aModule ) return;
360
361     LightApp_Displayer* aDisplayer = aModule->displayer();
362
363     SUIT_ViewManager* aManager = anApp->activeViewManager();
364
365     Qtx::VisibilityState state = ( aManager && aManager->getType() == SPlot2d_Viewer::Type() ) ?
366       GetStateByDisplaying(theDisplaying) : Qtx::UnpresentableState;
367     
368     if ( updateCurve )
369       SetVisibilityState( theCurve->GetEntry(), state );
370     
371     if ( updateTable ) {
372       Table_i* aTable = theCurve->getTable();
373       if ( aTable ) {
374         _PTR(SObject) TableSO = aStudy->studyDS()->FindObjectID( aTable->GetEntry() );
375         if ( TableSO ) {
376           bool isTableVisible = false;
377           if ( aDisplayer && state != Qtx::UnpresentableState ) {
378             _PTR(ChildIterator) Iter = aStudy->studyDS()->NewChildIterator( TableSO );
379             for ( ; Iter->More() && !isTableVisible ; Iter->Next() ) {
380               CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
381               if ( CORBA::is_nil( childObject ) ) continue;
382               VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>( VISU::GetServant( childObject.in() ).in() );
383               isTableVisible = aCurve && aDisplayer->IsDisplayed( aCurve->GetEntry().c_str() );
384             }
385           } // if ( aDisplayer ... )
386           if ( state != Qtx::UnpresentableState )
387             SetVisibilityState( aTable->GetEntry(), ( isTableVisible ? Qtx::ShownState : Qtx::HiddenState ) );
388           else 
389             SetVisibilityState( aTable->GetEntry(), state );
390         } // if ( TableSO )
391       } // if ( aTable )
392     } // if ( updateTable )
393
394     if ( updateContainers ) {
395       ContainerSet aContainers = theCurve->getContainers();
396       ContainerSet::ConstIterator it = aContainers.begin();
397       for ( ; it != aContainers.end(); it++ ) {
398         _PTR(SObject) aSObject = aStudy->studyDS()->FindObjectID( (*it).toLatin1().data() );
399         if ( !aSObject ) continue;
400         bool isContainerDisplayed = false;
401         if ( aDisplayer && state != Qtx::UnpresentableState ) {
402           CORBA::Object_var anObj = VISU::ClientSObjectToObject( aSObject );
403           if ( CORBA::is_nil( anObj ) ) continue;
404           VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>( VISU::GetServant( anObj.in() ).in() );
405           if ( !aContainer ) continue;
406           int nbCurves = aContainer->GetNbCurves();
407           for ( int k = 1; k <= nbCurves && !isContainerDisplayed; k++ ) {
408             VISU::Curve_i* aCurve = aContainer->GetCurve( k );
409             isContainerDisplayed = aCurve && aDisplayer->IsDisplayed( aCurve->GetEntry().c_str() );
410           }
411         } // if ( aDisplayer ... )
412         if ( state != Qtx::UnpresentableState )
413           SetVisibilityState( (*it).toLatin1().constData(), ( isContainerDisplayed ? Qtx::ShownState : Qtx::HiddenState ) );
414         else {
415           SetVisibilityState( (*it).toLatin1().constData(), state );
416         }
417       } // for ( ; it != aContainers.end(); it++ )
418     } //updateContainers    
419   }
420   
421   Qtx::VisibilityState GetStateByDisplaying(int theDisplaying)
422   {
423     Qtx::VisibilityState state = Qtx::UnpresentableState;
424     if(theDisplaying == eDisplayAll || 
425        theDisplaying == eDisplay    || 
426        theDisplaying == eDisplayOnly ) {
427       state = Qtx::ShownState;
428       
429     } else if (theDisplaying == eErase || theDisplaying == eEraseAll) {
430       state = Qtx::HiddenState;
431     }
432     return state;
433   }
434
435   void updateContainerVisibility(VISU::Container_i* theContainer)
436   {
437     if ( !theContainer ) return;
438     
439     SUIT_Session* aSession = SUIT_Session::session();
440     if (!aSession) return;
441     
442     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(aSession->activeApplication());
443     if ( !anApp ) return;
444
445     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
446     if ( !aStudy ) return;
447
448     SalomeApp_Module* aModule = dynamic_cast<SalomeApp_Module*>( anApp->module( "VISU" ) );
449     if ( !aModule ) return;
450
451     SUIT_ViewManager* aManager = anApp->activeViewManager();
452     Qtx::VisibilityState state = ( aManager && aManager->getType() == SPlot2d_Viewer::Type() ) ?
453       Qtx::HiddenState : Qtx::UnpresentableState;
454     
455     LightApp_Displayer* aDisplayer = aModule->displayer();
456
457     int nbCurves = theContainer->GetNbCurves();
458     if ( nbCurves > 0 && aDisplayer ) {
459       for ( int k = 1; k <= nbCurves; k++ ) {
460         VISU::Curve_i* aCurve = theContainer->GetCurve( k );
461         if ( aCurve && aDisplayer->IsDisplayed( aCurve->GetEntry().c_str() ) ) {
462           state = Qtx::ShownState;
463           break;
464         }             
465       }
466     }
467     aStudy->setVisibilityState( theContainer->GetEntry().c_str(), state );
468   }
469 }