QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgscreateannotationitemmaptool_impl.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscreateannotationitemmaptool_impl.cpp
3 ------------------------
4 Date : September 2021
5 Copyright : (C) 2021 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "qgsmapmouseevent.h"
22#include "qgsannotationlayer.h"
23#include "qgsstyle.h"
24#include "qgsmapcanvas.h"
25#include "qgsmarkersymbol.h"
26#include "qgslinesymbol.h"
27#include "qgsfillsymbol.h"
29#include "qgsapplication.h"
31
33
34//
35// QgsMapToolCaptureAnnotationItem
36//
37
38QgsMapToolCaptureAnnotationItem::QgsMapToolCaptureAnnotationItem( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode )
39 : QgsMapToolCapture( canvas, cadDockWidget, mode )
40{
41 mToolName = tr( "Annotation tool" );
42}
43
44QgsCreateAnnotationItemMapToolHandler *QgsMapToolCaptureAnnotationItem::handler()
45{
46 return mHandler;
47}
48
49QgsMapTool *QgsMapToolCaptureAnnotationItem::mapTool()
50{
51 return this;
52}
53
54QgsMapLayer *QgsMapToolCaptureAnnotationItem::layer() const
55{
56 return mHandler->targetLayer();
57}
58
59
60QgsMapToolCapture::Capabilities QgsMapToolCaptureAnnotationItem::capabilities() const
61{
62 // no geometry validation!
63 return SupportsCurves;
64}
65
66bool QgsMapToolCaptureAnnotationItem::supportsTechnique( Qgis::CaptureTechnique technique ) const
67{
68 switch ( technique )
69 {
74 return true;
75 }
77}
78
79
80//
81// QgsCreatePointTextItemMapTool
82//
83
84QgsCreatePointTextItemMapTool::QgsCreatePointTextItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
85 : QgsMapToolAdvancedDigitizing( canvas, cadDockWidget )
86 , mHandler( new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget ) )
87{
88
89}
90
91QgsCreatePointTextItemMapTool::~QgsCreatePointTextItemMapTool() = default;
92
93void QgsCreatePointTextItemMapTool::cadCanvasPressEvent( QgsMapMouseEvent *event )
94{
95 if ( event->button() != Qt::LeftButton )
96 return;
97
98 const QgsPointXY layerPoint = toLayerCoordinates( mHandler->targetLayer(), event->mapPoint() );
99
100 std::unique_ptr< QgsAnnotationPointTextItem > createdItem = std::make_unique< QgsAnnotationPointTextItem >( tr( "Text" ), layerPoint );
101 createdItem->setAlignment( Qt::AlignLeft );
103 // newly created point text items default to using symbology reference scale at the current map scale
104 createdItem->setUseSymbologyReferenceScale( true );
105 createdItem->setSymbologyReferenceScale( canvas()->scale() );
106 mHandler->pushCreatedItem( createdItem.release() );
107}
108
109QgsCreateAnnotationItemMapToolHandler *QgsCreatePointTextItemMapTool::handler()
110{
111 return mHandler;
112}
113
114QgsMapTool *QgsCreatePointTextItemMapTool::mapTool()
115{
116 return this;
117}
118
119
120
121//
122// QgsCreateMarkerMapTool
123//
124
125QgsCreateMarkerItemMapTool::QgsCreateMarkerItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
126 : QgsMapToolCaptureAnnotationItem( canvas, cadDockWidget, CapturePoint )
127{
128 mHandler = new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget, this );
129}
130
131void QgsCreateMarkerItemMapTool::cadCanvasReleaseEvent( QgsMapMouseEvent *event )
132{
133 if ( event->button() != Qt::LeftButton )
134 return;
135
136 const QgsPointXY layerPoint = toLayerCoordinates( mHandler->targetLayer(), event->mapPoint() );
137 std::unique_ptr< QgsAnnotationMarkerItem > createdItem = std::make_unique< QgsAnnotationMarkerItem >( QgsPoint( layerPoint ) );
138
139 std::unique_ptr< QgsMarkerSymbol > markerSymbol = QgsApplication::recentStyleHandler()->recentSymbol< QgsMarkerSymbol >( QStringLiteral( "marker_annotation_item" ) );
140 if ( !markerSymbol )
141 markerSymbol.reset( qgis::down_cast< QgsMarkerSymbol * >( QgsSymbol::defaultSymbol( QgsWkbTypes::PointGeometry ) ) );
142 createdItem->setSymbol( markerSymbol.release() );
143
144 // set reference scale to match canvas scale, but don't enable it by default for marker items
145 createdItem->setSymbologyReferenceScale( canvas()->scale() );
146
147 mHandler->pushCreatedItem( createdItem.release() );
148
149 stopCapturing();
150
151 cadDockWidget()->clearPoints();
152}
153
154//
155// QgsCreateLineMapTool
156//
157
158QgsCreateLineItemMapTool::QgsCreateLineItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
159 : QgsMapToolCaptureAnnotationItem( canvas, cadDockWidget, CaptureLine )
160{
161 mHandler = new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget, this );
162}
163
164void QgsCreateLineItemMapTool::lineCaptured( const QgsCurve *line )
165{
166 // do it!
167 std::unique_ptr< QgsAbstractGeometry > geometry( line->simplifiedTypeRef()->clone() );
168 if ( qgsgeometry_cast< QgsCurve * >( geometry.get() ) )
169 {
170 std::unique_ptr< QgsAnnotationLineItem > createdItem = std::make_unique< QgsAnnotationLineItem >( qgsgeometry_cast< QgsCurve * >( geometry.release() ) );
171
172 std::unique_ptr< QgsLineSymbol > lineSymbol = QgsApplication::recentStyleHandler()->recentSymbol< QgsLineSymbol >( QStringLiteral( "line_annotation_item" ) );
173 if ( !lineSymbol )
174 lineSymbol.reset( qgis::down_cast< QgsLineSymbol * >( QgsSymbol::defaultSymbol( QgsWkbTypes::LineGeometry ) ) );
175 createdItem->setSymbol( lineSymbol.release() );
176
177 // set reference scale to match canvas scale, but don't enable it by default for marker items
178 createdItem->setSymbologyReferenceScale( canvas()->scale() );
179
180 mHandler->pushCreatedItem( createdItem.release() );
181 }
182}
183
184//
185// QgsCreatePolygonItemMapTool
186//
187
188QgsCreatePolygonItemMapTool::QgsCreatePolygonItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
189 : QgsMapToolCaptureAnnotationItem( canvas, cadDockWidget, CapturePolygon )
190{
191 mHandler = new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget, this );
192}
193
194void QgsCreatePolygonItemMapTool::polygonCaptured( const QgsCurvePolygon *polygon )
195{
196 std::unique_ptr< QgsAbstractGeometry > geometry( polygon->exteriorRing()->simplifiedTypeRef()->clone() );
197 if ( qgsgeometry_cast< QgsCurve * >( geometry.get() ) )
198 {
199 std::unique_ptr< QgsCurvePolygon > newPolygon = std::make_unique< QgsCurvePolygon >();
200 newPolygon->setExteriorRing( qgsgeometry_cast< QgsCurve * >( geometry.release() ) );
201 std::unique_ptr< QgsAnnotationPolygonItem > createdItem = std::make_unique< QgsAnnotationPolygonItem >( newPolygon.release() );
202
203 std::unique_ptr< QgsFillSymbol > fillSymbol = QgsApplication::recentStyleHandler()->recentSymbol< QgsFillSymbol >( QStringLiteral( "polygon_annotation_item" ) );
204 if ( !fillSymbol )
205 fillSymbol.reset( qgis::down_cast< QgsFillSymbol * >( QgsSymbol::defaultSymbol( QgsWkbTypes::PolygonGeometry ) ) );
206 createdItem->setSymbol( fillSymbol.release() );
207
208 // set reference scale to match canvas scale, but don't enable it by default for marker items
209 createdItem->setSymbologyReferenceScale( canvas()->scale() );
210
211 mHandler->pushCreatedItem( createdItem.release() );
212 }
213}
214
216
CaptureTechnique
Capture technique.
Definition: qgis.h:153
@ Shape
Digitize shapes.
@ StraightSegments
Default capture mode - capture occurs with straight line segments.
@ CircularString
Capture in circular strings.
@ Streaming
Streaming points digitizing mode (points are automatically added as the mouse cursor moves).
virtual const QgsAbstractGeometry * simplifiedTypeRef() const SIP_HOLDGIL
Returns a reference to the simplest lossless representation of this geometry, e.g.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
The QgsAdvancedDigitizingDockWidget class is a dockable widget used to handle the CAD tools on top of...
static QgsRecentStyleHandler * recentStyleHandler()
Returns the handler for recently used style items.
A handler object for map tools which create annotation items.
Curve polygon geometry type.
const QgsCurve * exteriorRing() const SIP_HOLDGIL
Returns the curve polygon's exterior ring.
Abstract base class for curved geometry type.
Definition: qgscurve.h:36
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgsfillsymbol.h:30
A line symbol type, for rendering LineString and MultiLineString geometries.
Definition: qgslinesymbol.h:30
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:90
Base class for all map layer types.
Definition: qgsmaplayer.h:73
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
QgsPointXY mapPoint() const
mapPoint returns the point in coordinates
The QgsMapToolAdvancedDigitizing class is a QgsMapTool which gives event directly in map coordinates ...
QgsMapToolCapture is a base class capable of capturing point, lines and polygons.
Abstract base class for all map tools.
Definition: qgsmaptool.h:71
A marker symbol type, for rendering Point and MultiPoint geometries.
A class to represent a 2D point.
Definition: qgspointxy.h:59
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:477
QgsSymbol * recentSymbol(const QString &identifier) const
Returns a copy of the recently used symbol with the specified identifier, or nullptr if no symbol wit...
static QgsTextFormat defaultTextFormatForProject(QgsProject *project, QgsStyle::TextFormatContext context=QgsStyle::TextFormatContext::Labeling)
Returns the default text format to use for new text based objects for the specified project,...
Definition: qgsstyle.cpp:1217
@ Labeling
Text format used in labeling.
static QgsSymbol * defaultSymbol(QgsWkbTypes::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Definition: qgssymbol.cpp:704
#define BUILTIN_UNREACHABLE
Definition: qgis.h:3148