Salome HOME
Unused code removed.
[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         else
279           QFile::setPermissions( aTextureFileName, (QFile::Permissions)0x4FFFF );
280       }
281
282       if ( !anImageError.isEmpty() )
283       {
284         SUIT_MessageBox::warning( 0, QObject::tr( "SHAPE_IMAGE_ERROR" ),
285                                   QObject::tr( "IMAGE_CAN_NOT_BE_CREATED" ) + anImageError );
286       }
287
288       QPointF aPoint1( 0, 0 );            // 1: top left
289       QPointF aPoint2( aWidth, 0 );       // 2: top right
290       QPointF aPoint3( aWidth, aHeight ); // 3: bottom right
291       QPointF aPoint4( 0, aHeight );      // 4: bottom left
292
293       aPoint1 = aTrsf.map( aPoint1 );
294       aPoint2 = aTrsf.map( aPoint2 );
295       aPoint3 = aTrsf.map( aPoint3 );
296       aPoint4 = aTrsf.map( aPoint4 );
297
298       QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
299       QRectF aRect = aPolygon.boundingRect();
300
301       gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
302       gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
303       gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
304       gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
305
306       TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
307       TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
308       TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
309       TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
310
311       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
312       aWire.Closed( true );
313
314       setTextureFileName( aTextureFileName, false, false );
315       setFace( aWire, false, false );
316       isDeactivateSelection = true;
317     }
318     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
319     {
320       Handle(HYDROData_Profile) aProfile =
321         Handle(HYDROData_Profile)::DownCast( myObject );
322
323       TopoDS_Wire aProfileWire;
324
325       if ( aProfile->IsValid() ) {
326         TopoDS_Shape aProfileShape = aProfile->GetShape3D();
327
328         if ( !aProfileShape.IsNull() && 
329              aProfileShape.ShapeType() == TopAbs_WIRE ) {
330           aProfileWire = TopoDS::Wire( aProfileShape );
331         }
332       }
333
334       setWire( aProfileWire, false, false );  
335
336       QColor aWireColor = aProfile->GetBorderColor();
337       setBorderColor( aWireColor, false, false );
338     }
339     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
340               myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
341               myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
342     {
343       Handle(HYDROData_Object) aGeomObject =
344         Handle(HYDROData_Object)::DownCast( myObject );
345
346       TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
347
348       setShape( anObjShape, false, false );
349
350       QColor aFillingColor = aGeomObject->GetFillingColor();
351       QColor aBorderColor = aGeomObject->GetBorderColor();
352
353       setFillingColor( aFillingColor, false, false );
354       setBorderColor( aBorderColor, false, false );
355     }
356     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
357     {
358       Handle(HYDROData_DummyObject3D) anObject3D =
359         Handle(HYDROData_DummyObject3D)::DownCast( myObject );
360       TopoDS_Shape aShape3D = anObject3D->GetShape();
361
362       setShape( aShape3D, false, false );
363
364       QColor aFillingColor = anObject3D->GetFillingColor();
365       QColor aBorderColor = anObject3D->GetBorderColor();
366
367       setFillingColor( aFillingColor, false, false );
368       setBorderColor( aBorderColor, false, false );
369     }
370     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
371     {
372       Handle(HYDROData_ShapesGroup) aShapesGroup =
373         Handle(HYDROData_ShapesGroup)::DownCast( myObject );
374
375       TopTools_SequenceOfShape aShapes;
376       aShapesGroup->GetShapes( aShapes );
377
378       TopoDS_Compound aCompound;
379       BRep_Builder aCompoundBuilder;
380       aCompoundBuilder.MakeCompound( aCompound );
381
382       for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
383       {
384         const TopoDS_Shape& aShape = aShapes.Value( i );
385         aCompoundBuilder.Add( aCompound, aShape );
386       }
387
388       setShape( aCompound, false, false );  
389     }
390   }
391  
392   if ( myShape.IsNull() || !isVisible() )
393     return;
394
395   displayShape( theIsUpdateViewer );
396
397   if (isDeactivateSelection)
398     myContext->Deactivate(myShape);
399 }
400
401 void HYDROGUI_Shape::setVisible( const bool theState,
402                                  const bool theIsUpdateViewer )
403 {
404   myIsVisible = theState;
405
406   if ( myShape.IsNull() )
407     return;
408
409   if ( ( myIsVisible && myContext->IsDisplayed( myShape ) ) ||
410        ( !myIsVisible && !myContext->IsDisplayed( myShape ) ) )
411     return;
412
413   if ( myIsVisible ) {
414     displayShape( theIsUpdateViewer );
415   }
416   else
417     myContext->Erase( myShape, theIsUpdateViewer );
418 }
419
420 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
421 {
422   if ( myIsHighlight == theIsHighlight )
423     return;
424
425   myIsHighlight = theIsHighlight;
426
427   if ( myContext.IsNull() || myShape.IsNull() )
428     return;
429
430   colorShapeBorder( getActiveColor() );
431   displayShape( isUpdateViewer );
432 }
433
434 bool HYDROGUI_Shape::isHighlighted() const
435 {
436   return myIsHighlight;
437 }
438
439 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
440                               const bool         theToDisplay,
441                               const bool         theIsUpdateViewer )
442 {
443   myTopoShape = theWire;
444   myDisplayMode = AIS_WireFrame;
445
446   buildShape();
447   updateShape( theToDisplay, theIsUpdateViewer );
448 }
449
450 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
451                                const bool             theToDisplay,
452                                const bool             theIsUpdateViewer )
453 {
454   TopExp_Explorer anExp( theWires, TopAbs_WIRE );
455   TopoDS_Compound aCompound;
456   BRep_Builder aBuilder;
457     aBuilder.MakeCompound( aCompound );
458
459   for ( ; anExp.More(); anExp.Next() ) {
460     TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
461     if ( aWire.IsNull() ) {
462       continue;
463     }
464
465     BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
466     aMakeFace.Build();
467     if( aMakeFace.IsDone() ) {
468       aBuilder.Add( aCompound, aMakeFace.Face() );
469     }
470   }
471
472   myTopoShape = aCompound;
473   myDisplayMode = AIS_Shaded;
474
475   buildShape();
476   updateShape( theToDisplay, theIsUpdateViewer );
477 }
478
479 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
480                               const bool         theToDisplay,
481                               const bool         theIsUpdateViewer )
482 {
483   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
484   aFaceBuilder.Build();
485   if( aFaceBuilder.IsDone() )
486   {
487     TopoDS_Face aFace = aFaceBuilder.Face();
488     setFace( aFace, theToDisplay, theIsUpdateViewer );
489   }
490 }
491
492 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
493                               const bool         theToDisplay,
494                               const bool         theIsUpdateViewer )
495 {
496   myTopoShape = theFace;
497   myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
498   //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
499   //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
500
501   buildShape();
502   updateShape( theToDisplay, theIsUpdateViewer );
503 }
504
505 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
506                                const bool          theToDisplay,
507                                const bool          theIsUpdateViewer )
508 {
509   myTopoShape = theShape;
510   myDisplayMode = AIS_Shaded;
511
512   buildShape();
513   updateShape( theToDisplay, theIsUpdateViewer );
514 }
515
516 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
517                                       const bool    theToDisplay,
518                                       const bool    theIsUpdateViewer )
519 {
520   myFillingColor = theColor;
521   updateShape( theToDisplay, theIsUpdateViewer );
522 }
523
524 QColor HYDROGUI_Shape::getFillingColor() const
525 {
526   return myFillingColor;
527 }
528
529 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
530                                      const bool    theToDisplay,
531                                      const bool    theIsUpdateViewer )
532 {
533   myBorderColor = theColor;
534   updateShape( theToDisplay, theIsUpdateViewer );
535 }
536
537 QColor HYDROGUI_Shape::getBorderColor() const
538 {
539   return myBorderColor;
540 }
541
542 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
543 {
544   myHighlightColor = theColor;
545 }
546
547 QColor HYDROGUI_Shape::getHighlightColor() const
548 {
549   return myHighlightColor;
550 }
551
552 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
553                                          const bool     theToDisplay,
554                                          const bool     theIsUpdateViewer )
555 {
556   myTextureFileName = theFileName;
557   updateShape( theToDisplay, theIsUpdateViewer );
558 }
559
560 QString HYDROGUI_Shape::getTextureFileName() const
561 {
562   return myTextureFileName;
563 }
564
565 void HYDROGUI_Shape::setZLayer( const int theZLayer )
566 {
567   if ( myZLayer == theZLayer )
568     return;
569
570   myZLayer = theZLayer;
571   if ( !myShape.IsNull() && isVisible() && !myContext.IsNull() && myZLayer >= 0 )
572     myContext->SetZLayer( myShape, myZLayer );
573 }
574
575 void HYDROGUI_Shape::buildShape()
576 {
577   // Erase previously created shape
578   erase();
579
580   if ( myTopoShape.IsNull() ) {
581     myShape.Nullify();
582     return;
583   }
584
585   QString aTextureFileName = getTextureFileName();
586   bool anIsTexture = !aTextureFileName.isEmpty();
587
588   if ( anIsTexture )
589   {
590     myShape = new AIS_TexturedShape( myTopoShape );
591   }
592   else
593   {
594     myShape = new AIS_Shape( myTopoShape );
595   }
596
597   myShape->SetHLRAngleAndDeviation( 0.001 );
598
599   if ( !myObject.IsNull() )
600     myShape->SetOwner( myObject );
601
602   myShape->SetTransparency( 0 );
603   myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
604
605   if( anIsTexture )
606   {
607     Handle(AIS_TexturedShape) aTexturedShape = 
608       Handle(AIS_TexturedShape)::DownCast( myShape );
609
610     aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
611     aTexturedShape->SetTextureMapOn();
612     // Just use the texture image as is
613     aTexturedShape->DisableTextureModulate();
614     aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
615   }
616
617     // Init default params for shape
618   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
619   if ( !anAttributes.IsNull() )
620   {
621     Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
622     if ( !anIsoAspect.IsNull() ) {
623       anIsoAspect->SetNumber( 0 );
624       anAttributes->SetUIsoAspect( anIsoAspect );
625     }
626       
627     anIsoAspect = anAttributes->VIsoAspect();
628     if ( !anIsoAspect.IsNull() ) {
629       anIsoAspect->SetNumber( 0 );
630       anAttributes->SetVIsoAspect( anIsoAspect );
631     }
632
633     if ( myDisplayMode == AIS_Shaded )
634     {
635       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
636       if ( !aShadingAspect.IsNull() )
637       {
638         Graphic3d_MaterialAspect aMatAspect( Graphic3d_NOM_PLASTIC );
639         //aMatAspect.SetAmbient( 1 );
640         //aMatAspect.SetDiffuse( 0 );
641
642         aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
643         aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
644       }
645     }
646     else if ( myDisplayMode == AIS_WireFrame )
647     {
648       anAttributes->SetWireDraw( true );
649     }
650   }
651 }
652
653 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
654                                   const bool theIsUpdateViewer )
655 {
656   if ( myShape.IsNull() )
657     return;
658
659   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
660   if ( !anAttributes.IsNull() )
661   {
662     if ( myDisplayMode == AIS_Shaded )
663     {
664       // Coloring face filling
665       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
666       if ( !aShadingAspect.IsNull() )
667       {
668         Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
669                                       getQuantityColorVal( myFillingColor.green() ),
670                                       getQuantityColorVal( myFillingColor.blue() ),
671                                       Quantity_TOC_RGB );
672
673         aShadingAspect->SetColor( aFillingColor );
674         aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
675       }
676     }
677     else if ( myDisplayMode == AIS_WireFrame )
678     {
679     }
680
681     // Coloring borders
682     colorShapeBorder( getActiveColor() );
683   }
684
685   if ( !theToDisplay || !isVisible() || myContext.IsNull() )
686     return;
687   
688   displayShape( theIsUpdateViewer );
689 }
690
691 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
692 {
693   myContext->Display( myShape, Standard_False );
694
695   if ( myZLayer >= 0 )
696     myContext->SetZLayer( myShape, myZLayer );
697
698   myContext->UpdateCurrentViewer();
699 }
700
701 QColor HYDROGUI_Shape::getActiveColor() const
702 {
703   return isHighlighted() ? myHighlightColor : myBorderColor;
704 }
705
706 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
707 {
708   return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
709 }
710
711 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
712 {
713   if ( myShape.IsNull() )
714     return;
715
716   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
717   if ( anAttributes.IsNull() )
718     return;
719
720   Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
721                                getQuantityColorVal( theColor.green() ),
722                                getQuantityColorVal( theColor.blue() ),
723                                Quantity_TOC_RGB );
724   if ( myDisplayMode == AIS_Shaded )
725   {
726     if ( theColor.alpha() == 0 )
727     {
728       anAttributes->SetFaceBoundaryDraw( false );
729     }
730     else
731     {
732       anAttributes->SetFaceBoundaryDraw( true );
733
734       Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
735       if ( !aBoundaryAspect.IsNull() )
736       {
737         aBoundaryAspect->SetColor( aBorderColor );
738         anAttributes->SetFaceBoundaryAspect( aBoundaryAspect );
739       }
740     }
741   }
742   else if ( myDisplayMode == AIS_WireFrame )
743   {
744     myShape->SetColor( aBorderColor );
745   }
746 }
747
748 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
749 {
750   QString aResult;
751   if( !theImageObj.IsNull() )
752   {
753     QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
754
755     int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
756     QString aPrefix = QString( "image_%1" ).arg( aStudyId );
757
758     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
759     anEntry.replace( ':', '_' );
760
761     QString anExtension = "bmp";
762
763     aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
764   }
765   return aResult;
766 }
767
768 void HYDROGUI_Shape::removeTextureFile() const
769 {
770   QFile aFile( getTextureFileName() );
771   if( aFile.exists() )
772     aFile.remove();
773 }
774
775 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
776 {
777   double aSizeLimit = (double)theSizeLimit;
778   double aWidth = (double)theImage.width();
779   double aHeight = (double)theImage.height();
780   if( aWidth > aSizeLimit || aHeight > aSizeLimit )
781   {
782     if( aWidth > aHeight )
783     {
784       aHeight /= ( aWidth / aSizeLimit );
785       aWidth = aSizeLimit;
786     }
787     else
788     {
789       aWidth /= ( aHeight / aSizeLimit );
790       aHeight = aSizeLimit;
791     }
792   }
793   return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );
794 }