QGIS API Documentation 4.5.0-Master (3b5d115045b)
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
18#include "qgslogger.h"
19
20#include <QPicture>
21#include <QString>
22#include <QTransform>
23
24using namespace Qt::StringLiterals;
25
26Q_GUI_EXPORT extern int qt_defaultDpiX();
27Q_GUI_EXPORT extern int qt_defaultDpiY();
28
29QPainter::CompositionMode QgsPainting::getCompositionMode( Qgis::BlendMode blendMode )
30{
31 // Map Qgis::BlendMode::Normal to QPainter::CompositionMode
32 switch ( blendMode )
33 {
35 return QPainter::CompositionMode_SourceOver;
37 return QPainter::CompositionMode_Lighten;
39 return QPainter::CompositionMode_Screen;
41 return QPainter::CompositionMode_ColorDodge;
43 return QPainter::CompositionMode_Plus;
45 return QPainter::CompositionMode_Darken;
47 return QPainter::CompositionMode_Multiply;
49 return QPainter::CompositionMode_ColorBurn;
51 return QPainter::CompositionMode_Overlay;
53 return QPainter::CompositionMode_SoftLight;
55 return QPainter::CompositionMode_HardLight;
57 return QPainter::CompositionMode_Difference;
59 return QPainter::CompositionMode_Exclusion;
61 return QPainter::CompositionMode_Source;
63 return QPainter::CompositionMode_DestinationOver;
65 return QPainter::CompositionMode_Clear;
67 return QPainter::CompositionMode_Destination;
69 return QPainter::CompositionMode_SourceIn;
71 return QPainter::CompositionMode_DestinationIn;
73 return QPainter::CompositionMode_SourceOut;
75 return QPainter::CompositionMode_DestinationOut;
77 return QPainter::CompositionMode_SourceAtop;
79 return QPainter::CompositionMode_DestinationAtop;
81 return QPainter::CompositionMode_Xor;
82 default:
83 QgsDebugError( u"Blend mode %1 mapped to SourceOver"_s.arg( qgsEnumValueToKey( blendMode ) ) );
84 return QPainter::CompositionMode_SourceOver;
85 }
86}
87
88
89Qgis::BlendMode QgsPainting::getBlendModeEnum( QPainter::CompositionMode blendMode )
90{
91 // Map QPainter::CompositionMode to Qgis::BlendMode::Normal
92 switch ( blendMode )
93 {
94 case QPainter::CompositionMode_SourceOver:
96 case QPainter::CompositionMode_Lighten:
98 case QPainter::CompositionMode_Screen:
100 case QPainter::CompositionMode_ColorDodge:
102 case QPainter::CompositionMode_Plus:
104 case QPainter::CompositionMode_Darken:
106 case QPainter::CompositionMode_Multiply:
108 case QPainter::CompositionMode_ColorBurn:
110 case QPainter::CompositionMode_Overlay:
112 case QPainter::CompositionMode_SoftLight:
114 case QPainter::CompositionMode_HardLight:
116 case QPainter::CompositionMode_Difference:
118 case QPainter::CompositionMode_Exclusion:
120 case QPainter::CompositionMode_Source:
122 case QPainter::CompositionMode_DestinationOver:
124 case QPainter::CompositionMode_Clear:
126 case QPainter::CompositionMode_Destination:
128 case QPainter::CompositionMode_SourceIn:
130 case QPainter::CompositionMode_DestinationIn:
132 case QPainter::CompositionMode_SourceOut:
134 case QPainter::CompositionMode_DestinationOut:
136 case QPainter::CompositionMode_SourceAtop:
138 case QPainter::CompositionMode_DestinationAtop:
140 case QPainter::CompositionMode_Xor:
142 default:
143 QgsDebugError( u"Composition mode %1 mapped to Normal"_s.arg( blendMode ) );
145 }
146}
147
182
184 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
185)
186{
187 // QTransform maps points using X' = X * T (not X' = T * X !)
188 // So we are trying to solve the equation: U * T = V, where U = input triangle and V = output triangle
189 // Hence T = U^(-1) * V
190
191 const QTransform U( inX1, inY1, 1, inX2, inY2, 1, inX3, inY3, 1 );
192
193 const QTransform V( outX1, outY1, 1, outX2, outY2, 1, outX3, outY3, 1 );
194
195 return ( U.inverted( &ok ) ) * V;
196}
197
199 QPainter *painter, const QPolygonF &triangle, const QImage &textureImage, float textureX1, float textureY1, float textureX2, float textureY2, float textureX3, float textureY3
200)
201{
202 bool ok = false;
203 const QTransform brushTransform = triangleToTriangleTransform(
204 textureX1 * ( textureImage.width() - 1 ),
205 textureY1 * ( textureImage.height() - 1 ),
206 textureX2 * ( textureImage.width() - 1 ),
207 textureY2 * ( textureImage.height() - 1 ),
208 textureX3 * ( textureImage.width() - 1 ),
209 textureY3 * ( textureImage.height() - 1 ),
210 triangle.at( 0 ).x(),
211 triangle.at( 0 ).y(),
212 triangle.at( 1 ).x(),
213 triangle.at( 1 ).y(),
214 triangle.at( 2 ).x(),
215 triangle.at( 2 ).y(),
216 ok
217 );
218 if ( !ok )
219 return false;
220
221 // only store/restore the painter's current brush -- this is cheaper than saving/restoring the whole painter state
222 const QBrush previousBrush = painter->brush();
223
224 QBrush textureBrush( textureImage );
225 textureBrush.setTransform( brushTransform );
226
227 painter->setBrush( textureBrush );
228 painter->drawPolygon( triangle );
229 painter->setBrush( previousBrush );
230
231 return true;
232}
233
235{
236 return qt_defaultDpiX();
237}
238
240{
241 return qt_defaultDpiY();
242}
243
245{
246 // QPicture makes an assumption that we drawing to it with system DPI.
247 // Then when being drawn, it scales the painter. The following call
248 // negates the effect. There is no way of setting QPicture's DPI.
249 // See QTBUG-20361
250 painter->scale( static_cast< double >( QgsPainting::qtDefaultDpiX() ) / painter->device()->logicalDpiX(), static_cast< double >( QgsPainting::qtDefaultDpiY() ) / painter->device()->logicalDpiY() );
251}
252
253void QgsPainting::drawPicture( QPainter *painter, const QPointF &point, const QPicture &picture )
254{
255 // QPicture makes an assumption that we drawing to it with system DPI.
256 // Then when being drawn, it scales the painter. The following call
257 // negates the effect. There is no way of setting QPicture's DPI.
258 // See QTBUG-20361
259 const double xScale = static_cast< double >( QgsPainting::qtDefaultDpiX() ) / painter->device()->logicalDpiX();
260 const double yScale = static_cast< double >( QgsPainting::qtDefaultDpiY() ) / painter->device()->logicalDpiY();
261 painter->scale( xScale, yScale );
262 painter->drawPicture( QPointF( point.x() / xScale, point.y() / yScale ), picture );
263 painter->scale( 1 / xScale, 1 / yScale );
264}
265
266void QgsPainting::rotatePainterAroundPoint( QPainter *painter, const QPointF &point, double angle )
267{
268 painter->translate( point.x(), point.y() );
269 painter->rotate( angle );
270 painter->translate( -point.x(), -point.y() );
271}
272
273QPicture QgsPainting::deepCopyQPicture( const QPicture &picture )
274{
275 QPicture p;
276 // For some reason p.detach() doesn't seem to always work as intended
277 // Serialization/deserialization is a safe way to be ensured we don't
278 // share a copy.
279 p.setData( picture.data(), picture.size() );
280 p.setBoundingRect( picture.boundingRect() );
281 return p;
282}
BlendMode
Blending modes defining the available composition modes that can be used when painting.
Definition qgis.h:5512
@ SoftLight
Soft light.
Definition qgis.h:5522
@ Burn
Burn.
Definition qgis.h:5520
@ Destination
Destination.
Definition qgis.h:5529
@ Lighten
Lighten.
Definition qgis.h:5514
@ Subtract
Subtract.
Definition qgis.h:5525
@ Difference
Difference.
Definition qgis.h:5524
@ Screen
Screen.
Definition qgis.h:5515
@ SourceOut
Source out.
Definition qgis.h:5532
@ DestinationOut
Destination out.
Definition qgis.h:5533
@ DestinationIn
Destination in.
Definition qgis.h:5531
@ Overlay
Overlay.
Definition qgis.h:5521
@ SourceIn
Source in.
Definition qgis.h:5530
@ Addition
Addition.
Definition qgis.h:5517
@ DestinationOver
Destination over.
Definition qgis.h:5527
@ SourceAtop
Source atop.
Definition qgis.h:5534
@ DestinationAtop
Destination atop.
Definition qgis.h:5535
@ Normal
Normal.
Definition qgis.h:5513
@ Dodge
Dodge.
Definition qgis.h:5516
@ HardLight
Hard light.
Definition qgis.h:5523
@ Clear
Clear.
Definition qgis.h:5528
@ Multiply
Multiple.
Definition qgis.h:5519
@ Source
Source.
Definition qgis.h:5526
@ Darken
Darken.
Definition qgis.h:5518
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 void rotatePainterAroundPoint(QPainter *painter, const QPointF &point, double angle)
Rotates a painter by angle degrees clockwise around point.
static int qtDefaultDpiY()
Returns the default Qt vertical DPI.
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 int qtDefaultDpiX()
Returns the default Qt horizontal DPI.
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.
static QPicture deepCopyQPicture(const QPicture &picture)
Deep copies a QPicture, making a copy which is safe to use in a different thread.
static void applyScaleFixForQPictureDpi(QPainter *painter)
Applies a workaround to a painter to avoid an issue with incorrect scaling when drawing QPictures.
static void drawPicture(QPainter *painter, const QPointF &point, const QPicture &picture)
Draws a picture onto a painter, correctly applying workarounds to avoid issues with incorrect scaling...
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:8100
#define QgsDebugError(str)
Definition qgslogger.h:71
Q_GUI_EXPORT int qt_defaultDpiX()
Q_GUI_EXPORT int qt_defaultDpiY()