Salome HOME
Merge branch 'BR_HYDRO_IMPS_2016' of ssh://gitolite3@git.salome-platform.org/modules...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ShapeImage.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROGUI_ShapeImage.h>
20
21 #include <HYDROGUI_Tool.h>
22 #include <HYDROGUI_ImagePrs.h>
23
24 #include <HYDROData_Image.h>
25 #include <HYDROData_Document.h>
26
27 #include <gp_Pnt.hxx>
28
29 HYDROGUI_ShapeImage::HYDROGUI_ShapeImage( const Handle(AIS_InteractiveContext)& theContext,
30                                           const Handle(HYDROData_Image)&         theImage,
31                                           const int                             theZLayer )
32 : HYDROGUI_Shape( theContext, theImage, theZLayer )
33 {
34 }
35
36 HYDROGUI_ShapeImage::~HYDROGUI_ShapeImage()
37 {
38 }
39
40 void HYDROGUI_ShapeImage::update( bool theIsUpdateViewer, bool isDeactivateSelection )
41 {
42   setIsToUpdate( false );
43
44   buildShape();
45
46   HYDROGUI_Shape::update( theIsUpdateViewer, isDeactivateSelection );
47 }
48
49 Handle(AIS_InteractiveObject) HYDROGUI_ShapeImage::createShape() const
50 {
51   Handle(HYDROGUI_ImagePrs) aPrs;
52
53   Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( getObject() );
54   if ( !anImageObj.IsNull() )
55   {
56     aPrs = new HYDROGUI_ImagePrs( imagePixMap( anImageObj ), imageContour( anImageObj ) );
57   }
58
59   return aPrs;
60 }
61
62 Handle(Image_PixMap) HYDROGUI_ShapeImage::imagePixMap( const Handle(HYDROData_Image)& theImageObj ) const
63 {
64     Handle(Image_PixMap) aPix;
65     if ( !theImageObj.IsNull() )
66     {
67         QTransform aTrsf = theImageObj->Trsf();
68         QImage anImage = theImageObj->Image();
69 //        anImage = anImage.transformed( QTransform::fromScale( -1, -1 ) * aTrsf, Qt::SmoothTransformation );
70
71         if ( !anImage.isNull() )
72         {
73             // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
74             int aTrsfWidth = anImage.width();
75             int aDelta = aTrsfWidth % 4;
76             if ( aDelta > 0 )
77                 aTrsfWidth += ( 4 - aDelta );
78       
79             anImage = anImage.scaledToWidth( aTrsfWidth );
80
81             aPix = HYDROGUI_Tool::Pixmap( anImage );
82         }
83     }
84     return aPix;
85 }
86
87 QPolygonF HYDROGUI_ShapeImage::imageContour( const Handle(HYDROData_Image)& theImageObj ) const
88 {
89     QPolygonF aContour;
90     if ( !theImageObj.IsNull() )
91     {
92         QTransform aTrsf = theImageObj->Trsf();
93         QImage anImage = theImageObj->Image();
94
95         QPolygonF aPolygon = QPolygonF() << QPointF( 0, 0 ) << QPointF( anImage.width(), 0 ) 
96                                          << QPointF( anImage.width(), anImage.height() ) << QPointF( 0, anImage.height() );
97         aPolygon = aTrsf.map( aPolygon );
98
99         Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( theImageObj->Label() );
100         for ( QPolygonF::iterator it = aPolygon.begin(); it != aPolygon.end(); ++it )
101         {
102             gp_Pnt aPnt( (*it).x(), (*it).y(), 0 );
103             aDoc->Transform( aPnt, true );
104             aContour.append( QPointF( aPnt.X(), aPnt.Y() ) );
105         }
106     }
107     return aContour;
108 }