QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsvertexmarker.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvertexmarker.cpp - canvas item which shows a simple vertex marker
3  ---------------------
4  begin : February 2006
5  copyright : (C) 2006 by Martin Dobias
6  email : wonder.sk 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 
16 #include <QPainter>
17 
18 #include "qgsvertexmarker.h"
19 
20 
22  : QgsMapCanvasItem( mapCanvas )
23 {
24  updatePath();
25 }
26 
28 {
29  mIconType = type;
30  updatePath();
31 }
32 
34 {
35  mIconSize = iconSize;
36  updatePath();
37 }
38 
40 {
41  mCenter = point;
42  const QPointF pt = toCanvasCoordinates( mCenter );
43  setPos( pt );
44 }
45 
46 void QgsVertexMarker::setColor( const QColor &color )
47 {
48  mColor = color;
49  update();
50 }
51 
52 void QgsVertexMarker::setFillColor( const QColor &color )
53 {
54  mFillColor = color;
55  update();
56 }
57 
59 {
60  mPenWidth = width;
61 }
62 
63 void QgsVertexMarker::paint( QPainter *p )
64 {
65  QPen pen( mColor );
66  pen.setWidth( mPenWidth );
67  p->setPen( pen );
68  const QBrush brush( mFillColor );
69  p->setBrush( brush );
70  p->drawPath( mPath );
71 }
72 
73 void QgsVertexMarker::updatePath()
74 {
75  mPath = QPainterPath();
76 
77  const qreal s = ( mIconSize - 1 ) / 2.0;
78 
79  switch ( mIconType )
80  {
81  case ICON_NONE:
82  break;
83 
84  case ICON_CROSS:
85  mPath.moveTo( QPointF( -s, 0 ) );
86  mPath.lineTo( QPointF( s, 0 ) );
87  mPath.moveTo( QPointF( 0, -s ) );
88  mPath.lineTo( QPointF( 0, s ) );
89  break;
90 
91  case ICON_X:
92  mPath.moveTo( QPointF( -s, -s ) );
93  mPath.lineTo( QPointF( s, s ) );
94  mPath.moveTo( QPointF( -s, s ) );
95  mPath.lineTo( QPointF( s, -s ) );
96  break;
97 
98  case ICON_BOX:
99  mPath.addRect( QRectF( -s, -s, s * 2, s * 2 ) );
100  break;
101 
102  case ICON_CIRCLE:
103  mPath.addEllipse( QPointF( 0, 0 ), s, s );
104  break;
105 
107  mPath.moveTo( QPointF( -s, -s ) );
108  mPath.lineTo( QPointF( s, -s ) );
109  mPath.lineTo( QPointF( -s, s ) );
110  mPath.lineTo( QPointF( s, s ) );
111  mPath.lineTo( QPointF( -s, -s ) );
112  break;
113 
114  case ICON_TRIANGLE:
115  mPath.moveTo( QPointF( -s, s ) );
116  mPath.lineTo( QPointF( s, s ) );
117  mPath.lineTo( QPointF( 0, -s ) );
118  mPath.lineTo( QPointF( -s, s ) );
119  break;
120 
121  case ICON_RHOMBUS:
122  mPath.moveTo( QPointF( 0, -s ) );
123  mPath.lineTo( QPointF( -s, 0 ) );
124  mPath.lineTo( QPointF( 0, s ) );
125  mPath.lineTo( QPointF( s, 0 ) );
126  mPath.lineTo( QPointF( 0, -s ) );
127  break;
128 
130  mPath.moveTo( QPointF( -s, -s ) );
131  mPath.lineTo( QPointF( s, -s ) );
132  mPath.lineTo( QPointF( 0, s ) );
133  mPath.lineTo( QPointF( -s, -s ) );
134  break;
135  }
136 }
137 
139 {
140  const qreal s = qreal( mIconSize + mPenWidth ) / 2.0;
141  return QRectF( -s, -s, 2.0 * s, 2.0 * s );
142 }
143 
145 {
146  setCenter( mCenter );
147 }
QgsVertexMarker::setIconType
void setIconType(int iconType)
Definition: qgsvertexmarker.cpp:27
QgsVertexMarker::ICON_INVERTED_TRIANGLE
@ ICON_INVERTED_TRIANGLE
Added in QGIS 3.20.
Definition: qgsvertexmarker.h:60
QgsVertexMarker::ICON_TRIANGLE
@ ICON_TRIANGLE
Added in QGIS 3.12.
Definition: qgsvertexmarker.h:58
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsVertexMarker::ICON_CIRCLE
@ ICON_CIRCLE
Definition: qgsvertexmarker.h:56
QgsVertexMarker::QgsVertexMarker
QgsVertexMarker(QgsMapCanvas *mapCanvas)
Definition: qgsvertexmarker.cpp:21
QgsVertexMarker::ICON_NONE
@ ICON_NONE
Definition: qgsvertexmarker.h:52
QgsVertexMarker::setIconSize
void setIconSize(int iconSize)
Definition: qgsvertexmarker.cpp:33
QgsVertexMarker::ICON_CROSS
@ ICON_CROSS
Definition: qgsvertexmarker.h:53
QgsVertexMarker::ICON_DOUBLE_TRIANGLE
@ ICON_DOUBLE_TRIANGLE
Added in QGIS 3.0.
Definition: qgsvertexmarker.h:57
QgsGuiUtils::iconSize
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
Definition: qgsguiutils.cpp:264
QgsVertexMarker::color
QColor color() const
Returns the stroke color for the marker.
Definition: qgsvertexmarker.h:97
QgsVertexMarker::ICON_RHOMBUS
@ ICON_RHOMBUS
Added in QGIS 3.12.
Definition: qgsvertexmarker.h:59
QgsVertexMarker::boundingRect
QRectF boundingRect() const override
Definition: qgsvertexmarker.cpp:138
QgsMapCanvasItem
An abstract class for items that can be placed on the map canvas.
Definition: qgsmapcanvasitem.h:33
QgsVertexMarker::setFillColor
void setFillColor(const QColor &color)
Sets the fill color for the marker.
Definition: qgsvertexmarker.cpp:52
QgsMapCanvasItem::toCanvasCoordinates
QPointF toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
Definition: qgsmapcanvasitem.cpp:61
QgsVertexMarker::updatePosition
void updatePosition() override
called on changed extent or resize event to update position of the item
Definition: qgsvertexmarker.cpp:144
QgsVertexMarker::ICON_BOX
@ ICON_BOX
Definition: qgsvertexmarker.h:55
qgsvertexmarker.h
QgsVertexMarker::setPenWidth
void setPenWidth(int width)
Definition: qgsvertexmarker.cpp:58
QgsVertexMarker::setCenter
void setCenter(const QgsPointXY &point)
Sets the center point of the marker, in map coordinates.
Definition: qgsvertexmarker.cpp:39
QgsVertexMarker::setColor
void setColor(const QColor &color)
Sets the stroke color for the marker.
Definition: qgsvertexmarker.cpp:46
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsVertexMarker::paint
void paint(QPainter *p) override
function to be implemented by derived classes
Definition: qgsvertexmarker.cpp:63
QgsVertexMarker::ICON_X
@ ICON_X
Definition: qgsvertexmarker.h:54