Salome HOME
portage V8_5_0
[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 #include <QMessageBox>
29 #include <QApplication>
30 #include <QDesktopWidget>
31
32 HYDROGUI_ShapeImage::HYDROGUI_ShapeImage( const Handle(AIS_InteractiveContext)& theContext,
33                                           const Handle(HYDROData_Image)&         theImage,
34                                           const int                             theZLayer )
35 : HYDROGUI_Shape( theContext, theImage, theZLayer )
36 {
37 }
38
39 HYDROGUI_ShapeImage::~HYDROGUI_ShapeImage()
40 {
41 }
42
43 void HYDROGUI_ShapeImage::update( bool theIsUpdateViewer, bool isDeactivateSelection )
44 {
45   setIsToUpdate( false );
46
47   buildShape();
48
49   HYDROGUI_Shape::update( theIsUpdateViewer, isDeactivateSelection );
50 }
51
52 QList<Handle(AIS_InteractiveObject)> HYDROGUI_ShapeImage::createShape() const
53 {
54   QList<Handle(AIS_InteractiveObject)> shapes;
55
56   Handle(HYDROGUI_ImagePrs) aPrs;
57
58   Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( getObject() );
59   if ( !anImageObj.IsNull() )
60   {
61         QString aMsg;
62     aPrs = new HYDROGUI_ImagePrs( imagePixMap( anImageObj, aMsg ), imageContour( anImageObj ) );
63         if( !aMsg.isEmpty() )
64                 QMessageBox::warning( qApp->desktop(), "Warning", aMsg ); 
65     shapes.append( aPrs );
66   }
67
68   return shapes;
69 }
70
71 Handle(Image_PixMap) HYDROGUI_ShapeImage::imagePixMap( const Handle(HYDROData_Image)& theImageObj, QString& theMessage ) const
72 {
73     Handle(Image_PixMap) aPix;
74     if ( !theImageObj.IsNull() )
75     {
76         QTransform aTrsf = theImageObj->Trsf();
77         QImage anImage = theImageObj->Image();
78 //        anImage = anImage.transformed( QTransform::fromScale( -1, -1 ) * aTrsf, Qt::SmoothTransformation );
79
80         if ( !anImage.isNull() )
81         {
82             // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
83             int aTrsfWidth = anImage.width();
84             int aDelta = aTrsfWidth % 4;
85             if ( aDelta > 0 )
86                 aTrsfWidth += ( 4 - aDelta );
87       
88                         const int maxWidth = 7000;
89                         if( aTrsfWidth>maxWidth )
90                         {
91                                 aTrsfWidth = maxWidth;
92                                 theMessage = QString( "The size of image is scaled to %0" ).arg( aTrsfWidth );
93                         }
94             anImage = anImage.scaledToWidth( aTrsfWidth );
95                         int pix = anImage.width() * anImage.height();
96
97             aPix = HYDROGUI_Tool::Pixmap( anImage );
98         }
99     }
100     return aPix;
101 }
102
103 QPolygonF HYDROGUI_ShapeImage::imageContour( const Handle(HYDROData_Image)& theImageObj ) const
104 {
105     QPolygonF aContour;
106     if ( !theImageObj.IsNull() )
107     {
108         QTransform aTrsf = theImageObj->Trsf();
109         QImage anImage = theImageObj->Image();
110
111         QPolygonF aPolygon = QPolygonF() << QPointF( 0, 0 ) << QPointF( anImage.width(), 0 ) 
112                                          << QPointF( anImage.width(), anImage.height() ) << QPointF( 0, anImage.height() );
113         aPolygon = aTrsf.map( aPolygon );
114
115         Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( theImageObj->Label() );
116         for ( QPolygonF::iterator it = aPolygon.begin(); it != aPolygon.end(); ++it )
117         {
118             gp_Pnt aPnt( (*it).x(), (*it).y(), 0 );
119             aDoc->Transform( aPnt, true );
120             aContour.append( QPointF( aPnt.X(), aPnt.Y() ) );
121         }
122     }
123     return aContour;
124 }