QGIS API Documentation 3.99.0-Master (a8882ad4560)
Loading...
Searching...
No Matches
qgsannotationmaptool.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaptoolannotation.cpp
3 ----------------
4 copyright : (C) 2025 by Mathieu Pellerin
5 email : mathieu at opengis dot ch
6 ***************************************************************************/
7
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
19#include "qgsannotationitem.h"
20#include "qgsannotationlayer.h"
21#include "qgsmapcanvas.h"
23#include "qgsproject.h"
27
28#include "moc_qgsannotationmaptool.cpp"
29
34
35const QgsRenderedAnnotationItemDetails *QgsAnnotationMapTool::findClosestItemToPoint( const QgsPointXY &mapPoint, const QList<const QgsRenderedAnnotationItemDetails *> &items, QgsRectangle &bounds )
36{
37 const QgsRenderedAnnotationItemDetails *closestItem = nullptr;
38 double closestItemDistance = std::numeric_limits<double>::max();
39 double closestItemArea = std::numeric_limits<double>::max();
40
41 for ( const QgsRenderedAnnotationItemDetails *item : items )
42 {
43 const QgsAnnotationItem *annotationItem = annotationItemFromId( item->layerId(), item->itemId() );
44 if ( !annotationItem )
45 continue;
46
47 const QgsRectangle itemBounds = item->boundingBox();
48 const double itemDistance = itemBounds.contains( mapPoint ) ? 0 : itemBounds.distance( mapPoint );
49 if ( !closestItem || itemDistance < closestItemDistance || ( itemDistance == closestItemDistance && itemBounds.area() < closestItemArea ) )
50 {
51 closestItem = item;
52 closestItemDistance = itemDistance;
53 closestItemArea = itemBounds.area();
54 bounds = itemBounds;
55 }
56 }
57 return closestItem;
58}
59
61{
62 QgsAnnotationLayer *layer = qobject_cast<QgsAnnotationLayer *>( QgsProject::instance()->mapLayer( layerId ) );
63 if ( !layer && layerId == QgsProject::instance()->mainAnnotationLayer()->id() )
64 {
66 }
67 return layer;
68}
69
70QgsAnnotationItem *QgsAnnotationMapTool::annotationItemFromId( const QString &layerId, const QString &itemId )
71{
73 return layer ? layer->item( itemId ) : nullptr;
74}
A dockable widget used to handle the CAD tools on top of a selection of map tools.
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
Represents a map layer containing a set of georeferenced annotations, e.g.
QgsAnnotationLayer * annotationLayerFromId(const QString &layerId)
Returns the annotation layer matching a given ID.
QgsAnnotationMapTool(QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget)
Constructor for QgsAnnotationMapTool.
const QgsRenderedAnnotationItemDetails * findClosestItemToPoint(const QgsPointXY &mapPoint, const QList< const QgsRenderedAnnotationItemDetails * > &items, QgsRectangle &bounds)
Returns the closest item from a list of annotation items to a given map point.
QgsAnnotationItem * annotationItemFromId(const QString &layerId, const QString &itemId)
Returns the annotation item matching a given pair of layer and item IDs.
virtual QgsMapLayer * layer() const
Returns the layer associated with the map tool.
QgsAdvancedDigitizingDockWidget * cadDockWidget() const
QgsMapToolAdvancedDigitizing(QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget)
Creates an advanced digitizing maptool.
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
friend class QgsMapCanvas
Definition qgsmaptool.h:380
Represents a 2D point.
Definition qgspointxy.h:60
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsAnnotationLayer * mainAnnotationLayer()
Returns the main annotation layer associated with the project.
A rectangle specified with double values.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
double distance(const QgsPointXY &point) const
Returns the distance from point to the nearest point on the boundary of the rectangle.
Contains information about a rendered annotation item.