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