QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgspainting.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspainting.cpp
3 ---------------------
4 begin : July 2016
5 copyright : (C) 2016 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgspainting.h"
17#include "qgslogger.h"
18
19#include <QTransform>
20
21QPainter::CompositionMode QgsPainting::getCompositionMode( Qgis::BlendMode blendMode )
22{
23 // Map Qgis::BlendMode::Normal to QPainter::CompositionMode
24 switch ( blendMode )
25 {
27 return QPainter::CompositionMode_SourceOver;
29 return QPainter::CompositionMode_Lighten;
31 return QPainter::CompositionMode_Screen;
33 return QPainter::CompositionMode_ColorDodge;
35 return QPainter::CompositionMode_Plus;
37 return QPainter::CompositionMode_Darken;
39 return QPainter::CompositionMode_Multiply;
41 return QPainter::CompositionMode_ColorBurn;
43 return QPainter::CompositionMode_Overlay;
45 return QPainter::CompositionMode_SoftLight;
47 return QPainter::CompositionMode_HardLight;
49 return QPainter::CompositionMode_Difference;
51 return QPainter::CompositionMode_Exclusion;
53 return QPainter::CompositionMode_Source;
55 return QPainter::CompositionMode_DestinationOver;
57 return QPainter::CompositionMode_Clear;
59 return QPainter::CompositionMode_Destination;
61 return QPainter::CompositionMode_SourceIn;
63 return QPainter::CompositionMode_DestinationIn;
65 return QPainter::CompositionMode_SourceOut;
67 return QPainter::CompositionMode_DestinationOut;
69 return QPainter::CompositionMode_SourceAtop;
71 return QPainter::CompositionMode_DestinationAtop;
73 return QPainter::CompositionMode_Xor;
74 default:
75 QgsDebugError( QStringLiteral( "Blend mode %1 mapped to SourceOver" ).arg( qgsEnumValueToKey( blendMode ) ) );
76 return QPainter::CompositionMode_SourceOver;
77 }
78}
79
80
81Qgis::BlendMode QgsPainting::getBlendModeEnum( QPainter::CompositionMode blendMode )
82{
83 // Map QPainter::CompositionMode to Qgis::BlendMode::Normal
84 switch ( blendMode )
85 {
86 case QPainter::CompositionMode_SourceOver:
88 case QPainter::CompositionMode_Lighten:
90 case QPainter::CompositionMode_Screen:
92 case QPainter::CompositionMode_ColorDodge:
94 case QPainter::CompositionMode_Plus:
96 case QPainter::CompositionMode_Darken:
98 case QPainter::CompositionMode_Multiply:
100 case QPainter::CompositionMode_ColorBurn:
102 case QPainter::CompositionMode_Overlay:
104 case QPainter::CompositionMode_SoftLight:
106 case QPainter::CompositionMode_HardLight:
108 case QPainter::CompositionMode_Difference:
110 case QPainter::CompositionMode_Exclusion:
112 case QPainter::CompositionMode_Source:
114 case QPainter::CompositionMode_DestinationOver:
116 case QPainter::CompositionMode_Clear:
118 case QPainter::CompositionMode_Destination:
120 case QPainter::CompositionMode_SourceIn:
122 case QPainter::CompositionMode_DestinationIn:
124 case QPainter::CompositionMode_SourceOut:
126 case QPainter::CompositionMode_DestinationOut:
128 case QPainter::CompositionMode_SourceAtop:
130 case QPainter::CompositionMode_DestinationAtop:
132 case QPainter::CompositionMode_Xor:
134 default:
135 QgsDebugError( QStringLiteral( "Composition mode %1 mapped to Normal" ).arg( blendMode ) );
137 }
138}
139
174
175QTransform QgsPainting::triangleToTriangleTransform( double inX1, double inY1, double inX2, double inY2, double inX3, double inY3, double outX1, double outY1, double outX2, double outY2, double outX3, double outY3, bool &ok )
176{
177 // QTransform maps points using X' = X * T (not X' = T * X !)
178 // So we are trying to solve the equation: U * T = V, where U = input triangle and V = output triangle
179 // Hence T = U^(-1) * V
180
181 const QTransform U(
182 inX1, inY1, 1,
183 inX2, inY2, 1,
184 inX3, inY3, 1 );
185
186 const QTransform V(
187 outX1, outY1, 1,
188 outX2, outY2, 1,
189 outX3, outY3, 1
190 );
191
192 return ( U.inverted( &ok ) ) * V;
193}
194
195bool QgsPainting::drawTriangleUsingTexture( QPainter *painter, const QPolygonF &triangle, const QImage &textureImage, float textureX1, float textureY1, float textureX2, float textureY2, float textureX3, float textureY3 )
196{
197 bool ok = false;
198 const QTransform brushTransform = triangleToTriangleTransform(
199 textureX1 * ( textureImage.width() - 1 ), textureY1 * ( textureImage.height() - 1 ),
200 textureX2 * ( textureImage.width() - 1 ), textureY2 * ( textureImage.height() - 1 ),
201 textureX3 * ( textureImage.width() - 1 ), textureY3 * ( textureImage.height() - 1 ),
202 triangle.at( 0 ).x(), triangle.at( 0 ).y(),
203 triangle.at( 1 ).x(), triangle.at( 1 ).y(),
204 triangle.at( 2 ).x(), triangle.at( 2 ).y(),
205 ok
206 );
207 if ( !ok )
208 return false;
209
210 // only store/restore the painter's current brush -- this is cheaper than saving/restoring the whole painter state
211 const QBrush previousBrush = painter->brush();
212
213 QBrush textureBrush( textureImage );
214 textureBrush.setTransform( brushTransform );
215
216 painter->setBrush( textureBrush );
217 painter->drawPolygon( triangle );
218 painter->setBrush( previousBrush );
219
220 return true;
221}
BlendMode
Blending modes defining the available composition modes that can be used when painting.
Definition qgis.h:3413
@ SoftLight
Soft light.
@ Destination
Destination.
@ Lighten
Lighten.
@ Subtract
Subtract.
@ Difference
Difference.
@ SourceOut
Source out.
@ DestinationOut
Destination out.
@ DestinationIn
Destination in.
@ Overlay
Overlay.
@ SourceIn
Source in.
@ Addition
Addition.
@ DestinationOver
Destination over.
@ SourceAtop
Source atop.
@ DestinationAtop
Destination atop.
@ HardLight
Hard light.
@ Multiply
Multiple.
static bool drawTriangleUsingTexture(QPainter *painter, const QPolygonF &triangle, const QImage &textureImage, float textureX1, float textureY1, float textureX2, float textureY2, float textureX3, float textureY3)
Draws a triangle onto a painter using a mapped texture image.
static Qgis::BlendMode getBlendModeEnum(QPainter::CompositionMode blendMode)
Returns a Qgis::BlendMode corresponding to a QPainter::CompositionMode.
static bool isClippingMode(Qgis::BlendMode mode)
Returns true if mode is a clipping blend mode.
static QTransform triangleToTriangleTransform(double inX1, double inY1, double inX2, double inY2, double inX3, double inY3, double outX1, double outY1, double outX2, double outY2, double outX3, double outY3, bool &ok)
Calculates the QTransform which maps the triangle defined by the points (inX1, inY1),...
static QPainter::CompositionMode getCompositionMode(Qgis::BlendMode blendMode)
Returns a QPainter::CompositionMode corresponding to a Qgis::BlendMode.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:4536
#define QgsDebugError(str)
Definition qgslogger.h:38