Salome HOME
Updated copyright comment
[modules/gui.git] / src / ImageComposer / ImageComposer_Operator.cxx
1 // Copyright (C) 2013-2024  CEA, EDF, 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_Operator.h"
21 #include "ImageComposer_MetaTypes.h"
22 #include <QPixmap>
23 #include <QPainter>
24
25 /**
26   Constructor
27 */
28 ImageComposer_Operator::ImageComposer_Operator()
29 : myBackground( TRANSPARENT )
30 {
31 }
32
33 /**
34   Destructor
35 */
36 ImageComposer_Operator::~ImageComposer_Operator()
37 {
38 }
39
40 /**
41   Get operator arguments
42   @param theBackground the background color for result image
43 */
44 void ImageComposer_Operator::getArgs( QColor& theBackground ) const
45 {
46   theBackground = myBackground;
47 }
48
49 /**
50   Set operator arguments
51   @param theBackground the background color for result image
52 */
53 void ImageComposer_Operator::setArgs( const QColor& theBackground )
54 {
55   myBackground = theBackground;
56 }
57
58 /**
59   Perform the composing of images
60   @param theObj1 the first object to compose
61   @param theObj2 the second object to compose
62   @return the result image
63 */
64 ImageComposer_Image ImageComposer_Operator::process( const QVariant& theObj1,
65                                                      const QVariant& theObj2 ) const
66 {
67   ImageComposer_Image aResult;
68   if ( theObj1.isNull() || !theObj1.canConvert<ImageComposer_Image>() )
69     return aResult;
70
71   ImageComposer_Image anImage1 = theObj1.value<ImageComposer_Image>();
72
73   ImageComposer_Image anImage2;
74   if ( !theObj1.isNull() && theObj2.canConvert<ImageComposer_Image>() )
75     anImage2 = theObj2.value<ImageComposer_Image>();
76
77   QTransform aInvTransform = anImage1.transform().inverted();
78   anImage1.setTransform( anImage1.transform() * aInvTransform );
79   if( !anImage2.isNull() )
80     anImage2.setTransform( anImage2.transform() * aInvTransform );
81   
82   QVariant anImage1Var, anImage2Var;
83   anImage1Var.setValue<ImageComposer_Image>( anImage1 );
84   anImage2Var.setValue<ImageComposer_Image>( anImage2 );
85
86   QRectF aBounds = calcResultBoundingRect( anImage1Var, !anImage2.isNull() ? anImage2Var : theObj2 );
87
88   QTransform aTranslate;
89   aTranslate.translate( -aBounds.left(), -aBounds.top() );
90   anImage1.setTransform( anImage1.transform() * aTranslate );
91   anImage2.setTransform( anImage2.transform() * aTranslate );
92
93   QImage aResultImage( int(aBounds.width()), int(aBounds.height()), QImage::Format_ARGB32 );
94   aResultImage.fill( myBackground );
95
96   QPainter aPainter( &aResultImage );
97   //aPainter.setRenderHint( QPainter::SmoothPixmapTransform, true );
98   aPainter.setRenderHint( QPainter::Antialiasing, true );
99   aPainter.setRenderHint( QPainter::HighQualityAntialiasing, true );
100
101   anImage1Var.setValue<ImageComposer_Image>( anImage1 );
102   anImage2Var.setValue<ImageComposer_Image>( anImage2 );
103
104   drawResult( aPainter, anImage1Var, !anImage2.isNull() ? anImage2Var : theObj2 );
105
106   anImage1 = theObj1.value<ImageComposer_Image>();
107
108   QTransform aResultTransform = anImage1.transform();
109   aResultTransform.translate( aBounds.left(), aBounds.top() );
110
111   aResult = aResultImage;
112   aResult.setTransform( aResultTransform );
113
114   return aResult;
115 }
116
117 /**
118   Get the operator's arguments in the form of a binary array
119   @return the binary array with arguments
120 */
121 QByteArray ImageComposer_Operator::getBinArgs() const
122 {
123   QByteArray aData;
124   QDataStream aStream( &aData, QIODevice::WriteOnly );
125   storeArgs( aStream );
126   return aData;
127 }
128
129 /**
130   Set the operator's arguments in the form of a binary array
131   @param theData the binary array with arguments
132 */
133 void ImageComposer_Operator::setBinArgs( const QByteArray& theData )
134 {
135   QDataStream aStream( theData );
136   restoreArgs( aStream );
137 }
138
139 QStringList ImageComposer_Operator::dumpArgsToPython( QString& theArrayName ) const
140 {
141   QStringList aResList;
142
143   if ( theArrayName.isEmpty() )
144     theArrayName = "composer_args";
145
146   QString aStreamName = theArrayName + "_stream";
147
148   aResList << QString( "%1 = QByteArray();" ).arg( theArrayName );
149   aResList << QString( "%1 = QDataStream( %2, QIODevice.WriteOnly );" )
150               .arg( aStreamName ).arg( theArrayName );
151
152   //Dump background color
153   aResList << QString( "" );
154
155   aResList << QString( "background_color = QColor( %1, %2, %3, %4 );" )
156               .arg( myBackground.red() ).arg( myBackground.green() )
157               .arg( myBackground.blue() ).arg( myBackground.alpha() );
158
159   aResList << QString( "%1 << background_color;" ).arg( aStreamName );
160
161   return aResList;
162 }
163
164 /**
165   Store the operator's arguments to the stream
166   @param theStream the stream for storing
167 */
168 void ImageComposer_Operator::storeArgs( QDataStream& theStream ) const
169 {
170   theStream << myBackground;
171 }
172
173 /**
174   Restore the operator's arguments from the stream
175   @param theStream the stream for restoring
176 */
177 void ImageComposer_Operator::restoreArgs( QDataStream& theStream )
178 {
179   theStream >> myBackground;
180 }