Salome HOME
Implementation of the issue "20830: EDF 1357 GUI : Hide/Show Icon" for Post-Pro module.
[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)
351       return;
352     
353     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(aSession->activeApplication());
354     if( !anApp )
355       return;
356
357     SUIT_ViewManager* aManager = anApp->activeViewManager();
358
359     if( !aManager)
360       return;
361     
362     bool is2dViewActive = aManager->getType() == SPlot2d_Viewer::Type();
363
364     Qtx::VisibilityState state = is2dViewActive ? GetStateByDisplaying(theDisplaying) : Qtx::UnpresentableState;
365     
366     if(updateCurve)
367       SetVisibilityState(theCurve->GetEntry(), state);
368     
369     if(updateTable) {
370       Table_i* aTable = theCurve->getTable();
371       if(aTable) {
372         if( SalomeApp_Study* aStudy =  dynamic_cast<SalomeApp_Study*>(anApp->activeStudy()) ) {
373           _PTR(SObject) TableSO = aStudy->studyDS()->FindObjectID( aTable->GetEntry() );
374           if ( TableSO ) {
375             _PTR(ChildIterator) Iter = aStudy->studyDS()->NewChildIterator( TableSO );
376             bool isTableVisible = false;
377             LightApp_Displayer* aDisplayer = (dynamic_cast<SalomeApp_Module*>(anApp->activeModule()))->displayer();
378             if(aDisplayer && state != Qtx::UnpresentableState ) {
379               for ( ; Iter->More(); Iter->Next() ) {
380                 CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
381                 if( !CORBA::is_nil( childObject ) ) {
382                   CORBA::Object_ptr aCurve_ptr = VISU::Curve::_narrow( childObject );
383                   if( !CORBA::is_nil( aCurve_ptr ) ) {
384                     VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve_ptr).in());
385                     if(aDisplayer->IsDisplayed(aCurve->GetEntry().c_str())) {
386                       isTableVisible = true;
387                       break;
388                     }
389                   } //!CORBA::is_nil( aCurve )
390                 } // !CORBA::is_nil( childObject )
391               } //for
392               if(state != Qtx::UnpresentableState)
393                 SetVisibilityState(aTable->GetEntry(), (isTableVisible ? Qtx::ShownState : Qtx::HiddenState));
394               else 
395                 SetVisibilityState(aTable->GetEntry(), state);
396             } //aDisplayer
397           } //TableSO
398         } //aStudy
399       } //aTable
400     } //updateTable
401
402     if(updateContainers) {
403       if( SalomeApp_Study* aStudy =  dynamic_cast<SalomeApp_Study*>(anApp->activeStudy()) ) {
404         if(LightApp_Displayer* aDisplayer =  (dynamic_cast<SalomeApp_Module*>(anApp->activeModule()))->displayer()) { 
405           ContainerSet aContainers = theCurve->getContainers();
406           ContainerSet::ConstIterator it = aContainers.begin();
407           for(;it != aContainers.end();it++ ) {
408             if(state != Qtx::UnpresentableState) {
409               bool isContainerDisplayed = false;
410               _PTR(SObject) aSObject = aStudy->studyDS()->FindObjectID( (*it).toLatin1().data());
411               CORBA::Object_var anObj = VISU::ClientSObjectToObject( aSObject );
412               if( !CORBA::is_nil( anObj ) ) {
413                 if(VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>( VISU::GetServant( anObj ).in())) {
414                   int nbCurves = aContainer->GetNbCurves();
415                   for( int k=1; k<=nbCurves; k++ ) {
416                     VISU::Curve_i* aCurve = aContainer->GetCurve( k );
417                     if(aCurve && aDisplayer->IsDisplayed(aCurve->GetEntry().c_str())) {
418                       isContainerDisplayed = true;
419                       break;
420                     } // aCurve && aDisplater->IsDisplayed
421                   } //for k=1 ...
422                   SetVisibilityState(aContainer->GetEntry(), (isContainerDisplayed ? Qtx::ShownState : Qtx::HiddenState));
423                 } //aContainer
424               } //!CORBA::is_nil( anObj )
425             } //state != Qtx::UnpresentableState
426             else {
427               SetVisibilityState((*it).toLatin1().data(), state);
428             }
429           } // for Containers           
430         } //aDisplayer
431       } //aStudy
432     } //updateContainers    
433   } 
434
435   Qtx::VisibilityState GetStateByDisplaying(int theDisplaying) {
436     
437     Qtx::VisibilityState state = Qtx::UnpresentableState;
438     if(theDisplaying == eDisplayAll || 
439        theDisplaying == eDisplay    || 
440        theDisplaying == eDisplayOnly ) {
441       state = Qtx::ShownState;
442       
443     } else if (theDisplaying == eErase || theDisplaying == eEraseAll) {
444       state = Qtx::HiddenState;
445     }
446     return state;
447   }
448
449   void updateContainerVisibility(VISU::Container_i* theContainer) {
450     if(!theContainer)
451       return;
452     
453     if( SUIT_Session* aSession = SUIT_Session::session() ) {
454       if( SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(aSession->activeApplication()) ) {
455         if( SalomeApp_Study* aStudy =  dynamic_cast<SalomeApp_Study*>(anApp->activeStudy()) ) {
456           Qtx::VisibilityState state = Qtx::UnpresentableState;
457           int nbCurves = theContainer->GetNbCurves();
458           if( nbCurves > 0 ) {
459             if(LightApp_Displayer* aDisplayer =  (dynamic_cast<SalomeApp_Module*>(anApp->activeModule()))->displayer()) {
460               for( int k=1; k<=nbCurves; k++ ) {
461                 VISU::Curve_i* aCurve = theContainer->GetCurve( k );
462                 if(aCurve && aDisplayer->IsDisplayed(aCurve->GetEntry().c_str())) {
463                   state = Qtx::ShownState;
464                   break;
465                 }             
466               }
467               if ( state  == Qtx::UnpresentableState) {
468                 if( SUIT_ViewManager * vm = anApp->activeViewManager()){
469                   if(vm->getType() == SPlot2d_Viewer::Type()) {
470                     state = Qtx::HiddenState;
471                   }
472                 }
473               }
474             }
475           } else if(SUIT_ViewManager * vm = anApp->activeViewManager()){
476             if(vm->getType() == SPlot2d_Viewer::Type()) {
477               state = Qtx::HiddenState;
478             }
479           }
480           aStudy->setVisibilityState(theContainer->GetEntry().c_str(),state);
481         }
482       }
483     }
484   }
485 }