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_Operator.h"
21 #include "ImageComposer_MetaTypes.h"
28 ImageComposer_Operator::ImageComposer_Operator()
29 : myBackground( TRANSPARENT )
36 ImageComposer_Operator::~ImageComposer_Operator()
41 Get operator arguments
42 @param theBackground the background color for result image
44 void ImageComposer_Operator::getArgs( QColor& theBackground ) const
46 theBackground = myBackground;
50 Set operator arguments
51 @param theBackground the background color for result image
53 void ImageComposer_Operator::setArgs( const QColor& theBackground )
55 myBackground = theBackground;
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
64 ImageComposer_Image ImageComposer_Operator::process( const QVariant& theObj1,
65 const QVariant& theObj2 ) const
67 ImageComposer_Image aResult;
68 if ( theObj1.isNull() || !theObj1.canConvert<ImageComposer_Image>() )
71 ImageComposer_Image anImage1 = theObj1.value<ImageComposer_Image>();
73 ImageComposer_Image anImage2;
74 if ( !theObj1.isNull() && theObj2.canConvert<ImageComposer_Image>() )
75 anImage2 = theObj2.value<ImageComposer_Image>();
77 QTransform aInvTransform = anImage1.transform().inverted();
78 anImage1.setTransform( anImage1.transform() * aInvTransform );
79 if( !anImage2.isNull() )
80 anImage2.setTransform( anImage2.transform() * aInvTransform );
82 QVariant anImage1Var, anImage2Var;
83 anImage1Var.setValue<ImageComposer_Image>( anImage1 );
84 anImage2Var.setValue<ImageComposer_Image>( anImage2 );
86 QRectF aBounds = calcResultBoundingRect( anImage1Var, !anImage2.isNull() ? anImage2Var : theObj2 );
88 QTransform aTranslate;
89 aTranslate.translate( -aBounds.left(), -aBounds.top() );
90 anImage1.setTransform( anImage1.transform() * aTranslate );
91 anImage2.setTransform( anImage2.transform() * aTranslate );
93 QImage aResultImage( int(aBounds.width()), int(aBounds.height()), QImage::Format_ARGB32 );
94 aResultImage.fill( myBackground );
96 QPainter aPainter( &aResultImage );
97 //aPainter.setRenderHint( QPainter::SmoothPixmapTransform, true );
98 aPainter.setRenderHint( QPainter::Antialiasing, true );
99 aPainter.setRenderHint( QPainter::HighQualityAntialiasing, true );
101 anImage1Var.setValue<ImageComposer_Image>( anImage1 );
102 anImage2Var.setValue<ImageComposer_Image>( anImage2 );
104 drawResult( aPainter, anImage1Var, !anImage2.isNull() ? anImage2Var : theObj2 );
106 anImage1 = theObj1.value<ImageComposer_Image>();
108 QTransform aResultTransform = anImage1.transform();
109 aResultTransform.translate( aBounds.left(), aBounds.top() );
111 aResult = aResultImage;
112 aResult.setTransform( aResultTransform );
118 Get the operator's arguments in the form of a binary array
119 @return the binary array with arguments
121 QByteArray ImageComposer_Operator::getBinArgs() const
124 QDataStream aStream( &aData, QIODevice::WriteOnly );
125 storeArgs( aStream );
130 Set the operator's arguments in the form of a binary array
131 @param theData the binary array with arguments
133 void ImageComposer_Operator::setBinArgs( const QByteArray& theData )
135 QDataStream aStream( theData );
136 restoreArgs( aStream );
139 QStringList ImageComposer_Operator::dumpArgsToPython( QString& theArrayName ) const
141 QStringList aResList;
143 if ( theArrayName.isEmpty() )
144 theArrayName = "composer_args";
146 QString aStreamName = theArrayName + "_stream";
148 aResList << QString( "%1 = QByteArray();" ).arg( theArrayName );
149 aResList << QString( "%1 = QDataStream( %2, QIODevice.WriteOnly );" )
150 .arg( aStreamName ).arg( theArrayName );
152 //Dump background color
153 aResList << QString( "" );
155 aResList << QString( "background_color = QColor( %1, %2, %3, %4 );" )
156 .arg( myBackground.red() ).arg( myBackground.green() )
157 .arg( myBackground.blue() ).arg( myBackground.alpha() );
159 aResList << QString( "%1 << background_color;" ).arg( aStreamName );
165 Store the operator's arguments to the stream
166 @param theStream the stream for storing
168 void ImageComposer_Operator::storeArgs( QDataStream& theStream ) const
170 theStream << myBackground;
174 Restore the operator's arguments from the stream
175 @param theStream the stream for restoring
177 void ImageComposer_Operator::restoreArgs( QDataStream& theStream )
179 theStream >> myBackground;