Salome HOME
refs #497: redesign of HYDRO main menu.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ShapeImage.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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_ShapeImage.h>
24
25 #include <HYDROGUI_Tool.h>
26 #include <HYDROGUI_ImagePrs.h>
27
28 #include <HYDROData_Image.h>
29 #include <HYDROData_Document.h>
30
31 #include <gp_Pnt.hxx>
32
33 HYDROGUI_ShapeImage::HYDROGUI_ShapeImage( const Handle(AIS_InteractiveContext)& theContext,
34                                           const Handle_HYDROData_Image&         theImage,
35                                           const int                             theZLayer )
36 : HYDROGUI_Shape( theContext, theImage, theZLayer )
37 {
38 }
39
40 HYDROGUI_ShapeImage::~HYDROGUI_ShapeImage()
41 {
42 }
43
44 void HYDROGUI_ShapeImage::update( bool theIsUpdateViewer, bool isDeactivateSelection )
45 {
46   setIsToUpdate( false );
47
48   buildShape();
49
50   HYDROGUI_Shape::update( theIsUpdateViewer, isDeactivateSelection );
51 }
52
53 Handle(AIS_InteractiveObject) HYDROGUI_ShapeImage::createShape() const
54 {
55     Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( getObject() );
56     if ( anImageObj.IsNull() )
57         return Handle_AIS_InteractiveObject();
58
59     Handle(HYDROGUI_ImagePrs) aPrs = new HYDROGUI_ImagePrs( imagePixMap( anImageObj ), imageContour( anImageObj ) );
60     return aPrs;
61 }
62
63 Handle(Image_PixMap) HYDROGUI_ShapeImage::imagePixMap( const Handle(HYDROData_Image)& theImageObj ) const
64 {
65     Handle(Image_PixMap) aPix;
66     if ( !theImageObj.IsNull() )
67     {
68         QTransform aTrsf = theImageObj->Trsf();
69         QImage anImage = theImageObj->Image();
70 //        anImage = anImage.transformed( QTransform::fromScale( -1, -1 ) * aTrsf, Qt::SmoothTransformation );
71
72         if ( !anImage.isNull() )
73         {
74             // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
75             int aTrsfWidth = anImage.width();
76             int aDelta = aTrsfWidth % 4;
77             if ( aDelta > 0 )
78                 aTrsfWidth += ( 4 - aDelta );
79       
80             anImage = anImage.scaledToWidth( aTrsfWidth );
81
82             aPix = HYDROGUI_Tool::Pixmap( anImage );
83         }
84     }
85     return aPix;
86 }
87
88 QPolygonF HYDROGUI_ShapeImage::imageContour( const Handle_HYDROData_Image& theImageObj ) const
89 {
90     QPolygonF aContour;
91     if ( !theImageObj.IsNull() )
92     {
93         QTransform aTrsf = theImageObj->Trsf();
94         QImage anImage = theImageObj->Image();
95
96         QPolygonF aPolygon = QPolygonF() << QPointF( 0, 0 ) << QPointF( anImage.width(), 0 ) 
97                                          << QPointF( anImage.width(), anImage.height() ) << QPointF( 0, anImage.height() );
98         aPolygon = aTrsf.map( aPolygon );
99
100         Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( theImageObj->Label() );
101         for ( QPolygonF::iterator it = aPolygon.begin(); it != aPolygon.end(); ++it )
102         {
103             gp_Pnt aPnt( (*it).x(), (*it).y(), 0 );
104             aDoc->Transform( aPnt, true );
105             aContour.append( QPointF( aPnt.X(), aPnt.Y() ) );
106         }
107     }
108     return aContour;
109 }