QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsmaskpaintdevice.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaskpaintdevice.h
3 --------------------------------------
4 Date : February 2022
5 Copyright : (C) 2022 by Julien Cabieces
6 Email : julien dot cabieces at oslandia 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 "qgsmaskpaintdevice.h"
17
18
19Q_GUI_EXPORT extern int qt_defaultDpiX();
20Q_GUI_EXPORT extern int qt_defaultDpiY();
21
23
24QgsMaskPaintEngine::QgsMaskPaintEngine( bool usePathStroker )
25 : QPaintEngine( QPaintEngine::AllFeatures )
26 , mUsePathStroker( usePathStroker )
27{
28}
29
30QPainterPath QgsMaskPaintEngine::maskPainterPath() const
31{
32 return mMaskPainterPath;
33}
34
35void QgsMaskPaintEngine::drawPath( const QPainterPath &path )
36{
37 if ( mUsePathStroker )
38 {
39 QPen pen = painter()->pen();
40 QPainterPathStroker stroker( pen );
41 QPainterPath strokedPath = stroker.createStroke( path );
42 strokedPath = painter()->combinedTransform().map( strokedPath );
43 mMaskPainterPath.addPath( strokedPath );
44 }
45 else
46 {
47 mMaskPainterPath.addPath( path );
48 }
49}
50
51void QgsMaskPaintEngine::drawPolygon( const QPointF *points, int numPoints, QPaintEngine::PolygonDrawMode mode )
52{
53 Q_UNUSED( mode );
54
55 QPolygonF polygon;
56 polygon.reserve( numPoints );
57 for ( int i = 0; i < numPoints; ++i )
58 polygon << points[i];
59 mMaskPainterPath.addPolygon( polygon );
60}
61
63
65{
66 mPaintEngine = std::make_unique<QgsMaskPaintEngine>( usePathStroker );
67}
68
70{
71 return mPaintEngine.get();
72}
73
74int QgsMaskPaintDevice::metric( PaintDeviceMetric m ) const
75{
76 // copy/paste from qpicture.cpp
77 int val;
78 QRectF brect = mPaintEngine->maskPainterPath().boundingRect();
79 switch ( m )
80 {
81 case PdmWidth:
82 val = brect.width();
83 break;
84 case PdmHeight:
85 val = brect.height();
86 break;
87 case PdmWidthMM:
88 val = int( 25.4 / qt_defaultDpiX() * brect.width() );
89 break;
90 case PdmHeightMM:
91 val = int( 25.4 / qt_defaultDpiY() * brect.height() );
92 break;
93 case PdmDpiX:
94 case PdmPhysicalDpiX:
95 val = qt_defaultDpiX();
96 break;
97 case PdmDpiY:
98 case PdmPhysicalDpiY:
99 val = qt_defaultDpiY();
100 break;
101 case PdmNumColors:
102 val = 16777216;
103 break;
104 case PdmDepth:
105 val = 24;
106 break;
107 case PdmDevicePixelRatio:
108 val = 1;
109 break;
110 case PdmDevicePixelRatioScaled:
111 val = 1 * QPaintDevice::devicePixelRatioFScale();
112 break;
113 default:
114 val = 0;
115 qWarning( "QPicture::metric: Invalid metric command" );
116 }
117 return val;
118}
119
121{
122 return mPaintEngine->maskPainterPath();
123}
QgsMaskPaintDevice(bool usePathStroker=false)
QPainterPath maskPainterPath() const
Returns the mask painter path painted on this paint device.
int metric(PaintDeviceMetric metric) const override
QPaintEngine * paintEngine() const override
Q_GUI_EXPORT int qt_defaultDpiX()
Q_GUI_EXPORT int qt_defaultDpiY()