Salome HOME
refs #568: Land Cover: a draft of data model and implementation of dialog box and...
[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(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( getObject() );
52     if ( anImageObj.IsNull() )
53         return Handle_AIS_InteractiveObject();
54
55     Handle(HYDROGUI_ImagePrs) aPrs = new HYDROGUI_ImagePrs( imagePixMap( anImageObj ), imageContour( anImageObj ) );
56     return aPrs;
57 }
58
59 Handle(Image_PixMap) HYDROGUI_ShapeImage::imagePixMap( const Handle(HYDROData_Image)& theImageObj ) const
60 {
61     Handle(Image_PixMap) aPix;
62     if ( !theImageObj.IsNull() )
63     {
64         QTransform aTrsf = theImageObj->Trsf();
65         QImage anImage = theImageObj->Image();
66 //        anImage = anImage.transformed( QTransform::fromScale( -1, -1 ) * aTrsf, Qt::SmoothTransformation );
67
68         if ( !anImage.isNull() )
69         {
70             // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
71             int aTrsfWidth = anImage.width();
72             int aDelta = aTrsfWidth % 4;
73             if ( aDelta > 0 )
74                 aTrsfWidth += ( 4 - aDelta );
75       
76             anImage = anImage.scaledToWidth( aTrsfWidth );
77
78             aPix = HYDROGUI_Tool::Pixmap( anImage );
79         }
80     }
81     return aPix;
82 }
83
84 QPolygonF HYDROGUI_ShapeImage::imageContour( const Handle_HYDROData_Image& theImageObj ) const
85 {
86     QPolygonF aContour;
87     if ( !theImageObj.IsNull() )
88     {
89         QTransform aTrsf = theImageObj->Trsf();
90         QImage anImage = theImageObj->Image();
91
92         QPolygonF aPolygon = QPolygonF() << QPointF( 0, 0 ) << QPointF( anImage.width(), 0 ) 
93                                          << QPointF( anImage.width(), anImage.height() ) << QPointF( 0, anImage.height() );
94         aPolygon = aTrsf.map( aPolygon );
95
96         Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( theImageObj->Label() );
97         for ( QPolygonF::iterator it = aPolygon.begin(); it != aPolygon.end(); ++it )
98         {
99             gp_Pnt aPnt( (*it).x(), (*it).y(), 0 );
100             aDoc->Transform( aPnt, true );
101             aContour.append( QPointF( aPnt.X(), aPnt.Y() ) );
102         }
103     }
104     return aContour;
105 }