Salome HOME
25dcd7febcc136578e7b44a95300b51615b6e45e
[modules/gui.git] / src / ImageComposer / ImageComposer_CropOperator.cxx
1 // Copyright (C) 2013-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ImageComposer_CropOperator.h"
21 #include "ImageComposer_Image.h"
22 #include "ImageComposer_MetaTypes.h"
23
24 #include <QPixmap>
25 #include <QPainter>
26
27 /**
28   Constructor
29 */
30 ImageComposer_CropOperator::ImageComposer_CropOperator()
31 : ImageComposer_Operator()
32 {
33 }
34
35 /**
36 */
37 ImageComposer_CropOperator::~ImageComposer_CropOperator()
38 {
39 }
40
41 /**
42 */
43 QString ImageComposer_CropOperator::name() const
44 {
45   return Type();
46 }
47
48 /**
49 */
50 QRectF ImageComposer_CropOperator::calcResultBoundingRect( const QVariant&, 
51                                                            const QVariant& theObj2 ) const
52 {
53   QRectF aResRect;
54   if ( !theObj2.isNull() && theObj2.canConvert<QPainterPath>() )
55   {
56     QPainterPath aCropPath = theObj2.value<QPainterPath>();
57     aResRect = aCropPath.boundingRect();
58   }
59   return aResRect;
60 }
61
62 /**
63 */
64 void ImageComposer_CropOperator::drawResult( QPainter&       thePainter,
65                                              const QVariant& theObj1,
66                                              const QVariant& theObj2 ) const
67 {
68   if ( theObj1.isNull() || !theObj1.canConvert<ImageComposer_Image>() ||
69        theObj2.isNull() || !theObj2.canConvert<QPainterPath>() )
70     return;
71
72   ImageComposer_Image anImage1 = theObj1.value<ImageComposer_Image>();
73   QPainterPath anImgClipPath = theObj2.value<QPainterPath>();
74
75   QRectF aBounds = anImgClipPath.boundingRect();
76
77   QTransform aTranslate;
78   aTranslate.translate( -aBounds.left(), -aBounds.top() );
79
80   QPainterPath aClipPath = aTranslate.map( anImgClipPath );
81   thePainter.setClipPath( aClipPath );
82
83   anImage1.draw( thePainter );
84   //thePainter.fillPath( aClipPath, Qt::red );
85 }
86
87 /**
88 */
89 ImageComposer_Image ImageComposer_CropOperator::process( const QVariant& theObj1,
90                                                          const QVariant& theObj2 ) const
91 {
92   ImageComposer_Image aResult;
93   if ( theObj1.isNull() || !theObj1.canConvert<ImageComposer_Image>() ||
94        theObj2.isNull() || !theObj2.canConvert<QPainterPath>() )
95     return aResult;
96
97   ImageComposer_Image anImage1 = theObj1.value<ImageComposer_Image>();
98   QPainterPath aCropPath = theObj2.value<QPainterPath>();
99
100   QRect anImageRect( 0, 0, anImage1.width(), anImage1.height() );
101
102   QPainterPath anImageBoundsPath;
103   anImageBoundsPath.addPolygon( anImage1.transform().mapToPolygon( anImageRect ) );
104
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 ) ) );
109
110   return ImageComposer_Operator::process( theObj1, anImgClipPath );
111 }