Salome HOME
patch for crash in channel
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Shape.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Shape.h"
24
25 #include "HYDROGUI_DataObject.h"
26 #include "HYDROGUI_Tool.h"
27
28 #include <AIS_Drawer.hxx>
29 #include <AIS_TexturedShape.hxx>
30
31 #include <V3d_Viewer.hxx>
32
33 #include <BRepBuilderAPI_MakeEdge.hxx>
34 #include <BRepBuilderAPI_MakeWire.hxx>
35 #include <BRepBuilderAPI_MakeFace.hxx>
36
37 #include <gp_Pnt.hxx>
38
39 #include <Graphic3d_AspectFillArea3d.hxx>
40 #include <Graphic3d_MaterialAspect.hxx>
41
42 #include <HYDROData_Channel.h>
43 #include <HYDROData_Document.h>
44 #include <HYDROData_DummyObject3D.h>
45 #include <HYDROData_Image.h>
46 #include <HYDROData_ImmersibleZone.h>
47 #include <HYDROData_Obstacle.h>
48 #include <HYDROData_PolylineXY.h>
49 #include <HYDROData_Polyline3D.h>
50 #include <HYDROData_Profile.h>
51 #include <HYDROData_Region.h>
52 #include <HYDROData_ShapesGroup.h>
53 #include <HYDROData_Stream.h>
54 #include <HYDROData_Zone.h>
55
56 #include <TopoDS.hxx>
57 #include <TopoDS_Wire.hxx>
58 #include <TopoDS_Face.hxx>
59
60 #include <TopExp_Explorer.hxx>
61
62 #include <BRep_Builder.hxx>
63
64 #include <Precision.hxx>
65
66 #include <Prs3d_ShadingAspect.hxx>
67 #include <Prs3d_LineAspect.hxx>
68 #include <Prs3d_IsoAspect.hxx>
69
70 #include <SUIT_MessageBox.h>
71
72 #include <QColor>
73 #include <QFile>
74
75 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
76                                 const Handle(HYDROData_Entity)&       theObject,
77                                 const int                             theZLayer )
78 : myContext( theContext ),
79   myObject( theObject ),
80   myZLayer( theZLayer ),
81   myIsHighlight( false ),
82   myFillingColor( Qt::transparent ),
83   myBorderColor( Qt::black ),
84   myHighlightColor( Qt::white ),
85   myIsToUpdate( false ),
86   myIsVisible( true ),
87   myDisplayMode( AIS_WireFrame )
88 {
89 }
90
91 HYDROGUI_Shape::~HYDROGUI_Shape()
92 {
93   erase( false );
94
95   if ( !myShape.IsNull() )
96     myShape.Nullify();
97
98   removeTextureFile();
99 }
100
101 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
102 {
103   if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
104     return;
105
106   displayShape( theIsUpdateViewer );
107 }
108
109 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
110 {
111   if ( myContext.IsNull() || myShape.IsNull() )
112     return;
113
114   myContext->Erase( myShape, theIsUpdateViewer );
115 }
116
117 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
118 {
119   setIsToUpdate( false );
120
121   if ( myContext.IsNull() )
122     return;
123
124   bool isDeactivateSelection = false;
125   // Try to retrieve information from object
126   if ( !myObject.IsNull() )
127   {
128     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
129   
130     if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
131     {
132       Handle(HYDROData_ImmersibleZone) aZoneObj =
133         Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
134
135       TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
136       if ( !aZoneShape.IsNull() ) {
137         if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
138           TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
139           setFace( aZoneFace, false, false );
140         } else {
141           myTopoShape = aZoneShape;
142           myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
143
144           buildShape();
145           updateShape( false, false );
146         }
147       }
148
149       QColor aFillingColor = aZoneObj->GetFillingColor();
150       QColor aBorderColor = aZoneObj->GetBorderColor();
151
152       setFillingColor( aFillingColor, false, false );
153       setBorderColor( aBorderColor, false, false );
154     }
155     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
156     {
157       Handle(HYDROData_PolylineXY) aPolyline =
158         Handle(HYDROData_PolylineXY)::DownCast( myObject );
159
160       TopoDS_Shape aPolylineShape = aPolyline->GetShape();
161
162       if ( !aPolylineShape.IsNull() ) {
163         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
164           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
165           setWire( aPolylineWire, false, false );  
166         } else {
167           myTopoShape = aPolylineShape;
168           myDisplayMode = AIS_WireFrame;
169
170           buildShape();
171           updateShape( false, false );
172         }
173       }
174
175       QColor aWireColor = aPolyline->GetWireColor();
176       setBorderColor( aWireColor, false, false );
177     }
178     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
179     {
180       Handle(HYDROData_Polyline3D) aPolyline =
181         Handle(HYDROData_Polyline3D)::DownCast( myObject );
182
183       TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
184
185       if ( !aPolylineShape.IsNull() ) {
186         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
187           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
188           setWire( aPolylineWire, false, false );  
189         } else {
190           myTopoShape = aPolylineShape;
191           myDisplayMode = AIS_WireFrame;
192
193           buildShape();
194           updateShape( false, false );
195         }
196       }
197
198       QColor aWireColor = aPolyline->GetBorderColor();
199       setBorderColor( aWireColor, false, false );
200     }
201     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
202     {
203       Handle(HYDROData_Zone) aZone =
204         Handle(HYDROData_Zone)::DownCast( myObject );
205
206       TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
207
208       setFace( aZoneFace, false, false );
209       if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
210       {
211         // Red color for a zone with bathymetry conflict
212         setFillingColor( Qt::red );
213       }
214       else
215       {
216         // Generate the filling color for zone
217         QStringList aGeomObjectsNames;
218
219         HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
220         HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
221         for ( ; anIter.More(); anIter.Next() )
222         {
223           Handle(HYDROData_Object) aRefbject = 
224             Handle(HYDROData_Object)::DownCast( anIter.Value() );
225           if ( aRefbject.IsNull() )
226             continue;
227
228           QString aRefObjectName = aRefbject->GetName();
229           if ( aRefObjectName.isEmpty() )
230             continue;
231
232           aGeomObjectsNames.append( aRefObjectName );
233         }
234
235         setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
236       }
237     }
238     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
239     {
240       Handle(HYDROData_Image) anImageObj =
241         Handle(HYDROData_Image)::DownCast( myObject );
242
243       removeTextureFile();
244
245       QString aTextureFileName = generateTextureFileName( anImageObj );
246
247       QImage anImage = anImageObj->Image();
248       QString aFilePath = anImageObj->GetFilePath();
249       QTransform aTrsf = anImageObj->Trsf();
250
251       int aWidth = anImage.width();
252       int aHeight = anImage.height();
253
254       QString anImageError = "";
255
256       QTransform anInversion = QTransform::fromScale( -1, -1 );
257       anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
258
259       if ( anImage.isNull() )
260         anImageError = QObject::tr( "IMAGE_TRANSFORMATION_CAN_NOT_BE_APPLYED" );
261       else
262       {
263         // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
264         int aTrsfWidth = anImage.width();
265         int aDelta = aTrsfWidth % 4;
266         if ( aDelta > 0 )
267         {
268           aTrsfWidth += ( 4 - aDelta );
269         }
270         anImage = anImage.scaledToWidth( aTrsfWidth );
271
272         // temporary optimization, to reduce the saved image size (and the texture quality)
273         QImage anImageToSave = anImage; //RKV:reduceTexture( anImage, 500 );
274
275         bool isSaved = anImageToSave.save( aTextureFileName );
276         if ( !isSaved )
277           anImageError = QObject::tr( "FILE_CAN_NOT_BE_CREATED" ).arg( aTextureFileName );
278       }
279
280       if ( !anImageError.isEmpty() )
281       {
282         SUIT_MessageBox::warning( 0, QObject::tr( "SHAPE_IMAGE_ERROR" ),
283                                   QObject::tr( "IMAGE_CAN_NOT_BE_CREATED" ) + anImageError );
284       }
285
286       QPointF aPoint1( 0, 0 );            // 1: top left
287       QPointF aPoint2( aWidth, 0 );       // 2: top right
288       QPointF aPoint3( aWidth, aHeight ); // 3: bottom right
289       QPointF aPoint4( 0, aHeight );      // 4: bottom left
290
291       aPoint1 = aTrsf.map( aPoint1 );
292       aPoint2 = aTrsf.map( aPoint2 );
293       aPoint3 = aTrsf.map( aPoint3 );
294       aPoint4 = aTrsf.map( aPoint4 );
295
296       QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
297       QRectF aRect = aPolygon.boundingRect();
298
299       gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
300       gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
301       gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
302       gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
303
304       TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
305       TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
306       TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
307       TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
308
309       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
310       aWire.Closed( true );
311
312       setTextureFileName( aTextureFileName, false, false );
313       setFace( aWire, false, false );
314       isDeactivateSelection = true;
315     }
316     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
317     {
318       Handle(HYDROData_Profile) aProfile =
319         Handle(HYDROData_Profile)::DownCast( myObject );
320
321       TopoDS_Wire aProfileWire;
322
323       if ( aProfile->IsValid() ) {
324         TopoDS_Shape aProfileShape = aProfile->GetShape3D();
325
326         if ( !aProfileShape.IsNull() && 
327              aProfileShape.ShapeType() == TopAbs_WIRE ) {
328           aProfileWire = TopoDS::Wire( aProfileShape );
329         }
330       }
331
332       setWire( aProfileWire, false, false );  
333
334       QColor aWireColor = aProfile->GetBorderColor();
335       setBorderColor( aWireColor, false, false );
336     }
337     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
338               myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
339               myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
340     {
341       Handle(HYDROData_Object) aGeomObject =
342         Handle(HYDROData_Object)::DownCast( myObject );
343
344       TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
345
346       setShape( anObjShape, false, false );
347
348       QColor aFillingColor = aGeomObject->GetFillingColor();
349       QColor aBorderColor = aGeomObject->GetBorderColor();
350
351       setFillingColor( aFillingColor, false, false );
352       setBorderColor( aBorderColor, false, false );
353     }
354     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
355     {
356       Handle(HYDROData_DummyObject3D) anObject3D =
357         Handle(HYDROData_DummyObject3D)::DownCast( myObject );
358       TopoDS_Shape aShape3D = anObject3D->GetShape();
359
360       setShape( aShape3D, false, false );
361
362       QColor aFillingColor = anObject3D->GetFillingColor();
363       QColor aBorderColor = anObject3D->GetBorderColor();
364
365       setFillingColor( aFillingColor, false, false );
366       setBorderColor( aBorderColor, false, false );
367     }
368     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
369     {
370       Handle(HYDROData_ShapesGroup) aShapesGroup =
371         Handle(HYDROData_ShapesGroup)::DownCast( myObject );
372
373       TopTools_SequenceOfShape aShapes;
374       aShapesGroup->GetShapes( aShapes );
375
376       TopoDS_Compound aCompound;
377       BRep_Builder aCompoundBuilder;
378       aCompoundBuilder.MakeCompound( aCompound );
379
380       for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
381       {
382         const TopoDS_Shape& aShape = aShapes.Value( i );
383         aCompoundBuilder.Add( aCompound, aShape );
384       }
385
386       setShape( aCompound, false, false );  
387     }
388   }
389  
390   if ( myShape.IsNull() || !isVisible() )
391     return;
392
393   displayShape( theIsUpdateViewer );
394
395   if (isDeactivateSelection)
396     myContext->Deactivate(myShape);
397 }
398
399 void HYDROGUI_Shape::setVisible( const bool theState,
400                                  const bool theIsUpdateViewer )
401 {
402   myIsVisible = theState;
403
404   if ( myShape.IsNull() )
405     return;
406
407   if ( ( myIsVisible && myContext->IsDisplayed( myShape ) ) ||
408        ( !myIsVisible && !myContext->IsDisplayed( myShape ) ) )
409     return;
410
411   if ( myIsVisible ) {
412     displayShape( theIsUpdateViewer );
413   }
414   else
415     myContext->Erase( myShape, theIsUpdateViewer );
416 }
417
418 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
419 {
420   if ( myIsHighlight == theIsHighlight )
421     return;
422
423   myIsHighlight = theIsHighlight;
424
425   if ( myContext.IsNull() || myShape.IsNull() )
426     return;
427
428   colorShapeBorder( getActiveColor() );
429   displayShape( isUpdateViewer );
430 }
431
432 bool HYDROGUI_Shape::isHighlighted() const
433 {
434   return myIsHighlight;
435 }
436
437 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
438                               const bool         theToDisplay,
439                               const bool         theIsUpdateViewer )
440 {
441   myTopoShape = theWire;
442   myDisplayMode = AIS_WireFrame;
443
444   buildShape();
445   updateShape( theToDisplay, theIsUpdateViewer );
446 }
447
448 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
449                                const bool             theToDisplay,
450                                const bool             theIsUpdateViewer )
451 {
452   TopExp_Explorer anExp( theWires, TopAbs_WIRE );
453   TopoDS_Compound aCompound;
454   BRep_Builder aBuilder;
455     aBuilder.MakeCompound( aCompound );
456
457   for ( ; anExp.More(); anExp.Next() ) {
458     TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
459     if ( aWire.IsNull() ) {
460       continue;
461     }
462
463     BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
464     aMakeFace.Build();
465     if( aMakeFace.IsDone() ) {
466       aBuilder.Add( aCompound, aMakeFace.Face() );
467     }
468   }
469
470   myTopoShape = aCompound;
471   myDisplayMode = AIS_Shaded;
472
473   buildShape();
474   updateShape( theToDisplay, theIsUpdateViewer );
475 }
476
477 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
478                               const bool         theToDisplay,
479                               const bool         theIsUpdateViewer )
480 {
481   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
482   aFaceBuilder.Build();
483   if( aFaceBuilder.IsDone() )
484   {
485     TopoDS_Face aFace = aFaceBuilder.Face();
486     setFace( aFace, theToDisplay, theIsUpdateViewer );
487   }
488 }
489
490 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
491                               const bool         theToDisplay,
492                               const bool         theIsUpdateViewer )
493 {
494   myTopoShape = theFace;
495   myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
496   //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
497   //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
498
499   buildShape();
500   updateShape( theToDisplay, theIsUpdateViewer );
501 }
502
503 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
504                                const bool          theToDisplay,
505                                const bool          theIsUpdateViewer )
506 {
507   myTopoShape = theShape;
508   myDisplayMode = AIS_Shaded;
509
510   buildShape();
511   updateShape( theToDisplay, theIsUpdateViewer );
512 }
513
514 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
515                                       const bool    theToDisplay,
516                                       const bool    theIsUpdateViewer )
517 {
518   myFillingColor = theColor;
519   updateShape( theToDisplay, theIsUpdateViewer );
520 }
521
522 QColor HYDROGUI_Shape::getFillingColor() const
523 {
524   return myFillingColor;
525 }
526
527 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
528                                      const bool    theToDisplay,
529                                      const bool    theIsUpdateViewer )
530 {
531   myBorderColor = theColor;
532   updateShape( theToDisplay, theIsUpdateViewer );
533 }
534
535 QColor HYDROGUI_Shape::getBorderColor() const
536 {
537   return myBorderColor;
538 }
539
540 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
541 {
542   myHighlightColor = theColor;
543 }
544
545 QColor HYDROGUI_Shape::getHighlightColor() const
546 {
547   return myHighlightColor;
548 }
549
550 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
551                                          const bool     theToDisplay,
552                                          const bool     theIsUpdateViewer )
553 {
554   myTextureFileName = theFileName;
555   updateShape( theToDisplay, theIsUpdateViewer );
556 }
557
558 QString HYDROGUI_Shape::getTextureFileName() const
559 {
560   return myTextureFileName;
561 }
562
563 void HYDROGUI_Shape::setZLayer( const int theZLayer )
564 {
565   if ( myZLayer == theZLayer )
566     return;
567
568   myZLayer = theZLayer;
569   if ( !myShape.IsNull() && isVisible() && !myContext.IsNull() && myZLayer >= 0 )
570     myContext->SetZLayer( myShape, myZLayer );
571 }
572
573 void HYDROGUI_Shape::buildShape()
574 {
575   // Erase previously created shape
576   erase();
577
578   if ( myTopoShape.IsNull() ) {
579     myShape.Nullify();
580     return;
581   }
582
583   QString aTextureFileName = getTextureFileName();
584   bool anIsTexture = !aTextureFileName.isEmpty();
585
586   if ( anIsTexture )
587   {
588     myShape = new AIS_TexturedShape( myTopoShape );
589   }
590   else
591   {
592     myShape = new AIS_Shape( myTopoShape );
593   }
594
595   myShape->SetHLRAngleAndDeviation( 0.001 );
596
597   if ( !myObject.IsNull() )
598     myShape->SetOwner( myObject );
599
600   myShape->SetTransparency( 0 );
601   myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
602
603   if( anIsTexture )
604   {
605     Handle(AIS_TexturedShape) aTexturedShape = 
606       Handle(AIS_TexturedShape)::DownCast( myShape );
607
608     aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
609     aTexturedShape->SetTextureMapOn();
610     // Just use the texture image as is
611     aTexturedShape->DisableTextureModulate();
612     aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
613   }
614
615     // Init default params for shape
616   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
617   if ( !anAttributes.IsNull() )
618   {
619     Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
620     if ( !anIsoAspect.IsNull() ) {
621       anIsoAspect->SetNumber( 0 );
622       anAttributes->SetUIsoAspect( anIsoAspect );
623     }
624       
625     anIsoAspect = anAttributes->VIsoAspect();
626     if ( !anIsoAspect.IsNull() ) {
627       anIsoAspect->SetNumber( 0 );
628       anAttributes->SetVIsoAspect( anIsoAspect );
629     }
630
631     if ( myDisplayMode == AIS_Shaded )
632     {
633       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
634       if ( !aShadingAspect.IsNull() )
635       {
636         Graphic3d_MaterialAspect aMatAspect( Graphic3d_NOM_PLASTIC );
637         //aMatAspect.SetAmbient( 1 );
638         //aMatAspect.SetDiffuse( 0 );
639
640         aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
641         aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
642       }
643     }
644     else if ( myDisplayMode == AIS_WireFrame )
645     {
646       anAttributes->SetWireDraw( true );
647     }
648   }
649 }
650
651 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
652                                   const bool theIsUpdateViewer )
653 {
654   if ( myShape.IsNull() )
655     return;
656
657   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
658   if ( !anAttributes.IsNull() )
659   {
660     if ( myDisplayMode == AIS_Shaded )
661     {
662       // Coloring face filling
663       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
664       if ( !aShadingAspect.IsNull() )
665       {
666         Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
667                                       getQuantityColorVal( myFillingColor.green() ),
668                                       getQuantityColorVal( myFillingColor.blue() ),
669                                       Quantity_TOC_RGB );
670
671         aShadingAspect->SetColor( aFillingColor );
672         aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
673       }
674     }
675     else if ( myDisplayMode == AIS_WireFrame )
676     {
677     }
678
679     // Coloring borders
680     colorShapeBorder( getActiveColor() );
681   }
682
683   if ( !theToDisplay || !isVisible() || myContext.IsNull() )
684     return;
685   
686   displayShape( theIsUpdateViewer );
687 }
688
689 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
690 {
691   myContext->Display( myShape, Standard_False );
692
693   if ( myZLayer >= 0 )
694     myContext->SetZLayer( myShape, myZLayer );
695
696   myContext->UpdateCurrentViewer();
697 }
698
699 QColor HYDROGUI_Shape::getActiveColor() const
700 {
701   return isHighlighted() ? myHighlightColor : myBorderColor;
702 }
703
704 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
705 {
706   return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
707 }
708
709 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
710 {
711   if ( myShape.IsNull() )
712     return;
713
714   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
715   if ( anAttributes.IsNull() )
716     return;
717
718   Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
719                                getQuantityColorVal( theColor.green() ),
720                                getQuantityColorVal( theColor.blue() ),
721                                Quantity_TOC_RGB );
722   if ( myDisplayMode == AIS_Shaded )
723   {
724     if ( theColor.alpha() == 0 )
725     {
726       anAttributes->SetFaceBoundaryDraw( false );
727     }
728     else
729     {
730       anAttributes->SetFaceBoundaryDraw( true );
731
732       Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
733       if ( !aBoundaryAspect.IsNull() )
734       {
735         aBoundaryAspect->SetColor( aBorderColor );
736         anAttributes->SetFaceBoundaryAspect( aBoundaryAspect );
737       }
738     }
739   }
740   else if ( myDisplayMode == AIS_WireFrame )
741   {
742     myShape->SetColor( aBorderColor );
743   }
744 }
745
746 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
747 {
748   QString aResult;
749   if( !theImageObj.IsNull() )
750   {
751     QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
752
753     int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
754     QString aPrefix = QString( "image_%1" ).arg( aStudyId );
755
756     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
757     anEntry.replace( ':', '_' );
758
759     QString anExtension = "bmp";
760
761     aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
762   }
763   return aResult;
764 }
765
766 void HYDROGUI_Shape::removeTextureFile() const
767 {
768   QFile aFile( getTextureFileName() );
769   if( aFile.exists() )
770     aFile.remove();
771 }
772
773 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
774 {
775   double aSizeLimit = (double)theSizeLimit;
776   double aWidth = (double)theImage.width();
777   double aHeight = (double)theImage.height();
778   if( aWidth > aSizeLimit || aHeight > aSizeLimit )
779   {
780     if( aWidth > aHeight )
781     {
782       aHeight /= ( aWidth / aSizeLimit );
783       aWidth = aSizeLimit;
784     }
785     else
786     {
787       aWidth /= ( aHeight / aSizeLimit );
788       aHeight = aSizeLimit;
789     }
790   }
791   return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );
792 }