Salome HOME
Merge branch 'apl/22873_tmp'
[modules/geom.git] / src / GEOMGUI / GEOM_Displayer.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : GEOM_Displayer.cxx
25 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
26
27 #include "GEOM_Displayer.h"
28 #include "GeometryGUI.h"
29
30 #include <GEOM_Constants.h>
31 #include <GEOM_TypeFilter.h>
32 #include <GEOM_EdgeFilter.h>
33 #include <GEOM_FaceFilter.h>
34 #include <GEOM_CompoundFilter.h>
35 #include <GEOM_PreviewFilter.h>
36 #include <GEOM_LogicalFilter.h>
37 #include <GEOM_OCCFilter.h>
38
39 #include <GEOM_Actor.h>
40 #include <GEOM_AISDimension.hxx>
41 #include <GEOM_Annotation.hxx>
42 #include <GEOM_TopWireframeShape.hxx>
43 #include <GEOM_AISVector.hxx>
44 #include <GEOM_AISTrihedron.hxx>
45 #include <GEOM_VTKTrihedron.hxx>
46 #include <GEOM_VTKPropertyMaterial.hxx>
47
48 #include <GEOMGUI_DimensionProperty.h>
49 #include <GEOMGUI_AnnotationAttrs.h>
50 #include <GEOMGUI_AnnotationMgr.h>
51
52 #include <GEOMUtils.hxx>
53
54 #include <Material_Model.h>
55
56 #include <SUIT_Desktop.h>
57 #include <SUIT_ViewWindow.h>
58 #include <SUIT_Session.h>
59 #include <SUIT_ViewManager.h>
60 #include <SUIT_ResourceMgr.h>
61
62 #include <Basics_OCCTVersion.hxx>
63
64 #include <SalomeApp_Study.h>
65 #include <SalomeApp_Application.h>
66 #include <LightApp_SelectionMgr.h>
67 #include <LightApp_DataObject.h>
68 #include <SalomeApp_TypeFilter.h>
69 #include <SalomeApp_Tools.h>
70 #include "utilities.h"
71
72 #include <SALOME_ListIO.hxx>
73 #include <SALOME_Prs.h>
74 #include "utilities.h"
75
76 #include <SOCC_Prs.h>
77 #include <SOCC_ViewModel.h>
78
79 #include <SVTK_Prs.h>
80 #include <SVTK_ViewModel.h>
81
82 #include <OCCViewer_ViewWindow.h>
83 #include <OCCViewer_ViewPort3d.h>
84 #include <OCCViewer_Utilities.h>
85
86 // OCCT Includes
87 #include <AIS_Dimension.hxx>
88 #include <AIS_LengthDimension.hxx>
89 #include <AIS_DiameterDimension.hxx>
90 #include <AIS_AngleDimension.hxx>
91 #include <AIS_ListIteratorOfListOfInteractive.hxx>
92 #include <Aspect_PolygonOffsetMode.hxx>
93 #if OCC_VERSION_MAJOR < 7
94   #include <Aspect_ColorScale.hxx>
95 #endif
96 #include <Prs3d_IsoAspect.hxx>
97 #include <Prs3d_PointAspect.hxx>
98 #include <StdSelect_TypeOfEdge.hxx>
99 #include <StdSelect_TypeOfFace.hxx>
100 #include <StdSelect_DisplayMode.hxx>
101 #include <TopoDS_Face.hxx>
102 #include <BRep_Tool.hxx>
103 #include <Geom_Plane.hxx>
104 #include <Geom_Axis2Placement.hxx>
105 #include <Graphic3d_AspectFillArea3d.hxx>
106 #include <gp_Pln.hxx>
107 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
108 #include <TopoDS_Iterator.hxx>
109 #include <Graphic3d_AspectMarker3d.hxx>
110 #include <TopTools_MapOfShape.hxx>
111 #include <TopTools_ListOfShape.hxx>
112 #include <TopTools_ListIteratorOfListOfShape.hxx>
113 #include <TopoDS.hxx>
114 #include <NCollection_DataMap.hxx>
115 #include <NCollection_Map.hxx>
116
117 #include <Prs3d_ShadingAspect.hxx>
118
119 #include <BRepMesh_IncrementalMesh.hxx>
120
121 // VTK Includes
122 #include <vtkActorCollection.h>
123 #include <vtkProperty.h>
124
125 // CORBA Headers
126 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
127
128 #include <GEOMImpl_Types.hxx>
129
130 #include <TColStd_HArray1OfByte.hxx>
131
132 // If the next macro is defined, autocolor feature works for all sub-shapes;
133 // if it is undefined, autocolor feature works for groups only
134 #define GENERAL_AUTOCOLOR
135 // Below macro, when uncommented, switches on simplified (more performant) algorithm
136 // of auto-color picking up
137 #define SIMPLE_AUTOCOLOR
138
139 // Hard-coded value of shape deflection coefficient for VTK viewer
140 const double VTK_MIN_DEFLECTION = 0.001;
141 // If the next macro is defined, the deflection coefficient for VTK presentation
142 // is limited by VTK_MIN_DEFLECTION
143 //#define LIMIT_DEFLECTION_FOR_VTK
144
145 // Pixmap caching support
146 namespace
147 {
148   typedef NCollection_Map<Handle(GEOM_AISShape)>                    SetOfAISShapes;
149   typedef NCollection_DataMap<Handle(Image_PixMap), SetOfAISShapes> PixmapUsageMap;
150   typedef QMap<QString, Handle(Image_PixMap)>                       PixmapCacheMap;
151
152   static inline PixmapUsageMap& getPixmapUsageMap()
153   {
154     static PixmapUsageMap aMap;
155     return aMap;
156   }
157
158   static inline PixmapCacheMap& getPixmapCacheMap()
159   {
160     static PixmapCacheMap aMap;
161     return aMap;
162   }
163
164   //===========================================================================
165   // Function : getDefaultTexture
166   // Purpose  : Get default texture
167   //===========================================================================
168   static inline Handle(Image_PixMap) getDefaultTexture()
169   {
170     static Handle(Image_PixMap) aPixmap;
171     if ( aPixmap.IsNull() ) {
172       QPixmap px(":images/default_texture.png");
173       if ( !px.isNull() )
174         aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage() );
175     }
176     return aPixmap;
177   }
178
179   //===========================================================================
180   // Function : cacheTextureFor
181   // Purpose  : Load and cache image for the specified presentation.
182   //===========================================================================
183   static inline Handle(Image_PixMap) cacheTextureFor( const QString& thePath,
184                                                       const Handle(GEOM_AISShape)& theShape )
185   {
186     if ( thePath.isEmpty() )
187       return NULL;
188
189     PixmapUsageMap& aPixmapUsersMap = getPixmapUsageMap();
190     PixmapCacheMap& aPixmapCacheMap = getPixmapCacheMap();
191
192     Handle(Image_PixMap) aPixmap = aPixmapCacheMap.value( thePath, NULL );
193     if ( !aPixmap.IsNull() ) {
194       // point that the texture is used by the presentation
195       if ( !aPixmapUsersMap.IsBound( aPixmap ) )
196         aPixmapUsersMap.Bind( aPixmap, SetOfAISShapes() );
197
198       aPixmapUsersMap.ChangeFind( aPixmap ).Add( theShape );
199
200       return aPixmap;
201     }
202
203     // convert texture to compatible image format
204     QImage anImage = QImage( thePath ).convertToFormat( QImage::Format_ARGB32 );
205     if ( anImage.isNull() )
206       return NULL;
207
208     aPixmap = OCCViewer_Utilities::imageToPixmap( anImage );
209
210     aPixmapCacheMap.insert( thePath, aPixmap );
211
212     if ( !aPixmapUsersMap.IsBound( aPixmap ) )
213       aPixmapUsersMap.Bind( aPixmap, SetOfAISShapes() );
214
215     aPixmapUsersMap.ChangeFind( aPixmap ).Add( theShape );
216
217     return aPixmap;
218   }
219
220   //===========================================================================
221   // Function : releaseTextures
222   // Purpose  : Releases cached textures found for the specified presentation.
223   //===========================================================================
224   static inline void releaseTextures( const SALOME_OCCPrs* thePrs )
225   {
226     const SOCC_Prs* anOccPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
227
228     AIS_ListOfInteractive aListOfIO;
229
230     anOccPrs->GetObjects( aListOfIO );
231
232     AIS_ListIteratorOfListOfInteractive aIterateIO( aListOfIO );
233
234     PixmapUsageMap& aPixmapUsersMap = getPixmapUsageMap();
235     PixmapCacheMap& aPixmapCacheMap = getPixmapCacheMap();
236
237     for ( ; aIterateIO.More(); aIterateIO.Next() )
238     {
239       Handle(GEOM_AISShape) aAISShape =
240         Handle(GEOM_AISShape)::DownCast( aIterateIO.Value() );
241
242       if ( aAISShape.IsNull() )
243         continue;
244
245 #ifdef USE_TEXTURED_SHAPE
246       const Handle(Image_PixMap)& aPixmap = aAISShape->TexturePixMap();
247       if ( aPixmap.IsNull() )
248         continue;
249
250       if ( !aPixmapUsersMap.IsBound( aPixmap ) )
251         continue;
252
253       SetOfAISShapes& aUsersShapes = aPixmapUsersMap.ChangeFind( aPixmap );
254
255       aUsersShapes.Remove( aAISShape );
256
257       if ( aUsersShapes.IsEmpty() ) {
258         aPixmapUsersMap.UnBind( aPixmap );
259         aPixmapCacheMap.remove( aPixmapCacheMap.key( aPixmap ) );
260       }
261 #endif
262     }
263   }
264
265   uint randomize( uint size )
266   {
267     static bool initialized = false;
268     if ( !initialized ) {
269       qsrand( QDateTime::currentDateTime().toTime_t() );
270       initialized = true;
271     }
272     uint v = qrand();
273     v = uint( (double)( v ) / RAND_MAX * size );
274     v = qMax( uint(0), qMin ( v, size-1 ) );
275     return v;
276   }
277 } // namespace
278
279 //================================================================
280 // Function : getActiveStudy
281 // Purpose  : Get active study, returns 0 if no open study frame
282 //================================================================
283 static inline SalomeApp_Study* getActiveStudy()
284 {
285   SUIT_Session* session = SUIT_Session::session();
286   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
287   if ( app )
288     return ( SalomeApp_Study* )app->activeStudy();
289   return 0;
290 }
291
292 static inline int getViewManagerId( SALOME_View* theViewFrame) {
293   SUIT_ViewModel* aModel = dynamic_cast<SUIT_ViewModel*>(theViewFrame);
294   SUIT_ViewManager* aViewMgr = 0;
295   if (aModel != 0)
296     aViewMgr = aModel->getViewManager();
297   return ((aViewMgr == 0) ? -1 :aViewMgr->getGlobalId());
298 }
299
300 //================================================================
301 // Function : getTopAbsMode
302 // Purpose  : Get TopAbs_ShapeEnum value corresponding to the
303 //            one from GEOMImpl_Types.h
304 //================================================================
305 static inline int getTopAbsMode( const int implType )
306 {
307   switch ( implType )
308   {
309     case GEOM_COMPOUND  : return TopAbs_COMPOUND;
310     case GEOM_SOLID     : return TopAbs_SOLID;
311     case GEOM_SHELL     : return TopAbs_SHELL;
312     case GEOM_FACE      : return TopAbs_FACE;
313     case GEOM_WIRE      : return TopAbs_WIRE;
314     case GEOM_EDGE      : return TopAbs_EDGE;
315     case GEOM_POINT     : return TopAbs_VERTEX;
316     default             : return -1;
317   }
318 }
319
320 int GEOM_Displayer::getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
321 {
322   if ( shape.IsNull() )
323     return TopAbs_SHAPE;
324
325   int ret = shape.ShapeType();
326
327   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
328     TopoDS_Iterator it(shape, Standard_True, Standard_False);
329     for (; it.More(); it.Next()) {
330       TopoDS_Shape sub_shape = it.Value();
331       if ( sub_shape.IsNull() ) continue;
332       int stype = getMinMaxShapeType( sub_shape, ismin );
333       if ( stype == TopAbs_SHAPE ) continue;
334       if ( ismin && stype > ret )
335         ret = stype;
336       else if ( !ismin && ( ret < TopAbs_SOLID || stype < ret ) )
337         ret = stype;
338     }
339   }
340
341   return ret;
342 }
343
344 bool GEOM_Displayer::isCompoundOfVertices( const TopoDS_Shape& theShape )
345 {
346   return theShape.ShapeType() == TopAbs_COMPOUND && getMinMaxShapeType( theShape, false ) == TopAbs_VERTEX;
347 }
348
349 //================================================================
350 // Function : getFilter
351 // Purpose  : Get filter corresponding to the type of object
352 //            from GEOMImpl_Types.h
353 //================================================================
354 SUIT_SelectionFilter* GEOM_Displayer::getFilter( const int theMode )
355 {
356   SUIT_SelectionFilter* aFilter;
357
358   int aTopAbsMode = getTopAbsMode( theMode );
359   if ( aTopAbsMode != -1 )
360     aFilter = new GEOM_TypeFilter( getStudy(), aTopAbsMode, true ); //@ aFilter = new GEOM_TypeFilter( ( TopAbs_ShapeEnum )aTopAbsMode );
361   else
362     switch ( theMode )
363       {
364       case GEOM_LINE      : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Line ); break;
365       case GEOM_CIRCLE    : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Circle ); break;
366
367       case GEOM_PLANE     : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Plane ); break;
368       case GEOM_CYLINDER  : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cylinder ); break;
369       case GEOM_SPHERE    : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Sphere ); break;
370       case GEOM_TORUS     : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Torus ); break;
371       case GEOM_REVOLUTION: aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Revol ); break;
372       case GEOM_CONE      : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cone ); break;
373
374       case GEOM_PREVIEW   : aFilter = new GEOM_PreviewFilter( getStudy() ); break;
375
376       case GEOM_ALLSHAPES : aFilter = new GEOM_SelectionFilter(getStudy(), true ); break;
377       case GEOM_ALLGEOM   : aFilter = new SalomeApp_TypeFilter( getStudy(), "GEOM" ); break;
378
379       default             : aFilter = new GEOM_TypeFilter( getStudy(), theMode ); break;
380       }
381
382   return aFilter;
383 }
384
385 //================================================================
386 // Function : getComplexFilter
387 // Purpose  : Get compound filter corresponding to the type of
388 //            object from GEOMImpl_Types.h
389 //================================================================
390 SUIT_SelectionFilter* GEOM_Displayer::getComplexFilter( const QList<int>* aSubShapes)
391 {
392   GEOM_CompoundFilter* aFilter;
393
394   if(aSubShapes != NULL ) {
395     aFilter = new GEOM_CompoundFilter(getStudy());
396     QList<int> aTopAbsTypes;
397     QList<int>::const_iterator it;
398     for(it = aSubShapes->constBegin(); it != aSubShapes->constEnd(); ++it ) {
399       int topAbsMode = getTopAbsMode(*it);
400       if(topAbsMode != -1 )
401         aTopAbsTypes.append(topAbsMode);
402     }
403     aFilter->addSubTypes(aTopAbsTypes);
404   }
405
406   return aFilter;
407 }
408
409 //================================================================
410 // Function : getEntry
411 // Purpose  :
412 //================================================================
413 static std::string getEntry( GEOM::GEOM_BaseObject_ptr object )
414 {
415   SUIT_Session* session = SUIT_Session::session();
416   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
417   if ( app )
418   {
419     CORBA::String_var IOR = app->orb()->object_to_string( object );
420     if ( strcmp(IOR.in(), "") != 0 )
421     {
422       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
423       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
424       if ( SO )
425         return SO->GetID();
426     }
427   }
428   return "";
429 }
430
431 //================================================================
432 // Function : getName
433 // Purpose  :
434 //================================================================
435 static std::string getName( GEOM::GEOM_BaseObject_ptr object )
436 {
437   SUIT_Session* session = SUIT_Session::session();
438   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
439   if ( app )
440   {
441     CORBA::String_var IOR = app->orb()->object_to_string( object );
442     if ( strcmp(IOR.in(), "") != 0 )
443     {
444       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
445       _PTR(SObject) aSObj ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
446
447       _PTR(GenericAttribute) anAttr;
448
449       if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
450       {
451         _PTR(AttributeName) aNameAttr( anAttr );
452         return aNameAttr->Value();
453       }
454     }
455   }
456
457   return "";
458 }
459
460 //=================================================================
461 /*!
462  *  GEOM_Displayer::GEOM_Displayer
463  *  Constructor
464  */
465 //=================================================================
466 GEOM_Displayer::GEOM_Displayer( SalomeApp_Study* st )
467 : myIsRedisplayed( false )
468 {
469   if( st )
470     myApp = dynamic_cast<SalomeApp_Application*>( st->application() );
471   else
472     myApp = 0;
473
474   /* Shading Color */
475   SUIT_Session* session = SUIT_Session::session();
476   SUIT_ResourceMgr* resMgr = session->resourceMgr();
477
478   QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
479   myShadingColor = SalomeApp_Tools::color( col );
480
481   myDisplayMode = resMgr->integerValue("Geometry", "display_mode", 0);
482   myHasDisplayMode = false;
483
484   int aType = resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS);
485   
486   myTypeOfMarker = (Aspect_TypeOfMarker)(std::min((int)Aspect_TOM_RING3, std::max((int)Aspect_TOM_POINT, aType)));
487   myScaleOfMarker = (resMgr->integerValue("Geometry", "marker_scale", 1)-(int)GEOM::MS_10)*0.5 + 1.0;
488   myScaleOfMarker = std::min(7.0, std::max(1., myScaleOfMarker));
489
490   // Next properties provide a way to customize displaying of presentations;
491   // for instance, this is useful for preview
492   myColor = -1;
493   myTexture = "";
494   myNbIsos = -1;
495   myWidth = -1;
496   myTransparency = -1;
497   myType = -1;
498   myIsosColor = -1;
499   myIsosWidth = -1;
500
501   // This parameter is used for activisation/deactivisation (selection) of objects to be displayed
502   myToActivate = true;
503
504   // Activate parallel vizualisation by default.
505   // It can be switched OFF via the environment variable:
506   //     export PARALLEL_VISU=0
507   Standard_Boolean parallel_visu = Standard_True;
508   char* parallel_visu_env = getenv("PARALLEL_VISU");
509   if (parallel_visu_env && atoi(parallel_visu_env) == 0)
510     parallel_visu = Standard_False;
511   BRepMesh_IncrementalMesh::SetParallelDefault(parallel_visu);
512
513   myViewFrame = 0;
514
515 #if OCC_VERSION_MAJOR >= 7
516   myColorScale = new AIS_ColorScale;
517   myColorScale->SetZLayer (Graphic3d_ZLayerId_TopOSD);
518   myColorScale->SetTransformPersistence (
519     Graphic3d_TransformPers::FromDeprecatedParams(Graphic3d_TMF_2d, gp_Pnt (-1,-1,0)));
520 #endif
521
522   myFieldDataType = GEOM::FDT_Double;
523   myFieldDimension = 0;
524   myFieldStepRangeMin = 0;
525   myFieldStepRangeMax = 0;
526 }
527
528 //=================================================================
529 /*!
530  *  GEOM_Displayer::~GEOM_Displayer
531  *  Destructor
532  */
533 //=================================================================
534 GEOM_Displayer::~GEOM_Displayer()
535 {
536 }
537
538 //=================================================================
539 /*!
540  *  GEOM_Displayer::Display
541  *  Display interactive object in the current viewer
542  */
543 //=================================================================
544 void GEOM_Displayer::Display( const Handle(SALOME_InteractiveObject)& theIO,
545                              const bool updateViewer,
546                              SALOME_View* theViewFrame )
547 {
548   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
549   if ( vf )
550   {
551     SALOME_Prs* prs = buildPresentation( theIO->getEntry(), vf );
552
553     if ( prs )
554     {
555       vf->BeforeDisplay( this, prs );
556       vf->Display( this, prs );
557       vf->AfterDisplay( this, prs );
558
559       if ( updateViewer )
560         vf->Repaint();
561
562       delete prs;  // delete presentation because displayer is its owner
563     }
564   }
565 }
566
567 //=================================================================
568 /*!
569  *  GEOM_Displayer::Display
570  *  This overloaded Display() method can be useful for operations
571  *  not using dialog boxes.
572  */
573 //=================================================================
574 void GEOM_Displayer::Display( GEOM::GEOM_BaseObject_ptr theObj, const bool updateViewer )
575 {
576   if ( theObj->_is_nil() )
577     return;
578
579   std::string entry = getEntry( theObj );
580   if ( entry != "" ) {
581     Display(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
582             updateViewer);
583   }
584 }
585
586 //=================================================================
587 /*!
588  *  GEOM_Displayer::Erase
589  *  Erase interactive object in the current viewer
590  */
591 //=================================================================
592 void GEOM_Displayer::Erase( const Handle(SALOME_InteractiveObject)& theIO,
593                             const bool forced,
594                             const bool updateViewer,
595                             SALOME_View* theViewFrame )
596 {
597   if ( theIO.IsNull() )
598     return;
599
600   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
601
602   if ( vf ) {
603     SALOME_Prs* prs = vf->CreatePrs( theIO->getEntry() );
604     if ( prs ) {
605       vf->BeforeErase( this, prs );
606       vf->Erase( this, prs, forced );
607       vf->AfterErase( this, prs );
608       if ( updateViewer )
609         vf->Repaint();
610       delete prs;  // delete presentation because displayer is its owner
611     }
612   }
613 }
614
615 //=================================================================
616 /*!
617  *  GEOM_Displayer::Erase
618  *  Erase geometry object in the current viewer
619  */
620 //=================================================================
621 void GEOM_Displayer::Erase( GEOM::GEOM_BaseObject_ptr theObj,
622                             const bool forced,
623                             const bool updateViewer,
624                             SALOME_View* theViewFrame)
625 {
626   std::string entry = getEntry( theObj );
627   if ( entry != "" )
628   {
629     Erase(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
630           forced, updateViewer, theViewFrame);
631   }
632 }
633
634 //=================================================================
635 /*!
636  *  GEOM_Displayer::Redisplay
637  *  Redisplay (erase and then display again) interactive object
638  *  in the current viewer
639  */
640 //=================================================================
641 void GEOM_Displayer::Redisplay( const Handle(SALOME_InteractiveObject)& theIO,
642                                 const bool updateViewer,
643                                 const bool checkActiveViewer )
644 {
645   bool aRedisplayed = myIsRedisplayed;
646   myIsRedisplayed = true;
647   // Remove the object permanently (<forced> == true)
648   SUIT_Session* ses = SUIT_Session::session();
649   SUIT_Application* app = ses->activeApplication();
650   if ( app )
651   {
652     SUIT_Desktop* desk = app->desktop();
653     QList<SUIT_ViewWindow*> wnds = desk->windows();
654     SUIT_ViewWindow* wnd;
655     QListIterator<SUIT_ViewWindow*> it( wnds );
656     while ( it.hasNext() && (wnd = it.next()) )
657     {
658       SUIT_ViewManager* vman = wnd->getViewManager();
659       if ( vman )
660       {
661         SUIT_ViewModel* vmodel = vman->getViewModel();
662         if ( vmodel )
663         {
664           SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
665           if ( view )
666           {
667             if ( view->isVisible( theIO ) || ( checkActiveViewer && view == GetActiveView() ) )
668             {
669               Redisplay( theIO, updateViewer, view );
670             }
671           }
672         }
673       }
674     }
675   }
676   myIsRedisplayed = aRedisplayed;
677 }
678
679 //=================================================================
680 /*!
681  *  GEOM_Displayer::Redisplay
682  *  Redisplay (erase and then display again) interactive object
683  *  in the specified view
684  */
685 //=================================================================
686 void GEOM_Displayer::Redisplay( const Handle(SALOME_InteractiveObject)& theIO,
687                                 const bool theUpdateViewer,
688                                 SALOME_View* theViewFrame )
689 {
690   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
691   if ( !vf )
692   {
693     return;
694   }
695
696   bool aRedisplayed = myIsRedisplayed;
697   myIsRedisplayed = true;
698   Erase( theIO, true, false, theViewFrame );
699   Display( theIO, theUpdateViewer, theViewFrame );
700   myIsRedisplayed = aRedisplayed;
701   // hide annotations for erased presentation
702   SUIT_Session* session = SUIT_Session::session();
703   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
704   GeometryGUI* aModule = dynamic_cast<GeometryGUI*>( anApp->activeModule() );
705   if ( aModule ) {
706     if ( !theViewFrame->isVisible( theIO ) ) {
707       aModule->GetAnnotationMgr()->EraseVisibleAnnotations(QString(theIO->getEntry()), dynamic_cast<SOCC_Viewer*>( theViewFrame ));
708     }
709     else {
710       aModule->GetAnnotationMgr()->DisplayVisibleAnnotations(QString(theIO->getEntry()), dynamic_cast<SOCC_Viewer*>( theViewFrame ));
711     }
712   }
713 }
714
715 //=================================================================
716 /*!
717  *  GEOM_Displayer::Display
718  *  Calls Display() method for each object in the given list
719  */
720 //=================================================================
721 void GEOM_Displayer::Display( const SALOME_ListIO& theIOList, const bool updateViewer )
722 {
723   SALOME_ListIteratorOfListIO Iter( theIOList );
724   for ( ; Iter.More(); Iter.Next() ) {
725     Display( Iter.Value(), false );
726   }
727   if ( updateViewer )
728     UpdateViewer();
729 }
730
731 void GEOM_Displayer::UpdateVisibility( SALOME_View* v, const SALOME_Prs* p, bool on )
732 {
733   SalomeApp_Study* aStudy = getStudy();
734   int vId = -1;
735   if ( v ) vId = getViewManagerId( v );
736
737   if ( p ) {
738     QString entry = p->GetEntry();
739     if ( !entry.isEmpty() ) {
740       if ( vId != -1 )
741         aStudy->setObjectProperty( vId, entry, GEOM::propertyName( GEOM::Visibility ), on );
742       setVisibilityState( entry, on ? Qtx::ShownState : Qtx::HiddenState );
743     }
744   }
745   else {
746     if ( vId != -1 ) {
747       aStudy->setObjectProperty( vId, GEOM::propertyName( GEOM::Visibility ), on );
748     }
749   }
750 }
751
752 Quantity_Color GEOM_Displayer::qColorFromResources( const QString& property, const QColor& defColor )
753 {
754   // VSR: this method can be improved in future:
755   // to improve performance, the default values from resource manager should be cached in the displayer
756   return SalomeApp_Tools::color( SUIT_Session::session()->resourceMgr()->colorValue( "Geometry", property, defColor ) );
757 }
758
759 QColor GEOM_Displayer::colorFromResources( const QString& property, const QColor& defColor )
760 {
761   // VSR: this method can be improved in future:
762   // to improve performance, the default values from resource manager should be cached in the displayer
763   return SUIT_Session::session()->resourceMgr()->colorValue( "Geometry", property, defColor );
764 }
765
766 void GEOM_Displayer::updateShapeProperties( const Handle(GEOM_AISShape)& AISShape, bool create )
767 {
768   // check that shape is not null
769   if ( AISShape.IsNull() ) return;
770   
771   // check that study is active
772   SalomeApp_Study* study = getStudy();
773   if ( !study ) return;
774
775   if ( myShape.ShapeType() != TopAbs_VERTEX && // fix pb with not displayed points
776        !TopoDS_Iterator(myShape).More() )
777     return; // NPAL15983 (Bug when displaying empty groups)
778
779   // set interactive object
780
781   Handle( SALOME_InteractiveObject ) anIO;
782
783   if ( !myIO.IsNull() ) {
784     AISShape->setIO( myIO );
785     AISShape->SetOwner( myIO );
786     anIO = myIO;
787   }
788   else if ( !myName.empty() ) {
789     // workaround to allow selection of temporary objects
790     static int tempId = 0;
791     anIO = new SALOME_InteractiveObject( QString( "TEMP_%1" ).arg( tempId++ ).toLatin1().data(), "GEOM", myName.c_str() );
792     AISShape->setIO( anIO );
793     AISShape->SetOwner( anIO );
794   }
795
796   // flag:  only vertex or compound of vertices is processed (specific handling)
797   bool onlyVertex = myShape.ShapeType() == TopAbs_VERTEX || isCompoundOfVertices( myShape );
798   // presentation study entry (empty for temporary objects like preview)
799   QString entry = !anIO.IsNull() ? QString( anIO->getEntry() ) : QString();
800   // flag: temporary object
801   bool isTemporary = entry.isEmpty() || entry.startsWith( "TEMP_" );
802   // currently active view window's ID (-1 if no active view)
803   int aMgrId = !anIO.IsNull() ? getViewManagerId( myViewFrame ) : -1;
804
805   // get presentation properties
806   PropMap propMap = getObjectProperties( study, entry, myViewFrame );
807
808   // Temporary staff: vertex must be infinite for correct visualization
809   AISShape->SetInfiniteState( myShape.Infinite() ); // || myShape.ShapeType() == TopAbs_VERTEX // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
810
811   // set material
812   Material_Model material;
813   // if predefined color isn't set in displayer(via GEOM_Displayer::SetColor() function)
814   if( !HasColor() )
815     material.fromProperties( propMap.value( GEOM::propertyName( GEOM::Material ) ).toString() );
816   // - set front material properties
817   AISShape->SetCurrentFacingModel( Aspect_TOFM_FRONT_SIDE );
818   AISShape->SetMaterial( material.getMaterialOCCAspect( true ) );
819   // - set back material properties
820   AISShape->SetCurrentFacingModel( Aspect_TOFM_BACK_SIDE );
821   AISShape->SetMaterial( material.getMaterialOCCAspect( false ) );
822   // - switch to default (both sides) facing mode
823   AISShape->SetCurrentFacingModel( Aspect_TOFM_BOTH_SIDE );
824
825   // set colors
826
827   // - shading color
828   if ( HasColor()  ) {
829     // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function;
830     // we set it to the shape not taking into account material properties
831     AISShape->SetShadingColor( (Quantity_NameOfColor)GetColor() );
832   }
833   else if ( !material.isPhysical() ) {
834     // shading color from properties is used only for non-physical materials
835     AISShape->SetShadingColor( SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::ShadingColor ) ).value<QColor>() ) );
836   }
837
838   // - wireframe color
839   Handle(Prs3d_LineAspect) anAspect = AISShape->Attributes()->LineAspect();
840   anAspect->SetColor( HasColor() ? (Quantity_NameOfColor)GetColor() : 
841                       SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::WireframeColor ) ).value<QColor>() ) );
842   AISShape->Attributes()->SetLineAspect( anAspect );
843   
844   // - unfree boundaries color
845   anAspect = AISShape->Attributes()->UnFreeBoundaryAspect();
846   anAspect->SetColor( HasColor() ? (Quantity_NameOfColor)GetColor() : 
847                       SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::WireframeColor ) ).value<QColor>() ) );
848   AISShape->Attributes()->SetUnFreeBoundaryAspect( anAspect );
849   
850   // - free boundaries color
851   anAspect = AISShape->Attributes()->FreeBoundaryAspect();
852   anAspect->SetColor( HasColor() ? (Quantity_NameOfColor)GetColor() : 
853                       SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::FreeBndColor ) ).value<QColor>() ) );
854   AISShape->Attributes()->SetFreeBoundaryAspect( anAspect );
855   
856   // - standalone edges color
857   anAspect = AISShape->Attributes()->WireAspect();
858   anAspect->SetColor( HasColor() ? (Quantity_NameOfColor)GetColor() : 
859                       SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::LineColor ) ).value<QColor>() ) );
860   AISShape->Attributes()->SetWireAspect( anAspect );
861   
862   // - color for edges in shading+edges mode
863   AISShape->SetEdgesInShadingColor( SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::OutlineColor ) ).value<QColor>() ) );
864
865   // - color of labels (textual fields and shape name)
866   AISShape->SetLabelColor( qColorFromResources( "label_color", QColor( 255, 255, 255 ) ) );
867
868   // set display mode
869   AISShape->SetDisplayMode( HasDisplayMode() ? 
870                             // predefined display mode, manually set to displayer via GEOM_Displayer::SetDisplayMode() function 
871                             GetDisplayMode() :
872                             // display mode from properties
873                             propMap.value( GEOM::propertyName( GEOM::DisplayMode ) ).toInt() );
874
875   // - face boundaries color and line width
876   anAspect = AISShape->Attributes()->FaceBoundaryAspect();
877   anAspect->SetColor( SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::OutlineColor ) ).value<QColor>() ) );
878   anAspect->SetWidth( HasWidth() ? GetWidth() : propMap.value( GEOM::propertyName( GEOM::LineWidth ) ).toInt() );
879   AISShape->Attributes()->SetFaceBoundaryAspect( anAspect );
880
881   // set display vectors flag
882   AISShape->SetDisplayVectors( propMap.value( GEOM::propertyName( GEOM::EdgesDirection ) ).toBool() );
883
884   // set display vertices flag
885   bool isVerticesMode = propMap.value( GEOM::propertyName( GEOM::Vertices ) ).toBool();
886   AISShape->SetDisplayVertices( isVerticesMode );
887
888   // set display name flag
889   bool isNameMode = propMap.value( GEOM::propertyName( GEOM::ShowName ) ).toBool();
890   AISShape->SetDisplayName( isNameMode );
891
892   // set transparency
893   if( HasTransparency() ) {
894     AISShape->SetTransparency( GetTransparency() );
895   } else {
896     AISShape->SetTransparency( propMap.value( GEOM::propertyName( GEOM::Transparency ) ).toDouble() );
897   }
898
899   // set iso properties
900   int uIsos = propMap.value( GEOM::propertyName( GEOM::NbIsos ) ).toString().split( GEOM::subSectionSeparator() )[0].toInt();
901   int vIsos = propMap.value( GEOM::propertyName( GEOM::NbIsos ) ).toString().split( GEOM::subSectionSeparator() )[1].toInt();
902   Quantity_Color isosColor = SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::IsosColor ) ).value<QColor>() );
903   int isosWidth = propMap.value( GEOM::propertyName( GEOM::IsosWidth ) ).toInt();
904   Handle(Prs3d_IsoAspect) uIsoAspect = AISShape->Attributes()->UIsoAspect();
905   Handle(Prs3d_IsoAspect) vIsoAspect = AISShape->Attributes()->VIsoAspect();
906
907   if ( HasIsosColor() ) {
908     uIsoAspect->SetColor( (Quantity_NameOfColor)GetIsosColor() );
909     vIsoAspect->SetColor( (Quantity_NameOfColor)GetIsosColor() );
910   }
911   else {
912     uIsoAspect->SetColor( isosColor );
913     vIsoAspect->SetColor( isosColor );
914   }
915
916   if ( HasIsosWidth() ) {
917     uIsoAspect->SetWidth( GetIsosWidth() );
918     vIsoAspect->SetWidth( GetIsosWidth() );
919   }
920   else {
921     uIsoAspect->SetWidth( isosWidth );
922     vIsoAspect->SetWidth( isosWidth );
923   }
924   
925   if ( HasNbIsos() ) {
926     uIsoAspect->SetNumber( GetNbIsos() );
927     vIsoAspect->SetNumber( GetNbIsos() );
928   }
929   else {
930     uIsoAspect->SetNumber( uIsos );
931     vIsoAspect->SetNumber( vIsos );
932   }
933
934   AISShape->Attributes()->SetUIsoAspect( uIsoAspect );
935   AISShape->Attributes()->SetVIsoAspect( vIsoAspect );
936
937   // set deflection coefficient
938   // ... to avoid to small values of the coefficient, its lower value is limited 
939   AISShape->SetOwnDeviationCoefficient( qMax( propMap.value( GEOM::propertyName( GEOM::Deflection ) ).toDouble(), GEOM::minDeflection() ) );
940
941   // set texture
942   QString aImagePath;
943   if ( HasTexture() ) {
944     // predefined display texture, manually set to displayer via GEOM_Displayer::SetTexture() function 
945     aImagePath = GetTexture().c_str();
946     if ( ! entry.isEmpty() ) {
947       // check that study is active
948       SalomeApp_Study* study = getActiveStudy();
949       if ( study ) {
950         // Store the texture in object properties for next displays
951         study->setObjectProperty( aMgrId, entry, GEOM::propertyName( GEOM::Texture ), QString( GetTexture().c_str() ) );
952         study->setObjectProperty( aMgrId, entry, GEOM::propertyName( GEOM::DisplayMode ), 3 );
953         
954         // Update propeties map
955         propMap = getObjectProperties( study, entry, myViewFrame );
956       }
957     }
958   }
959   else {
960     aImagePath = propMap.value( GEOM::propertyName( GEOM::Texture ) ).toString();
961   }
962
963 #ifdef USE_TEXTURED_SHAPE
964   Handle(Image_PixMap) aPixmap;
965   if ( !aImagePath.isEmpty() )
966     aPixmap = cacheTextureFor( aImagePath, AISShape );
967   else
968     aPixmap = getDefaultTexture();
969
970   // apply image to shape
971   if ( !aPixmap.IsNull() ) {
972     AISShape->SetTexturePixMap( aPixmap );
973     AISShape->SetTextureMapOn();
974     AISShape->DisableTextureModulate();
975   }
976   else {
977     AISShape->SetTextureMapOff();
978   }
979 #endif
980
981   // set line width
982   AISShape->SetWidth( HasWidth() ?
983                       // predefined line width, manually set to displayer via GEOM_Displayer::SetLineWidth() function 
984                       GetWidth() :
985                       // libe width from properties
986                       propMap.value( GEOM::propertyName( GEOM::LineWidth ) ).toInt() );
987
988   // set top-level flag
989   AISShape->setTopLevel( propMap.value( GEOM::propertyName( GEOM::TopLevel ) ).toBool() );
990
991   // set point marker (for vertex / compound of vertices only)
992   if ( onlyVertex || isVerticesMode ) {
993     QStringList aList = propMap.value( GEOM::propertyName( GEOM::PointMarker ) ).toString().split( GEOM::subSectionSeparator() );
994     if ( aList.size() == 2 ) {
995       // standard marker string contains "TypeOfMarker:ScaleOfMarker"
996       int aTypeOfMarker = aList[0].toInt();
997       double aScaleOfMarker = (aList[1].toInt() + 1) * 0.5;
998       Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
999       anAspect->SetScale( aScaleOfMarker );
1000       anAspect->SetTypeOfMarker( (Aspect_TypeOfMarker)( aTypeOfMarker-1 ) );
1001       anAspect->SetColor( HasColor() ? 
1002                           // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
1003                           (Quantity_NameOfColor)GetColor() : 
1004                           // color from properties
1005                           SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::PointColor ) ).value<QColor>() ) );
1006       AISShape->Attributes()->SetPointAspect( anAspect );
1007     }
1008     else if ( aList.size() == 1 ) {
1009       // custom marker string contains "IdOfTexture"
1010       int textureId = aList[0].toInt();
1011       Standard_Integer aWidth, aHeight;
1012       Handle(TColStd_HArray1OfByte) aTexture =
1013         GeometryGUI::getTexture( study, textureId, aWidth, aHeight );
1014       if ( !aTexture.IsNull() ) {
1015         Handle(Prs3d_PointAspect) aTextureAspect =
1016           new Prs3d_PointAspect( HasColor() ? 
1017                                  // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
1018                                  (Quantity_NameOfColor)GetColor() : 
1019                                  // color from properties 
1020                                  SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::PointColor ) ).value<QColor>() ),
1021                                  aWidth, aHeight,
1022                                  aTexture );
1023         AISShape->Attributes()->SetPointAspect( aTextureAspect );
1024       }
1025     }
1026   }
1027
1028   // set field step data
1029   AISShape->setFieldStepInfo( myFieldDataType,
1030                               myFieldDimension,
1031                               myFieldStepData,
1032                               myFieldStepName,
1033                               myFieldStepRangeMin,
1034                               myFieldStepRangeMax );
1035
1036   if ( create && !isTemporary && aMgrId != -1 ) {
1037     // set properties to the study
1038     study->setObjectProperties( aMgrId, entry, propMap );
1039   }
1040
1041   // AISShape->SetName(???); ??? necessary to set name ???
1042 }
1043
1044 void GEOM_Displayer::updateActorProperties( GEOM_Actor* actor, bool create )
1045 {
1046   // check that actor is not null
1047   if ( !actor ) return;
1048   
1049   // check that study is active
1050   SalomeApp_Study* study = getStudy();
1051   if ( !study ) return;
1052
1053   // set interactive object
1054
1055   Handle( SALOME_InteractiveObject ) anIO;
1056
1057   if ( !myIO.IsNull() ) {
1058     actor->setIO( myIO );
1059     anIO = myIO;
1060   }
1061   else if ( !myName.empty() ) {
1062     // workaround to allow selection of temporary objects
1063     static int tempId = 0;
1064     anIO = new SALOME_InteractiveObject( QString( "TEMP_VTK_%1" ).arg( tempId++ ).toLatin1().data(), "GEOM", myName.c_str() );
1065     actor->setIO( anIO );
1066   }
1067
1068   // presentation study entry (empty for temporary objects like preview)
1069   QString entry = !anIO.IsNull() ? QString( anIO->getEntry() ) : QString();
1070   // flag: temporary object
1071   bool isTemporary = entry.isEmpty() || entry.startsWith( "TEMP_" );
1072   // currently active view window's ID (-1 if no active view)
1073   int aMgrId = !anIO.IsNull() ? getViewManagerId( myViewFrame ) : -1;
1074
1075   // get presentation properties
1076   PropMap propMap = getObjectProperties( study, entry, myViewFrame );
1077   QColor c;
1078
1079   /////////////////////////////////////////////////////////////////////////
1080   // VSR: for VTK viewer currently deflection coefficient is hardcoded
1081   //      due to performance problem
1082   // actor->SetShape(myShape,aDefPropMap.value(GEOM::propertyName( GEOM::Deflection )).toDouble(),myType == GEOM_VECTOR);
1083   /////////////////////////////////////////////////////////////////////////
1084   if ( !actor->getTopo().IsSame( myShape ) )
1085 #ifdef LIMIT_DEFLECTION_FOR_VTK
1086     actor->SetShape( myShape, VTK_MIN_DEFLECTION, myType == GEOM_VECTOR );
1087 #else
1088     actor->SetShape( myShape, qMax( propMap.value( GEOM::propertyName( GEOM::Deflection ) ).toDouble(), GEOM::minDeflection() ), myType == GEOM_VECTOR );
1089 #endif
1090
1091   // set material
1092   Material_Model material;
1093   material.fromProperties( propMap.value( GEOM::propertyName( GEOM::Material ) ).toString() );
1094   std::vector<vtkProperty*> mprops;
1095   mprops.push_back( material.getMaterialVTKProperty( true ) );
1096   mprops.push_back( material.getMaterialVTKProperty( false) );
1097   actor->SetMaterial( mprops );
1098
1099   // set iso-lines properties
1100
1101   // - set number of iso-lines
1102   int nbIsos[2]= { 1, 1 };
1103   if ( HasNbIsos() ) {
1104     nbIsos[0] = GetNbIsos();
1105     nbIsos[1] = GetNbIsos();
1106   }
1107   else {
1108     QStringList isos = propMap.value( GEOM::propertyName( GEOM::NbIsos ) ).toString().split( GEOM::subSectionSeparator() );
1109     nbIsos[0] = isos[0].toInt();
1110     nbIsos[1] = isos[1].toInt();
1111   }
1112   actor->SetNbIsos( nbIsos );
1113
1114   // - set iso-lines width
1115   actor->SetIsosWidth( HasIsosWidth() ? GetIsosWidth() : propMap.value( GEOM::propertyName( GEOM::IsosWidth ) ).toInt() );
1116
1117   // - set iso-lines color
1118   if ( HasIsosColor() )
1119     c = SalomeApp_Tools::color( Quantity_Color((Quantity_NameOfColor)GetIsosColor()) );
1120   else
1121     c = propMap.value( GEOM::propertyName( GEOM::IsosColor ) ).value<QColor>();
1122   actor->SetIsosColor( c.redF(), c.greenF(), c.blueF() );
1123
1124   // set colors
1125
1126   if ( HasColor()  ) {
1127     // - same color for all sub-actors
1128     Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
1129     actor->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
1130   }
1131   else {
1132     // shading color (for non-physical materials)
1133     if ( !material.isPhysical() ) {
1134       c = propMap.value( GEOM::propertyName( GEOM::ShadingColor ) ).value<QColor>();
1135       actor->GetFrontMaterial()->SetColor( c.redF(), c.greenF(), c.blueF() );
1136       actor->GetBackMaterial()->SetColor( c.redF(), c.greenF(), c.blueF() );
1137     }
1138
1139     // - standalone edge color
1140     c = propMap.value( GEOM::propertyName( GEOM::LineColor ) ).value<QColor>();
1141     actor->SetIsolatedEdgeColor( c.redF(), c.greenF(), c.blueF() );
1142
1143     c = propMap.value( GEOM::propertyName( GEOM::WireframeColor ) ).value<QColor>();
1144     // - shared edges color ???
1145     actor->SetSharedEdgeColor( c.redF(), c.greenF(), c.blueF() );
1146
1147     c = propMap.value( GEOM::propertyName( GEOM::FreeBndColor ) ).value<QColor>();
1148     // - free edges color ???
1149     actor->SetFreeEdgeColor( c.redF(), c.greenF(), c.blueF() );
1150
1151     // - point color
1152     c = propMap.value( GEOM::propertyName( GEOM::PointColor ) ).value<QColor>();
1153     actor->SetPointColor( c.redF(), c.greenF(), c.blueF() );
1154   }
1155
1156   // - color for edges in shading+edges mode
1157   c = propMap.value( GEOM::propertyName( GEOM::OutlineColor ) ).value<QColor>();
1158   actor->SetEdgesInShadingColor( c.redF(), c.greenF(), c.blueF() );
1159
1160   // - color of labels (shape name)
1161   c = colorFromResources( "label_color", QColor( 255, 255, 255 ) );
1162   actor->SetLabelColor( c.redF(), c.greenF(), c.blueF() );
1163
1164   // set opacity
1165   if( HasTransparency() ) {
1166     actor->SetOpacity( 1.0 - GetTransparency() );
1167   } else {
1168     actor->SetOpacity( 1.0 - propMap.value( GEOM::propertyName( GEOM::Transparency ) ).toDouble() );
1169   }
1170
1171   // set line width
1172   actor->SetWidth( HasWidth() ?
1173                    // predefined line width, manually set to displayer via GEOM_Displayer::SetLineWidth() function 
1174                    GetWidth() :
1175                    // libe width from properties
1176                    propMap.value( GEOM::propertyName( GEOM::LineWidth ) ).toInt() );
1177   
1178   // set display vectors flag
1179   actor->SetVectorMode( propMap.value( GEOM::propertyName( GEOM::EdgesDirection ) ).toBool() );
1180
1181   // set display vertices flag
1182   actor->SetVerticesMode( propMap.value( GEOM::propertyName( GEOM::Vertices ) ).toBool() );
1183
1184   // set display name flag
1185   actor->SetNameMode( propMap.value( GEOM::propertyName( GEOM::ShowName ) ).toBool() );
1186
1187   // set display mode
1188   int displayMode = HasDisplayMode() ? 
1189     // predefined display mode, manually set to displayer via GEOM_Displayer::SetDisplayMode() function 
1190     GetDisplayMode() :
1191     // display mode from properties
1192     propMap.value( GEOM::propertyName( GEOM::DisplayMode ) ).toInt();
1193
1194   // specific processing of 'shading with edges' mode, as VTK provides only the following standard display modes:
1195   // Points - 0, Wireframe - 1, Surface - 2, Insideframe - 3, SurfaceWithEdges - 4
1196   // GEOM actor allows alternative display modes (see VTKViewer::Representation enum) and enum in GEOM_Actor:
1197   // eWireframe - 0, eShading - 1, eShadingWithEdges - 3
1198
1199   if ( displayMode == 2 )
1200       // this is 'Shading with edges' mode => we have to do the correct mapping to EDisplayMode
1201       // enum in GEOM_Actor (and further to VTKViewer::Representation enum)
1202     displayMode++;
1203   actor->setDisplayMode( displayMode );
1204
1205   if ( myToActivate )
1206     actor->PickableOn();
1207   else
1208     actor->PickableOff();
1209
1210   if ( create && !isTemporary && aMgrId != -1 ) {
1211     // set properties to the study
1212     study->setObjectProperties( aMgrId, entry, propMap );
1213   }
1214 }
1215
1216 //=================================================================
1217 /*!
1218  *  GEOM_Displayer::updateDimensions
1219  *  Creates or renews dimension presentation for the IO.
1220  */
1221 //=================================================================
1222 void GEOM_Displayer::updateDimensions( const Handle(SALOME_InteractiveObject)& theIO,
1223                                        SALOME_OCCPrs* thePrs,
1224                                        const gp_Ax3& theShapeLCS )
1225 {
1226   SalomeApp_Study* aStudy = getStudy();
1227   if ( !aStudy )
1228   {
1229     return;
1230   }
1231
1232   if ( theIO.IsNull() )
1233   {
1234     return;
1235   }
1236
1237   SOCC_Prs* anOccPrs = dynamic_cast<SOCC_Prs*>( thePrs );
1238
1239   AIS_ListOfInteractive aListOfIO;
1240
1241   anOccPrs->GetObjects( aListOfIO );
1242
1243   AIS_ListIteratorOfListOfInteractive aIterateIO( aListOfIO );
1244
1245   // remove outdated presentations of dimensions
1246   for ( ; aIterateIO.More(); aIterateIO.Next() )
1247   {
1248     const Handle(AIS_InteractiveObject)& anIO = aIterateIO.Value();
1249     if ( !anIO->IsKind( STANDARD_TYPE( AIS_Dimension ) ) )
1250     {
1251       continue;
1252     }
1253
1254     aListOfIO.Remove( aIterateIO );
1255
1256     if ( !aIterateIO.More() )
1257     {
1258       break;
1259     }
1260   }
1261
1262   // prepare dimension styling
1263   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
1264
1265   QColor  aQColor       = aResMgr->colorValue  ( "Geometry", "dimensions_color", QColor( 0, 255, 0 ) );
1266   int     aLineWidth    = aResMgr->integerValue( "Geometry", "dimensions_line_width", 1 );
1267   QFont   aFont         = aResMgr->fontValue   ( "Geometry", "dimensions_font", QFont("Y14.5M-2009", 14) );
1268   double  anArrowLength = aResMgr->doubleValue ( "Geometry", "dimensions_arrow_length", 5 );
1269   bool    isUnitsShown  = aResMgr->booleanValue( "Geometry", "dimensions_show_units", false );
1270   QString aUnitsLength  = aResMgr->stringValue ( "Geometry", "dimensions_length_units", "m" );
1271   QString aUnitsAngle   = aResMgr->stringValue ( "Geometry", "dimensions_angle_units", "deg" );
1272   bool    aUseText3d    = aResMgr->booleanValue( "Geometry", "dimensions_use_text3d", false );
1273
1274   // restore dimension presentation from saved attribute or property data
1275   AIS_ListOfInteractive aRestoredDimensions;
1276
1277   QVariant aProperty = aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
1278                                                   theIO->getEntry(),
1279                                                   GEOM::propertyName( GEOM::Dimensions ),
1280                                                   QVariant() );
1281
1282   GEOMGUI_DimensionProperty aRecords;
1283
1284   if ( aProperty.isValid() && aProperty.canConvert<GEOMGUI_DimensionProperty>() )
1285   {
1286     aRecords = aProperty.value<GEOMGUI_DimensionProperty>();
1287   }
1288   else
1289   {
1290     aRecords.LoadFromAttribute( getStudy(), theIO->getEntry() );
1291   }
1292   
1293   // create up-to-date dimension presentations
1294   for ( int aPrsIt = 0; aPrsIt < aRecords.GetNumber(); ++aPrsIt )
1295   {
1296     if ( !aRecords.IsVisible( aPrsIt ) )
1297     {
1298       continue;
1299     }
1300
1301     // init dimension by type
1302     Handle(AIS_Dimension) aPrs;
1303     switch( aRecords.GetType( aPrsIt ) )
1304     {
1305       case GEOMGUI_DimensionProperty::DimensionType_Length :
1306       {
1307         Handle(GEOM_AISLength) aLength = new GEOM_AISLength( aPrsIt );
1308         aRecords.GetRecord( aPrsIt )->AsLength()->Update( aLength, theShapeLCS );
1309         aPrs = aLength;
1310         break;
1311       }
1312
1313       case GEOMGUI_DimensionProperty::DimensionType_Diameter :
1314       {
1315         Handle(GEOM_AISDiameter) aDiam = new GEOM_AISDiameter( aPrsIt );
1316         aRecords.GetRecord( aPrsIt )->AsDiameter()->Update( aDiam, theShapeLCS );
1317         aPrs = aDiam;
1318         break;
1319       }
1320
1321       case GEOMGUI_DimensionProperty::DimensionType_Angle :
1322       {
1323         Handle(GEOM_AISAngle) anAng = new GEOM_AISAngle( aPrsIt );
1324         aRecords.GetRecord( aPrsIt )->AsAngle()->Update( anAng, theShapeLCS );
1325         aPrs = anAng;
1326         break;
1327       }
1328     }
1329
1330     aPrs->SetOwner( theIO );
1331
1332     Quantity_Color aColor( aQColor.redF(), aQColor.greenF(), aQColor.blueF(), Quantity_TOC_RGB );
1333
1334     Handle(Prs3d_DimensionAspect) aStyle = new Prs3d_DimensionAspect();
1335
1336     aStyle->SetCommonColor( aColor );
1337     aStyle->MakeUnitsDisplayed( (Standard_Boolean) isUnitsShown );
1338     aStyle->MakeText3d( aUseText3d );
1339     aStyle->MakeTextShaded( Standard_True );
1340     int fsize = aFont.pixelSize() != -1 ? aFont.pixelSize() : aFont.pointSize();
1341     aStyle->SetExtensionSize( fsize * 0.5 );
1342     aStyle->TextAspect()->SetFont( aFont.family().toLatin1().data() );
1343     aStyle->TextAspect()->SetHeight( fsize );
1344     aStyle->ArrowAspect()->SetLength( anArrowLength );
1345     aStyle->LineAspect()->SetWidth( aLineWidth );
1346     aStyle->SetTextHorizontalPosition( aPrs->DimensionAspect()->TextHorizontalPosition() );
1347     aStyle->SetTextVerticalPosition( aPrs->DimensionAspect()->TextVerticalPosition() );
1348     aStyle->SetArrowOrientation( aPrs->DimensionAspect()->ArrowOrientation() );
1349     aPrs->SetDimensionAspect( aStyle );
1350     aPrs->SetPolygonOffsets( Aspect_POM_Fill, -1.0, -1.0 );
1351     aPrs->Attributes()->SetDimLengthDisplayUnits( aUnitsLength.toLatin1().data() );
1352     aPrs->Attributes()->SetDimAngleDisplayUnits( aUnitsAngle.toLatin1().data() );
1353
1354     if ( aPrs->IsKind( STANDARD_TYPE(AIS_AngleDimension) ) )
1355     {
1356       // show degree symbol for dimension instead of label "deg"
1357       if ( aUnitsAngle == "deg" )
1358       {
1359         aPrs->SetSpecialSymbol(0xB0);
1360         aPrs->SetDisplaySpecialSymbol( isUnitsShown ? AIS_DSS_After : AIS_DSS_No );
1361         aStyle->MakeUnitsDisplayed(Standard_False);
1362       }
1363       else
1364       {
1365         aPrs->SetDisplaySpecialSymbol(AIS_DSS_No);
1366         aStyle->MakeUnitsDisplayed( (Standard_Boolean) isUnitsShown );
1367       }
1368     }
1369     else
1370     {
1371       aStyle->MakeUnitsDisplayed( (Standard_Boolean) isUnitsShown );
1372     }
1373
1374     aListOfIO.Append( aPrs );
1375   }
1376
1377   // update presentation
1378   anOccPrs->Clear();
1379
1380   for ( aIterateIO.Initialize( aListOfIO ); aIterateIO.More(); aIterateIO.Next() )
1381   {
1382     anOccPrs->AddObject( aIterateIO.Value() );
1383   }
1384 }
1385
1386 //=================================================================
1387 /*!
1388  *  GEOM_Displayer::Erase
1389  *  Calls Erase() method for each object in the given list
1390  */
1391 //=================================================================
1392 void GEOM_Displayer::Erase( const SALOME_ListIO& theIOList,
1393                             const bool forced,
1394                             const bool updateViewer )
1395 {
1396   SALOME_ListIteratorOfListIO Iter( theIOList );
1397   for ( ; Iter.More(); Iter.Next() )
1398     Erase( Iter.Value(), forced, false );
1399
1400   if ( updateViewer )
1401     UpdateViewer();
1402 }
1403
1404 //=================================================================
1405 /*!
1406  *  GEOM_Displayer::Redisplay
1407  *  Calls Redisplay() method for each object in the given list
1408  */
1409 //=================================================================
1410 void GEOM_Displayer::Redisplay( const SALOME_ListIO& theIOList,
1411                                 const bool updateViewer,
1412                                 const bool checkActiveViewer )
1413 {
1414   SALOME_ListIteratorOfListIO Iter( theIOList );
1415   for ( ; Iter.More(); Iter.Next() )
1416     Redisplay( Iter.Value(), false, checkActiveViewer );
1417
1418   if ( updateViewer )
1419     UpdateViewer();
1420 }
1421
1422 //=================================================================
1423 /*!
1424  *  GEOM_Displayer::Redisplay
1425  *  Calls Redisplay() method for each object in the given list
1426  */
1427 //=================================================================
1428 void GEOM_Displayer::Redisplay( const SALOME_ListIO& theIOList,
1429                                 const bool theUpdateViewer,
1430                                 SALOME_View* theViewFrame )
1431 {
1432   SALOME_ListIteratorOfListIO anIter( theIOList );
1433   for ( ; anIter.More(); anIter.Next() )
1434   {
1435     Redisplay( anIter.Value(), false, theViewFrame );
1436   }
1437
1438   if ( theUpdateViewer )
1439   {
1440     UpdateViewer();
1441   }
1442 }
1443
1444 //=================================================================
1445 /*!
1446  *  GEOM_Displayer::Update
1447  *  Update OCC presentaion
1448  *  [ Reimplemented from SALOME_Displayer ]
1449  */
1450 //=================================================================
1451 void GEOM_Displayer::Update( SALOME_OCCPrs* prs )
1452 {
1453   SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( prs );
1454   SalomeApp_Study* study = getStudy();
1455
1456   if ( !occPrs || myShape.IsNull() || !study )
1457     return;
1458
1459   if ( myType == GEOM_MARKER && myShape.ShapeType() == TopAbs_FACE )
1460   {
1461     // 
1462     // specific processing for local coordinate system presentation
1463     // 
1464
1465     TopoDS_Face aFace = TopoDS::Face( myShape );
1466     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( aFace ) );
1467     if ( !aPlane.IsNull() )
1468     {
1469       gp_Ax3 aPos = aPlane->Pln().Position();
1470       Handle(Geom_Axis2Placement) aPlc = new Geom_Axis2Placement( aPos.Ax2() );
1471
1472       Handle(GEOM_AISTrihedron) aTrh;
1473
1474       if ( occPrs->IsNull() )
1475       {
1476         // new presentation is being created
1477         aTrh = new GEOM_AISTrihedron( aPlc );
1478         occPrs->AddObject( aTrh );
1479       }
1480       else
1481       {
1482         // presentation is being updated
1483         AIS_ListOfInteractive aList;
1484         occPrs->GetObjects( aList );
1485         AIS_ListIteratorOfListOfInteractive anIter( aList );
1486         for ( ; anIter.More() && aTrh.IsNull(); anIter.Next() ) {
1487           aTrh = Handle(GEOM_AISTrihedron)::DownCast( anIter.Value() );
1488         }
1489       }
1490         
1491       if ( !aTrh.IsNull() ) {
1492         // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
1493         if ( HasColor() )
1494           aTrh->SetColor( (Quantity_NameOfColor)GetColor() );
1495         // predefined line width, manually set to displayer via GEOM_Displayer::SetLineWidth() function 
1496         if ( HasWidth() )
1497           aTrh->SetWidth( GetWidth() );
1498         
1499         if ( !myIO.IsNull() )
1500         {
1501           aTrh->setIO( myIO );
1502           aTrh->SetOwner( myIO );
1503         }
1504         aTrh->SetComponent( aPlc );
1505         aTrh->SetToUpdate();
1506       }
1507       occPrs->SetToActivate( ToActivate() );
1508     }
1509   }
1510   else
1511   {
1512     // 
1513     // processing for usual geometry presentation
1514     // 
1515
1516     // if presentation is empty we try to create new one
1517     if ( occPrs->IsNull() )
1518     {
1519       // create presentation (specific for vectors)
1520       Handle(GEOM_AISShape) AISShape = ( myType == GEOM_VECTOR ) ? new GEOM_AISVector( myShape, "" )
1521                                                                  : new GEOM_AISShape ( myShape, "" );
1522
1523       if( myType == GEOM_FIELD_STEP )
1524         AISShape->SetHilightMode( GEOM_AISShape::CustomHighlight );
1525       // update shape properties
1526       updateShapeProperties( AISShape, true );
1527
1528       // add shape to the presentation
1529       occPrs->AddObject( AISShape );
1530
1531       // In accordance with ToActivate() value object will be activated/deactivated
1532       // when it will be displayed
1533       occPrs->SetToActivate( ToActivate() );
1534
1535       if ( AISShape->isTopLevel() && GEOM_AISShape::topLevelDisplayMode() == GEOM_AISShape::TopShowAdditionalWActor ) {
1536         // 21671: EDF 1829 GEOM : Bring to front selected objects (continuation):
1537
1538         // create additional wireframe shape
1539         Handle(GEOM_TopWireframeShape) aWirePrs = new GEOM_TopWireframeShape(myShape);
1540         aWirePrs->SetWidth(AISShape->Width());
1541         if ( !myIO.IsNull() ) {
1542           aWirePrs->setIO( myIO );
1543           aWirePrs->SetOwner( myIO );
1544         }
1545
1546         // add shape to the presentation
1547         occPrs->AddObject( aWirePrs );
1548       }
1549     }
1550     // if presentation is found -> set again shape for it
1551     else
1552     {
1553       AIS_ListOfInteractive IOList;
1554       occPrs->GetObjects( IOList );
1555       AIS_ListIteratorOfListOfInteractive Iter( IOList );
1556       for ( ; Iter.More(); Iter.Next() )
1557       {
1558         Handle(GEOM_AISShape) AISShape = Handle(GEOM_AISShape)::DownCast( Iter.Value() );
1559         if ( AISShape.IsNull() )
1560           continue;
1561
1562         // re-set shape (it might be changed)
1563         if ( AISShape->Shape() != myShape )
1564           AISShape->Set( myShape );
1565
1566         // update shape properties
1567         updateShapeProperties( AISShape, false );
1568
1569         // force updating
1570         AISShape->UpdateSelection();
1571         AISShape->SetToUpdate();
1572       }
1573     }
1574
1575     updateDimensions( myIO, occPrs, gp_Ax3().Transformed( myShape.Location().Transformation() ) );
1576   }
1577 }
1578
1579 //=================================================================
1580 /*!
1581  *  GEOM_Displayer::Update
1582  *  Update VTK presentaion
1583  *  [ Reimplemented from SALOME_Displayer ]
1584  */
1585 //=================================================================
1586 void GEOM_Displayer::Update( SALOME_VTKPrs* prs )
1587 {
1588   SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
1589   SalomeApp_Study* study = getStudy();
1590
1591   if ( !vtkPrs || myShape.IsNull() || !study )
1592     return;
1593
1594   if ( myType == GEOM_MARKER && myShape.ShapeType() == TopAbs_FACE )
1595   {
1596     // 
1597     // specific processing for local coordinate system presentation
1598     // 
1599
1600     TopoDS_Face aFace = TopoDS::Face( myShape );
1601     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( aFace ) );
1602     if ( !aPlane.IsNull() ) {
1603       gp_Ax3 aPos = aPlane->Pln().Position();
1604       Handle(Geom_Axis2Placement) aPlc = new Geom_Axis2Placement( aPos.Ax2() );
1605
1606       GEOM_VTKTrihedron* aTrh = 0;
1607
1608       if ( vtkPrs->IsNull() ) {
1609         // new presentation is being created
1610         aTrh = GEOM_VTKTrihedron::New();
1611         vtkPrs->AddObject( aTrh );
1612       }
1613       else {
1614         // presentation is being updated
1615         vtkActorCollection* actors = vtkPrs->GetObjects();
1616         if ( actors ) {
1617           actors->InitTraversal();
1618           vtkActor* a = actors->GetNextActor();
1619           while ( a && !aTrh ) {
1620             aTrh = GEOM_VTKTrihedron::SafeDownCast( a );
1621             a = actors->GetNextActor();
1622           }
1623         }
1624       }
1625       
1626       if ( aTrh ) {
1627         // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
1628         if ( HasColor() ) {
1629           Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
1630           aTrh->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
1631         }
1632 #ifdef VTK_TRIHEDRON_WIDTH
1633         // 
1634         // VSR: currently isn't supported
1635         //
1636         // predefined line width, manually set to displayer via GEOM_Displayer::SetLineWidth() function 
1637         if ( HasWidth() )
1638           aTrh->SetWidth( GetWidth() );
1639 #endif
1640
1641         if ( !myIO.IsNull() )
1642           aTrh->setIO( myIO );
1643
1644         aTrh->SetPlacement( aPlc );
1645       }
1646     }
1647   }
1648   else
1649   {
1650     // 
1651     // processing for usual geometry presentation
1652     // 
1653
1654     // if presentation is empty we try to create new one
1655     if ( vtkPrs->IsNull() )
1656     {
1657       // create an actor
1658       GEOM_Actor* actor = GEOM_Actor::New();
1659       // update actor properties
1660       updateActorProperties( actor, true );
1661       // add actor to the presentation
1662       vtkPrs->AddObject( actor );
1663     }
1664     else {
1665       // presentation is being updated
1666       vtkActorCollection* actors = vtkPrs->GetObjects();
1667       if ( actors ) {
1668         actors->InitTraversal();
1669         vtkActor* a = actors->GetNextActor();
1670         while ( a ) {
1671           GEOM_Actor* actor = GEOM_Actor::SafeDownCast( a );
1672           if ( actor ) {
1673             // update actor properties
1674             updateActorProperties( actor, false );
1675             a = actors->GetNextActor();
1676           }
1677         }
1678       }
1679     }
1680   }
1681 }
1682
1683 //=================================================================
1684 /*!
1685  *  GEOM_Displayer::BuildPrs
1686  *  Build presentation accordint to the current viewer type
1687  */
1688 //=================================================================
1689 SALOME_Prs* GEOM_Displayer::BuildPrs( GEOM::GEOM_Object_ptr theObj )
1690 {
1691   if ( theObj->_is_nil() )
1692     return 0;
1693
1694   myViewFrame = GetActiveView();
1695   if ( myViewFrame == 0 )
1696     return 0;
1697
1698   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
1699   if ( aPrs == 0 )
1700     return 0;
1701
1702   internalReset();
1703   setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), theObj ) );
1704   myType = theObj->GetType();
1705
1706   // Update presentation
1707   UpdatePrs( aPrs );
1708
1709   return aPrs;
1710 }
1711
1712 //=================================================================
1713 /*!
1714  *  GEOM_Displayer::BuildPrs
1715  *  Build presentation accordint to the current viewer type
1716  */
1717 //=================================================================
1718 SALOME_Prs* GEOM_Displayer::BuildPrs( const TopoDS_Shape& theShape )
1719 {
1720   myViewFrame = GetActiveView();
1721   if ( theShape.IsNull() || myViewFrame == 0 )
1722     return 0;
1723
1724   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
1725   if ( aPrs == 0 )
1726     return 0;
1727
1728   internalReset();
1729   setShape( theShape );
1730   myType = -1;
1731
1732   UpdatePrs( aPrs );
1733
1734   return aPrs;
1735 }
1736
1737 //=================================================================
1738 /*!
1739  *  GEOM_Displayer::buildPresentation
1740  *  Builds/finds object's presentation for the current viewer
1741  *  Calls corresponding Update() method by means of double dispatch
1742  *  [ internal ]
1743  */
1744 //=================================================================
1745 SALOME_Prs* GEOM_Displayer::buildPresentation( const QString& entry,
1746                                                SALOME_View* theViewFrame )
1747 {
1748   SALOME_Prs* prs = 0;
1749   internalReset();
1750
1751   myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
1752
1753   if ( myViewFrame )
1754   {
1755     prs = LightApp_Displayer::buildPresentation( entry, theViewFrame );
1756     if ( prs )
1757     {
1758       Handle( SALOME_InteractiveObject ) theIO = new SALOME_InteractiveObject();
1759       theIO->setEntry( entry.toLatin1().constData() );
1760       if ( !theIO.IsNull() )
1761       {
1762         // set interactive object
1763         setIO( theIO );
1764         //  Find SOBject (because shape should be published previously)
1765         SUIT_Session* session = SUIT_Session::session();
1766         SUIT_Application* app = session->activeApplication();
1767         if ( app )
1768         {
1769           SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
1770           if ( study )
1771           {
1772             _PTR(SObject) SO ( study->studyDS()->FindObjectID( theIO->getEntry() ) );
1773             if ( SO )
1774             {
1775               // get CORBA reference to data object
1776               CORBA::Object_var object = GeometryGUI::ClientSObjectToObject(SO);
1777               if ( !CORBA::is_nil( object ) )
1778               {
1779                 // downcast to GEOM base object
1780                 GEOM::GEOM_BaseObject_var GeomBaseObject = GEOM::GEOM_BaseObject::_narrow( object );
1781                 if ( !GeomBaseObject->_is_nil() )
1782                 {
1783                   myType = GeomBaseObject->GetType();
1784
1785                   // downcast to GEOM object
1786                   GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( GeomBaseObject );
1787                   if ( myType == GEOM_FIELD_STEP )
1788                   {
1789                     // get the GEOM object from the field's shape
1790                     GEOM::GEOM_FieldStep_var GeomFieldStep = GEOM::GEOM_FieldStep::_narrow( GeomBaseObject );
1791                     if ( !GeomFieldStep->_is_nil() )
1792                     {
1793                       GEOM::GEOM_Field_var GeomField = GeomFieldStep->GetField();
1794                       if ( !GeomField->_is_nil() )
1795                         GeomObject = GeomField->GetShape();
1796                     }
1797
1798                     // read the field step information
1799                     readFieldStepInfo( GeomFieldStep );
1800                   }
1801
1802                   if ( !GeomObject->_is_nil() )
1803                   {
1804                     theIO->setName( GeomObject->GetName() );
1805                     // finally set shape
1806                     setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), GeomObject ) );
1807                   }
1808                 }
1809               }
1810             }
1811           }
1812         }
1813       }
1814       UpdatePrs( prs );  // Update presentation by using of the double dispatch
1815     }
1816   }
1817   return prs;
1818 }
1819
1820 //=================================================================
1821 /*!
1822  *  GEOM_Displayer::buildSubshapePresentation
1823  *  Builds/finds object's presentation for the current viewer
1824  *  Calls corresponding Update() method by means of double dispatch
1825  *  For not published objects (for Mantis issue 0020435)
1826  */
1827 //=================================================================
1828 SALOME_Prs* GEOM_Displayer::buildSubshapePresentation(const TopoDS_Shape& aShape,
1829                                                       const QString& entry,
1830                                                       SALOME_View* theViewFrame)
1831 {
1832   SALOME_Prs* prs = 0;
1833   internalReset();
1834
1835   myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
1836
1837   if (myViewFrame)
1838   {
1839     prs = LightApp_Displayer::buildPresentation(entry, theViewFrame);
1840     if (prs)
1841     {
1842       Handle(SALOME_InteractiveObject) theIO = new SALOME_InteractiveObject();
1843       theIO->setEntry(entry.toLatin1().constData());
1844       if (!theIO.IsNull())
1845       {
1846         // set interactive object
1847         setIO(theIO);
1848         // finally set shape
1849         setShape(aShape);
1850         myType = GEOM_SUBSHAPE;
1851       }
1852       UpdatePrs(prs);  // Update presentation by using of the double dispatch
1853     }
1854   }
1855   return prs;
1856 }
1857
1858 //=================================================================
1859 /*!
1860  *  GEOM_Displayer::internalReset
1861  *  Resets internal data
1862  *  [internal]
1863  */
1864 //=================================================================
1865 void GEOM_Displayer::internalReset()
1866 {
1867   myIO.Nullify();
1868   myShape.Nullify();
1869
1870   myFieldDataType = GEOM::FDT_Double;
1871   myFieldDimension = 0;
1872   myFieldStepData.clear();
1873   myFieldStepName.Clear();
1874   myFieldStepRangeMin = 0;
1875   myFieldStepRangeMax = 0;
1876 }
1877
1878 //=================================================================
1879 /*!
1880  *  GEOM_Displayer::LocalSelection
1881  *  Activate selection of CAD shapes with activisation of selection
1882  *  of their sub-shapes (with opened local context for OCC viewer)
1883  */
1884 //=================================================================
1885 void GEOM_Displayer::LocalSelection( const Handle(SALOME_InteractiveObject)& theIO, const std::list<int> modes )
1886 {
1887   SUIT_Session* session = SUIT_Session::session();
1888   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1889   if ( !app )
1890     return;
1891
1892   LightApp_SelectionMgr* sm = app->selectionMgr();
1893
1894   // remove all filters from selection
1895   sm->clearFilters();
1896
1897   SALOME_View* vf = GetActiveView();
1898   if ( vf ) {
1899     if (!theIO.IsNull() && !vf->isVisible(theIO))
1900       Display(theIO);
1901     SALOME_Prs* prs = vf->CreatePrs( theIO.IsNull() ? 0 : theIO->getEntry() );
1902     vf->LocalSelection( prs, modes );
1903     delete prs;  // delete presentation because displayer is its owner
1904   }
1905 }
1906
1907 //=================================================================
1908 /*!
1909  *  GEOM_Displayer::LocalSelection
1910  *  Activate selection of CAD shapes with activisation of selection
1911  *  of their sub-shapes (with opened local context for OCC viewer)
1912  */
1913 //=================================================================
1914 void GEOM_Displayer::LocalSelection( const Handle(SALOME_InteractiveObject)& theIO, const int theMode )
1915 {
1916   std::list<int> modes;
1917   modes.push_back( theMode );
1918   LocalSelection( theIO, modes );
1919 }
1920
1921 //=================================================================
1922 /*!
1923  *  GEOM_Displayer::globalSelection
1924  *  Activate selection of CAD shapes without activisation of selection
1925  *  of their sub-shapes (without opened local context for OCC viewer)
1926  */
1927 //=================================================================
1928 void GEOM_Displayer::GlobalSelection( const int theMode, const bool update )
1929 {
1930   TColStd_MapOfInteger aModes;
1931   aModes.Add( theMode );
1932   GlobalSelection( aModes, update );
1933 }
1934
1935 //=================================================================
1936 /*!
1937  *  GEOM_Displayer::globalSelection
1938  *  Activate selection of CAD shapes without activisation of selection
1939  *  of their sub-shapes (without opened local context for OCC viewer)
1940  */
1941 //=================================================================
1942 void GEOM_Displayer::GlobalSelection( const TColStd_MapOfInteger& theModes,
1943                                       const bool update, const QList<int>* theSubShapes )
1944 {
1945   SUIT_Session* session = SUIT_Session::session();
1946   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1947   if ( !app )
1948     return;
1949
1950   SALOME_View* vf = GetActiveView();
1951   if ( vf == 0 )
1952     return;
1953
1954   // Close local context
1955   vf->GlobalSelection( update );
1956
1957   // Set selection filters in accordance with current mode
1958   LightApp_SelectionMgr* sm = app->selectionMgr();
1959   if ( !sm )
1960     return;
1961
1962   // Remove from selection temporary objects if necessary
1963   if ( !theModes.Contains( GEOM_PREVIEW ) )
1964     clearTemporary( sm );
1965
1966   //@ aSel->ClearIndex();
1967
1968   sm->clearFilters();
1969
1970   // Remove filters from AIS_InteractiveContext
1971   Handle(AIS_InteractiveContext) ic;
1972   SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
1973   if ( viewer )
1974     {
1975       ic = viewer->getAISContext();
1976       if ( !ic.IsNull() )
1977         ic->RemoveFilters();
1978     }
1979
1980   if ( theModes.Contains( GEOM_ALLOBJECTS ) )
1981     return;
1982
1983   SUIT_SelectionFilter* aFilter;
1984   if ( theModes.Extent() == 1 )
1985     {
1986       int aMode = TColStd_MapIteratorOfMapOfInteger( theModes ).Key();
1987
1988       if ( aMode == GEOM_COMPOUNDFILTER )
1989         aFilter = getComplexFilter( theSubShapes );
1990       else
1991         aFilter = getFilter( aMode );
1992     }
1993   else if ( theModes.Extent() > 1 )
1994     {
1995       TColStd_MapOfInteger aTopAbsModes;
1996       TColStd_MapIteratorOfMapOfInteger anIter( theModes );
1997       QList<SUIT_SelectionFilter*> aListOfFilters;
1998       for ( ; anIter.More(); anIter.Next() )
1999         {
2000           SUIT_SelectionFilter* aFilter;
2001           int aMode = anIter.Key();
2002           if ( aMode == GEOM_COMPOUNDFILTER )
2003             aFilter = getComplexFilter( theSubShapes );
2004           else
2005             aFilter = getFilter( aMode );
2006
2007           if ( aFilter )
2008             aListOfFilters.append( aFilter );
2009         }
2010
2011       aFilter = new GEOM_LogicalFilter( aListOfFilters, GEOM_LogicalFilter::LO_OR );
2012     }
2013   else
2014     return;
2015
2016   if ( aFilter )
2017     {
2018       sm->installFilter( aFilter );
2019       if ( !ic.IsNull() )
2020         {
2021           Handle(GEOM_OCCFilter) anOCCFilter = new GEOM_OCCFilter( sm );
2022           ic->AddFilter( anOCCFilter );
2023         }
2024     }
2025 }
2026
2027 //=================================================================
2028 /*!
2029  *  GEOM_Displayer::LocalSelection
2030  *  Activate selection of CAD shapes with activisation of selection
2031  *  of their sub-shapes (with opened local context for OCC viewer)
2032  */
2033 //=================================================================
2034 void GEOM_Displayer::LocalSelection( const SALOME_ListIO& theIOList, const std::list<int> modes )
2035 {
2036   SALOME_ListIteratorOfListIO Iter( theIOList );
2037   for ( ; Iter.More(); Iter.Next() )
2038     LocalSelection( Iter.Value(), modes );
2039 }
2040
2041 //=================================================================
2042 /*!
2043  *  GEOM_Displayer::LocalSelection
2044  *  Activate selection of CAD shapes with activisation of selection
2045  *  of their sub-shapes (with opened local context for OCC viewer)
2046  */
2047 //=================================================================
2048 void GEOM_Displayer::LocalSelection( const SALOME_ListIO& theIOList, const int theMode )
2049 {
2050   std::list<int> modes;
2051   modes.push_back( theMode );
2052   LocalSelection( theIOList, modes );
2053 }
2054
2055 //=================================================================
2056 /*!
2057  *  GEOM_Displayer::BeforeDisplay
2058  *  Called before displaying of pars. Close local context
2059  *  [ Reimplemented from SALOME_Displayer ]
2060  */
2061 //=================================================================
2062 void GEOM_Displayer::BeforeDisplay( SALOME_View* v, const SALOME_OCCPrs* )
2063 {
2064   SOCC_Viewer* vf = dynamic_cast<SOCC_Viewer*>( v );
2065   if ( vf )
2066   {
2067     Handle(AIS_InteractiveContext) ic = vf->getAISContext();
2068     if ( !ic.IsNull() )
2069     {
2070       if ( ic->HasOpenedContext() )
2071       ic->CloseAllContexts();
2072     }
2073   }
2074 }
2075
2076 void GEOM_Displayer::AfterDisplay( SALOME_View* v, const SALOME_OCCPrs* p )
2077 {
2078   UpdateColorScale(false,false);
2079
2080   // visualize annotations for displayed presentation
2081   SUIT_Session* session = SUIT_Session::session();
2082   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
2083   GeometryGUI* aModule = dynamic_cast<GeometryGUI*>( anApp->activeModule() );
2084   if ( aModule ) {
2085     if ( !myIsRedisplayed ) {
2086       aModule->GetAnnotationMgr()->DisplayVisibleAnnotations(QString(p->GetEntry()), dynamic_cast<SOCC_Viewer*>( v ));
2087     } else {
2088       aModule->GetAnnotationMgr()->UpdateVisibleAnnotations(QString(p->GetEntry()), dynamic_cast<SOCC_Viewer*>( v ));
2089     }
2090   }
2091 }
2092
2093 void GEOM_Displayer::BeforeErase( SALOME_View* v, const SALOME_OCCPrs* p )
2094 {
2095   LightApp_Displayer::BeforeErase( v, p );
2096   releaseTextures( p );
2097 }
2098
2099 void GEOM_Displayer::AfterErase( SALOME_View* v, const SALOME_OCCPrs* p )
2100 {
2101   LightApp_Displayer::AfterErase( v, p );
2102   UpdateColorScale(false,false);
2103
2104   if ( !myIsRedisplayed ) {
2105     // hide annotations for erased presentation
2106     SUIT_Session* session = SUIT_Session::session();
2107     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
2108     GeometryGUI* aModule = dynamic_cast<GeometryGUI*>( anApp->activeModule() );
2109     if ( aModule )
2110       aModule->GetAnnotationMgr()->EraseVisibleAnnotations(QString(p->GetEntry()), dynamic_cast<SOCC_Viewer*>( v ));
2111   }
2112 }
2113
2114 //=================================================================
2115 /*!
2116  *  GEOM_Displayer::SetColor
2117  *  Set color for shape displaying. If it is equal -1 then default color is used.
2118  *  Available values are from Quantity_NameOfColor enumeration
2119  */
2120 //=================================================================
2121 void GEOM_Displayer::SetColor( const int color )
2122 {
2123   if ( color == -1 )
2124     UnsetColor();
2125   else
2126   {
2127     myColor = color;
2128     myShadingColor = Quantity_Color( (Quantity_NameOfColor)color );
2129   }
2130 }
2131
2132 int GEOM_Displayer::GetColor() const
2133 {
2134   return myColor;
2135 }
2136
2137 bool GEOM_Displayer::HasColor() const
2138 {
2139   return myColor != -1;
2140 }
2141
2142 void GEOM_Displayer::UnsetColor()
2143 {
2144   myColor = -1;
2145
2146   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
2147   QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
2148   myShadingColor = SalomeApp_Tools::color( col );
2149 }
2150
2151 //=================================================================
2152 /*!
2153  *  GEOM_Displayer::SetTransparency
2154  *  Set transparency for shape displaying.
2155  */
2156 //=================================================================
2157 double GEOM_Displayer::SetTransparency( const double transparency )
2158 {
2159   double prevTransparency = myTransparency;
2160   myTransparency = transparency;
2161   return prevTransparency;
2162 }
2163
2164 //=================================================================
2165 /*!
2166  *  GEOM_Displayer::GetTransparency
2167  *  Get transparency for shape displaying.
2168  */
2169 //=================================================================
2170 double GEOM_Displayer::GetTransparency() const
2171 {
2172   return myTransparency;
2173 }
2174
2175 //=================================================================
2176 /*!
2177  *  GEOM_Displayer::HasTransparency
2178  *  Check if transparency for shape displaying is set.
2179  */
2180 //=================================================================
2181 bool GEOM_Displayer::HasTransparency() const
2182 {
2183   return myTransparency >= 0;
2184 }
2185
2186 //=================================================================
2187 /*!
2188  *  GEOM_Displayer::UnsetTransparency
2189  *  Unset transparency for shape displaying.
2190  */
2191 //=================================================================
2192 double GEOM_Displayer::UnsetTransparency()
2193 {
2194   return SetTransparency( -1 );
2195 }
2196
2197 //=================================================================
2198 /*!
2199  *  GEOM_Displayer::SetTexture
2200  */
2201 //=================================================================
2202 void GEOM_Displayer::SetTexture( const std::string& texureFileName )
2203 {
2204   myTexture = texureFileName;
2205 }
2206
2207 bool GEOM_Displayer::HasTexture() const
2208 {
2209   return myTexture != "";
2210 }
2211
2212 std::string GEOM_Displayer::GetTexture() const
2213 {
2214   return myTexture;
2215 }
2216
2217 //=================================================================
2218 /*!
2219  *  GEOM_Displayer::SetWidth
2220  *  Set width of shape displaying. If it is equal -1 then default width is used.
2221  */
2222 //=================================================================
2223 void GEOM_Displayer::SetWidth( const double width )
2224 {
2225   myWidth = width;
2226 }
2227
2228 double GEOM_Displayer::GetWidth() const
2229 {
2230   return myWidth;
2231 }
2232
2233 bool GEOM_Displayer::HasWidth() const
2234 {
2235   return myWidth != -1;
2236 }
2237
2238 void GEOM_Displayer::UnsetWidth()
2239 {
2240   myWidth = -1;
2241 }
2242
2243 int GEOM_Displayer::GetIsosWidth() const
2244 {
2245   return myIsosWidth;
2246 }
2247
2248 void GEOM_Displayer::SetIsosWidth(const int width)
2249 {
2250   myIsosWidth = width;
2251 }
2252
2253 bool GEOM_Displayer::HasIsosWidth() const
2254 {
2255   return myIsosWidth != -1;
2256 }
2257
2258 int GEOM_Displayer::SetNbIsos( const int nbIsos )
2259 {
2260   int prevNbIsos = myNbIsos;
2261   myNbIsos = nbIsos;
2262   return prevNbIsos;
2263 }
2264
2265 int GEOM_Displayer::UnsetNbIsos()
2266 {
2267   return SetNbIsos( -1 );
2268 }
2269
2270 int GEOM_Displayer::GetNbIsos() const
2271 {
2272   return myNbIsos;
2273 }
2274
2275 bool GEOM_Displayer::HasNbIsos() const
2276 {
2277   return myNbIsos >= 0;
2278 }
2279
2280 int GEOM_Displayer::SetIsosColor( const int color )
2281 {
2282   int prevColor = myIsosColor;
2283   myIsosColor = color;
2284   return prevColor;
2285 }
2286
2287 int GEOM_Displayer::GetIsosColor() const
2288 {
2289   return myIsosColor;
2290 }
2291
2292 bool GEOM_Displayer::HasIsosColor() const
2293 {
2294   return myIsosColor != -1;
2295 }
2296
2297 int GEOM_Displayer::UnsetIsosColor()
2298 {
2299   return SetIsosColor( -1 );
2300 }
2301
2302 //=================================================================
2303 /*!
2304  *  GEOM_Displayer::SetToActivate
2305  *  This method is used for activisation/deactivisation of objects to be displayed
2306  */
2307 //=================================================================
2308 void GEOM_Displayer::SetToActivate( const bool toActivate )
2309 {
2310   myToActivate = toActivate;
2311 }
2312 bool GEOM_Displayer::ToActivate() const
2313 {
2314   return myToActivate;
2315 }
2316
2317 //=================================================================
2318 /*!
2319  *  GEOM_Displayer::clearTemporary
2320  *  Removes from selection temporary objects
2321  */
2322 //=================================================================
2323 void GEOM_Displayer::clearTemporary( LightApp_SelectionMgr* theSelMgr )
2324 {
2325   SALOME_ListIO selected, toSelect;
2326   theSelMgr->selectedObjects( selected );
2327
2328   for (  SALOME_ListIteratorOfListIO it( selected ) ; it.More(); it.Next() ) {
2329     Handle(SALOME_InteractiveObject) io = it.Value();
2330     if ( !io.IsNull() && io->hasEntry() && strncmp( io->getEntry(), "TEMP_", 5 ) != 0 )
2331       toSelect.Append( it.Value() );
2332   }
2333
2334   theSelMgr->setSelectedObjects( toSelect, true );
2335 }
2336
2337 void GEOM_Displayer::SetName( const char* theName )
2338 {
2339   myName = theName;
2340 }
2341
2342 void GEOM_Displayer::UnsetName()
2343 {
2344   myName = "";
2345 }
2346
2347 SalomeApp_Study* GEOM_Displayer::getStudy() const
2348 {
2349   return dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
2350 }
2351
2352 void GEOM_Displayer::setIO( const Handle(SALOME_InteractiveObject)& theIO )
2353 {
2354   myIO = theIO;
2355 }
2356
2357 void GEOM_Displayer::setShape( const TopoDS_Shape& theShape )
2358 {
2359   myShape = theShape;
2360 }
2361
2362 void GEOM_Displayer::setFieldStepInfo( const GEOM::field_data_type theFieldDataType,
2363                                        const int theFieldDimension,
2364                                        const QList<QVariant>& theFieldStepData,
2365                                        const TCollection_AsciiString& theFieldStepName,
2366                                        const double theFieldStepRangeMin,
2367                                        const double theFieldStepRangeMax )
2368 {
2369   myFieldDataType = theFieldDataType;
2370   myFieldDimension = theFieldDimension;
2371   myFieldStepData = theFieldStepData;
2372   myFieldStepName = theFieldStepName;
2373   myFieldStepRangeMin = theFieldStepRangeMin;
2374   myFieldStepRangeMax = theFieldStepRangeMax;
2375 }
2376
2377 bool GEOM_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
2378 {
2379   _PTR(SObject) anObj = getStudy()->studyDS()->FindObjectID( (const char*)entry.toLatin1() );
2380   GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(anObj)); // enable displaying of GEOM objects
2381   GEOM::GEOM_FieldStep_var aFieldStepObj = GEOM::GEOM_FieldStep::_narrow(GeometryGUI::ClientSObjectToObject(anObj)); // enable displaying of GEOM field steps
2382   GEOM::GEOM_Gen_var aCompObj = GEOM::GEOM_Gen::_narrow(GeometryGUI::ClientSObjectToObject(anObj)); // enable displaying of whole GEOM component
2383   return ( !CORBA::is_nil( aGeomObj ) || !CORBA::is_nil( aFieldStepObj ) || !CORBA::is_nil( aCompObj ) ) &&
2384          (viewer_type == SOCC_Viewer::Type() || viewer_type == SVTK_Viewer::Type());
2385 }
2386
2387 int GEOM_Displayer::SetDisplayMode( const int theMode )
2388 {
2389   int aPrevMode = myDisplayMode;
2390   if ( theMode != -1 ) {
2391     myDisplayMode = theMode;
2392     myHasDisplayMode = true;
2393   }
2394   else {
2395     UnsetDisplayMode();
2396   }
2397   return aPrevMode;
2398 }
2399
2400 int GEOM_Displayer::GetDisplayMode() const
2401 {
2402   return myDisplayMode;
2403 }
2404
2405 int GEOM_Displayer::UnsetDisplayMode()
2406 {
2407   int aPrevMode = myDisplayMode;
2408   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
2409   myDisplayMode = resMgr->integerValue( "Geometry", "display_mode", 0 );
2410   myHasDisplayMode = false;
2411   return aPrevMode;
2412 }
2413
2414 bool GEOM_Displayer::HasDisplayMode() const
2415 {
2416   return myHasDisplayMode;
2417 }
2418
2419 SALOMEDS::Color GEOM_Displayer::getPredefinedUniqueColor()
2420 {
2421   static QList<QColor> colors;
2422
2423   if ( colors.isEmpty() ) {
2424
2425     for (int s = 0; s < 2 ; s++)
2426     {
2427       for (int v = 100; v >= 40; v = v - 20)
2428       {
2429         for (int h = 0; h < 359 ; h = h + 60)
2430         {
2431           colors.append(QColor::fromHsv(h, 255 - s * 127, v * 255 / 100));
2432         }
2433       }
2434     }
2435   }
2436
2437   static int currentColor = randomize( colors.size() );
2438
2439   SALOMEDS::Color color;
2440   color.R = (double)colors[currentColor].red()   / 255.0;
2441   color.G = (double)colors[currentColor].green() / 255.0;
2442   color.B = (double)colors[currentColor].blue()  / 255.0;
2443
2444   currentColor = (currentColor+1) % colors.count();
2445
2446   return color;
2447 }
2448
2449 SALOMEDS::Color GEOM_Displayer::getUniqueColor( const QList<SALOMEDS::Color>& theReservedColors )
2450 {
2451   int aHue = -1;
2452   int aTolerance = 64;
2453   int anIterations = 0;
2454   int aPeriod = 5;
2455
2456   while( 1 )
2457   {
2458     anIterations++;
2459     if( anIterations % aPeriod == 0 )
2460     {
2461       aTolerance /= 2;
2462       if( aTolerance < 1 )
2463         break;
2464     }
2465     //std::cout << "Iteration N" << anIterations << " (tolerance=" << aTolerance << ")"<< std::endl;
2466
2467     aHue = (int)( 360.0 * rand() / RAND_MAX );
2468     //std::cout << "Hue = " << aHue << std::endl;
2469
2470     //std::cout << "Auto colors : ";
2471     bool ok = true;
2472     QList<SALOMEDS::Color>::const_iterator it = theReservedColors.constBegin();
2473     QList<SALOMEDS::Color>::const_iterator itEnd = theReservedColors.constEnd();
2474     for( ; it != itEnd; ++it )
2475     {
2476       SALOMEDS::Color anAutoColor = *it;
2477       QColor aQColor( (int)( anAutoColor.R * 255.0 ), (int)( anAutoColor.G * 255.0 ), (int)( anAutoColor.B * 255.0 ) );
2478
2479       int h, s, v;
2480       aQColor.getHsv( &h, &s, &v );
2481       //std::cout << h << " ";
2482       if( abs( h - aHue ) < aTolerance )
2483       {
2484         ok = false;
2485         //std::cout << "break (diff = " << abs( h - aHue ) << ")";
2486         break;
2487       }
2488     }
2489     //std::cout << std::endl;
2490
2491     if( ok )
2492       break;
2493   }
2494
2495   //std::cout << "Hue of the returned color = " << aHue << std::endl;
2496   QColor aColor;
2497   aColor.setHsv( aHue, 255, 255 );
2498
2499   SALOMEDS::Color aSColor;
2500   aSColor.R = (double)aColor.red() / 255.0;
2501   aSColor.G = (double)aColor.green() / 255.0;
2502   aSColor.B = (double)aColor.blue() / 255.0;
2503
2504   return aSColor;
2505 }
2506
2507 PropMap GEOM_Displayer::getObjectProperties( SalomeApp_Study* study,
2508                                              const QString& entry,
2509                                              SALOME_View* view )
2510 {
2511   // get default properties for the explicitly specified default view type
2512   PropMap propMap = GEOM_Displayer::getDefaultPropertyMap();
2513
2514   if ( study && view ) {
2515     SUIT_ViewModel* viewModel = dynamic_cast<SUIT_ViewModel*>( view );
2516     SUIT_ViewManager* viewMgr = ( viewModel != 0 ) ? viewModel->getViewManager() : 0;
2517     int viewId = ( viewMgr != 0 ) ? viewMgr->getGlobalId() : -1;
2518   
2519     if ( viewModel && viewId != -1 ) {
2520       // get properties from the study
2521       PropMap storedMap = study->getObjectProperties( viewId, entry );
2522       // overwrite default properties from stored ones (that are specified)
2523       for ( int prop = GEOM::Visibility; prop <= GEOM::LastProperty; prop++ ) {
2524         if ( storedMap.contains( GEOM::propertyName( (GEOM::Property)prop ) ) )
2525           propMap.insert( GEOM::propertyName( (GEOM::Property)prop ), 
2526                           storedMap.value( GEOM::propertyName( (GEOM::Property)prop ) ) );
2527       }
2528       // ... specific processing for color
2529       // ... current implementation is to use same stored color for all aspects
2530       // ... (TODO) possible future improvements about free boundaries, standalone edges etc colors can be here
2531       if ( storedMap.contains( GEOM::propertyName( GEOM::Color ) ) ) {
2532         propMap.insert( GEOM::propertyName( GEOM::ShadingColor ),   storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
2533         propMap.insert( GEOM::propertyName( GEOM::WireframeColor ), storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
2534         propMap.insert( GEOM::propertyName( GEOM::LineColor ),      storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
2535         propMap.insert( GEOM::propertyName( GEOM::FreeBndColor ),   storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
2536         propMap.insert( GEOM::propertyName( GEOM::PointColor ),     storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
2537       }
2538
2539       if ( !entry.isEmpty() ) {
2540         // get CORBA reference to geom object
2541         _PTR(SObject) SO( study->studyDS()->FindObjectID( entry.toStdString() ) );
2542         if ( SO ) {
2543           CORBA::Object_var object = GeometryGUI::ClientSObjectToObject( SO );
2544           if ( !CORBA::is_nil( object ) ) {
2545             GEOM::GEOM_Object_var geomObject = GEOM::GEOM_Object::_narrow( object );
2546             if ( !CORBA::is_nil( geomObject ) ) { // to check
2547               // check that geom object has color properly set
2548               bool hasColor = false;
2549               SALOMEDS::Color aSColor = getColor( geomObject, hasColor );
2550               // set color from geometry object (only once, if it is not yet set in GUI)
2551               // current implementation is to use same color for all aspects
2552               // (TODO) possible future improvements about free boundaries, standalone edges etc colors can be here
2553               if ( hasColor && !storedMap.contains( GEOM::propertyName( GEOM::Color ) ) ) {
2554                 QColor objColor = QColor::fromRgbF( aSColor.R, aSColor.G, aSColor.B );
2555                 propMap.insert( GEOM::propertyName( GEOM::ShadingColor ),   objColor );
2556                 propMap.insert( GEOM::propertyName( GEOM::WireframeColor ), objColor );
2557                 propMap.insert( GEOM::propertyName( GEOM::LineColor ),      objColor );
2558                 propMap.insert( GEOM::propertyName( GEOM::FreeBndColor ),   objColor );
2559                 propMap.insert( GEOM::propertyName( GEOM::PointColor ),     objColor );
2560               }
2561               // check that object has point marker properly set
2562               GEOM::marker_type mType = geomObject->GetMarkerType();
2563               GEOM::marker_size mSize = geomObject->GetMarkerSize();
2564               int mTextureId = geomObject->GetMarkerTexture();
2565               bool hasMarker = ( mType > GEOM::MT_NONE && mType < GEOM::MT_USER && mSize > GEOM::MS_NONE && mSize <= GEOM::MS_70 ) || 
2566                                ( mType == GEOM::MT_USER && mTextureId > 0 );
2567               // set point marker from geometry object (only once, if it is not yet set in GUI)
2568               if ( hasMarker && !storedMap.contains( GEOM::propertyName( GEOM::PointMarker ) ) ) {
2569                 if ( mType > GEOM::MT_NONE && mType < GEOM::MT_USER ) {
2570                   // standard type
2571                   propMap.insert( GEOM::propertyName( GEOM::PointMarker ),
2572                                   QString( "%1%2%3" ).arg( (int)mType ).arg( GEOM::subSectionSeparator() ).arg( (int)mSize ) );
2573                 }
2574                 else if ( mType == GEOM::MT_USER ) {
2575                   // custom texture
2576                   propMap.insert( GEOM::propertyName( GEOM::PointMarker ), QString::number( mTextureId ) );
2577                 }
2578               }
2579             }
2580           }
2581         }
2582       }
2583     }
2584   }
2585   return propMap;
2586 }
2587
2588 PropMap GEOM_Displayer::getDefaultPropertyMap()
2589 {
2590   PropMap propMap;
2591
2592   // get resource manager
2593   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
2594
2595   // fill in the properties map with default values
2596
2597   // - visibility (false by default)
2598   propMap.insert( GEOM::propertyName( GEOM::Visibility ), false );
2599
2600   // - nb isos (take default value from preferences)
2601   propMap.insert( GEOM::propertyName( GEOM::NbIsos ),
2602                   QString( "%1%2%3" ).
2603                   arg( resMgr->integerValue( "Geometry", "iso_number_u", 1 ) ).
2604                   arg( GEOM::subSectionSeparator() ).
2605                   arg( resMgr->integerValue( "Geometry", "iso_number_v", 1 ) ) );
2606
2607   // - transparency (opacity = 1-transparency)
2608   propMap.insert( GEOM::propertyName( GEOM::Transparency ),
2609                   resMgr->integerValue( "Geometry", "transparency", 0 ) / 100. );
2610
2611   // - display mode (take default value from preferences)
2612   propMap.insert( GEOM::propertyName( GEOM::DisplayMode ),
2613                   resMgr->integerValue( "Geometry", "display_mode", 0 ) );
2614
2615   // - show edges direction flag (false by default)
2616   propMap.insert( GEOM::propertyName( GEOM::EdgesDirection ), false );
2617
2618   // - show vertices flag (false by default)
2619   propMap.insert( GEOM::propertyName( GEOM::Vertices ), false );
2620
2621   // - show name flag (false by default)
2622   propMap.insert( GEOM::propertyName( GEOM::ShowName ), false );
2623
2624   // - shading color (take default value from preferences)
2625   propMap.insert( GEOM::propertyName( GEOM::ShadingColor ),
2626                   colorFromResources( "shading_color", QColor( 255, 255, 0 ) ) );
2627
2628   // - wireframe color (take default value from preferences)
2629   propMap.insert( GEOM::propertyName( GEOM::WireframeColor ),
2630                   colorFromResources( "wireframe_color", QColor( 255, 255, 0 ) ) );
2631
2632   // - standalone edges color (take default value from preferences)
2633   propMap.insert( GEOM::propertyName( GEOM::LineColor ),
2634                   colorFromResources( "line_color", QColor( 255, 0, 0 ) ) );
2635
2636   // - free boundaries color (take default value from preferences)
2637   propMap.insert( GEOM::propertyName( GEOM::FreeBndColor ),
2638                   colorFromResources( "free_bound_color", QColor( 0, 255, 0 ) ) );
2639
2640   // - points color (take default value from preferences)
2641   propMap.insert( GEOM::propertyName( GEOM::PointColor ),
2642                   colorFromResources( "point_color", QColor( 255, 255, 0 ) ) );
2643
2644   // - isos color (take default value from preferences)
2645   propMap.insert( GEOM::propertyName( GEOM::IsosColor ),
2646                   colorFromResources( "isos_color", QColor( 200, 200, 200 ) ) );
2647
2648   // - outlines color (take default value from preferences)
2649   propMap.insert( GEOM::propertyName( GEOM::OutlineColor ),
2650                   colorFromResources( "edges_in_shading_color", QColor( 180, 180, 180 ) ) );
2651
2652   // - deflection coefficient (take default value from preferences)
2653   propMap.insert( GEOM::propertyName( GEOM::Deflection ),
2654                   resMgr->doubleValue( "Geometry", "deflection_coeff", 0.001 ) );
2655
2656   // - material (take default value from preferences)
2657   Material_Model material;
2658   material.fromResources( resMgr->stringValue( "Geometry", "material", "Plastic" ) );
2659   propMap.insert( GEOM::propertyName( GEOM::Material ), material.toProperties() );
2660
2661   // - edge width (take default value from preferences)
2662   propMap.insert( GEOM::propertyName( GEOM::LineWidth ),
2663                   resMgr->integerValue( "Geometry", "edge_width", 1 ) );
2664
2665   // - isos width (take default value from preferences)
2666   propMap.insert( GEOM::propertyName( GEOM::IsosWidth ),
2667                   resMgr->integerValue( "Geometry", "isolines_width", 1 ) );
2668
2669   // - point marker (take default value from preferences)
2670   propMap.insert( GEOM::propertyName( GEOM::PointMarker ),
2671                   QString( "%1%2%3" ).
2672                   arg( resMgr->integerValue( "Geometry", "type_of_marker", 1 ) + 1 ).
2673                   arg( GEOM::subSectionSeparator() ).
2674                   arg( resMgr->integerValue( "Geometry", "marker_scale", 1 ) ) );
2675
2676   // - top-level flag (false by default)
2677   propMap.insert( GEOM::propertyName( GEOM::TopLevel ), false );
2678
2679   return propMap;
2680 }
2681
2682 SALOMEDS::Color GEOM_Displayer::getColor(GEOM::GEOM_Object_var theGeomObject, bool& hasColor) {
2683   SALOMEDS::Color aSColor;
2684   hasColor = false;
2685
2686   SUIT_Session* session = SUIT_Session::session();
2687   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
2688
2689   if ( app && !theGeomObject->_is_nil()) {
2690     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
2691
2692     if ( study ) {
2693       _PTR(Study) aStudy = study->studyDS();
2694       aSColor = theGeomObject->GetColor();
2695       hasColor = aSColor.R >= 0 && aSColor.G >= 0 && aSColor.B >= 0;
2696       if ( !hasColor ) {
2697 #ifdef GENERAL_AUTOCOLOR // auto-color for all sub-shapes
2698         bool general_autocolor = true;
2699 #else                    // auto-color for groups only
2700         bool general_autocolor = false;
2701 #endif                   // GENERAL_AUTOCOLOR
2702         if ( general_autocolor || theGeomObject->GetType() == GEOM_GROUP ) {
2703           GEOM::GEOM_Object_var aMainObject = theGeomObject->GetMainShape();
2704           if ( !CORBA::is_nil( aMainObject ) && aMainObject->GetAutoColor() ) {
2705 #ifdef SIMPLE_AUTOCOLOR  // simplified algorithm for auto-colors
2706             aSColor = getPredefinedUniqueColor();
2707             hasColor = true;
2708 #else                    // old algorithm  for auto-colors
2709             QList<SALOMEDS::Color> aReservedColors;
2710             CORBA::String_var IOR = app->orb()->object_to_string( aMainObject );
2711             _PTR(SObject) aMainSObject( aStudy->FindObjectIOR( IOR.in() ) );
2712             if ( aMainSObject ) {
2713               _PTR(ChildIterator) it( aStudy->NewChildIterator( aMainSObject ) );
2714               for ( ; it->More(); it->Next() ) {
2715                 _PTR(SObject) aChildSObject( it->Value() );
2716                 GEOM::GEOM_Object_var aChildObject =
2717                   GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aChildSObject));
2718                 if ( CORBA::is_nil( aChildObject ) )
2719                   continue;
2720
2721                 SALOMEDS::Color aReservedColor = aChildObject->GetColor();
2722                 if ( aReservedColor.R >= 0 && aReservedColor.G >= 0 && aReservedColor.B >= 0 )
2723                   aReservedColors.append( aReservedColor );
2724               }
2725             }
2726             aSColor = getUniqueColor( aReservedColors );
2727             hasColor = true;
2728 #endif                   // SIMPLE_AUTOCOLOR
2729           }
2730         }
2731       }
2732     }
2733   }
2734   return aSColor;
2735 }
2736
2737
2738 void GEOM_Displayer::EraseWithChildren(const Handle(SALOME_InteractiveObject)& theIO,
2739                                        const bool eraseOnlyChildren) {
2740   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
2741   if ( !app )
2742     return;
2743
2744   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
2745   if ( !appStudy )
2746     return;
2747
2748   LightApp_DataObject* parent = appStudy->findObjectByEntry(theIO->getEntry());
2749
2750   if( !parent)
2751     return;
2752
2753   // Erase from all views
2754   QList<SALOME_View*> views;
2755   SALOME_View* view;
2756   ViewManagerList vmans = app->viewManagers();
2757   SUIT_ViewManager* vman;
2758   foreach ( vman, vmans ) {
2759     SUIT_ViewModel* vmod = vman->getViewModel();
2760     view = dynamic_cast<SALOME_View*> ( vmod );
2761     if ( view )
2762       views.append( view );
2763   }
2764
2765   if( views.count() == 0 )
2766     return;
2767
2768   //Erase childrens w/o update views
2769   DataObjectList listObj = parent->children( true );
2770   SUIT_DataObject* obj;
2771   foreach( obj, listObj ) {
2772     LightApp_DataObject* l_obj = dynamic_cast<LightApp_DataObject*>(obj);
2773     if(l_obj)
2774       foreach ( view, views ) {
2775       Handle(SALOME_InteractiveObject) anIO =
2776         new SALOME_InteractiveObject(qPrintable(l_obj->entry()), "GEOM", "");
2777       Erase(anIO, false, false, view);
2778     }
2779   }
2780
2781   //Erase parent with view update or repaint views
2782   foreach ( view, views ) {
2783     if(!eraseOnlyChildren)
2784       Erase(theIO, false, true, view);
2785     else
2786       view->Repaint();
2787   }
2788 }
2789
2790 void GEOM_Displayer::readFieldStepInfo( GEOM::GEOM_FieldStep_var theGeomFieldStep )
2791 {
2792   if( theGeomFieldStep->_is_nil() )
2793     return;
2794
2795   GEOM::GEOM_Field_var aGeomField = theGeomFieldStep->GetField();
2796   if( aGeomField->_is_nil() )
2797     return;
2798
2799   GEOM::GEOM_Object_var aGeomFieldShape = aGeomField->GetShape();
2800   if( aGeomFieldShape->_is_nil() )
2801     return;
2802
2803   TCollection_AsciiString aFieldStepName( theGeomFieldStep->GetName() );
2804   TCollection_AsciiString aFieldName( aGeomField->GetName() );
2805   TCollection_AsciiString aShapeName( aGeomFieldShape->GetName() );
2806
2807   aFieldStepName = aShapeName + "\n" + aFieldName + "\n" + aFieldStepName;
2808
2809   GEOM::field_data_type aFieldDataType = aGeomField->GetDataType();
2810
2811   int aFieldDimension = aGeomField->GetDimension();
2812
2813   GEOM::string_array_var aFieldComponents = aGeomField->GetComponents();
2814   int aFieldNbComponents = aFieldComponents->length();
2815
2816   QList<QVariant> aFieldStepData;
2817   if( aFieldDataType == GEOM::FDT_Bool )
2818   {
2819     GEOM::GEOM_BoolFieldStep_var aGeomBoolFieldStep = GEOM::GEOM_BoolFieldStep::_narrow( theGeomFieldStep );
2820     if ( !aGeomBoolFieldStep->_is_nil() )
2821     {
2822       GEOM::short_array_var aValues = aGeomBoolFieldStep->GetValues();
2823       for( size_t i = 0, n = aValues->length(); i < n; i++ )
2824         aFieldStepData << (bool)aValues[i];
2825     }
2826   }
2827   else if( aFieldDataType == GEOM::FDT_Int )
2828   {
2829     GEOM::GEOM_IntFieldStep_var aGeomIntFieldStep = GEOM::GEOM_IntFieldStep::_narrow( theGeomFieldStep );
2830     if ( !aGeomIntFieldStep->_is_nil() )
2831     {
2832       GEOM::ListOfLong_var aValues = aGeomIntFieldStep->GetValues();
2833       for( size_t i = 0, n = aValues->length(); i < n; i++ )
2834         aFieldStepData << (qlonglong)aValues[i];
2835     }
2836   }
2837   else if( aFieldDataType == GEOM::FDT_Double )
2838   {
2839     GEOM::GEOM_DoubleFieldStep_var aGeomDoubleFieldStep = GEOM::GEOM_DoubleFieldStep::_narrow( theGeomFieldStep );
2840     if ( !aGeomDoubleFieldStep->_is_nil() )
2841     {
2842       GEOM::ListOfDouble_var aValues = aGeomDoubleFieldStep->GetValues();
2843       for( size_t i = 0, n = aValues->length(); i < n; i++ )
2844         aFieldStepData << (double)aValues[i];
2845     }
2846   }
2847   else if( aFieldDataType == GEOM::FDT_String )
2848   {
2849     GEOM::GEOM_StringFieldStep_var aGeomStringFieldStep = GEOM::GEOM_StringFieldStep::_narrow( theGeomFieldStep );
2850     if ( !aGeomStringFieldStep->_is_nil() )
2851     {
2852       GEOM::string_array_var aValues = aGeomStringFieldStep->GetValues();
2853       for( size_t i = 0, n = aValues->length(); i < n; i++ )
2854         aFieldStepData << QString( aValues[i] );
2855     }
2856   }
2857
2858   double aFieldStepRangeMin = 0, aFieldStepRangeMax = 0;
2859   aFieldStepData = groupFieldData( aFieldStepData,
2860                                    aFieldNbComponents,
2861                                    aFieldDataType == GEOM::FDT_String,
2862                                    aFieldStepRangeMin,
2863                                    aFieldStepRangeMax );
2864
2865   setFieldStepInfo( aFieldDataType,
2866                     aFieldDimension,
2867                     aFieldStepData,
2868                     aFieldStepName,
2869                     aFieldStepRangeMin,
2870                     aFieldStepRangeMax );
2871 }
2872
2873 QList<QVariant> GEOM_Displayer::groupFieldData( const QList<QVariant>& theFieldStepData,
2874                                                 const int theFieldNbComponents,
2875                                                 const bool theIsString,
2876                                                 double& theFieldStepRangeMin,
2877                                                 double& theFieldStepRangeMax )
2878 {
2879   QList<QVariant> aResultList;
2880   theFieldStepRangeMin = 0;
2881   theFieldStepRangeMax = 0;
2882
2883   if( theFieldStepData.isEmpty() || theFieldNbComponents < 1 )
2884     return aResultList;
2885
2886   int aNbSubShapes = theFieldStepData.count() / theFieldNbComponents;
2887
2888   QList<QVariant> aGroupedList;
2889
2890   bool anIsBoolean = false;
2891   for( int aSubShape = 0; aSubShape < aNbSubShapes; aSubShape++ )
2892   {
2893     double aNorm = 0;
2894     QStringList aStringList;
2895
2896     int aBaseIndex = aSubShape * theFieldNbComponents;
2897     for( int aComponent = 0; aComponent < theFieldNbComponents; aComponent++ )
2898     {
2899       int anIndex = aComponent + aBaseIndex;
2900
2901       const QVariant& aVariant = theFieldStepData[ anIndex ];
2902       if( theIsString )
2903       {
2904         if( aVariant.type() == QVariant::String )
2905           aStringList << aVariant.toString();
2906       }
2907       else
2908       {
2909         double aValue = 0;
2910         if( aVariant.type() == QVariant::Bool )
2911         {
2912           aValue = aVariant.toBool() ? 1.0 : 0.0;
2913           aNorm += aValue;
2914           anIsBoolean = true;
2915         }
2916         else
2917         {
2918           if( aVariant.type() == QVariant::LongLong )
2919             aValue = double( aVariant.toLongLong() );
2920           else if( aVariant.type() == QVariant::Double )
2921             aValue = aVariant.toDouble();
2922           aNorm += aValue * aValue;
2923         }
2924       }
2925     }
2926
2927     if( theIsString )
2928       aGroupedList << aStringList.join( "\n" );
2929     else
2930     {
2931       if( anIsBoolean )
2932         aNorm /= theFieldNbComponents;
2933       else
2934         aNorm = pow( aNorm, 0.5 );
2935
2936       if( aGroupedList.isEmpty() )
2937         theFieldStepRangeMin = theFieldStepRangeMax = aNorm;
2938       else
2939       {
2940         theFieldStepRangeMin = Min( theFieldStepRangeMin, aNorm );
2941         theFieldStepRangeMax = Max( theFieldStepRangeMax, aNorm );
2942       }
2943
2944       aGroupedList << aNorm;
2945     }
2946   }
2947
2948   if( anIsBoolean )
2949   {
2950     theFieldStepRangeMin = 0.0;
2951     theFieldStepRangeMax = 1.0;
2952   }
2953
2954   SUIT_Session* session = SUIT_Session::session();
2955   SUIT_ResourceMgr* resMgr = session->resourceMgr();
2956   Standard_Integer aNbIntervals = resMgr->integerValue( "Geometry", "scalar_bar_nb_intervals", 20 );
2957
2958   QListIterator<QVariant> anIter( aGroupedList );
2959   while( anIter.hasNext() )
2960   {
2961     const QVariant& aVariant = anIter.next();
2962     if( theIsString )
2963       aResultList << aVariant;
2964     else
2965     {
2966       QColor aQColor;
2967       Quantity_Color aColor;
2968       if( FindColor( aVariant.toDouble(), theFieldStepRangeMin, theFieldStepRangeMax, anIsBoolean ? 2 : aNbIntervals, aColor ) )
2969         aQColor = QColor::fromRgbF( aColor.Red(), aColor.Green(), aColor.Blue() );
2970       aResultList << aQColor;
2971     }
2972   }
2973   return aResultList;
2974 }
2975
2976 // Note: the method is copied from AIS_ColorScale class
2977 Standard_Integer GEOM_Displayer::HueFromValue( const Standard_Integer aValue,
2978                                                const Standard_Integer aMin,
2979                                                const Standard_Integer aMax )
2980 {
2981   Standard_Integer minLimit( 0 ), maxLimit( 230 );
2982
2983   Standard_Integer aHue = maxLimit;
2984   if ( aMin != aMax )
2985     aHue = (Standard_Integer)( maxLimit - ( maxLimit - minLimit ) * ( aValue - aMin ) / ( aMax - aMin ) );
2986
2987   aHue = Min( Max( minLimit, aHue ), maxLimit );
2988
2989   return aHue;
2990 }
2991
2992 // Note: the method is copied from AIS_ColorScale class
2993 Standard_Boolean GEOM_Displayer::FindColor( const Standard_Real aValue, 
2994                                             const Standard_Real aMin,
2995                                             const Standard_Real aMax,
2996                                             const Standard_Integer ColorsCount,
2997                                             Quantity_Color& aColor )
2998 {
2999   if( aValue<aMin || aValue>aMax || aMax<aMin )
3000     return Standard_False;
3001
3002   else
3003   {
3004     Standard_Real IntervNumber = 0;
3005     if( aValue<aMin )
3006       IntervNumber = 0;
3007     else if( aValue>aMax )
3008       IntervNumber = ColorsCount-1;
3009     else if( Abs( aMax-aMin ) > Precision::Approximation() )
3010       IntervNumber = Floor( Standard_Real( ColorsCount ) * ( aValue - aMin ) / ( aMax - aMin ) ); // 'Ceiling' replaced with 'Floor'
3011
3012     Standard_Integer Interv = Standard_Integer( IntervNumber );
3013
3014     aColor = Quantity_Color( HueFromValue( Interv, 0, ColorsCount - 1 ), 1.0, 1.0, Quantity_TOC_HLS );
3015
3016     return Standard_True;
3017   } 
3018 }
3019
3020 void GEOM_Displayer::UpdateColorScale( const bool theIsRedisplayFieldSteps, const bool updateViewer ) 
3021 {
3022   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
3023   if( !aStudy )
3024     return;
3025
3026   SOCC_Viewer* aViewModel = dynamic_cast<SOCC_Viewer*>( GetActiveView() );
3027   if( !aViewModel )
3028     return;
3029
3030 #if OCC_VERSION_MAJOR < 7
3031   Handle(V3d_Viewer) aViewer = aViewModel->getViewer3d();
3032   if( aViewer.IsNull() )
3033     return;
3034
3035   aViewer->InitActiveViews();
3036   if( !aViewer->MoreActiveViews() )
3037     return;
3038
3039   Handle(V3d_View) aView = aViewer->ActiveView();
3040   if( aView.IsNull() )
3041     return;
3042 #endif
3043
3044   Standard_Boolean anIsDisplayColorScale = Standard_False;
3045   TCollection_AsciiString aColorScaleTitle;
3046   Standard_Real aColorScaleMin = 0, aColorScaleMax = 0;
3047   Standard_Boolean anIsBoolean = Standard_False;
3048
3049   SALOME_ListIO aSelectedObjects;
3050   myApp->selectionMgr()->selectedObjects( aSelectedObjects );
3051   if( aSelectedObjects.Extent() == 1 )
3052   {
3053     Handle(SALOME_InteractiveObject) anIO = aSelectedObjects.First();
3054     if( !anIO.IsNull() )
3055     {
3056       SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>( aViewModel->CreatePrs( anIO->getEntry() ) );
3057       if( aPrs )
3058       {
3059         AIS_ListOfInteractive aList;
3060         aPrs->GetObjects( aList );
3061         AIS_ListIteratorOfListOfInteractive anIter( aList );
3062         for( ; anIter.More(); anIter.Next() )
3063         {
3064           Handle(GEOM_AISShape) aShape = Handle(GEOM_AISShape)::DownCast( anIter.Value() );
3065           if( !aShape.IsNull() )
3066           {
3067             GEOM::field_data_type aFieldDataType;
3068             int aFieldDimension;
3069             QList<QVariant> aFieldStepData;
3070             TCollection_AsciiString aFieldStepName;
3071             double aFieldStepRangeMin, aFieldStepRangeMax;
3072             aShape->getFieldStepInfo( aFieldDataType,
3073                                       aFieldDimension,
3074                                       aFieldStepData,
3075                                       aFieldStepName,
3076                                       aFieldStepRangeMin,
3077                                       aFieldStepRangeMax );
3078             if( !aFieldStepData.isEmpty() && aFieldDataType != GEOM::FDT_String )
3079             {
3080               anIsDisplayColorScale = Standard_True;
3081               aColorScaleTitle = aFieldStepName;
3082               aColorScaleMin = aFieldStepRangeMin;
3083               aColorScaleMax = aFieldStepRangeMax;
3084               anIsBoolean = aFieldDataType == GEOM::FDT_Bool;
3085             }
3086           }
3087         }
3088       }
3089     }
3090   }
3091
3092   if( anIsDisplayColorScale )
3093   {
3094     SUIT_Session* session = SUIT_Session::session();
3095     SUIT_ResourceMgr* resMgr = session->resourceMgr();
3096
3097     Standard_Real anXPos = resMgr->doubleValue( "Geometry", "scalar_bar_x_position", 0.05 );
3098     Standard_Real anYPos = resMgr->doubleValue( "Geometry", "scalar_bar_y_position", 0.1 );
3099     Standard_Real aWidth = resMgr->doubleValue( "Geometry", "scalar_bar_width", 0.2 );
3100     Standard_Real aHeight = resMgr->doubleValue( "Geometry", "scalar_bar_height", 0.5 );
3101     Standard_Integer aTextHeight = resMgr->integerValue( "Geometry", "scalar_bar_text_height", 14 );
3102     Standard_Integer aNbIntervals = resMgr->integerValue( "Geometry", "scalar_bar_nb_intervals", 20 );
3103
3104 #if OCC_VERSION_MAJOR < 7
3105     Handle(Aspect_ColorScale) myColorScale = aView->ColorScale();
3106     if( !myColorScale.IsNull() )
3107     {
3108 #endif
3109
3110     myColorScale->SetXPosition( anXPos );
3111     myColorScale->SetYPosition( anYPos );
3112     myColorScale->SetWidth( aWidth );
3113     myColorScale->SetHeight( aHeight );
3114
3115     myColorScale->SetTextHeight( aTextHeight );
3116     myColorScale->SetNumberOfIntervals( anIsBoolean ? 2 : aNbIntervals );
3117
3118     myColorScale->SetTitle( aColorScaleTitle );
3119     myColorScale->SetRange( aColorScaleMin, aColorScaleMax );
3120
3121 #if OCC_VERSION_MAJOR < 7
3122     }
3123     if( !aView->ColorScaleIsDisplayed() )
3124       aView->ColorScaleDisplay();
3125   }
3126   else
3127     if( aView->ColorScaleIsDisplayed() )
3128       aView->ColorScaleErase();
3129 #else
3130     if( !aViewModel->getAISContext()->IsDisplayed( myColorScale ) )
3131       aViewModel->getAISContext()->Display( myColorScale );
3132   }
3133   else
3134     if( aViewModel->getAISContext()->IsDisplayed( myColorScale ) )
3135       aViewModel->getAISContext()->Erase( myColorScale );
3136 #endif
3137
3138   if( theIsRedisplayFieldSteps )
3139   {
3140     _PTR(Study) aStudyDS = aStudy->studyDS();
3141     QList<SUIT_ViewManager*> vmList;
3142     myApp->viewManagers( vmList );
3143     for( QList<SUIT_ViewManager*>::Iterator vmIt = vmList.begin(); vmIt != vmList.end(); vmIt++ )
3144     {
3145       if( SUIT_ViewManager* aViewManager = *vmIt )
3146       {
3147         const ObjMap& anObjects = aStudy->getObjectProperties( aViewManager->getGlobalId() );
3148         for( ObjMap::ConstIterator objIt = anObjects.begin(); objIt != anObjects.end(); objIt++ )
3149         {
3150           _PTR(SObject) aSObj( aStudyDS->FindObjectID( objIt.key().toLatin1().constData() ) );
3151           if( aSObj )
3152           {
3153             CORBA::Object_var anObject = GeometryGUI::ClientSObjectToObject( aSObj );
3154             if( !CORBA::is_nil( anObject ) )
3155             {
3156               GEOM::GEOM_FieldStep_var aFieldStep = GEOM::GEOM_FieldStep::_narrow( anObject );
3157               if( !aFieldStep->_is_nil() )
3158               {
3159                 CORBA::String_var aStepEntry = aFieldStep->GetStudyEntry();
3160                 Handle(SALOME_InteractiveObject) aStepIO =
3161                   new SALOME_InteractiveObject( aStepEntry.in(), "GEOM", "TEMP_IO" );
3162                 Redisplay( aStepIO, false, false );
3163               }
3164             }
3165           }
3166         }
3167       }
3168     }
3169   }
3170   if(updateViewer)
3171     UpdateViewer();
3172 }