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