Salome HOME
Icons are shown in the object browser tree.
[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   }
318  
319   if ( myShape.IsNull() || !isVisible() )
320     return;
321
322   myContext->Display( myShape, theIsUpdateViewer );
323 }
324
325 void HYDROGUI_Shape::setVisible( const bool theState,
326                                  const bool theIsUpdateViewer )
327 {
328   if ( myIsVisible == theState )
329     return;
330
331   myIsVisible = theState;
332
333   if ( myShape.IsNull() )
334     return;
335
336   if ( myIsVisible )
337     myContext->Display( myShape, theIsUpdateViewer );
338   else
339     myContext->Erase( myShape, theIsUpdateViewer );
340 }
341
342 void HYDROGUI_Shape::highlight( bool theIsHighlight )
343 {
344   if ( myIsHighlight == theIsHighlight )
345     return;
346
347   myIsHighlight = theIsHighlight;
348
349   if ( myContext.IsNull() || myShape.IsNull() )
350     return;
351
352   colorShapeBorder( getActiveColor() );
353   myContext->Display( myShape );
354 }
355
356 bool HYDROGUI_Shape::isHighlighted() const
357 {
358   return myIsHighlight;
359 }
360
361 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
362                               const bool         theToDisplay,
363                               const bool         theIsUpdateViewer )
364 {
365   myTopoShape = theWire;
366   myDisplayMode = AIS_WireFrame;
367
368   buildShape();
369   updateShape( theToDisplay, theIsUpdateViewer );
370 }
371
372 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
373                                const bool             theToDisplay,
374                                const bool             theIsUpdateViewer )
375 {
376   TopExp_Explorer anExp( theWires, TopAbs_WIRE );
377   TopoDS_Compound aCompound;
378   BRep_Builder aBuilder;
379     aBuilder.MakeCompound( aCompound );
380
381   for ( ; anExp.More(); anExp.Next() ) {
382     TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
383     if ( aWire.IsNull() ) {
384       continue;
385     }
386
387     BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
388     aMakeFace.Build();
389     if( aMakeFace.IsDone() ) {
390       aBuilder.Add( aCompound, aMakeFace.Face() );
391     }
392   }
393
394   myTopoShape = aCompound;
395   myDisplayMode = AIS_Shaded;
396
397   buildShape();
398   updateShape( theToDisplay, theIsUpdateViewer );
399 }
400
401 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
402                               const bool         theToDisplay,
403                               const bool         theIsUpdateViewer )
404 {
405   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
406   aFaceBuilder.Build();
407   if( aFaceBuilder.IsDone() )
408   {
409     TopoDS_Face aFace = aFaceBuilder.Face();
410     setFace( aFace, theToDisplay, theIsUpdateViewer );
411   }
412 }
413
414 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
415                               const bool         theToDisplay,
416                               const bool         theIsUpdateViewer )
417 {
418   myTopoShape = theFace;
419   myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
420   //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
421   //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
422
423   buildShape();
424   updateShape( theToDisplay, theIsUpdateViewer );
425 }
426
427 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
428                                const bool          theToDisplay,
429                                const bool          theIsUpdateViewer )
430 {
431   myTopoShape = theShape;
432   myDisplayMode = AIS_Shaded;
433
434   buildShape();
435   updateShape( theToDisplay, theIsUpdateViewer );
436 }
437
438 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
439                                       const bool    theToDisplay,
440                                       const bool    theIsUpdateViewer )
441 {
442   myFillingColor = theColor;
443   updateShape( theToDisplay, theIsUpdateViewer );
444 }
445
446 QColor HYDROGUI_Shape::getFillingColor() const
447 {
448   return myFillingColor;
449 }
450
451 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
452                                      const bool    theToDisplay,
453                                      const bool    theIsUpdateViewer )
454 {
455   myBorderColor = theColor;
456   updateShape( theToDisplay, theIsUpdateViewer );
457 }
458
459 QColor HYDROGUI_Shape::getBorderColor() const
460 {
461   return myBorderColor;
462 }
463
464 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
465 {
466   myHighlightColor = theColor;
467 }
468
469 QColor HYDROGUI_Shape::getHighlightColor() const
470 {
471   return myHighlightColor;
472 }
473
474 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
475                                          const bool     theToDisplay,
476                                          const bool     theIsUpdateViewer )
477 {
478   myTextureFileName = theFileName;
479   updateShape( theToDisplay, theIsUpdateViewer );
480 }
481
482 QString HYDROGUI_Shape::getTextureFileName() const
483 {
484   return myTextureFileName;
485 }
486
487 void HYDROGUI_Shape::buildShape()
488 {
489   // Erase previously created shape
490   erase();
491
492   if ( myTopoShape.IsNull() ) {
493     myShape.Nullify();
494     return;
495   }
496
497   QString aTextureFileName = getTextureFileName();
498   bool anIsTexture = !aTextureFileName.isEmpty();
499
500   if ( anIsTexture )
501   {
502     myShape = new AIS_TexturedShape( myTopoShape );
503   }
504   else
505   {
506     myShape = new AIS_Shape( myTopoShape );
507   }
508
509   if ( !myObject.IsNull() )
510     myShape->SetOwner( myObject );
511
512   myShape->SetTransparency( 0 );
513   myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
514
515   if( anIsTexture )
516   {
517     Handle(AIS_TexturedShape) aTexturedShape = 
518       Handle(AIS_TexturedShape)::DownCast( myShape );
519
520     aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
521     aTexturedShape->SetTextureMapOn();
522     // Just use the texture image as is
523     aTexturedShape->DisableTextureModulate();
524     aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
525   }
526
527     // Init default params for shape
528   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
529   if ( !anAttributes.IsNull() )
530   {
531     if ( myDisplayMode == AIS_Shaded )
532     {
533       Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
534       if ( !anIsoAspect.IsNull() )
535         anIsoAspect->SetNumber( 0 );
536       
537       anIsoAspect = anAttributes->VIsoAspect();
538       if ( !anIsoAspect.IsNull() )
539         anIsoAspect->SetNumber( 0 );
540
541       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
542       if ( !aShadingAspect.IsNull() )
543       {
544         Graphic3d_MaterialAspect aMatAspect;
545         aMatAspect.SetAmbient( 1 );
546         aMatAspect.SetDiffuse( 0 );
547
548         aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
549         aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
550       }
551     }
552     else if ( myDisplayMode == AIS_WireFrame )
553     {
554       anAttributes->SetWireDraw( true );
555     }
556   }
557 }
558
559 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
560                                   const bool theIsUpdateViewer )
561 {
562   if ( myShape.IsNull() )
563     return;
564
565   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
566   if ( !anAttributes.IsNull() )
567   {
568     if ( myDisplayMode == AIS_Shaded )
569     {
570       // Coloring face filling
571       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
572       if ( !aShadingAspect.IsNull() )
573       {
574         Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
575                                       getQuantityColorVal( myFillingColor.green() ),
576                                       getQuantityColorVal( myFillingColor.blue() ),
577                                       Quantity_TOC_RGB );
578
579         aShadingAspect->SetColor( aFillingColor );
580         aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
581       }
582     }
583     else if ( myDisplayMode == AIS_WireFrame )
584     {
585     }
586
587     // Coloring borders
588     colorShapeBorder( getActiveColor() );
589   }
590
591   if ( !theToDisplay || !isVisible() || myContext.IsNull() )
592     return;
593   
594   myContext->Display( myShape, theIsUpdateViewer );
595 }
596
597 QColor HYDROGUI_Shape::getActiveColor() const
598 {
599   return isHighlighted() ? myHighlightColor : myBorderColor;
600 }
601
602 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
603 {
604   return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
605 }
606
607 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
608 {
609   if ( myShape.IsNull() )
610     return;
611
612   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
613   if ( anAttributes.IsNull() )
614     return;
615
616   Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
617                                getQuantityColorVal( theColor.green() ),
618                                getQuantityColorVal( theColor.blue() ),
619                                Quantity_TOC_RGB );
620   if ( myDisplayMode == AIS_Shaded )
621   {
622     if ( theColor.alpha() == 0 )
623     {
624       anAttributes->SetFaceBoundaryDraw( false );
625     }
626     else
627     {
628       anAttributes->SetFaceBoundaryDraw( true );
629
630       Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
631       if ( !aBoundaryAspect.IsNull() )
632         aBoundaryAspect->SetColor( aBorderColor );
633     }
634   }
635   else if ( myDisplayMode == AIS_WireFrame )
636   {
637     myShape->SetColor( aBorderColor );
638   }
639 }
640
641 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
642 {
643   QString aResult;
644   if( !theImageObj.IsNull() )
645   {
646     QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
647
648     int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
649     QString aPrefix = QString( "image_%1" ).arg( aStudyId );
650
651     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
652     anEntry.replace( ':', '_' );
653
654     QString anExtension = "bmp";
655
656     aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
657   }
658   return aResult;
659 }
660
661 void HYDROGUI_Shape::removeTextureFile() const
662 {
663   QFile aFile( getTextureFileName() );
664   if( aFile.exists() )
665     aFile.remove();
666 }
667
668 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
669 {
670   double aSizeLimit = (double)theSizeLimit;
671   double aWidth = (double)theImage.width();
672   double aHeight = (double)theImage.height();
673   if( aWidth > aSizeLimit || aHeight > aSizeLimit )
674   {
675     if( aWidth > aHeight )
676     {
677       aHeight /= ( aWidth / aSizeLimit );
678       aWidth = aSizeLimit;
679     }
680     else
681     {
682       aWidth /= ( aHeight / aSizeLimit );
683       aHeight = aSizeLimit;
684     }
685   }
686   return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );
687 }