]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_ScalarMap_i.cc
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / VISU_I / VISU_ScalarMap_i.cc
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  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 //  File   : VISU_PrsObject_i.cxx
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #include "VISU_ScalarMap_i.hh"
28 #include "VISU_Prs3dUtils.hh"
29
30 #include "VISU_Result_i.hh"
31
32 #include "VISU_ScalarMapAct.h"
33 #include "VISU_ScalarMapPL.hxx"
34 #include "VISU_LookupTable.hxx"
35 #include "VISU_ScalarBarActor.hxx"
36 #include "VISU_Convertor.hxx"
37
38 #include "SUIT_ResourceMgr.h"
39
40 #include "SUIT_Session.h"
41 #include "SALOME_Event.hxx"
42 #include "SalomeApp_Study.h"
43 #include "SalomeApp_Application.h"
44
45 #include <vtkDataSetMapper.h>
46 #include <vtkTextProperty.h>
47
48 #ifdef _DEBUG_
49 static int MYDEBUG = 0;
50 #else
51 static int MYDEBUG = 0;
52 #endif
53
54 static int INCMEMORY = 4;
55
56
57 //----------------------------------------------------------------------------
58 size_t
59 VISU::ScalarMap_i
60 ::IsPossible(Result_i* theResult, 
61              const std::string& theMeshName, 
62              VISU::Entity theEntity, 
63              const std::string& theFieldName, 
64              CORBA::Long theTimeStampNumber, 
65              bool theIsMemoryCheck)
66 {
67   size_t aResult = 0;
68   try{
69     if(theResult){
70       bool anIsEstimated = true;
71       VISU::Result_i::PInput anInput = theResult->GetInput(theMeshName,
72                                                            theEntity,
73                                                            theFieldName,
74                                                            theTimeStampNumber);
75       size_t aSize = anInput->GetTimeStampOnMeshSize(theMeshName,
76                                                      (VISU::TEntity)theEntity,
77                                                      theFieldName,
78                                                      theTimeStampNumber,
79                                                      anIsEstimated);
80       aResult = 1;
81       if(theIsMemoryCheck){
82         if(anIsEstimated)
83           aSize *= INCMEMORY;
84         aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
85         if(MYDEBUG) 
86           MESSAGE("ScalarMap_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<aResult);
87       }
88     }
89   }catch(std::exception& exc){
90     INFOS("Follow exception was occured :\n"<<exc.what());
91   }catch(...){
92     INFOS("Unknown exception was occured!");
93   }
94   return aResult;
95 }
96
97 //----------------------------------------------------------------------------
98 int VISU::ScalarMap_i::myNbPresent = 0;
99
100 //----------------------------------------------------------------------------
101 QString
102 VISU::ScalarMap_i
103 ::GenerateName() 
104
105   return VISU::GenerateName("ScalarMap",myNbPresent++);
106 }
107
108 //----------------------------------------------------------------------------
109 const string VISU::ScalarMap_i::myComment = "SCALARMAP";
110
111 //----------------------------------------------------------------------------
112 const char* 
113 VISU::ScalarMap_i
114 ::GetComment() const 
115
116   return myComment.c_str();
117 }
118
119 //----------------------------------------------------------------------------
120 const char*
121 VISU::ScalarMap_i
122 ::GetIconName()
123 {
124   if (!IsGroupsUsed())
125     return "ICON_TREE_SCALAR_MAP";
126   else
127     return "ICON_TREE_SCALAR_MAP_GROUPS";
128 }
129
130 //----------------------------------------------------------------------------
131 void
132 VISU::ScalarMap_i
133 ::UpdateIcon()
134 {
135   SALOMEDS::SObject_var aSObject = GetSObject();
136   if(CORBA::is_nil(aSObject))
137     return;
138
139   SALOMEDS::Study_var aStudyDocument = GetStudyDocument();
140   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudyDocument->NewBuilder();
141
142   // Check if the icon needs to be updated, update if necessary
143   SALOMEDS::GenericAttribute_var anAttr = 
144     aStudyBuilder->FindOrCreateAttribute(aSObject, "AttributePixMap");
145   SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
146
147   CORBA::String_var aPixMapName = aPixmap->GetPixMap();
148   if(strcmp(GetIconName(), aPixMapName.in()) != 0)
149     aPixmap->SetPixMap(GetIconName());
150
151   // Update Object Browser
152   SUIT_Session* aSession = SUIT_Session::session();
153   QPtrList<SUIT_Application> anApplications = aSession->applications();
154   QPtrListIterator<SUIT_Application> anIter(anApplications);
155   while(SUIT_Application* aSApp = anIter.current()){
156     if(SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(aSApp)){
157       if(SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(anApp->activeStudy())){
158         if(_PTR(Study) aCStudy = aStudy->studyDS()){
159           if(aStudyDocument->StudyId() == aCStudy->StudyId()){
160             anApp->updateObjectBrowser();
161             break;
162           }
163         }
164       }
165     }
166     ++anIter;
167   }
168 }
169
170 //----------------------------------------------------------------------------
171 VISU::ScalarMap_i::
172 ScalarMap_i(EPublishInStudyMode thePublishInStudyMode) :
173   ColoredPrs3d_i(thePublishInStudyMode)
174 {}
175
176 //----------------------------------------------------------------------------
177 VISU::ScalarMap_i
178 ::~ScalarMap_i()
179 {}
180
181
182 //----------------------------------------------------------------------------
183 void
184 VISU::ScalarMap_i
185 ::SameAs(const Prs3d_i* theOrigin)
186 {
187   TSuperClass::SameAs(theOrigin);
188
189   if(const ScalarMap_i* aPrs3d = dynamic_cast<const ScalarMap_i*>(theOrigin)){
190     ScalarMap_i* anOrigin = const_cast<ScalarMap_i*>(aPrs3d);
191  
192     SetScaling(anOrigin->GetScaling());
193     
194     Update();
195   }
196 }
197
198
199 //----------------------------------------------------------------------------
200 /**
201  * Creates Scalar Map and initialises it from resources
202  */
203 VISU::Storable* 
204 VISU::ScalarMap_i
205 ::Create(const std::string& theMeshName, 
206          VISU::Entity theEntity,
207          const std::string& theFieldName, 
208          CORBA::Long theTimeStampNumber)
209 {
210   TSuperClass::Create(theMeshName,theEntity,theFieldName,theTimeStampNumber);
211
212   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
213
214   if( aResourceMgr->booleanValue("VISU", "scalar_bar_logarithmic", false) )
215     SetScaling(VISU::LOGARITHMIC);
216   else
217     SetScaling(VISU::LINEAR);
218
219   return this;
220 }
221
222 //----------------------------------------------------------------------------
223 CORBA::Float
224 VISU::ScalarMap_i
225 ::GetMemorySize()
226 {
227   return TSuperClass::GetMemorySize();
228 }
229
230 //----------------------------------------------------------------------------
231 VISU::Storable* 
232 VISU::ScalarMap_i
233 ::Restore(SALOMEDS::SObject_ptr theSObject,
234           const Storable::TRestoringMap& theMap)
235 {
236   if(!TSuperClass::Restore(theSObject, theMap))
237     return NULL;
238
239   SetScaling(VISU::Scaling(VISU::Storable::FindValue(theMap,"myScaling").toInt()));
240
241   // Check if the icon needs to be updated, update if necessary
242   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
243   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
244   SALOMEDS::GenericAttribute_var anAttr = 
245     aStudyBuilder->FindOrCreateAttribute(theSObject, "AttributePixMap");
246   SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
247
248   CORBA::String_var aPixMapName = aPixmap->GetPixMap();
249   if(strcmp(GetIconName(), aPixMapName.in()) != 0)
250     aPixmap->SetPixMap(GetIconName());
251
252   return this;
253 }
254
255 //----------------------------------------------------------------------------
256 void 
257 VISU::ScalarMap_i
258 ::ToStream(std::ostringstream& theStr)
259 {
260   TSuperClass::ToStream(theStr);
261
262   Storable::DataToStream( theStr, "myScaling",        GetScaling() );
263 }
264
265
266 //----------------------------------------------------------------------------
267 VISU::Scaling 
268 VISU::ScalarMap_i
269 ::GetScaling()
270 {
271   return VISU::Scaling(GetSpecificPL()->GetScaling());
272 }
273
274 //----------------------------------------------------------------------------
275 void
276 VISU::ScalarMap_i
277 ::SetScaling(VISU::Scaling theScaling)
278 {
279   VISU::TSetModified aModified(this);
280   
281   ProcessVoidEvent(new TVoidMemFun1ArgEvent<VISU_ScalarMapPL, int>
282                    (GetSpecificPL(), &VISU_ColoredPL::SetScaling, theScaling));
283 }
284
285 //----------------------------------------------------------------------------
286 void
287 VISU::ScalarMap_i
288 ::SetSourceGeometry()
289 {
290   int aNbGroups = myGroupNames.size();
291   if(aNbGroups != 0){
292     GetSpecificPL()->SetSourceGeometry();
293     myGroupNames.clear();
294     UpdateIcon();
295   }
296 }
297
298 //----------------------------------------------------------------------------
299 void
300 VISU::ScalarMap_i
301 ::AddMeshOnGroup(const char* theGroupName)
302 {
303   VISU::Result_i::PInput anInput = GetCResult()->GetInput();
304   VISU::PUnstructuredGridIDMapper anIDMapper = anInput->GetMeshOnGroup(GetCMeshName(), theGroupName);
305   if(anIDMapper){
306     int aNbGroups  = myGroupNames.size();
307     if(myGroupNames.find(theGroupName) == myGroupNames.end()){
308       GetSpecificPL()->AddGeometry(anIDMapper->GetOutput());
309       myGroupNames.insert(theGroupName);
310       if(aNbGroups == 0)
311         UpdateIcon();
312     }
313   }
314 }
315
316
317 //----------------------------------------------------------------------------
318 void
319 VISU::ScalarMap_i
320 ::RemoveAllGeom()
321 {
322   int aNbGroups  = myGroupNames.size();
323   GetSpecificPL()->ClearGeometry();
324   myGroupNames.clear();
325   if(aNbGroups != 0)
326     UpdateIcon();
327 }
328
329
330 //----------------------------------------------------------------------------
331 void 
332 VISU::ScalarMap_i
333 ::DoSetInput(bool theIsInitilizePipe, bool theReInit)
334 {
335   VISU::Result_i::PInput anInput = GetCResult()->GetInput(GetCMeshName(),
336                                                           GetEntity(),
337                                                           GetCFieldName(),
338                                                           GetTimeStampNumber());
339   if(!anInput)
340     throw std::runtime_error("Mesh_i::Build - GetCResult()->GetInput() == NULL !!!");
341
342   SetField(anInput->GetField(GetCMeshName(),GetTEntity(),GetCFieldName()));
343   if(!GetField())
344     throw std::runtime_error("There is no Field with the parameters !!!");
345
346   VISU::PUnstructuredGridIDMapper anIDMapper =
347     anInput->GetTimeStampOnMesh(GetCMeshName(),GetTEntity(),GetCFieldName(),GetTimeStampNumber());
348
349   if(!anIDMapper) 
350     throw std::runtime_error("There is no TimeStamp with the parameters !!!");
351
352   GetSpecificPL()->SetUnstructuredGridIDMapper(anIDMapper);
353 }
354
355
356 //----------------------------------------------------------------------------
357 bool
358 VISU::ScalarMap_i
359 ::CheckIsPossible() 
360 {
361   return IsPossible(GetCResult(),GetCMeshName(),GetEntity(),GetCFieldName(),GetTimeStampNumber(),true);
362 }
363
364
365 //----------------------------------------------------------------------------
366 void
367 VISU::ScalarMap_i
368 ::CreatePipeLine(VISU_PipeLine* thePipeLine)
369 {
370   if(MYDEBUG) MESSAGE("ScalarMap_i::CreatePipeLine() - "<<thePipeLine);
371   if(!thePipeLine){
372     myScalarMapPL = VISU_ScalarMapPL::New();
373     myScalarMapPL->GetMapper()->SetScalarVisibility(1);
374   }else
375     myScalarMapPL = dynamic_cast<VISU_ScalarMapPL*>(thePipeLine);
376
377   TSuperClass::CreatePipeLine(myScalarMapPL);
378 }
379
380 //----------------------------------------------------------------------------
381 void
382 VISU::ScalarMap_i
383 ::SetMapScale(double theMapScale)
384 {
385   GetSpecificPL()->SetMapScale(theMapScale);
386 }
387
388 //----------------------------------------------------------------------------
389 VISU_Actor* 
390 VISU::ScalarMap_i
391 ::CreateActor(bool toSupressShrinking)
392 {
393   VISU_ScalarMapAct* anActor = VISU_ScalarMapAct::New();
394   try{
395     TSuperClass::CreateActor(anActor);
396     anActor->SetBarVisibility(true);
397     SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
398     int  aDispMode = aResourceMgr->integerValue("VISU", "scalar_map_represent", 2);
399     bool toShrink  = aResourceMgr->booleanValue("VISU", "scalar_map_shrink", false);
400     bool toUseShading = aResourceMgr->booleanValue("VISU", "represent_shading", false);
401     anActor->SetRepresentation(aDispMode);
402     if (toShrink && !toSupressShrinking) anActor->SetShrink();
403     anActor->SetShading(toUseShading);
404     UpdateActor(anActor);
405   }catch(...){
406     anActor->Delete();
407     throw;
408   }
409   return anActor;
410 }
411
412 //----------------------------------------------------------------------------
413 VISU_Actor* 
414 VISU::ScalarMap_i
415 ::CreateActor()
416 {
417   return CreateActor(false);
418 }
419
420 //----------------------------------------------------------------------------
421 void
422 VISU::ScalarMap_i
423 ::UpdateActor(VISU_Actor* theActor) 
424 {
425   if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
426     VISU_ScalarBarActor *aScalarBar = anActor->GetScalarBar();
427     aScalarBar->SetLookupTable(GetSpecificPL()->GetBarTable());
428     aScalarBar->SetTitle(GetScalarBarTitle().c_str());
429     aScalarBar->SetOrientation(GetBarOrientation());
430     aScalarBar->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
431     aScalarBar->GetPositionCoordinate()->SetValue(GetPosX(),GetPosY());
432     aScalarBar->SetWidth(GetWidth());
433     aScalarBar->SetHeight(GetHeight());
434     aScalarBar->SetRatios(GetTitleWidth(), GetTitleHeight(),
435                           GetLabelWidth(), GetLabelHeight(),
436                           GetBarWidth(), GetBarHeight());
437     aScalarBar->SetNumberOfLabels(GetLabels());
438     aScalarBar->SetLabelFormat(GetLabelsFormat());
439
440     vtkFloatingPointType anRGB[3];
441
442     vtkTextProperty* aTitleProp = aScalarBar->GetTitleTextProperty();
443     aTitleProp->SetFontFamily(GetTitFontType());
444
445     GetTitleColor(&anRGB[0],&anRGB[1],&anRGB[2]);
446     aTitleProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
447
448     IsBoldTitle()? aTitleProp->BoldOn() : aTitleProp->BoldOff();
449     IsItalicTitle()? aTitleProp->ItalicOn() : aTitleProp->ItalicOff();
450     IsShadowTitle()? aTitleProp->ShadowOn() : aTitleProp->ShadowOff();
451
452     vtkTextProperty* aLabelProp = aScalarBar->GetLabelTextProperty();
453     aLabelProp->SetFontFamily(GetLblFontType());
454
455     GetLabelColor(&anRGB[0],&anRGB[1],&anRGB[2]);
456     aLabelProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
457
458     IsBoldLabel()? aLabelProp->BoldOn() : aLabelProp->BoldOff();
459     IsItalicLabel()? aLabelProp->ItalicOn() : aLabelProp->ItalicOff();
460     IsShadowLabel()? aLabelProp->ShadowOn() : aLabelProp->ShadowOff();
461
462     aScalarBar->Modified();
463   }
464   TSuperClass::UpdateActor(theActor);
465 }