1 // Copyright (C) 2013-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ImageComposer_CropOperator.h"
21 #include "ImageComposer_Image.h"
22 #include "ImageComposer_MetaTypes.h"
30 ImageComposer_CropOperator::ImageComposer_CropOperator()
31 : ImageComposer_Operator()
37 ImageComposer_CropOperator::~ImageComposer_CropOperator()
43 QString ImageComposer_CropOperator::name() const
50 QRectF ImageComposer_CropOperator::calcResultBoundingRect( const QVariant&,
51 const QVariant& theObj2 ) const
54 if ( !theObj2.isNull() && theObj2.canConvert<QPainterPath>() )
56 QPainterPath aCropPath = theObj2.value<QPainterPath>();
57 aResRect = aCropPath.boundingRect();
64 void ImageComposer_CropOperator::drawResult( QPainter& thePainter,
65 const QVariant& theObj1,
66 const QVariant& theObj2 ) const
68 if ( theObj1.isNull() || !theObj1.canConvert<ImageComposer_Image>() ||
69 theObj2.isNull() || !theObj2.canConvert<QPainterPath>() )
72 ImageComposer_Image anImage1 = theObj1.value<ImageComposer_Image>();
73 QPainterPath anImgClipPath = theObj2.value<QPainterPath>();
75 QRectF aBounds = anImgClipPath.boundingRect();
77 QTransform aTranslate;
78 aTranslate.translate( -aBounds.left(), -aBounds.top() );
80 QPainterPath aClipPath = aTranslate.map( anImgClipPath );
81 thePainter.setClipPath( aClipPath );
83 anImage1.draw( thePainter );
84 //thePainter.fillPath( aClipPath, Qt::red );
89 ImageComposer_Image ImageComposer_CropOperator::process( const QVariant& theObj1,
90 const QVariant& theObj2 ) const
92 ImageComposer_Image aResult;
93 if ( theObj1.isNull() || !theObj1.canConvert<ImageComposer_Image>() ||
94 theObj2.isNull() || !theObj2.canConvert<QPainterPath>() )
97 ImageComposer_Image anImage1 = theObj1.value<ImageComposer_Image>();
98 QPainterPath aCropPath = theObj2.value<QPainterPath>();
100 QRect anImageRect( 0, 0, anImage1.width(), anImage1.height() );
102 QPainterPath anImageBoundsPath;
103 anImageBoundsPath.addPolygon( anImage1.transform().mapToPolygon( anImageRect ) );
105 // clipping path mapped to first image's local CS
106 QVariant anImgClipPath;
107 anImgClipPath.setValue<QPainterPath>(
108 anImage1.transform().inverted().map( aCropPath.intersected( anImageBoundsPath ) ) );
110 return ImageComposer_Operator::process( theObj1, anImgClipPath );