-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsannotationmarkeritem.cpp
180 lines (150 loc) · 6.24 KB
/
qgsannotationmarkeritem.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
177
178
179
180
/***************************************************************************
qgsannotationmarkeritem.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 "qgsannotationmarkeritem.h"
#include "qgssymbol.h"
#include "qgssymbollayerutils.h"
#include "qgsmarkersymbol.h"
#include "qgsannotationitemnode.h"
#include "qgsannotationitemeditoperation.h"
QgsAnnotationMarkerItem::QgsAnnotationMarkerItem( const QgsPoint &point )
: QgsAnnotationItem()
, mPoint( point )
, mSymbol( std::make_unique< QgsMarkerSymbol >() )
{
}
QgsAnnotationMarkerItem::~QgsAnnotationMarkerItem() = default;
QString QgsAnnotationMarkerItem::type() const
{
return QStringLiteral( "marker" );
}
void QgsAnnotationMarkerItem::render( QgsRenderContext &context, QgsFeedback * )
{
QPointF pt;
if ( context.coordinateTransform().isValid() )
{
double x = mPoint.x();
double y = mPoint.y();
double z = 0.0;
context.coordinateTransform().transformInPlace( x, y, z );
pt = QPointF( x, y );
}
else
pt = mPoint.toQPointF();
context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
mSymbol->startRender( context );
mSymbol->renderPoint( pt, nullptr, context );
mSymbol->stopRender( context );
}
bool QgsAnnotationMarkerItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
{
element.setAttribute( QStringLiteral( "x" ), qgsDoubleToString( mPoint.x() ) );
element.setAttribute( QStringLiteral( "y" ), qgsDoubleToString( mPoint.y() ) );
element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "markerSymbol" ), mSymbol.get(), document, context ) );
writeCommonProperties( element, document, context );
return true;
}
Qgis::AnnotationItemFlags QgsAnnotationMarkerItem::flags() const
{
// in truth this should depend on whether the marker symbol is scale dependent or not!
return Qgis::AnnotationItemFlag::ScaleDependentBoundingBox;
}
QList<QgsAnnotationItemNode> QgsAnnotationMarkerItem::nodes() const
{
return { QgsAnnotationItemNode( QgsVertexId( 0, 0, 0 ), mPoint, Qgis::AnnotationItemNodeType::VertexHandle )};
}
bool QgsAnnotationMarkerItem::applyEdit( QgsAbstractAnnotationItemEditOperation *operation )
{
if ( QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation ) )
{
mPoint = QgsPoint( moveOperation->after() );
return true;
}
else
{
return false;
}
}
QgsAnnotationItemEditOperationTransientResults *QgsAnnotationMarkerItem::transientEditResults( QgsAbstractAnnotationItemEditOperation *operation )
{
if ( QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation ) )
{
return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry::fromPointXY( moveOperation->after() ) );
}
return nullptr;
}
QgsAnnotationMarkerItem *QgsAnnotationMarkerItem::create()
{
return new QgsAnnotationMarkerItem( QgsPoint() );
}
bool QgsAnnotationMarkerItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
{
const double x = element.attribute( QStringLiteral( "x" ) ).toDouble();
const double y = element.attribute( QStringLiteral( "y" ) ).toDouble();
mPoint = QgsPoint( x, y );
const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
if ( !symbolElem.isNull() )
setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( symbolElem, context ) );
readCommonProperties( element, context );
return true;
}
QgsAnnotationMarkerItem *QgsAnnotationMarkerItem::clone()
{
std::unique_ptr< QgsAnnotationMarkerItem > item = std::make_unique< QgsAnnotationMarkerItem >( mPoint );
item->setSymbol( mSymbol->clone() );
item->copyCommonProperties( this );
return item.release();
}
QgsRectangle QgsAnnotationMarkerItem::boundingBox() const
{
return QgsRectangle( mPoint.x(), mPoint.y(), mPoint.x(), mPoint.y() );
}
QgsRectangle QgsAnnotationMarkerItem::boundingBox( QgsRenderContext &context ) const
{
QPointF pt;
if ( context.coordinateTransform().isValid() )
{
double x = mPoint.x();
double y = mPoint.y();
double z = 0.0;
context.coordinateTransform().transformInPlace( x, y, z );
pt = QPointF( x, y );
}
else
pt = mPoint.toQPointF();
context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
mSymbol->startRender( context );
const QRectF boundsInPixels = mSymbol->bounds( pt, context );
mSymbol->stopRender( context );
const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
return context.coordinateTransform().transformBoundingBox( boundsMapUnits, QgsCoordinateTransform::ReverseTransform );
}
bool QgsAnnotationMarkerItem::transform( const QTransform &transform )
{
mPoint.transform( transform );
return true;
}
const QgsMarkerSymbol *QgsAnnotationMarkerItem::symbol() const
{
return mSymbol.get();
}
void QgsAnnotationMarkerItem::setSymbol( QgsMarkerSymbol *symbol )
{
mSymbol.reset( symbol );
}