/
qgspointmarkeritem.cpp
200 lines (165 loc) · 5.55 KB
/
qgspointmarkeritem.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/***************************************************************************
qgspointmarkeritem.cpp
----------------------
begin : April 2016
copyright : (C) 2016 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 "qgspointmarkeritem.h"
#include "qgssymbol.h"
#include "qgsmapcanvas.h"
#include "qgsmapsettings.h"
#include "qgsproject.h"
#include "qgsexpressioncontextutils.h"
#include <QPainter>
#include <cmath>
//
// QgsMapCanvasSymbolItem
//
QgsMapCanvasSymbolItem::QgsMapCanvasSymbolItem( QgsMapCanvas *canvas )
: QgsMapCanvasItem( canvas )
, mOpacityEffect( new QgsDrawSourceEffect() )
{
setCacheMode( QGraphicsItem::ItemCoordinateCache );
}
QgsRenderContext QgsMapCanvasSymbolItem::renderContext( QPainter *painter )
{
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::atlasScope( nullptr );
if ( mMapCanvas )
{
context << QgsExpressionContextUtils::mapSettingsScope( mMapCanvas->mapSettings() )
<< new QgsExpressionContextScope( mMapCanvas->expressionContextScope() );
}
else
{
context << QgsExpressionContextUtils::mapSettingsScope( QgsMapSettings() );
}
//context << QgsExpressionContextUtils::layerScope( mLayer );
context.setFeature( mFeature );
context.setFields( mFeature.fields() );
//setup render context
QgsMapSettings ms = mMapCanvas->mapSettings();
ms.setExpressionContext( context );
QgsRenderContext rc = QgsRenderContext::fromMapSettings( ms );
rc.setPainter( painter );
return rc;
}
void QgsMapCanvasSymbolItem::paint( QPainter *painter )
{
if ( !painter )
{
return;
}
QgsRenderContext rc = renderContext( painter );
bool useEffect = !qgsDoubleNear( mOpacityEffect->opacity(), 1.0 );
if ( useEffect )
{
//use a paint effect to reduce opacity. If we directly set the opacity on the painter, then the symbol will NOT
//be correctly "flattened" and parts of the symbol which should be obscured will show through
mOpacityEffect->begin( rc );
}
mSymbol->startRender( rc, mFeature.fields() );
renderSymbol( rc, mFeature );
mSymbol->stopRender( rc );
if ( useEffect )
{
mOpacityEffect->end( rc );
}
}
void QgsMapCanvasSymbolItem::setSymbol( std::unique_ptr< QgsSymbol > symbol )
{
mSymbol = std::move( symbol );
}
const QgsSymbol *QgsMapCanvasSymbolItem::symbol() const
{
return mSymbol.get();
}
void QgsMapCanvasSymbolItem::setFeature( const QgsFeature &feature )
{
mFeature = feature;
}
void QgsMapCanvasSymbolItem::setOpacity( double opacity )
{
mOpacityEffect->setOpacity( opacity );
}
double QgsMapCanvasSymbolItem::opacity() const
{
return mOpacityEffect->opacity();
}
//
// QgsPointMarkerItem
//
QgsMapCanvasMarkerSymbolItem::QgsMapCanvasMarkerSymbolItem( QgsMapCanvas *canvas )
: QgsMapCanvasSymbolItem( canvas )
{
setSymbol( qgis::make_unique< QgsMarkerSymbol >() );
}
void QgsMapCanvasMarkerSymbolItem::setPointLocation( const QgsPointXY &p )
{
mLocation = toCanvasCoordinates( p );
}
void QgsMapCanvasMarkerSymbolItem::updateSize()
{
QgsRenderContext rc = renderContext( nullptr );
markerSymbol()->startRender( rc, mFeature.fields() );
QRectF bounds = markerSymbol()->bounds( mLocation, rc, mFeature );
markerSymbol()->stopRender( rc );
QgsRectangle r( mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( static_cast<int>( bounds.x() ),
static_cast<int>( bounds.y() ) ),
mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( static_cast<int>( bounds.x() + bounds.width() * 2 ),
static_cast<int>( bounds.y() + bounds.height() * 2 ) ) );
setRect( r );
}
void QgsMapCanvasMarkerSymbolItem::renderSymbol( QgsRenderContext &context, const QgsFeature &feature )
{
markerSymbol()->renderPoint( mLocation - pos(), &feature, context );
}
QgsMarkerSymbol *QgsMapCanvasMarkerSymbolItem::markerSymbol()
{
QgsMarkerSymbol *marker = dynamic_cast< QgsMarkerSymbol * >( mSymbol.get() );
Q_ASSERT( marker );
return marker;
}
//
// QgsLineMarkerItem
//
QgsMapCanvasLineSymbolItem::QgsMapCanvasLineSymbolItem( QgsMapCanvas *canvas )
: QgsMapCanvasSymbolItem( canvas )
{
setSymbol( qgis::make_unique< QgsLineSymbol >() );
}
void QgsMapCanvasLineSymbolItem::setLine( const QPolygonF &line )
{
mLine = line;
update();
}
void QgsMapCanvasLineSymbolItem::setLine( const QLineF &line )
{
mLine.clear();
mLine << line.p1() << line.p2();
update();
}
QRectF QgsMapCanvasLineSymbolItem::boundingRect() const
{
return mMapCanvas->rect();
}
void QgsMapCanvasLineSymbolItem::renderSymbol( QgsRenderContext &context, const QgsFeature &feature )
{
lineSymbol()->renderPolyline( mLine, &feature, context );
}
QgsLineSymbol *QgsMapCanvasLineSymbolItem::lineSymbol()
{
QgsLineSymbol *symbol = dynamic_cast< QgsLineSymbol * >( mSymbol.get() );
Q_ASSERT( symbol );
return symbol;
}