QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
46void QgsVertexMarker::setColor( const QColor &color )
47{
48 mColor = color;
49 update();
50}
51
52void QgsVertexMarker::setFillColor( const QColor &color )
53{
54 mFillColor = color;
55 update();
56}
57
59{
60 mPenWidth = width;
61}
62
63void 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
73void 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}
An abstract class for items that can be placed on the map canvas.
QPointF toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:93
A class to represent a 2D point.
Definition: qgspointxy.h:60
QColor color() const
Returns the stroke color for the marker.
void setFillColor(const QColor &color)
Sets the fill color for the marker.
void setPenWidth(int width)
@ ICON_TRIANGLE
Added in QGIS 3.12.
@ ICON_DOUBLE_TRIANGLE
Added in QGIS 3.0.
@ ICON_INVERTED_TRIANGLE
Added in QGIS 3.20.
@ ICON_RHOMBUS
Added in QGIS 3.12.
void setIconSize(int iconSize)
void setCenter(const QgsPointXY &point)
Sets the center point of the marker, in map coordinates.
QRectF boundingRect() const override
void setIconType(int iconType)
void setColor(const QColor &color)
Sets the stroke color for the marker.
void updatePosition() override
called on changed extent or resize event to update position of the item
void paint(QPainter *p) override
function to be implemented by derived classes
QgsVertexMarker(QgsMapCanvas *mapCanvas)
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.