28 : mEnabled( other.enabled() )
29 , mDrawMode( other.drawMode() )
40 delete mEffectPainter;
56 if ( element.isNull() )
61 QDomElement effectElement = doc.createElement( QStringLiteral(
"effect" ) );
62 effectElement.setAttribute( QStringLiteral(
"type" ),
type() );
65 for ( QgsStringMap::iterator it = props.begin(); it != props.end(); ++it )
67 QDomElement propEl = doc.createElement( QStringLiteral(
"prop" ) );
68 propEl.setAttribute( QStringLiteral(
"k" ), it.key() );
69 propEl.setAttribute( QStringLiteral(
"v" ), it.value() );
70 effectElement.appendChild( propEl );
73 element.appendChild( effectElement );
79 if ( element.isNull() )
87 QDomElement e = element.firstChildElement();
90 if ( e.tagName() != QLatin1String(
"prop" ) )
96 QString propKey = e.attribute( QStringLiteral(
"k" ) );
97 QString propValue = e.attribute( QStringLiteral(
"v" ) );
98 props[propKey] = propValue;
100 e = e.nextSiblingElement();
112 mSourceImage =
nullptr;
120 mPrevPainter = context.
painter();
123 mTempPicture =
new QPicture();
125 delete mEffectPainter;
126 mEffectPainter =
new QPainter();
127 mEffectPainter->begin( mTempPicture );
134 if ( !mEffectPainter )
137 mEffectPainter->end();
138 delete mEffectPainter;
139 mEffectPainter =
nullptr;
143 mPrevPainter =
nullptr;
147 context.
painter()->setPen( Qt::NoPen );
148 context.
painter()->setBrush( Qt::NoBrush );
151 render( *mTempPicture, context );
155 mTempPicture =
nullptr;
164 painter.drawPicture( 0, 0, *mPicture );
169 painter.drawPicture( 0, 0, *mPicture );
186 QRectF bounds = imageBoundingRect( context );
187 mSourceImage =
new QImage( bounds.width(), bounds.height(), QImage::Format_ARGB32 );
188 mSourceImage->fill( Qt::transparent );
189 QPainter imagePainter( mSourceImage );
190 imagePainter.setRenderHint( QPainter::Antialiasing );
191 imagePainter.translate( -bounds.left(), -bounds.top() );
192 imagePainter.drawPicture( 0, 0, *mPicture );
200 return imageBoundingRect( context ).topLeft();
215 painter->scale( static_cast< double >(
qt_defaultDpiX() ) / painter->device()->logicalDpiX(),
216 static_cast< double >(
qt_defaultDpiY() ) / painter->device()->logicalDpiY() );
219 QRectF QgsPaintEffect::imageBoundingRect(
const QgsRenderContext &context )
const 221 return boundingRect( mPicture->boundingRect(), context );
241 QPainter *painter = context.
painter();
243 if ( mBlendMode == QPainter::CompositionMode_SourceOver &&
qgsDoubleNear( mOpacity, 1.0 ) )
254 painter->setCompositionMode( mBlendMode );
255 painter->drawImage(
imageOffset( context ), image );
268 props.insert( QStringLiteral(
"enabled" ),
mEnabled ?
"1" :
"0" );
269 props.insert( QStringLiteral(
"draw_mode" ), QString::number(
int(
mDrawMode ) ) );
270 props.insert( QStringLiteral(
"blend_mode" ), QString::number(
int( mBlendMode ) ) );
271 props.insert( QStringLiteral(
"opacity" ), QString::number( mOpacity ) );
278 QPainter::CompositionMode mode =
static_cast< QPainter::CompositionMode
>( props.value( QStringLiteral(
"blend_mode" ) ).toInt( &ok ) );
283 if ( props.contains( QStringLiteral(
"transparency" ) ) )
285 double transparency = props.value( QStringLiteral(
"transparency" ) ).toDouble( &ok );
288 mOpacity = 1.0 - transparency;
293 double opacity = props.value( QStringLiteral(
"opacity" ) ).toDouble( &ok );
299 mEnabled = props.value( QStringLiteral(
"enabled" ), QStringLiteral(
"1" ) ).toInt();
309 : mRenderContext( renderContext )
312 mPainter = renderContext.
painter();
317 : mRenderContext( renderContext )
320 mPainter = mRenderContext.
painter();
322 mEffect->
begin( mRenderContext );
327 Q_ASSERT( !mEffect );
330 mEffect->
begin( mRenderContext );
337 mEffect->
end( mRenderContext );
static void multiplyOpacity(QImage &image, double factor)
Multiplies opacity of image pixel values by a factor.
virtual QgsStringMap properties() const =0
Returns the properties describing the paint effect encoded in a string format.
virtual QRectF boundingRect(const QRectF &rect, const QgsRenderContext &context) const
Returns the bounding rect required for drawing the effect.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
DrawMode drawMode() const
Returns the draw mode for the effect.
bool enabled() const
Returns whether the effect is enabled.
Base class for visual effects which can be applied to QPicture drawings.
Q_GUI_EXPORT int qt_defaultDpiX()
QMap< QString, QString > QgsStringMap
QImage * sourceAsImage(QgsRenderContext &context)
Returns the source QPicture rendered to a new QImage.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsDrawSource effect from a properties string map.
void setEnabled(bool enabled)
Sets whether the effect is enabled.
QgsDrawSourceEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
virtual QString type() const =0
Returns the effect type.
QgsStringMap properties() const override
Returns the properties describing the paint effect encoded in a string format.
QgsEffectPainter(QgsRenderContext &renderContext)
QgsEffectPainter constructor.
virtual ~QgsPaintEffect()
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
Q_GUI_EXPORT int qt_defaultDpiY()
void fixQPictureDpi(QPainter *painter) const
Applies a workaround to a QPainter to avoid an issue with incorrect scaling when drawing QPictures...
QPointF imageOffset(const QgsRenderContext &context) const
Returns the offset which should be used when drawing the source image on to a destination render cont...
void readProperties(const QgsStringMap &props) override
Reads a string map of an effect's properties and restores the effect to the state described by the pr...
bool requiresQPainterDpiFix
void setEffect(QgsPaintEffect *effect)
Sets the effect to be painted.
DrawMode
Drawing modes for effects.
virtual void readProperties(const QgsStringMap &props)=0
Reads a string map of an effect's properties and restores the effect to the state described by the pr...
virtual bool saveProperties(QDomDocument &doc, QDomElement &element) const
Saves the current state of the effect to a DOM element.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
void drawSource(QPainter &painter)
Draws the source QPicture onto the specified painter.
virtual void render(QPicture &picture, QgsRenderContext &context)
Renders a picture using the effect.
virtual void draw(QgsRenderContext &context)=0
Handles drawing of the effect's result on to the specified render context.
void draw(QgsRenderContext &context) override
Handles drawing of the effect's result on to the specified render context.
QgsPaintEffect()=default
Constructor for QgsPaintEffect.
A paint effect which draws the source picture with minor or no alterations.
virtual void end(QgsRenderContext &context)
Ends interception of paint operations to a render context, and draws the result to the render context...
virtual void begin(QgsRenderContext &context)
Begins intercepting paint operations to a render context.
void setDrawMode(DrawMode drawMode)
Sets the draw mode for the effect.