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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROGUI_ShapeImage.h>
21 #include <HYDROGUI_Tool.h>
22 #include <HYDROGUI_ImagePrs.h>
24 #include <HYDROData_Image.h>
25 #include <HYDROData_Document.h>
28 #include <QMessageBox>
29 #include <QApplication>
30 #include <QDesktopWidget>
32 HYDROGUI_ShapeImage::HYDROGUI_ShapeImage( const Handle(AIS_InteractiveContext)& theContext,
33 const Handle(HYDROData_Image)& theImage,
35 : HYDROGUI_Shape( theContext, theImage, theZLayer )
39 HYDROGUI_ShapeImage::~HYDROGUI_ShapeImage()
43 void HYDROGUI_ShapeImage::update( bool theIsUpdateViewer, bool isDeactivateSelection )
45 setIsToUpdate( false );
49 HYDROGUI_Shape::update( theIsUpdateViewer, isDeactivateSelection );
52 QList<Handle(AIS_InteractiveObject)> HYDROGUI_ShapeImage::createShape() const
54 QList<Handle(AIS_InteractiveObject)> shapes;
56 Handle(HYDROGUI_ImagePrs) aPrs;
58 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( getObject() );
59 if ( !anImageObj.IsNull() )
62 aPrs = new HYDROGUI_ImagePrs( imagePixMap( anImageObj, aMsg ), imageContour( anImageObj ) );
64 QMessageBox::warning( qApp->desktop(), "Warning", aMsg );
65 shapes.append( aPrs );
71 Handle(Image_PixMap) HYDROGUI_ShapeImage::imagePixMap( const Handle(HYDROData_Image)& theImageObj, QString& theMessage ) const
73 Handle(Image_PixMap) aPix;
74 if ( !theImageObj.IsNull() )
76 QTransform aTrsf = theImageObj->Trsf();
77 QImage anImage = theImageObj->Image();
78 // anImage = anImage.transformed( QTransform::fromScale( -1, -1 ) * aTrsf, Qt::SmoothTransformation );
80 if ( !anImage.isNull() )
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;
86 aTrsfWidth += ( 4 - aDelta );
88 const int maxWidth = 7000;
89 if( aTrsfWidth>maxWidth )
91 aTrsfWidth = maxWidth;
92 theMessage = QString( "The size of image is scaled to %0" ).arg( aTrsfWidth );
94 anImage = anImage.scaledToWidth( aTrsfWidth );
95 int pix = anImage.width() * anImage.height();
97 aPix = HYDROGUI_Tool::Pixmap( anImage );
103 QPolygonF HYDROGUI_ShapeImage::imageContour( const Handle(HYDROData_Image)& theImageObj ) const
106 if ( !theImageObj.IsNull() )
108 QTransform aTrsf = theImageObj->Trsf();
109 QImage anImage = theImageObj->Image();
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 );
115 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( theImageObj->Label() );
116 for ( QPolygonF::iterator it = aPolygon.begin(); it != aPolygon.end(); ++it )
118 gp_Pnt aPnt( (*it).x(), (*it).y(), 0 );
119 aDoc->Transform( aPnt, true );
120 aContour.append( QPointF( aPnt.X(), aPnt.Y() ) );