19#include "moc_qgsmapcanvasannotationitem.cpp"
37 , mAnnotation( annotation )
39 setFlag( QGraphicsItem::ItemIsSelectable,
true );
45 connect( mAnnotation, &
QgsAnnotation::moved,
this, &QgsMapCanvasAnnotationItem::setFeatureForMapPosition );
54 connect( mAnnotation, &QgsAnnotation::destroyed,
this, &QgsMapCanvasAnnotationItem::annotationDeleted );
57 setFeatureForMapPosition();
93void QgsMapCanvasAnnotationItem::updateBoundingRect()
95 prepareGeometryChange();
98 const double fillSymbolBleed = mAnnotation && mAnnotation->
fillSymbol() ?
101 const double mmToPixelScale =
mMapCanvas->physicalDpiX() / 25.4;
105 mBoundingRect = QRectF( - fillSymbolBleed, -fillSymbolBleed,
106 mmToPixelScale * mAnnotation->
frameSizeMm().width() + fillSymbolBleed * 2,
107 mmToPixelScale * mAnnotation->
frameSizeMm().height() + fillSymbolBleed * 2 );
111 double halfSymbolSize = 0.0;
114 halfSymbolSize = scaledSymbolSize() / 2.0;
120 const QSizeF frameSize = mAnnotation ? QSizeF( mAnnotation->
frameSizeMm().width() * mmToPixelScale,
121 mAnnotation->
frameSizeMm().height() * mmToPixelScale ) : QSizeF( 0.0, 0.0 );
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 );
131void QgsMapCanvasAnnotationItem::onCanvasLayersChanged()
139 else if ( !mAnnotation->
mapLayer() )
149void QgsMapCanvasAnnotationItem::setFeatureForMapPosition()
165 mapPosition = ct.transform( mapPosition );
171 QgsRectangle searchRect( mapPosition.
x() - halfIdentifyWidth, mapPosition.
y() - halfIdentifyWidth,
172 mapPosition.
x() + halfIdentifyWidth, mapPosition.
y() + halfIdentifyWidth );
183void QgsMapCanvasAnnotationItem::annotationDeleted()
185 mAnnotation =
nullptr;
189void QgsMapCanvasAnnotationItem::drawSelectionBoxes( QPainter *p )
const
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 ) );
207 const QPointF itemPos = mapFromScene( pos );
209 const int cursorSensitivity = 7;
212 std::fabs( itemPos.x() ) < cursorSensitivity && std::fabs( itemPos.y() ) < cursorSensitivity )
217 const double mmToPixelScale =
mMapCanvas->logicalDpiX() / 25.4;
220 const QSizeF frameSize = mAnnotation ? mAnnotation->
frameSizeMm() * mmToPixelScale : QSizeF( 0, 0 );
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;
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() ) );
241 else if ( right && up )
245 else if ( left && down )
249 else if ( right && down )
253 if ( left && inframe )
257 if ( right && inframe )
265 if ( down && inframe )
281 switch ( moveAction )
284 return Qt::ArrowCursor;
287 return Qt::SizeAllCursor;
290 return Qt::SizeVerCursor;
293 return Qt::SizeHorCursor;
296 return Qt::SizeFDiagCursor;
299 return Qt::SizeBDiagCursor;
301 return Qt::ArrowCursor;
305double QgsMapCanvasAnnotationItem::scaledSymbolSize()
const
317 const double dpmm =
mMapCanvas->logicalDpiX() / 25.4;
323 if ( !mAnnotation || !mAnnotation->
isVisible() )
330 mAnnotation->
render( rc );
334 drawSelectionBoxes( painter );
@ 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.
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.
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.
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...
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
QRectF boundingRect() const override
void updatePosition() override
called on changed extent or resize event to update position of the item
MouseMoveAction
Mouse actions for interacting with item.
@ ResizeFrameRight
Resize frame right.
@ ResizeFrameRightUp
Resize frame right up.
@ MoveFramePosition
Moving position of frame relative to annotation.
@ ResizeFrameUp
Resize frame up.
@ ResizeFrameRightDown
Resize frame right down.
@ MoveMapPosition
Moving annotation map position.
@ ResizeFrameDown
Resize frame down.
@ ResizeFrameLeftUp
Resize frame left up.
@ ResizeFrameLeft
Resize frame left.
@ 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.
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.
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.