Salome HOME
4b9bf388e0cdd2a83136eaca172ef8bbc46550df
[modules/gui.git] / src / ImageComposer / ImageComposer_Image.cxx
1 // Copyright (C) 2013-2023  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_Image.h"
21 #include "ImageComposer_CropOperator.h"
22 #include "ImageComposer_CutOperator.h"
23 #include "ImageComposer_FuseOperator.h"
24 #include "ImageComposer_MetaTypes.h"
25
26 #include <QPainter>
27
28 QColor ImageComposer_Image::myDefaultBackground = TRANSPARENT;
29
30 /**
31   Constructor
32 */
33 ImageComposer_Image::ImageComposer_Image()
34 {
35 }
36
37 /**
38   Copy constructor
39 */
40 ImageComposer_Image::ImageComposer_Image( const ImageComposer_Image& theImage )
41  : QImage( theImage ),
42    myTransform( theImage.myTransform )
43 {
44 }
45
46 /**
47   Copy constructor
48 */
49 ImageComposer_Image::ImageComposer_Image( const QImage& theImage )
50  : QImage( theImage )
51 {
52 }
53
54 /**
55   Destructor
56 */
57 ImageComposer_Image::~ImageComposer_Image()
58 {
59 }
60
61 /**
62   Get current image transformation
63   @return current image transformation
64 */
65 QTransform ImageComposer_Image::transform() const
66 {
67   return myTransform;
68 }
69
70 /**
71   Change current image transformation
72   @param theTransform a new image transformation
73 */
74 void ImageComposer_Image::setTransform( const QTransform& theTransform )
75 {
76   myTransform = theTransform;
77 }
78
79 /**
80   Change current image transformation
81   @param theDx the image translation along X axis of the global CS
82   @param theDy the image translation along Y axis of the global CS
83   @param theRotationDeg the image rotation in degrees around the image center
84 */
85 void ImageComposer_Image::setLocalTransform( qreal theDx, qreal theDy, qreal theRotationDeg )
86 {
87   QTransform aTr;
88   aTr.translate( width()*0.5, height()*0.5 );
89   aTr.rotate( theRotationDeg );
90   aTr.translate( -width()*0.5, -height()*0.5 );
91   aTr.translate( theDx, theDy );
92   myTransform = aTr;
93 }
94
95 /**
96   Get image's bounding rectangle in the global CS
97   @return image's bounding rectangle in the global CS
98 */
99 QRectF ImageComposer_Image::boundingRect() const
100 {
101   QRect aRect( 0, 0, width(), height() );
102   return myTransform.mapToPolygon( aRect ).boundingRect();
103 }
104
105 /**
106   Draw the image using the given painter
107   @param thePainter the painter for image drawing
108 */
109 void ImageComposer_Image::draw( QPainter& thePainter ) const
110 {
111   thePainter.save();
112   thePainter.setTransform( myTransform, true );
113   thePainter.drawImage( 0, 0, *this );
114   thePainter.restore();
115 }
116
117 /**
118   Operator of another image assignment
119   @param theImage the image to assign
120   @return the assigned image
121 */
122 const ImageComposer_Image& ImageComposer_Image::operator = ( const ImageComposer_Image& theImage )
123 {
124   myTransform = theImage.myTransform;
125   QImage::operator = ( ( const QImage& ) theImage );
126   return theImage;
127 }
128
129 /**
130   Operator of another image assignment
131   @param theImage the image to assign
132   @return the assigned image
133 */
134 const QImage& ImageComposer_Image::operator = ( const QImage& theImage )
135 {
136   QImage::operator = ( ( const QImage& ) theImage );
137   return theImage;
138 }
139
140 /**
141   Apply the given operator to the image
142   @param theOperator the operator to be applied
143   @param theImage the additional image to compose (optional)
144   @return the result image
145 */
146 ImageComposer_Image ImageComposer_Image::apply( const ImageComposer_Operator& theOperator,
147                                                 const QVariant&               theOtherObj ) const
148 {
149   QVariant aVarData;
150   aVarData.setValue<ImageComposer_Image>( *this );
151
152   return theOperator.process( aVarData, theOtherObj );
153 }
154
155 /**
156   Get default background color used for image operators
157   @return default background color 
158 */
159 QColor ImageComposer_Image::defaultBackground()
160 {
161   return myDefaultBackground;
162 }
163
164 /**
165   Change default background color used for image operators
166   @param theDefaultBackground a new default background color 
167 */
168 void ImageComposer_Image::setDefaultBackground( const QColor& theDefaultBackground )
169 {
170   myDefaultBackground = theDefaultBackground;
171 }
172
173 /**
174   Operator of image cropping by a rectangle
175   @param theRect the rectangle for cropping
176   @return cropped image
177 */
178 ImageComposer_Image ImageComposer_Image::operator & ( const QRect& theRect ) const
179 {
180   QPainterPath aPath;
181   aPath.addRect( theRect );
182   return operator &( aPath );
183 }
184
185 /**
186   Operator of image cropping by a path
187   @param thePath the path for cropping
188   @return cropped image
189 */
190 ImageComposer_Image ImageComposer_Image::operator & ( const QPainterPath& thePath ) const
191 {
192   ImageComposer_CropOperator anOp;
193   anOp.setArgs( myDefaultBackground );
194
195   QVariant aVarData;
196   aVarData.setValue<QPainterPath>( thePath );
197
198   return apply( anOp, aVarData );
199 }
200
201 /**
202   Operator of image cutting by another image
203   @param theImage the image for cutting
204   @return cut image
205 */
206 ImageComposer_Image ImageComposer_Image::operator & ( const ImageComposer_Image& theImage ) const
207 {
208   ImageComposer_CutOperator anOp;
209   anOp.setArgs( myDefaultBackground );
210
211   QVariant aVarData;
212   aVarData.setValue<ImageComposer_Image>( theImage );
213
214   return apply( anOp, aVarData );
215 }
216
217 /**
218   Operator of image fusing with another image
219   @param theImage the image for fusing
220   @return fused image
221 */
222 ImageComposer_Image ImageComposer_Image::operator | ( const ImageComposer_Image& theImage ) const
223 {
224   ImageComposer_FuseOperator anOp;
225   anOp.setArgs( myDefaultBackground );
226
227   QVariant aVarData;
228   aVarData.setValue<ImageComposer_Image>( theImage );
229
230   return apply( anOp, aVarData );
231 }