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