QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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"
23#include "qgsannotationlayer.h"
24#include "qgsstyle.h"
25#include "qgsmapcanvas.h"
26#include "qgsmarkersymbol.h"
27#include "qgslinesymbol.h"
28#include "qgsfillsymbol.h"
30#include "qgsapplication.h"
32#include "qgscurvepolygon.h"
33
35
36//
37// QgsMapToolCaptureAnnotationItem
38//
39
40QgsMapToolCaptureAnnotationItem::QgsMapToolCaptureAnnotationItem( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode )
41 : QgsMapToolCapture( canvas, cadDockWidget, mode )
42{
43 mToolName = tr( "Annotation tool" );
44}
45
46QgsCreateAnnotationItemMapToolHandler *QgsMapToolCaptureAnnotationItem::handler()
47{
48 return mHandler;
49}
50
51QgsMapTool *QgsMapToolCaptureAnnotationItem::mapTool()
52{
53 return this;
54}
55
56QgsMapLayer *QgsMapToolCaptureAnnotationItem::layer() const
57{
58 return mHandler->targetLayer();
59}
60
61
62QgsMapToolCapture::Capabilities QgsMapToolCaptureAnnotationItem::capabilities() const
63{
64 // no geometry validation!
65 return SupportsCurves;
66}
67
68bool QgsMapToolCaptureAnnotationItem::supportsTechnique( Qgis::CaptureTechnique technique ) const
69{
70 switch ( technique )
71 {
76 return true;
77 }
79}
80
81
82//
83// QgsCreatePointTextItemMapTool
84//
85
86QgsCreatePointTextItemMapTool::QgsCreatePointTextItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
87 : QgsMapToolAdvancedDigitizing( canvas, cadDockWidget )
88 , mHandler( new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget ) )
89{
90
91}
92
93QgsCreatePointTextItemMapTool::~QgsCreatePointTextItemMapTool() = default;
94
95void QgsCreatePointTextItemMapTool::cadCanvasPressEvent( QgsMapMouseEvent *event )
96{
97 if ( event->button() != Qt::LeftButton )
98 return;
99
100 const QgsPointXY layerPoint = toLayerCoordinates( mHandler->targetLayer(), event->mapPoint() );
101
102 std::unique_ptr< QgsAnnotationPointTextItem > createdItem = std::make_unique< QgsAnnotationPointTextItem >( tr( "Text" ), layerPoint );
103 createdItem->setAlignment( Qt::AlignLeft );
105 // newly created point text items default to using symbology reference scale at the current map scale
106 createdItem->setUseSymbologyReferenceScale( true );
107 createdItem->setSymbologyReferenceScale( canvas()->scale() );
108 mHandler->pushCreatedItem( createdItem.release() );
109}
110
111QgsCreateAnnotationItemMapToolHandler *QgsCreatePointTextItemMapTool::handler()
112{
113 return mHandler;
114}
115
116QgsMapTool *QgsCreatePointTextItemMapTool::mapTool()
117{
118 return this;
119}
120
121
122
123//
124// QgsCreateMarkerMapTool
125//
126
127QgsCreateMarkerItemMapTool::QgsCreateMarkerItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
128 : QgsMapToolCaptureAnnotationItem( canvas, cadDockWidget, CapturePoint )
129{
130 mHandler = new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget, this );
131}
132
133void QgsCreateMarkerItemMapTool::cadCanvasReleaseEvent( QgsMapMouseEvent *event )
134{
135 if ( event->button() != Qt::LeftButton )
136 return;
137
138 const QgsPointXY layerPoint = toLayerCoordinates( mHandler->targetLayer(), event->mapPoint() );
139 std::unique_ptr< QgsAnnotationMarkerItem > createdItem = std::make_unique< QgsAnnotationMarkerItem >( QgsPoint( layerPoint ) );
140
141 std::unique_ptr< QgsMarkerSymbol > markerSymbol = QgsApplication::recentStyleHandler()->recentSymbol< QgsMarkerSymbol >( QStringLiteral( "marker_annotation_item" ) );
142 if ( !markerSymbol )
143 markerSymbol.reset( qgis::down_cast< QgsMarkerSymbol * >( QgsSymbol::defaultSymbol( Qgis::GeometryType::Point ) ) );
144 createdItem->setSymbol( markerSymbol.release() );
145
146 // set reference scale to match canvas scale, but don't enable it by default for marker items
147 createdItem->setSymbologyReferenceScale( canvas()->scale() );
148
149 mHandler->pushCreatedItem( createdItem.release() );
150
151 stopCapturing();
152
153 cadDockWidget()->clearPoints();
154}
155
156//
157// QgsCreateLineMapTool
158//
159
160QgsCreateLineItemMapTool::QgsCreateLineItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
161 : QgsMapToolCaptureAnnotationItem( canvas, cadDockWidget, CaptureLine )
162{
163 mHandler = new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget, this );
164}
165
166void QgsCreateLineItemMapTool::lineCaptured( const QgsCurve *line )
167{
168 if ( line->isEmpty() )
169 return;
170
171 // do it!
172 std::unique_ptr< QgsAbstractGeometry > geometry( line->simplifiedTypeRef()->clone() );
173 if ( qgsgeometry_cast< QgsCurve * >( geometry.get() ) )
174 {
175 std::unique_ptr< QgsAnnotationLineItem > createdItem = std::make_unique< QgsAnnotationLineItem >( qgsgeometry_cast< QgsCurve * >( geometry.release() ) );
176
177 std::unique_ptr< QgsLineSymbol > lineSymbol = QgsApplication::recentStyleHandler()->recentSymbol< QgsLineSymbol >( QStringLiteral( "line_annotation_item" ) );
178 if ( !lineSymbol )
179 lineSymbol.reset( qgis::down_cast< QgsLineSymbol * >( QgsSymbol::defaultSymbol( Qgis::GeometryType::Line ) ) );
180 createdItem->setSymbol( lineSymbol.release() );
181
182 // set reference scale to match canvas scale, but don't enable it by default for marker items
183 createdItem->setSymbologyReferenceScale( canvas()->scale() );
184
185 mHandler->pushCreatedItem( createdItem.release() );
186 }
187}
188
189//
190// QgsCreatePolygonItemMapTool
191//
192
193QgsCreatePolygonItemMapTool::QgsCreatePolygonItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
194 : QgsMapToolCaptureAnnotationItem( canvas, cadDockWidget, CapturePolygon )
195{
196 mHandler = new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget, this );
197}
198
199void QgsCreatePolygonItemMapTool::polygonCaptured( const QgsCurvePolygon *polygon )
200{
201 if ( polygon->isEmpty() )
202 return;
203
204 std::unique_ptr< QgsAbstractGeometry > geometry( polygon->exteriorRing()->simplifiedTypeRef()->clone() );
205 if ( qgsgeometry_cast< QgsCurve * >( geometry.get() ) )
206 {
207 std::unique_ptr< QgsCurvePolygon > newPolygon = std::make_unique< QgsCurvePolygon >();
208 newPolygon->setExteriorRing( qgsgeometry_cast< QgsCurve * >( geometry.release() ) );
209 std::unique_ptr< QgsAnnotationPolygonItem > createdItem = std::make_unique< QgsAnnotationPolygonItem >( newPolygon.release() );
210
211 std::unique_ptr< QgsFillSymbol > fillSymbol = QgsApplication::recentStyleHandler()->recentSymbol< QgsFillSymbol >( QStringLiteral( "polygon_annotation_item" ) );
212 if ( !fillSymbol )
213 fillSymbol.reset( qgis::down_cast< QgsFillSymbol * >( QgsSymbol::defaultSymbol( Qgis::GeometryType::Polygon ) ) );
214 createdItem->setSymbol( fillSymbol.release() );
215
216 // set reference scale to match canvas scale, but don't enable it by default for marker items
217 createdItem->setSymbologyReferenceScale( canvas()->scale() );
218
219 mHandler->pushCreatedItem( createdItem.release() );
220 }
221}
222
223//
224// QgsCreateLineTextItemMapTool
225//
226
227QgsCreateLineTextItemMapTool::QgsCreateLineTextItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
228 : QgsMapToolCaptureAnnotationItem( canvas, cadDockWidget, CaptureLine )
229{
230 mHandler = new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget, this );
231}
232
233void QgsCreateLineTextItemMapTool::lineCaptured( const QgsCurve *line )
234{
235 if ( line->isEmpty() )
236 return;
237
238 // do it!
239 std::unique_ptr< QgsAbstractGeometry > geometry( line->simplifiedTypeRef()->clone() );
240 if ( qgsgeometry_cast< QgsCurve * >( geometry.get() ) )
241 {
242 std::unique_ptr< QgsAnnotationLineTextItem > createdItem = std::make_unique< QgsAnnotationLineTextItem >( tr( "Text" ), qgsgeometry_cast< QgsCurve * >( geometry.release() ) );
243
244 std::unique_ptr< QgsLineSymbol > lineSymbol = QgsApplication::recentStyleHandler()->recentSymbol< QgsLineSymbol >( QStringLiteral( "line_annotation_item" ) );
245 if ( !lineSymbol )
246 lineSymbol.reset( qgis::down_cast< QgsLineSymbol * >( QgsSymbol::defaultSymbol( Qgis::GeometryType::Line ) ) );
247
249
250 // newly created point text items default to using symbology reference scale at the current map scale
251 createdItem->setUseSymbologyReferenceScale( true );
252 createdItem->setSymbologyReferenceScale( canvas()->scale() );
253 mHandler->pushCreatedItem( createdItem.release() );
254 }
255}
256
CaptureTechnique
Capture technique.
Definition: qgis.h:294
@ 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).
@ Polygon
Polygons.
virtual const QgsAbstractGeometry * simplifiedTypeRef() const
Returns a reference to the simplest lossless representation of this geometry, e.g.
virtual bool isEmpty() const
Returns true if the geometry is empty.
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
Returns the curve polygon's exterior ring.
bool isEmpty() const override
Returns true if the geometry is empty.
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
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:93
Base class for all map layer types.
Definition: qgsmaplayer.h:75
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.
QFlags< Capability > Capabilities
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:60
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:481
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:1249
@ Labeling
Text format used in labeling.
static QgsSymbol * defaultSymbol(Qgis::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Definition: qgssymbol.cpp:705
#define BUILTIN_UNREACHABLE
Definition: qgis.h:5853