QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsmapcanvasannotationitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmapcanvasannotationitem.cpp
3 ------------------------------
4 begin : January 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : 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
19#include "moc_qgsmapcanvasannotationitem.cpp"
20#include "qgsannotation.h"
21#include "qgsmapcanvas.h"
22#include "qgsmaptool.h"
23#include "qgsvectorlayer.h"
24#include "qgsfeatureiterator.h"
25#include "qgsexception.h"
26#include "qgssymbollayerutils.h"
27#include "qgsproject.h"
29#include "qgsfillsymbol.h"
30#include "qgsmarkersymbol.h"
31
32#include <QPainter>
33
34
36 : QgsMapCanvasItem( mapCanvas )
37 , mAnnotation( annotation )
38{
39 setFlag( QGraphicsItem::ItemIsSelectable, true );
40 if ( mapCanvas && !mapCanvas->annotationsVisible() )
41 setVisible( false );
42
43 connect( mAnnotation, &QgsAnnotation::appearanceChanged, this, [this] { update(); } );
44 connect( mAnnotation, &QgsAnnotation::moved, this, [this] { updatePosition(); } );
45 connect( mAnnotation, &QgsAnnotation::moved, this, &QgsMapCanvasAnnotationItem::setFeatureForMapPosition );
46 connect( mMapCanvas, &QgsMapCanvas::destinationCrsChanged, this, [ = ] { updatePosition(); } );
47
48 connect( mAnnotation, &QgsAnnotation::appearanceChanged, this, &QgsMapCanvasAnnotationItem::updateBoundingRect );
49
50 connect( mMapCanvas, &QgsMapCanvas::layersChanged, this, &QgsMapCanvasAnnotationItem::onCanvasLayersChanged );
51 connect( mAnnotation, &QgsAnnotation::mapLayerChanged, this, &QgsMapCanvasAnnotationItem::onCanvasLayersChanged );
52
53 //lifetime is tied to annotation!
54 connect( mAnnotation, &QgsAnnotation::destroyed, this, &QgsMapCanvasAnnotationItem::annotationDeleted );
55
57 setFeatureForMapPosition();
58}
59
61{
62 if ( !mAnnotation )
63 return;
64
65 if ( mAnnotation->hasFixedMapPosition() )
66 {
68 QgsPointXY coord = mAnnotation->mapPosition();
69 try
70 {
71 coord = t.transform( coord );
72 }
73 catch ( QgsCsException & )
74 {}
75 setPos( toCanvasCoordinates( coord ) );
76 }
77 else
78 {
79 //relative position
80
81 const double x = mAnnotation->relativePosition().x() * mMapCanvas->width();
82 const double y = mAnnotation->relativePosition().y() * mMapCanvas->height();
83 setPos( x, y );
84 }
85 updateBoundingRect();
86}
87
89{
90 return mBoundingRect;
91}
92
93void QgsMapCanvasAnnotationItem::updateBoundingRect()
94{
95 prepareGeometryChange();
96
98 const double fillSymbolBleed = mAnnotation && mAnnotation->fillSymbol() ?
100
101 const double mmToPixelScale = mMapCanvas->physicalDpiX() / 25.4;
102
103 if ( mAnnotation && !mAnnotation->hasFixedMapPosition() )
104 {
105 mBoundingRect = QRectF( - fillSymbolBleed, -fillSymbolBleed,
106 mmToPixelScale * mAnnotation->frameSizeMm().width() + fillSymbolBleed * 2,
107 mmToPixelScale * mAnnotation->frameSizeMm().height() + fillSymbolBleed * 2 );
108 }
109 else
110 {
111 double halfSymbolSize = 0.0;
112 if ( mAnnotation && mAnnotation->markerSymbol() )
113 {
114 halfSymbolSize = scaledSymbolSize() / 2.0;
115 }
116
117 const QPointF offset = mAnnotation ? QPointF( mAnnotation->frameOffsetFromReferencePointMm().x() * mmToPixelScale,
118 mAnnotation->frameOffsetFromReferencePointMm().y() * mmToPixelScale ) : QPointF( 0, 0 );
119
120 const QSizeF frameSize = mAnnotation ? QSizeF( mAnnotation->frameSizeMm().width() * mmToPixelScale,
121 mAnnotation->frameSizeMm().height() * mmToPixelScale ) : QSizeF( 0.0, 0.0 );
122
123 const double xMinPos = std::min( -halfSymbolSize, offset.x() - fillSymbolBleed );
124 const double xMaxPos = std::max( halfSymbolSize, offset.x() + frameSize.width() + fillSymbolBleed );
125 const double yMinPos = std::min( -halfSymbolSize, offset.y() - fillSymbolBleed );
126 const double yMaxPos = std::max( halfSymbolSize, offset.y() + frameSize.height() + fillSymbolBleed );
127 mBoundingRect = QRectF( xMinPos, yMinPos, xMaxPos - xMinPos, yMaxPos - yMinPos );
128 }
129}
130
131void QgsMapCanvasAnnotationItem::onCanvasLayersChanged()
132{
133 if ( !mAnnotation )
134 return;
136 {
137 setVisible( false );
138 }
139 else if ( !mAnnotation->mapLayer() )
140 {
141 setVisible( true );
142 }
143 else
144 {
145 setVisible( mMapCanvas->mapSettings().layers( true ).contains( mAnnotation->mapLayer() ) );
146 }
147}
148
149void QgsMapCanvasAnnotationItem::setFeatureForMapPosition()
150{
151 if ( !mAnnotation || !mAnnotation->hasFixedMapPosition() )
152 return;
153
154 QgsVectorLayer *vectorLayer = qobject_cast< QgsVectorLayer * >( mAnnotation->mapLayer() );
155 if ( !vectorLayer )
156 return;
157
158 const double halfIdentifyWidth = QgsMapTool::searchRadiusMU( mMapCanvas );
159 QgsPointXY mapPosition = mAnnotation->mapPosition();
160
161 try
162 {
164 if ( ct.isValid() )
165 mapPosition = ct.transform( mapPosition );
166 }
167 catch ( QgsCsException & )
168 {
169 }
170
171 QgsRectangle searchRect( mapPosition.x() - halfIdentifyWidth, mapPosition.y() - halfIdentifyWidth,
172 mapPosition.x() + halfIdentifyWidth, mapPosition.y() + halfIdentifyWidth );
173
174 searchRect = mMapCanvas->mapSettings().mapToLayerCoordinates( vectorLayer, searchRect );
175
176 QgsFeatureIterator fit = vectorLayer->getFeatures( QgsFeatureRequest().setFilterRect( searchRect ).setFlags( Qgis::FeatureRequestFlag::ExactIntersect ).setLimit( 1 ) );
177
178 QgsFeature currentFeature;
179 ( void )fit.nextFeature( currentFeature );
180 mAnnotation->setAssociatedFeature( currentFeature );
181}
182
183void QgsMapCanvasAnnotationItem::annotationDeleted()
184{
185 mAnnotation = nullptr;
186 deleteLater();
187}
188
189void QgsMapCanvasAnnotationItem::drawSelectionBoxes( QPainter *p ) const
190{
191 if ( !p )
192 {
193 return;
194 }
195
196 const double handlerSize = 10;
197 p->setPen( Qt::NoPen );
198 p->setBrush( QColor( 200, 200, 210, 120 ) );
199 p->drawRect( QRectF( mBoundingRect.left(), mBoundingRect.top(), handlerSize, handlerSize ) );
200 p->drawRect( QRectF( mBoundingRect.right() - handlerSize, mBoundingRect.top(), handlerSize, handlerSize ) );
201 p->drawRect( QRectF( mBoundingRect.right() - handlerSize, mBoundingRect.bottom() - handlerSize, handlerSize, handlerSize ) );
202 p->drawRect( QRectF( mBoundingRect.left(), mBoundingRect.bottom() - handlerSize, handlerSize, handlerSize ) );
203}
204
206{
207 const QPointF itemPos = mapFromScene( pos );
208
209 const int cursorSensitivity = 7;
210
211 if ( mAnnotation && mAnnotation->hasFixedMapPosition() &&
212 std::fabs( itemPos.x() ) < cursorSensitivity && std::fabs( itemPos.y() ) < cursorSensitivity ) //move map point if position is close to the origin
213 {
214 return MoveMapPosition;
215 }
216
217 const double mmToPixelScale = mMapCanvas->logicalDpiX() / 25.4;
218
219 const QPointF offset = mAnnotation && mAnnotation->hasFixedMapPosition() ? mAnnotation->frameOffsetFromReferencePointMm() * mmToPixelScale : QPointF( 0, 0 );
220 const QSizeF frameSize = mAnnotation ? mAnnotation->frameSizeMm() * mmToPixelScale : QSizeF( 0, 0 );
221
222 bool left, right, up, down, inframe;
223 left = std::fabs( itemPos.x() - offset.x() ) < cursorSensitivity;
224 right = std::fabs( itemPos.x() - ( offset.x() + frameSize.width() ) ) < cursorSensitivity;
225 up = std::fabs( itemPos.y() - offset.y() ) < cursorSensitivity;
226 down = std::fabs( itemPos.y() - ( offset.y() + frameSize.height() ) ) < cursorSensitivity;
227 inframe = (
228 itemPos.x() + cursorSensitivity >= offset.x() &&
229 itemPos.x() - cursorSensitivity <= ( offset.x() + frameSize.width() ) &&
230 itemPos.y() + cursorSensitivity >= offset.y() &&
231 itemPos.y() - cursorSensitivity <= ( offset.y() + frameSize.height() ) );
232
233 // Resize actions are only available if the item is selected
234 // Otherwise, mouse handles are not visible
235 if ( isSelected() )
236 {
237 if ( left && up )
238 {
239 return ResizeFrameLeftUp;
240 }
241 else if ( right && up )
242 {
243 return ResizeFrameRightUp;
244 }
245 else if ( left && down )
246 {
247 return ResizeFrameLeftDown;
248 }
249 else if ( right && down )
250 {
252 }
253 if ( left && inframe )
254 {
255 return ResizeFrameLeft;
256 }
257 if ( right && inframe )
258 {
259 return ResizeFrameRight;
260 }
261 if ( up && inframe )
262 {
263 return ResizeFrameUp;
264 }
265 if ( down && inframe )
266 {
267 return ResizeFrameDown;
268 }
269 }
270
271 //finally test if pos is in the frame area
272 if ( inframe )
273 {
274 return MoveFramePosition;
275 }
276 return NoAction;
277}
278
280{
281 switch ( moveAction )
282 {
283 case NoAction:
284 return Qt::ArrowCursor;
285 case MoveMapPosition:
287 return Qt::SizeAllCursor;
288 case ResizeFrameUp:
289 case ResizeFrameDown:
290 return Qt::SizeVerCursor;
291 case ResizeFrameLeft:
292 case ResizeFrameRight:
293 return Qt::SizeHorCursor;
296 return Qt::SizeFDiagCursor;
299 return Qt::SizeBDiagCursor;
300 default:
301 return Qt::ArrowCursor;
302 }
303}
304
305double QgsMapCanvasAnnotationItem::scaledSymbolSize() const
306{
307 if ( !mAnnotation || !mAnnotation->markerSymbol() )
308 {
309 return 0.0;
310 }
311
312 if ( !mMapCanvas )
313 {
314 return mAnnotation->markerSymbol()->size();
315 }
316
317 const double dpmm = mMapCanvas->logicalDpiX() / 25.4;
318 return dpmm * mAnnotation->markerSymbol()->size();
319}
320
321void QgsMapCanvasAnnotationItem::paint( QPainter *painter )
322{
323 if ( !mAnnotation || !mAnnotation->isVisible() )
324 return;
325
328
329 if ( mAnnotation )
330 mAnnotation->render( rc );
331
332 if ( isSelected() )
333 {
334 drawSelectionBoxes( painter );
335 }
336}
@ ExactIntersect
Use exact geometry intersection (slower) instead of bounding boxes.
@ Antialiasing
Use antialiasing while drawing.
Abstract base class for annotation items which are drawn over a map.
void appearanceChanged()
Emitted whenever the annotation's appearance changes.
bool hasFixedMapPosition
QgsCoordinateReferenceSystem mapPositionCrs() const
Returns the CRS of the map position, or an invalid CRS if the annotation does not have a fixed map po...
QPointF frameOffsetFromReferencePointMm() const
Returns the annotation's frame's offset (in millimeters) from the mapPosition() reference point.
QgsMarkerSymbol * markerSymbol() const
Returns the symbol that is drawn at the annotation's map position.
void moved()
Emitted when the annotation's position has changed and items need to be moved to reflect this.
QgsMapLayer * mapLayer() const
Returns the map layer associated with the annotation.
virtual void setAssociatedFeature(const QgsFeature &feature)
Sets the feature associated with the annotation.
QgsPointXY mapPosition
void render(QgsRenderContext &context) const
Renders the annotation to a target render context.
bool isVisible() const
Returns true if the annotation is visible and should be rendered.
QgsFillSymbol * fillSymbol() const
Returns the symbol that is used for rendering the annotation frame.
QSizeF frameSizeMm() const
Returns the size (in millimeters) of the annotation's frame (the main area in which the annotation's ...
QPointF relativePosition() const
Returns the relative position of the annotation, if it is not attached to a fixed map position.
void mapLayerChanged()
Emitted when the map layer associated with the annotation changes.
Class for doing transforms between two map coordinate systems.
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform the point from the source CRS to the destination CRS.
Custom exception class for Coordinate Reference System related exceptions.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
This class wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsMapCanvasAnnotationItem(QgsAnnotation *annotation, QgsMapCanvas *mapCanvas)
Constructor for QgsMapCanvasAnnotationItem.
MouseMoveAction moveActionForPosition(QPointF pos) const
Returns the mouse move behavior for a given position in scene coordinates.
Qt::CursorShape cursorShapeForAction(MouseMoveAction moveAction) const
Returns matching cursor shape for a mouse move action.
void paint(QPainter *painter) override
function to be implemented by derived classes
void updatePosition() override
called on changed extent or resize event to update position of the item
MouseMoveAction
Mouse actions for interacting with item.
@ ResizeFrameRightUp
Resize frame right up.
@ MoveFramePosition
Moving position of frame relative to annotation.
@ ResizeFrameRightDown
Resize frame right down.
@ MoveMapPosition
Moving annotation map position.
@ ResizeFrameLeftUp
Resize frame left up.
@ ResizeFrameLeftDown
Resize frame left down.
An abstract class for items that can be placed on the map canvas.
QPointF toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
QgsMapCanvas * mMapCanvas
pointer to map canvas
Map canvas is a class for displaying all GIS data types on a canvas.
void destinationCrsChanged()
Emitted when map CRS has changed.
void layersChanged()
Emitted when a new set of layers has been received.
bool annotationsVisible() const
Returns true if annotations are visible within the map canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QList< QgsMapLayer * > layers(bool expandGroupLayers=false) const
Returns the list of layers which will be rendered in the map.
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer's CRS
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
static double searchRadiusMU(const QgsRenderContext &context)
Gets search radius in map units for given context.
double size() const
Returns the estimated size for the whole symbol, which is the maximum size of all marker symbol layer...
A class to represent a 2D point.
Definition qgspointxy.h:60
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
static QgsProject * instance()
Returns the QgsProject singleton instance.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
static QgsRenderContext fromQPainter(QPainter *painter)
Creates a default render context given a pixel based QPainter destination.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.