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