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