QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgstransformeffect.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstransformeffect.cpp
3 ----------------------
4 begin : March 2015
5 copyright : (C) 2015 Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgstransformeffect.h"
19
20#include "qgssymbollayerutils.h"
21#include "qgsunittypes.h"
22
23#include <QPicture>
24#include <QTransform>
25
27{
28 QgsTransformEffect *newEffect = new QgsTransformEffect();
29 newEffect->readProperties( map );
30 return newEffect;
31}
32
34{
35 if ( !enabled() || !context.painter() || source().isNull() )
36 return;
37
38 QPainter *painter = context.painter();
39
40 //apply transformations
41 const QgsScopedQPainterState painterState( painter );
42
43 const QTransform t = createTransform( context );
44 painter->setTransform( t, true );
45 drawSource( *painter );
46}
47
49{
50 QVariantMap props;
51 props.insert( QStringLiteral( "reflect_x" ), mReflectX ? "1" : "0" );
52 props.insert( QStringLiteral( "reflect_y" ), mReflectY ? "1" : "0" );
53 props.insert( QStringLiteral( "scale_x" ), QString::number( mScaleX ) );
54 props.insert( QStringLiteral( "scale_y" ), QString::number( mScaleY ) );
55 props.insert( QStringLiteral( "rotation" ), QString::number( mRotation ) );
56 props.insert( QStringLiteral( "shear_x" ), QString::number( mShearX ) );
57 props.insert( QStringLiteral( "shear_y" ), QString::number( mShearY ) );
58 props.insert( QStringLiteral( "translate_x" ), QString::number( mTranslateX ) );
59 props.insert( QStringLiteral( "translate_y" ), QString::number( mTranslateY ) );
60 props.insert( QStringLiteral( "translate_unit" ), QgsUnitTypes::encodeUnit( mTranslateUnit ) );
61 props.insert( QStringLiteral( "translate_unit_scale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mTranslateMapUnitScale ) );
62 props.insert( QStringLiteral( "enabled" ), mEnabled ? "1" : "0" );
63 props.insert( QStringLiteral( "draw_mode" ), QString::number( int( mDrawMode ) ) );
64 return props;
65}
66
67void QgsTransformEffect::readProperties( const QVariantMap &props )
68{
69 mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
70 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
71 mReflectX = props.value( QStringLiteral( "reflect_x" ), QStringLiteral( "0" ) ).toInt();
72 mReflectY = props.value( QStringLiteral( "reflect_y" ), QStringLiteral( "0" ) ).toInt();
73 mScaleX = props.value( QStringLiteral( "scale_x" ), QStringLiteral( "1.0" ) ).toDouble();
74 mScaleY = props.value( QStringLiteral( "scale_y" ), QStringLiteral( "1.0" ) ).toDouble();
75 mRotation = props.value( QStringLiteral( "rotation" ), QStringLiteral( "0.0" ) ).toDouble();
76 mShearX = props.value( QStringLiteral( "shear_x" ), QStringLiteral( "0.0" ) ).toDouble();
77 mShearY = props.value( QStringLiteral( "shear_y" ), QStringLiteral( "0.0" ) ).toDouble();
78 mTranslateX = props.value( QStringLiteral( "translate_x" ), QStringLiteral( "0.0" ) ).toDouble();
79 mTranslateY = props.value( QStringLiteral( "translate_y" ), QStringLiteral( "0.0" ) ).toDouble();
80 mTranslateUnit = QgsUnitTypes::decodeRenderUnit( props.value( QStringLiteral( "translate_unit" ) ).toString() );
81 mTranslateMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( QStringLiteral( "translate_unit_scale" ) ).toString() );
82}
83
85{
86 QgsTransformEffect *newEffect = new QgsTransformEffect( *this );
87 return newEffect;
88}
89
90QRectF QgsTransformEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
91{
92 const QTransform t = createTransform( context );
93 return t.mapRect( rect );
94}
95
96QTransform QgsTransformEffect::createTransform( const QgsRenderContext &context ) const
97{
98 QTransform t;
99
100 const QPicture &pic = source();
101 if ( pic.isNull() )
102 return t;
103
104 const int width = pic.boundingRect().width();
105 const int height = pic.boundingRect().height();
106 const int top = pic.boundingRect().top();
107 const int left = pic.boundingRect().left();
108
109 //remember that the below operations are effectively performed in the opposite order
110 //so, first the reflection applies, then scale, shear, rotate and lastly translation
111
112 const double translateX = context.convertToPainterUnits( mTranslateX, mTranslateUnit, mTranslateMapUnitScale );
113 const double translateY = context.convertToPainterUnits( mTranslateY, mTranslateUnit, mTranslateMapUnitScale );
114
115 t.translate( translateX + left + width / 2.0,
116 translateY + top + height / 2.0 );
117
118 t.rotate( mRotation );
119 t.shear( mShearX, mShearY );
120 t.scale( mScaleX, mScaleY );
121
122 if ( mReflectX || mReflectY )
123 {
124 t.scale( mReflectX ? -1 : 1, mReflectY ? -1 : 1 );
125 }
126
127 t.translate( -left - width / 2.0,
128 -top - height / 2.0 );
129
130 return t;
131}
QgsPaintEffect()=default
void drawSource(QPainter &painter)
Draws the source QPicture onto the specified painter.
const QPicture & source() const
Returns the source QPicture.
bool enabled() const
Returns whether the effect is enabled.
DrawMode
Drawing modes for effects.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
Scoped object for saving and restoring a QPainter object's state.
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
double translateX() const
Returns the transform x translation.
QgsTransformEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
QgsTransformEffect()=default
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsTransformEffect effect from a properties string map.
double translateY() const
Returns the transform y translation.
void draw(QgsRenderContext &context) override
Handles drawing of the effect's result on to the specified render context.
QRectF boundingRect(const QRectF &rect, const QgsRenderContext &context) const override
Returns the bounding rect required for drawing the effect.
void readProperties(const QVariantMap &props) override
Reads a string map of an effect's properties and restores the effect to the state described by the pr...
QVariantMap properties() const override
Returns the properties describing the paint effect encoded in a string format.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.