Salome HOME
Implementation of the issue 21285: EDF 1877 SMESH: Color of groups is only visible...
[modules/smesh.git] / src / OBJECT / SMESH_ActorUtils.cxx
1 // Copyright (C) 2007-2011  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
23 #include "SMESH_ActorUtils.h"
24 #include "SMESH_Actor.h"
25
26 #include "SUIT_Tools.h"
27 #include "SUIT_Session.h"
28 #include "SUIT_ResourceMgr.h"
29 #include <SALOMEconfig.h> // To fix some redefinition
30 #include "SalomeApp_Application.h"
31
32 #ifndef DISABLE_PLOT2DVIEWER
33 #include <SPlot2d_ViewModel.h>
34 #include <SPlot2d_Histogram.h>
35 #include <Plot2d_ViewManager.h>
36 #endif
37
38 #include <Qtx.h>
39
40
41 #include "utilities.h"
42
43 #include <vtkUnstructuredGrid.h>
44 #include <vtkXMLUnstructuredGridWriter.h>
45 #include <vtkUnstructuredGridWriter.h>
46
47 //#ifdef _DEBUG_
48 //static int MYDEBUG = 1;
49 //#else
50 //static int MYDEBUG = 0;
51 //#endif
52
53 namespace SMESH
54 {
55
56   vtkFloatingPointType
57   GetFloat( const QString& theValue, 
58             vtkFloatingPointType theDefault )
59   {
60     int pos = theValue.indexOf( ":" );
61     vtkFloatingPointType val = theDefault;
62     if( pos>=0 ) 
63     {
64       QString name = theValue.right( theValue.length()-pos-1 ),
65               sect = theValue.left( pos );
66       if( !name.isEmpty() && !sect.isEmpty() )
67         val = GetFloat( name, sect, theDefault );
68     }
69     return val;
70   }
71
72   vtkFloatingPointType
73   GetFloat( const QString& theValue, 
74             const QString& theSection, 
75             vtkFloatingPointType theDefault )
76   {
77     vtkFloatingPointType val = theDefault;
78     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
79     if( mgr )
80       val = (vtkFloatingPointType) mgr->doubleValue( theSection, theValue, theDefault );
81
82     return val;
83   }
84
85   void
86   WriteUnstructuredGrid(vtkUnstructuredGrid* theGrid, 
87                         const char* theFileName)
88   {
89     vtkXMLUnstructuredGridWriter* aWriter = vtkXMLUnstructuredGridWriter::New();
90     aWriter->SetFileName(theFileName);
91     aWriter->SetInput(theGrid);
92     aWriter->SetDataModeToAscii();
93     if(theGrid->GetNumberOfCells()){
94       aWriter->Write();
95     }
96     aWriter->Delete();
97   }
98
99   QColor
100   GetColor( const QString& theSect, 
101             const QString& theName, 
102             const QColor& def )
103   {
104     QColor c = def;
105     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
106     if ( mgr )
107       c = mgr->colorValue( theSect, theName, def );
108     return c;
109   }
110
111   void
112   GetColor( const QString& theSect, 
113             const QString& theName, 
114             int& r, 
115             int& g, 
116             int& b, 
117             const QColor& def )
118   {
119     QColor c = def;
120     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
121     if ( mgr )
122       c = mgr->colorValue( theSect, theName, def );
123
124     SUIT_Tools::rgbSet( SUIT_Tools::rgbSet( c ), r, g, b );
125   }
126
127   void
128   GetColor( const QString& theSect, 
129             const QString& theName, 
130             vtkFloatingPointType& r, 
131             vtkFloatingPointType& g, 
132             vtkFloatingPointType& b, 
133             const QColor& def )
134   {
135     int ir( 0 ), ig( 0 ), ib( 0 );
136     GetColor( theSect, theName, ir, ig, ib, def );
137     r = ir / 255.;
138     g = ig / 255.;
139     b = ib / 255.;
140   }
141
142
143   void
144   GetColor(  const QString& theSect, 
145              const QString& theName, 
146              QColor& color,
147              int& delta,
148              QString def) 
149   {
150     
151     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
152     if ( mgr ) {
153       QString str = mgr->stringValue( theSect, theName, def );
154       Qtx::stringToBiColor(str,color,delta);
155     }
156   }
157
158
159 #ifndef DISABLE_PLOT2DVIEWER
160   //=======================================================================
161   /**
162      Get histogram from the input actor
163      Repaint/Remove the histogram in/from each opened Plot2D Viewer 
164   */
165   //=======================================================================
166   void ProcessIn2DViewers( SMESH_Actor *theActor, Viewer2dActionType aType ) {
167     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
168     
169     if(!anApp || !theActor)
170       return;
171     
172     SPlot2d_Histogram* aHistogram = 0;
173     
174     if(theActor->GetPlot2Histogram())
175       if(aType == UpdateIn2dViewer)
176         aHistogram = theActor->UpdatePlot2Histogram();
177       else
178         aHistogram = theActor->GetPlot2Histogram();
179     else 
180       return;
181     
182     ViewManagerList aViewManagerList;
183     anApp->viewManagers(SPlot2d_Viewer::Type(), aViewManagerList);
184     
185     aType = aHistogram->getPointList().empty() ? RemoveFrom2dViewer : aType;
186     
187     SUIT_ViewManager* aViewManager;
188     foreach( aViewManager, aViewManagerList ) {
189       if (Plot2d_ViewManager* aManager = dynamic_cast<Plot2d_ViewManager*>(aViewManager)) {
190         if (SPlot2d_Viewer* aViewer = dynamic_cast<SPlot2d_Viewer*>(aManager->getViewModel())) {
191           if (Plot2d_ViewFrame* aViewFrame = aViewer->getActiveViewFrame()) {
192             if(aType == UpdateIn2dViewer )
193               aViewFrame->displayObject(aHistogram, true);
194             else if (aType == RemoveFrom2dViewer)
195               aViewFrame->eraseObject(aHistogram, true);
196           }
197         }
198       }
199     }
200   }
201 #endif //DISABLE_PLOT2DVIEWER
202   
203 }