/
qgsannotationpolygonitem.cpp
176 lines (148 loc) · 5.68 KB
/
qgsannotationpolygonitem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/***************************************************************************
qgsannotationpolygonitem.cpp
----------------
begin : July 2020
copyright : (C) 2020 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsannotationpolygonitem.h"
#include "qgssymbol.h"
#include "qgssymbollayerutils.h"
#include "qgssurface.h"
#include "qgsfillsymbol.h"
#include "qgsannotationitemnode.h"
QgsAnnotationPolygonItem::QgsAnnotationPolygonItem( QgsCurvePolygon *polygon )
: QgsAnnotationItem()
, mPolygon( polygon )
, mSymbol( std::make_unique< QgsFillSymbol >() )
{
}
QgsAnnotationPolygonItem::~QgsAnnotationPolygonItem() = default;
QString QgsAnnotationPolygonItem::type() const
{
return QStringLiteral( "polygon" );
}
void QgsAnnotationPolygonItem::render( QgsRenderContext &context, QgsFeedback * )
{
auto transformRing = [&context]( QPolygonF & pts )
{
//transform the QPolygonF to screen coordinates
if ( context.coordinateTransform().isValid() )
{
try
{
context.coordinateTransform().transformPolygon( pts );
}
catch ( QgsCsException & )
{
// we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
}
}
// remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
pts.erase( std::remove_if( pts.begin(), pts.end(),
[]( const QPointF point )
{
return !std::isfinite( point.x() ) || !std::isfinite( point.y() );
} ), pts.end() );
QPointF *ptr = pts.data();
for ( int i = 0; i < pts.size(); ++i, ++ptr )
{
context.mapToPixel().transformInPlace( ptr->rx(), ptr->ry() );
}
};
QPolygonF exterior = mPolygon->exteriorRing()->asQPolygonF();
transformRing( exterior );
QVector<QPolygonF> rings;
rings.reserve( mPolygon->numInteriorRings() );
for ( int i = 0; i < mPolygon->numInteriorRings(); ++i )
{
QPolygonF ring = mPolygon->interiorRing( i )->asQPolygonF();
transformRing( ring );
rings.append( ring );
}
mSymbol->startRender( context );
mSymbol->renderPolygon( exterior, rings.empty() ? nullptr : &rings, nullptr, context );
mSymbol->stopRender( context );
}
bool QgsAnnotationPolygonItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
{
element.setAttribute( QStringLiteral( "wkt" ), mPolygon->asWkt() );
element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "lineSymbol" ), mSymbol.get(), document, context ) );
writeCommonProperties( element, document, context );
return true;
}
QList<QgsAnnotationItemNode> QgsAnnotationPolygonItem::nodes() const
{
QList< QgsAnnotationItemNode > res;
auto processRing = [&res]( const QgsCurve * ring, int ringId )
{
// we don't want a duplicate node for the closed ring vertex
const int count = ring->isClosed() ? ring->numPoints() - 1 : ring->numPoints();
res.reserve( res.size() + count );
for ( int i = 0; i < count; ++i )
{
res << QgsAnnotationItemNode( QgsVertexId( 0, ringId, i ), QgsPointXY( ring->xAt( i ), ring->yAt( i ) ), Qgis::AnnotationItemNodeType::VertexHandle );
}
};
if ( const QgsCurve *ring = mPolygon->exteriorRing() )
{
processRing( ring, 0 );
}
for ( int i = 0; i < mPolygon->numInteriorRings(); ++i )
{
processRing( mPolygon->interiorRing( i ), i + 1 );
}
return res;
}
QgsGeometry QgsAnnotationPolygonItem::rubberBandGeometry() const
{
return QgsGeometry( mPolygon->clone() );
}
bool QgsAnnotationPolygonItem::transform( const QTransform &transform )
{
mPolygon->transform( transform );
return true;
}
QgsAnnotationPolygonItem *QgsAnnotationPolygonItem::create()
{
return new QgsAnnotationPolygonItem( new QgsPolygon() );
}
bool QgsAnnotationPolygonItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
{
const QString wkt = element.attribute( QStringLiteral( "wkt" ) );
const QgsGeometry geometry = QgsGeometry::fromWkt( wkt );
if ( const QgsCurvePolygon *polygon = qgsgeometry_cast< const QgsCurvePolygon * >( geometry.constGet() ) )
mPolygon.reset( polygon->clone() );
const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
if ( !symbolElem.isNull() )
setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( symbolElem, context ) );
readCommonProperties( element, context );
return true;
}
QgsAnnotationPolygonItem *QgsAnnotationPolygonItem::clone()
{
std::unique_ptr< QgsAnnotationPolygonItem > item = std::make_unique< QgsAnnotationPolygonItem >( mPolygon->clone() );
item->setSymbol( mSymbol->clone() );
item->copyCommonProperties( this );;
return item.release();
}
QgsRectangle QgsAnnotationPolygonItem::boundingBox() const
{
return mPolygon->boundingBox();
}
const QgsFillSymbol *QgsAnnotationPolygonItem::symbol() const
{
return mSymbol.get();
}
void QgsAnnotationPolygonItem::setSymbol( QgsFillSymbol *symbol )
{
mSymbol.reset( symbol );
}