QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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"
20 #include "qgsannotationlineitem.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"
30 #include "qgsrecentstylehandler.h"
31 
33 
34 //
35 // QgsMapToolCaptureAnnotationItem
36 //
37 
38 QgsMapToolCaptureAnnotationItem::QgsMapToolCaptureAnnotationItem( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode )
39  : QgsMapToolCapture( canvas, cadDockWidget, mode )
40 {
41  mToolName = tr( "Annotation tool" );
42 }
43 
44 QgsCreateAnnotationItemMapToolHandler *QgsMapToolCaptureAnnotationItem::handler()
45 {
46  return mHandler;
47 }
48 
49 QgsMapTool *QgsMapToolCaptureAnnotationItem::mapTool()
50 {
51  return this;
52 }
53 
54 QgsMapLayer *QgsMapToolCaptureAnnotationItem::layer() const
55 {
56  return mHandler->targetLayer();
57 }
58 
59 
60 QgsMapToolCapture::Capabilities QgsMapToolCaptureAnnotationItem::capabilities() const
61 {
62  // no geometry validation!
63  return SupportsCurves;
64 }
65 
66 bool QgsMapToolCaptureAnnotationItem::supportsTechnique( Qgis::CaptureTechnique technique ) const
67 {
68  switch ( technique )
69  {
74  return true;
75  }
77 }
78 
79 
80 //
81 // QgsCreatePointTextItemMapTool
82 //
83 
84 QgsCreatePointTextItemMapTool::QgsCreatePointTextItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
85  : QgsMapToolAdvancedDigitizing( canvas, cadDockWidget )
86  , mHandler( new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget ) )
87 {
88 
89 }
90 
91 QgsCreatePointTextItemMapTool::~QgsCreatePointTextItemMapTool() = default;
92 
93 void 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 
109 QgsCreateAnnotationItemMapToolHandler *QgsCreatePointTextItemMapTool::handler()
110 {
111  return mHandler;
112 }
113 
114 QgsMapTool *QgsCreatePointTextItemMapTool::mapTool()
115 {
116  return this;
117 }
118 
119 
120 
121 //
122 // QgsCreateMarkerMapTool
123 //
124 
125 QgsCreateMarkerItemMapTool::QgsCreateMarkerItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
126  : QgsMapToolCaptureAnnotationItem( canvas, cadDockWidget, CapturePoint )
127 {
128  mHandler = new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget, this );
129 }
130 
131 void 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 
158 QgsCreateLineItemMapTool::QgsCreateLineItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
159  : QgsMapToolCaptureAnnotationItem( canvas, cadDockWidget, CaptureLine )
160 {
161  mHandler = new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget, this );
162 }
163 
164 void 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 
188 QgsCreatePolygonItemMapTool::QgsCreatePolygonItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
189  : QgsMapToolCaptureAnnotationItem( canvas, cadDockWidget, CapturePolygon )
190 {
191  mHandler = new QgsCreateAnnotationItemMapToolHandler( canvas, cadDockWidget, this );
192 }
193 
194 void 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 
QgsCurve
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
QgsApplication::recentStyleHandler
static QgsRecentStyleHandler * recentStyleHandler()
Returns the handler for recently used style items.
Definition: qgsapplication.cpp:2435
QgsMapMouseEvent::mapPoint
QgsPointXY mapPoint() const
mapPoint returns the point in coordinates
Definition: qgsmapmouseevent.h:88
QgsSymbol::defaultSymbol
static QgsSymbol * defaultSymbol(QgsWkbTypes::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Definition: qgssymbol.cpp:673
qgsmapcanvas.h
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsCurvePolygon::exteriorRing
const QgsCurve * exteriorRing() const SIP_HOLDGIL
Returns the curve polygon's exterior ring.
Definition: qgscurvepolygon.h:96
QgsCurvePolygon
Curve polygon geometry type.
Definition: qgscurvepolygon.h:34
qgsannotationpolygonitem.h
QgsStyle::defaultTextFormatForProject
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
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:480
QgsCreateAnnotationItemMapToolHandler
A handler object for map tools which create annotation items.
Definition: qgscreateannotationitemmaptool.h:40
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
qgsannotationlineitem.h
qgsapplication.h
QgsMapTool
Abstract base class for all map tools. Map tools are user interactive tools for manipulating the map ...
Definition: qgsmaptool.h:70
qgsannotationmarkeritem.h
Qgis::CaptureTechnique::CircularString
@ CircularString
Capture in circular strings.
QgsMarkerSymbol
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgsmarkersymbol.h:30
QgsAbstractGeometry::simplifiedTypeRef
virtual const QgsAbstractGeometry * simplifiedTypeRef() const SIP_HOLDGIL
Returns a reference to the simplest lossless representation of this geometry, e.g.
Definition: qgsabstractgeometry.cpp:287
QgsAbstractGeometry::clone
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
QgsLineSymbol
A line symbol type, for rendering LineString and MultiLineString geometries.
Definition: qgslinesymbol.h:29
BUILTIN_UNREACHABLE
#define BUILTIN_UNREACHABLE
Definition: qgis.h:2907
qgsannotationlayer.h
QgsRecentStyleHandler::recentSymbol
QgsSymbol * recentSymbol(const QString &identifier) const
Returns a copy of the recently used symbol with the specified identifier, or nullptr if no symbol wit...
Definition: qgsrecentstylehandler.cpp:29
QgsAdvancedDigitizingDockWidget
The QgsAdvancedDigitizingDockWidget class is a dockable widget used to handle the CAD tools on top of...
Definition: qgsadvanceddigitizingdockwidget.h:50
Qgis::CaptureTechnique::Shape
@ Shape
Digitize shapes.
qgsstyle.h
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
qgscreateannotationitemmaptool_impl.h
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas....
Definition: qgsmapmouseevent.h:35
QgsWkbTypes::LineGeometry
@ LineGeometry
Definition: qgswkbtypes.h:143
qgsadvanceddigitizingdockwidget.h
QgsWkbTypes::PointGeometry
@ PointGeometry
Definition: qgswkbtypes.h:142
qgsannotationpointtextitem.h
qgsrecentstylehandler.h
Qgis::CaptureTechnique::StraightSegments
@ StraightSegments
Default capture mode - capture occurs with straight line segments.
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
qgsmarkersymbol.h
QgsFillSymbol
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgsfillsymbol.h:29
QgsStyle::TextFormatContext::Labeling
@ Labeling
Text format used in labeling.
QgsMapToolCapture
Definition: qgsmaptoolcapture.h:49
Qgis::CaptureTechnique
CaptureTechnique
Capture technique.
Definition: qgis.h:152
qgsmapmouseevent.h
QgsMapToolAdvancedDigitizing
The QgsMapToolAdvancedDigitizing class is a QgsMapTool which gives event directly in map coordinates ...
Definition: qgsmaptooladvanceddigitizing.h:36
Qgis::CaptureTechnique::Streaming
@ Streaming
Streaming points digitizing mode (points are automatically added as the mouse cursor moves).
qgsfillsymbol.h
qgslinesymbol.h