Salome HOME
e2963b9dccb81e5da127f5ef2f629976eb91736d
[modules/visu.git] / src / VISU_I / VISU_Tools.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : VISU_Tools.cxx
23 //  Author : Oleg UVAROV
24 //  Module : VISU
25 //
26 #include "VISU_Tools.h"
27
28 #include "VISU_Gen_i.hh"
29 #include "VISU_Table_i.hh"
30 #include "VISU_ViewManager_i.hh"
31
32 #include <SalomeApp_Study.h>
33 #include <SalomeApp_Application.h>
34
35 #include <SPlot2d_ViewModel.h>
36 #include <Plot2d_ViewFrame.h>
37 #include <Plot2d_ViewManager.h>
38
39 #include <SUIT_ResourceMgr.h>
40
41 //=============================================================================
42 namespace VISU
43 {
44   //------------------------------------------------------------
45   // Internal function used by several public functions below
46   void
47   UpdateCurve(VISU::Curve_i* theCurve,
48               Plot2d_ViewFrame* thePlot,
49               SPlot2d_Curve* plotCurve,
50               int theDisplaying)
51   {
52     if ( theDisplaying == VISU::eErase ) {
53       if ( plotCurve && thePlot )
54         thePlot->eraseCurve( plotCurve, false );
55     }
56     else if ( theDisplaying == VISU::eDisplay || theDisplaying == VISU::eDisplayOnly ) {
57       if ( plotCurve ) {
58         plotCurve->setHorTitle( theCurve->GetHorTitle().c_str() );
59         //plotCurve->setVerTitle( ( theCurve->GetVerTitle().c_str() ) );
60         plotCurve->setVerTitle( theCurve->GetName().c_str() );
61         plotCurve->setHorUnits( theCurve->GetHorUnits().c_str() );
62         plotCurve->setVerUnits( theCurve->GetVerUnits().c_str() );
63         double* xList = 0;
64         double* yList = 0;
65         QStringList zList;
66         int     nbPoints = theCurve->GetData( xList, yList, zList );
67         if ( nbPoints > 0 && xList && yList ) {
68           plotCurve->setData( xList, yList, nbPoints, zList );
69         }
70         if ( !theCurve->IsAuto() ) {
71           plotCurve->setLine( (Plot2d::LineType)theCurve->GetLine(), theCurve->GetLineWidth() );
72           plotCurve->setMarker( (Plot2d::MarkerType)theCurve->GetMarker() );
73           SALOMEDS::Color color = theCurve->GetColor();
74           plotCurve->setColor( QColor( (int)(color.R*255.), (int)(color.G*255.), (int)(color.B*255.) ) );
75         }
76         plotCurve->setAutoAssign( theCurve->IsAuto() );
77         if( thePlot )
78           thePlot->displayCurve( plotCurve, false );
79       }
80       else {
81         Plot2d_Curve* crv = theCurve->CreatePresentation();
82         if ( crv ) {
83           if( thePlot )
84             thePlot->displayCurve( crv, false );
85           theCurve->SetLine( (VISU::Curve::LineType)crv->getLine(), crv->getLineWidth() );
86           theCurve->SetMarker( (VISU::Curve::MarkerType)crv->getMarker());
87           SALOMEDS::Color newColor;
88           newColor.R = crv->getColor().red()/255.;
89           newColor.G = crv->getColor().green()/255.;
90           newColor.B = crv->getColor().blue()/255.;
91           theCurve->SetColor( newColor );
92           crv->setAutoAssign( theCurve->IsAuto() );
93         }
94       }
95     }
96   }
97
98   //------------------------------------------------------------
99   void
100   PlotTable(SalomeApp_Study* theStudy,
101             Plot2d_ViewFrame* thePlot,
102             VISU::Table_i* table,
103             int theDisplaying)
104   {
105     if ( !thePlot )
106       return;
107
108     if ( theDisplaying == VISU::eDisplayOnly )
109       thePlot->EraseAll();
110     QList<Plot2d_Curve*> clist;
111     thePlot->getCurves( clist );
112     _PTR(Study) aStudy = theStudy->studyDS();
113     _PTR(SObject) TableSO = aStudy->FindObjectID( table->GetEntry() );
114     if ( TableSO ) {
115       _PTR(ChildIterator) Iter = aStudy->NewChildIterator( TableSO );
116       for ( ; Iter->More(); Iter->Next() ) {
117         CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
118         if( !CORBA::is_nil( childObject ) ) {
119           CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
120           if( !CORBA::is_nil( aCurve ) ) {
121             VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
122             SPlot2d_Curve* plotCurve = 0;
123             SPlot2d_Curve* tmpCurve;
124             for ( int i = 0; i < clist.count(); i++ ) {
125               tmpCurve = dynamic_cast<SPlot2d_Curve*>( clist.at( i ) );
126               if (tmpCurve && tmpCurve->hasIO() &&
127                   theCurve->GetEntry() == tmpCurve->getIO()->getEntry()) {
128                 plotCurve = tmpCurve;
129                 break;
130               }
131             }
132
133             UpdateCurve( theCurve, thePlot, plotCurve, theDisplaying );
134
135             if ( theDisplaying == VISU::eErase && plotCurve ) {
136               clist.removeAll(plotCurve );
137             }
138           }
139         }
140       }
141       thePlot->Repaint();
142     }
143   }
144
145   //------------------------------------------------------------
146   void
147   PlotCurve(Plot2d_ViewFrame* thePlot,
148             VISU::Curve_i* theCurve,
149             int theDisplaying)
150   {
151     if ( !thePlot )
152       return;
153
154 //  if ( theDisplaying == VISU::eDisplayOnly )
155 //    thePlot->EraseAll();
156     QList<Plot2d_Curve*> clist;
157     thePlot->getCurves( clist );
158     SPlot2d_Curve* plotCurve = 0;
159     SPlot2d_Curve* tmpCurve;
160     if(theDisplaying == VISU::eErase) {
161       // 23.06.2008 skl for IPAL17672
162       for (int i = 0; i < clist.count(); i++) {
163         tmpCurve = dynamic_cast<SPlot2d_Curve*>(clist.at(i));
164         if (tmpCurve && tmpCurve->hasIO() &&
165             theCurve->GetEntry() == tmpCurve->getIO()->getEntry()) {
166           plotCurve = tmpCurve;
167           thePlot->eraseCurve(clist.at(i));
168           break;
169         }
170       }
171       UpdateCurve(theCurve, thePlot, plotCurve, theDisplaying);
172     }
173     else {
174       for (int i = 0; i < clist.count(); i++) {
175         tmpCurve = dynamic_cast<SPlot2d_Curve*>(clist.at(i));
176         if (tmpCurve && tmpCurve->hasIO() &&
177             theCurve->GetEntry() == tmpCurve->getIO()->getEntry()) {
178           plotCurve = tmpCurve;
179         }
180         else if (theDisplaying == VISU::eDisplayOnly) {
181           thePlot->eraseCurve(clist.at(i));
182         }
183       }
184       UpdateCurve(theCurve, thePlot, plotCurve, theDisplaying);
185     }
186
187     thePlot->Repaint();
188   }
189
190   //------------------------------------------------------------
191   void
192   PlotRemoveCurve(SalomeApp_Application* theApp,
193                   VISU::Curve_i* pCrv)
194   {
195     QString anEntry = pCrv->GetEntry().c_str();
196     ViewManagerList pvm_list;
197     theApp->viewManagers( SPlot2d_Viewer::Type(), pvm_list );
198     ViewManagerList::Iterator pvm_it = pvm_list.begin();
199     for( ; pvm_it != pvm_list.end(); pvm_it++ ){
200       Plot2d_ViewManager* pvm = dynamic_cast<Plot2d_ViewManager*>( *pvm_it );
201       if( pvm ){
202         SPlot2d_Viewer* aSPlot2d = dynamic_cast<SPlot2d_Viewer*>( pvm->getViewModel() );
203         if( aSPlot2d ){
204           Plot2d_ViewFrame* thePlot = aSPlot2d->getActiveViewFrame();
205           if(thePlot){
206             QList<Plot2d_Curve*> clist;
207             thePlot->getCurves( clist );
208             for (int i = 0; i < clist.count(); i++) {
209               if(SPlot2d_Curve* plotCurve = dynamic_cast<SPlot2d_Curve*>(clist[i]))
210                 if(plotCurve->hasIO() && (plotCurve->getIO()->getEntry() == anEntry))
211                   thePlot->eraseCurve(clist[i]);
212             }
213           }
214         }
215       }
216     }
217   }
218   
219   //------------------------------------------------------------
220   // Internal function used by the function below
221   SPlot2d_Curve* GetCurveByIO( const Handle(SALOME_InteractiveObject)& theIObject,
222                                Plot2d_ViewFrame* thePlot )
223   {
224     if ( !theIObject.IsNull() && thePlot ) {
225       CurveDict aCurves = thePlot->getCurves();
226       CurveDict::Iterator it = aCurves.begin();
227       for( ; it != aCurves.end(); ++it ) {
228         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
229         if(aCurve) {
230           if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
231             return aCurve;
232         }
233       }
234     }
235     return NULL;
236   }
237
238   //------------------------------------------------------------
239   void
240   PlotContainer(Plot2d_ViewFrame* thePlot,
241                 VISU::Container_i* container,
242                 int theDisplaying)
243   {
244     if ( !thePlot )
245       return;
246
247     if ( theDisplaying == VISU::eDisplayOnly )
248       thePlot->EraseAll();
249     QList<Plot2d_Curve*> clist;
250     thePlot->getCurves( clist );
251     if ( container->GetNbCurves() > 0 ) {
252       int nbCurves = container->GetNbCurves();
253       for ( int k = 1; k <= nbCurves; k++ ) {
254         VISU::Curve_i* theCurve = container->GetCurve( k );
255         if ( theCurve && theCurve->IsValid() ) {
256           SPlot2d_Curve* plotCurve = GetCurveByIO(new SALOME_InteractiveObject(theCurve->GetEntry().c_str(), "", ""), thePlot);
257
258           UpdateCurve( theCurve, thePlot, plotCurve, theDisplaying );
259
260           if ( plotCurve && theDisplaying == VISU::eErase ) {
261             clist.removeAll( plotCurve );
262           }
263         }
264       }
265     }
266     thePlot->Repaint();
267     if(GetResourceMgr()->booleanValue("VISU","automatic_fit_all",false)){
268       thePlot->fitAll();
269     }
270     qApp->processEvents();
271   }
272
273   //------------------------------------------------------------
274   void
275   CreatePlot(VISU_Gen_i* theVisuGen,
276              Plot2d_ViewFrame* thePlot,
277              _PTR(SObject) theTableSO)
278   {
279     _PTR(GenericAttribute) anAttr;
280     if ( theTableSO &&
281          ( theTableSO->FindAttribute( anAttr, "AttributeTableOfInteger" ) ||
282            theTableSO->FindAttribute( anAttr, "AttributeTableOfReal" ) ) ) {
283       CORBA::Object_var aTable = VISU::ClientSObjectToObject(theTableSO);
284       CORBA::Object_var aContainer = theVisuGen->CreateContainer();
285
286       if ( !CORBA::is_nil( aTable ) && !CORBA::is_nil( aContainer ) ) {
287         VISU::Table_i*     pTable     = dynamic_cast<VISU::Table_i*>(VISU::GetServant(aTable).in());
288         VISU::Container_i* pContainer = dynamic_cast<VISU::Container_i*>(VISU::GetServant(aContainer).in());
289
290         if ( pContainer && pTable ) {
291           for ( int i = 2; i <= pTable->GetNbRows(); i++ ) {
292             CORBA::Object_var aNewCurve = theVisuGen->CreateCurve( pTable->_this(), 1, i );
293             if( !CORBA::is_nil( aNewCurve ) ) {
294               VISU::Curve_i* pCrv = dynamic_cast<VISU::Curve_i*>( VISU::GetServant(aNewCurve).in() );
295               if ( pCrv ) {
296                 pContainer->AddCurve( pCrv->_this() );
297               }
298             }
299           }
300           PlotContainer( thePlot, pContainer, VISU::eDisplay );
301
302           QString anEntry = pContainer->GetEntry().c_str();
303           _PTR(Study) aStudy = theTableSO->GetStudy();
304           _PTR(SObject) aContainerSO = aStudy->FindObjectID(anEntry.toLatin1().data());
305           _PTR(SObject) aParentSO = aContainerSO->GetFather();
306         }
307       }
308     }
309   }
310 }