QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
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 // do it!
169 std::unique_ptr< QgsAbstractGeometry > geometry( line->simplifiedTypeRef()->clone() );
170 if ( qgsgeometry_cast< QgsCurve * >( geometry.get() ) )
171 {
172 std::unique_ptr< QgsAnnotationLineItem > createdItem = std::make_unique< QgsAnnotationLineItem >( qgsgeometry_cast< QgsCurve * >( geometry.release() ) );
173
174 std::unique_ptr< QgsLineSymbol > lineSymbol = QgsApplication::recentStyleHandler()->recentSymbol< QgsLineSymbol >( QStringLiteral( "line_annotation_item" ) );
175 if ( !lineSymbol )
176 lineSymbol.reset( qgis::down_cast< QgsLineSymbol * >( QgsSymbol::defaultSymbol( Qgis::GeometryType::Line ) ) );
177 createdItem->setSymbol( lineSymbol.release() );
178
179 // set reference scale to match canvas scale, but don't enable it by default for marker items
180 createdItem->setSymbologyReferenceScale( canvas()->scale() );
181
182 mHandler->pushCreatedItem( createdItem.release() );
183 }
184}
185
186//
187// QgsCreatePolygonItemMapTool
188//
189
190QgsCreatePolygonItemMapTool::QgsCreatePolygonItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
191 : QgsMapToolCaptureAnnotationItem( canvas, cadDockWidget, CapturePolygon )
192{
193 mHandler = new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget, this );
194}
195
196void QgsCreatePolygonItemMapTool::polygonCaptured( const QgsCurvePolygon *polygon )
197{
198 std::unique_ptr< QgsAbstractGeometry > geometry( polygon->exteriorRing()->simplifiedTypeRef()->clone() );
199 if ( qgsgeometry_cast< QgsCurve * >( geometry.get() ) )
200 {
201 std::unique_ptr< QgsCurvePolygon > newPolygon = std::make_unique< QgsCurvePolygon >();
202 newPolygon->setExteriorRing( qgsgeometry_cast< QgsCurve * >( geometry.release() ) );
203 std::unique_ptr< QgsAnnotationPolygonItem > createdItem = std::make_unique< QgsAnnotationPolygonItem >( newPolygon.release() );
204
205 std::unique_ptr< QgsFillSymbol > fillSymbol = QgsApplication::recentStyleHandler()->recentSymbol< QgsFillSymbol >( QStringLiteral( "polygon_annotation_item" ) );
206 if ( !fillSymbol )
207 fillSymbol.reset( qgis::down_cast< QgsFillSymbol * >( QgsSymbol::defaultSymbol( Qgis::GeometryType::Polygon ) ) );
208 createdItem->setSymbol( fillSymbol.release() );
209
210 // set reference scale to match canvas scale, but don't enable it by default for marker items
211 createdItem->setSymbologyReferenceScale( canvas()->scale() );
212
213 mHandler->pushCreatedItem( createdItem.release() );
214 }
215}
216
217//
218// QgsCreateLineTextItemMapTool
219//
220
221QgsCreateLineTextItemMapTool::QgsCreateLineTextItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
222 : QgsMapToolCaptureAnnotationItem( canvas, cadDockWidget, CaptureLine )
223{
224 mHandler = new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget, this );
225}
226
227void QgsCreateLineTextItemMapTool::lineCaptured( const QgsCurve *line )
228{
229 // do it!
230 std::unique_ptr< QgsAbstractGeometry > geometry( line->simplifiedTypeRef()->clone() );
231 if ( qgsgeometry_cast< QgsCurve * >( geometry.get() ) )
232 {
233 std::unique_ptr< QgsAnnotationLineTextItem > createdItem = std::make_unique< QgsAnnotationLineTextItem >( tr( "Text" ), qgsgeometry_cast< QgsCurve * >( geometry.release() ) );
234
235 std::unique_ptr< QgsLineSymbol > lineSymbol = QgsApplication::recentStyleHandler()->recentSymbol< QgsLineSymbol >( QStringLiteral( "line_annotation_item" ) );
236 if ( !lineSymbol )
237 lineSymbol.reset( qgis::down_cast< QgsLineSymbol * >( QgsSymbol::defaultSymbol( Qgis::GeometryType::Line ) ) );
238
240
241 // newly created point text items default to using symbology reference scale at the current map scale
242 createdItem->setUseSymbologyReferenceScale( true );
243 createdItem->setSymbologyReferenceScale( canvas()->scale() );
244 mHandler->pushCreatedItem( createdItem.release() );
245 }
246}
247
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 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.
Abstract base class for curved geometry type.
Definition qgscurve.h:36
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
A line symbol type, for rendering LineString and MultiLineString geometries.
Map canvas is a class for displaying all GIS data types on a canvas.
Base class for all map layer types.
Definition qgsmaplayer.h:74
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.
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,...
@ Labeling
Text format used in labeling.
static QgsSymbol * defaultSymbol(Qgis::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
#define BUILTIN_UNREACHABLE
Definition qgis.h:4993