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