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