QGIS API Documentation 3.99.0-Master (d270888f95f)
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 <QString>
25#include <QTransform>
26
27using namespace Qt::StringLiterals;
28
30{
31 QgsTransformEffect *newEffect = new QgsTransformEffect();
32 newEffect->readProperties( map );
33 return newEffect;
34}
35
37{
38 if ( !enabled() || !context.painter() || source().isNull() )
39 return;
40
41 QPainter *painter = context.painter();
42
43 //apply transformations
44 const QgsScopedQPainterState painterState( painter );
45
46 const QTransform t = createTransform( context );
47 painter->setTransform( t, true );
48 drawSource( *painter );
49}
50
52{
53 QVariantMap props;
54 props.insert( u"reflect_x"_s, mReflectX ? "1" : "0" );
55 props.insert( u"reflect_y"_s, mReflectY ? "1" : "0" );
56 props.insert( u"scale_x"_s, QString::number( mScaleX ) );
57 props.insert( u"scale_y"_s, QString::number( mScaleY ) );
58 props.insert( u"rotation"_s, QString::number( mRotation ) );
59 props.insert( u"shear_x"_s, QString::number( mShearX ) );
60 props.insert( u"shear_y"_s, QString::number( mShearY ) );
61 props.insert( u"translate_x"_s, QString::number( mTranslateX ) );
62 props.insert( u"translate_y"_s, QString::number( mTranslateY ) );
63 props.insert( u"translate_unit"_s, QgsUnitTypes::encodeUnit( mTranslateUnit ) );
64 props.insert( u"translate_unit_scale"_s, QgsSymbolLayerUtils::encodeMapUnitScale( mTranslateMapUnitScale ) );
65 props.insert( u"enabled"_s, mEnabled ? "1" : "0" );
66 props.insert( u"draw_mode"_s, QString::number( int( mDrawMode ) ) );
67 return props;
68}
69
70void QgsTransformEffect::readProperties( const QVariantMap &props )
71{
72 mEnabled = props.value( u"enabled"_s, u"1"_s ).toInt();
73 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( u"draw_mode"_s, u"2"_s ).toInt() );
74 mReflectX = props.value( u"reflect_x"_s, u"0"_s ).toInt();
75 mReflectY = props.value( u"reflect_y"_s, u"0"_s ).toInt();
76 mScaleX = props.value( u"scale_x"_s, u"1.0"_s ).toDouble();
77 mScaleY = props.value( u"scale_y"_s, u"1.0"_s ).toDouble();
78 mRotation = props.value( u"rotation"_s, u"0.0"_s ).toDouble();
79 mShearX = props.value( u"shear_x"_s, u"0.0"_s ).toDouble();
80 mShearY = props.value( u"shear_y"_s, u"0.0"_s ).toDouble();
81 mTranslateX = props.value( u"translate_x"_s, u"0.0"_s ).toDouble();
82 mTranslateY = props.value( u"translate_y"_s, u"0.0"_s ).toDouble();
83 mTranslateUnit = QgsUnitTypes::decodeRenderUnit( props.value( u"translate_unit"_s ).toString() );
84 mTranslateMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( u"translate_unit_scale"_s ).toString() );
85}
86
88{
89 QgsTransformEffect *newEffect = new QgsTransformEffect( *this );
90 return newEffect;
91}
92
93QRectF QgsTransformEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
94{
95 const QTransform t = createTransform( context );
96 return t.mapRect( rect );
97}
98
99QTransform QgsTransformEffect::createTransform( const QgsRenderContext &context ) const
100{
101 QTransform t;
102
103 const QPicture &pic = source();
104 if ( pic.isNull() )
105 return t;
106
107 const int width = pic.boundingRect().width();
108 const int height = pic.boundingRect().height();
109 const int top = pic.boundingRect().top();
110 const int left = pic.boundingRect().left();
111
112 //remember that the below operations are effectively performed in the opposite order
113 //so, first the reflection applies, then scale, shear, rotate and lastly translation
114
115 const double translateX = context.convertToPainterUnits( mTranslateX, mTranslateUnit, mTranslateMapUnitScale );
116 const double translateY = context.convertToPainterUnits( mTranslateY, mTranslateUnit, mTranslateMapUnitScale );
117
118 t.translate( translateX + left + width / 2.0,
119 translateY + top + height / 2.0 );
120
121 t.rotate( mRotation );
122 t.shear( mShearX, mShearY );
123 t.scale( mScaleX, mScaleY );
124
125 if ( mReflectX || mReflectY )
126 {
127 t.scale( mReflectX ? -1 : 1, mReflectY ? -1 : 1 );
128 }
129
130 t.translate( -left - width / 2.0,
131 -top - height / 2.0 );
132
133 return t;
134}
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.