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