QGIS API Documentation 3.41.0-Master (092dc69654e)
Loading...
Searching...
No Matches
qgslegendpatchshape.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslegendpatchshape.cpp
3 -------------------
4begin : April 2020
5copyright : (C) 2020 by Nyall Dawson
6email : 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 "qgslegendpatchshape.h"
19#include "qgsstyle.h"
20
21QgsLegendPatchShape::QgsLegendPatchShape( Qgis::SymbolType type, const QgsGeometry &geometry, bool preserveAspectRatio )
22 : mSymbolType( type )
23 , mGeometry( geometry )
24 , mPreserveAspectRatio( preserveAspectRatio )
25{
26
27}
28
30{
31 return mGeometry.isNull() || mGeometry.isEmpty();
32}
33
35{
36 return mGeometry;
37}
38
40{
41 mGeometry = geometry;
42}
43
45{
46 return mPreserveAspectRatio;
47}
48
49void QgsLegendPatchShape::setPreserveAspectRatio( bool preserveAspectRatio )
50{
51 mPreserveAspectRatio = preserveAspectRatio;
52}
53
55{
56 return mScaleToTargetSize;
57}
58
60{
61 mScaleToTargetSize = scale;
62}
63
65{
66 QgsGeometry geom = mGeometry;
67 if ( mScaleToTargetSize )
68 {
69 // scale and translate to desired size
70
71 const QRectF bounds = mGeometry.boundingBox().toRectF();
72
73 double dx = 0;
74 double dy = 0;
75 if ( mPreserveAspectRatio && bounds.height() > 0 && bounds.width() > 0 )
76 {
77 const double scaling = std::min( size.width() / bounds.width(), size.height() / bounds.height() );
78 const QSizeF scaledSize = bounds.size() * scaling;
79 dx = ( size.width() - scaledSize.width() ) / 2.0;
80 dy = ( size.height() - scaledSize.height() ) / 2.0;
81 size = scaledSize;
82 }
83
84 // important -- the transform needs to flip from north-up to painter style "increasing y down" coordinates
85 const QPolygonF targetRectPoly = QPolygonF() << QPointF( dx, dy + size.height() )
86 << QPointF( dx + size.width(), dy + size.height() )
87 << QPointF( dx + size.width(), dy )
88 << QPointF( dx, dy );
89 QTransform t;
90
91 if ( bounds.width() > 0 && bounds.height() > 0 )
92 {
93 QPolygonF patchRectPoly = QPolygonF( bounds );
94 //workaround QT Bug #21329
95 patchRectPoly.pop_back();
96
97 QTransform::quadToQuad( patchRectPoly, targetRectPoly, t );
98 }
99 else if ( bounds.width() > 0 )
100 {
101 t = QTransform::fromScale( size.width() / bounds.width(), 1 ).translate( -bounds.left(), size.height() / 2 - bounds.y() );
102 }
103 else if ( bounds.height() > 0 )
104 {
105 t = QTransform::fromScale( 1, size.height() / bounds.height() ).translate( size.width() / 2 - bounds.x(), -bounds.top() );
106 }
107
108 geom.transform( t );
109 }
110 return geom;
111}
112
113QList<QList<QPolygonF> > QgsLegendPatchShape::toQPolygonF( Qgis::SymbolType type, QSizeF size ) const
114{
115 if ( isNull() || type != mSymbolType )
116 return QgsStyle::defaultStyle()->defaultPatchAsQPolygonF( type, size );
117
118 const QgsGeometry geom = scaledGeometry( size );
120 {
121 QPolygonF points;
122 points << QPointF( size.width() / 2, size.height() / 2 );
123 return QList< QList<QPolygonF> >() << ( QList< QPolygonF >() << points );
124 }
125
126 return QgsSymbolLayerUtils::toQPolygonF( geom, type );
127}
128
129void QgsLegendPatchShape::readXml( const QDomElement &element, const QgsReadWriteContext & )
130{
131 mGeometry = QgsGeometry::fromWkt( element.attribute( QStringLiteral( "wkt" ) ) );
132 mPreserveAspectRatio = element.attribute( QStringLiteral( "preserveAspect" ) ).toInt();
133 mSymbolType = static_cast< Qgis::SymbolType >( element.attribute( QStringLiteral( "type" ) ).toInt() );
134}
135
136void QgsLegendPatchShape::writeXml( QDomElement &element, QDomDocument &, const QgsReadWriteContext & ) const
137{
138 element.setAttribute( QStringLiteral( "wkt" ), mGeometry.isNull() ? QString() : mGeometry.asWkt( ) );
139 element.setAttribute( QStringLiteral( "preserveAspect" ), mPreserveAspectRatio ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
140 element.setAttribute( QStringLiteral( "type" ), QString::number( static_cast< int >( mSymbolType ) ) );
141}
142
144{
145 return mSymbolType;
146}
147
149{
150 mSymbolType = type;
151}
SymbolType
Symbol types.
Definition qgis.h:574
@ Marker
Marker symbol.
@ MultiPoint
MultiPoint.
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
static Q_INVOKABLE QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Q_INVOKABLE QString asWkt(int precision=17) const
Exports the geometry to WKT.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
bool scaleToOutputSize() const
Returns true if the patch shape should by resized to the desired target size when rendering.
void setSymbolType(Qgis::SymbolType type)
Sets the symbol type associated with this patch.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Read settings from a DOM element.
QList< QList< QPolygonF > > toQPolygonF(Qgis::SymbolType type, QSizeF size) const
Converts the patch shape to a set of QPolygonF objects representing how the patch should be drawn for...
QgsGeometry scaledGeometry(QSizeF size) const
Returns the patch shape's geometry, scaled to the given size.
void setGeometry(const QgsGeometry &geometry)
Sets the geometry for the patch shape.
bool preserveAspectRatio() const
Returns true if the patch shape should preserve its aspect ratio when it is resized to fit a desired ...
bool isNull() const
Returns true if the patch shape is a null QgsLegendPatchShape, which indicates that the default legen...
QgsLegendPatchShape()=default
Constructor for a null QgsLegendPatchShape.
void setPreserveAspectRatio(bool preserve)
Sets whether the patch shape should preserve its aspect ratio when it is resized to fit a desired leg...
void writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
QgsGeometry geometry() const
Returns the geometry for the patch shape.
void setScaleToOutputSize(bool scale)
Sets whether the patch shape should by resized to the desired target size when rendering.
Qgis::SymbolType symbolType() const
Returns the symbol type associated with this patch.
The class is used as a container of context for various read/write operations on other objects.
QRectF toRectF() const
Returns a QRectF with same coordinates as the rectangle.
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
Definition qgsstyle.cpp:146
QList< QList< QPolygonF > > defaultPatchAsQPolygonF(Qgis::SymbolType type, QSizeF size) const
Returns the default patch geometry for the given symbol type and size as a set of QPolygonF objects (...
static QList< QList< QPolygonF > > toQPolygonF(const QgsGeometry &geometry, Qgis::SymbolType type)
Converts a geometry to a set of QPolygonF objects representing how the geometry should be drawn for a...
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.