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