QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsgeometryrubberband.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometryrubberband.cpp
3  -------------------------
4  begin : December 2014
5  copyright : (C) 2014 by Marco Hugentobler
6  email : marco at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsgeometryrubberband.h"
19 #include "qgsabstractgeometry.h"
20 #include "qgsmapcanvas.h"
21 #include "qgspoint.h"
22 #include <QPainter>
23 
25  mIconSize( 5 ), mIconType( ICON_BOX ), mGeometryType( geomType )
26 {
27  mPen = QPen( QColor( 255, 0, 0 ) );
28  mBrush = QBrush( QColor( 255, 0, 0 ) );
29 }
30 
32 {
33 }
34 
35 void QgsGeometryRubberBand::paint( QPainter *painter )
36 {
37  if ( !mGeometry || !painter )
38  {
39  return;
40  }
41 
42  QgsScopedQPainterState painterState( painter );
43  painter->translate( -pos() );
44 
45  if ( mGeometryType == QgsWkbTypes::PolygonGeometry )
46  {
47  painter->setBrush( mBrush );
48  }
49  else
50  {
51  painter->setBrush( Qt::NoBrush );
52  }
53  painter->setPen( mPen );
54 
55 
56  std::unique_ptr< QgsAbstractGeometry > paintGeom( mGeometry->clone() );
57 
58  paintGeom->transform( mMapCanvas->getCoordinateTransform()->transform() );
59  paintGeom->draw( *painter );
60 
61  if ( !mDrawVertices )
62  return;
63 
64  //draw vertices
65  QgsVertexId vertexId;
66  QgsPoint vertex;
67  while ( paintGeom->nextVertex( vertexId, vertex ) )
68  {
69  drawVertex( painter, vertex.x(), vertex.y() );
70  }
71 }
72 
74 {
75  return mGeometryType;
76 }
77 
79 {
80  mGeometryType = geometryType;
81 }
82 
83 void QgsGeometryRubberBand::drawVertex( QPainter *p, double x, double y )
84 {
85  qreal s = ( mIconSize - 1 ) / 2.0;
86 
87  switch ( mIconType )
88  {
89  case ICON_NONE:
90  break;
91 
92  case ICON_CROSS:
93  p->drawLine( QLineF( x - s, y, x + s, y ) );
94  p->drawLine( QLineF( x, y - s, x, y + s ) );
95  break;
96 
97  case ICON_X:
98  p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
99  p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
100  break;
101 
102  case ICON_BOX:
103  p->drawLine( QLineF( x - s, y - s, x + s, y - s ) );
104  p->drawLine( QLineF( x + s, y - s, x + s, y + s ) );
105  p->drawLine( QLineF( x + s, y + s, x - s, y + s ) );
106  p->drawLine( QLineF( x - s, y + s, x - s, y - s ) );
107  break;
108 
109  case ICON_FULL_BOX:
110  p->drawRect( x - s, y - s, mIconSize, mIconSize );
111  break;
112 
113  case ICON_CIRCLE:
114  p->drawEllipse( x - s, y - s, mIconSize, mIconSize );
115  break;
116  }
117 }
118 
120 {
121  mGeometry.reset( geom );
122 
123  if ( mGeometry )
124  {
125  setRect( rubberBandRectangle() );
126  }
127 }
128 
130 {
131  if ( mGeometry )
132  {
133  mGeometry->moveVertex( id, newPos );
134  setRect( rubberBandRectangle() );
135  }
136 }
137 
139 {
140  mBrush.setColor( c );
141 }
142 
144 {
145  mPen.setColor( c );
146 }
147 
149 {
150  mPen.setWidth( width );
151 }
152 
153 void QgsGeometryRubberBand::setLineStyle( Qt::PenStyle penStyle )
154 {
155  mPen.setStyle( penStyle );
156 }
157 
158 void QgsGeometryRubberBand::setBrushStyle( Qt::BrushStyle brushStyle )
159 {
160  mBrush.setStyle( brushStyle );
161 }
162 
164 {
165  mDrawVertices = isVerticesDrawn;
166 }
167 
168 QgsRectangle QgsGeometryRubberBand::rubberBandRectangle() const
169 {
170  qreal scale = mMapCanvas->mapUnitsPerPixel();
171  qreal s = ( mIconSize - 1 ) / 2.0 * scale;
172  qreal p = mPen.width() * scale;
173  return mGeometry->boundingBox().buffered( s + p );
174 }
QgsGeometryRubberBand::paint
void paint(QPainter *painter) override
function to be implemented by derived classes
Definition: qgsgeometryrubberband.cpp:35
qgsmapcanvas.h
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
QgsMapCanvasItem::mMapCanvas
QgsMapCanvas * mMapCanvas
pointer to map canvas
Definition: qgsmapcanvasitem.h:82
QgsGeometryRubberBand::ICON_CROSS
@ ICON_CROSS
A cross is used to highlight points (+)
Definition: qgsgeometryrubberband.h:72
qgsgeometryrubberband.h
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsGeometryRubberBand::ICON_CIRCLE
@ ICON_CIRCLE
A circle is used to highlight points (○)
Definition: qgsgeometryrubberband.h:87
qgspoint.h
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
QgsGeometryRubberBand::ICON_BOX
@ ICON_BOX
A box is used to highlight points (□)
Definition: qgsgeometryrubberband.h:82
QgsMapCanvasItem::setRect
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
Definition: qgsmapcanvasitem.cpp:74
QgsGeometryRubberBand::geometryType
QgsWkbTypes::GeometryType geometryType() const
Returns which geometry is handled by the rubber band, polygon or line.
Definition: qgsgeometryrubberband.cpp:73
QgsGeometryRubberBand::setFillColor
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
Definition: qgsgeometryrubberband.cpp:138
QgsPoint::y
double y
Definition: qgspoint.h:42
QgsMapCanvasItem
An abstract class for items that can be placed on the map canvas.
Definition: qgsmapcanvasitem.h:34
QgsGeometryRubberBand::setLineStyle
void setLineStyle(Qt::PenStyle penStyle)
Sets pen style.
Definition: qgsgeometryrubberband.cpp:153
QgsGeometryRubberBand::QgsGeometryRubberBand
QgsGeometryRubberBand(QgsMapCanvas *mapCanvas, QgsWkbTypes::GeometryType geomType=QgsWkbTypes::LineGeometry)
Definition: qgsgeometryrubberband.cpp:24
QgsPoint::x
Q_GADGET double x
Definition: qgspoint.h:41
QgsScopedQPainterState
Scoped object for saving and restoring a QPainter object's state.
Definition: qgsrendercontext.h:1120
QgsMapCanvas::getCoordinateTransform
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
Definition: qgsmapcanvas.cpp:335
QgsGeometryRubberBand::setGeometry
virtual void setGeometry(QgsAbstractGeometry *geom)
Sets geometry (takes ownership). Geometry is expected to be in map coordinates.
Definition: qgsgeometryrubberband.cpp:119
QgsGeometryRubberBand::ICON_FULL_BOX
@ ICON_FULL_BOX
A full box is used to highlight points (■)
Definition: qgsgeometryrubberband.h:92
QgsMapToPixel::transform
QgsPointXY transform(const QgsPointXY &p) const
Transform the point from map (world) coordinates to device coordinates.
Definition: qgsmaptopixel.cpp:217
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:74
QgsGeometryRubberBand::setBrushStyle
void setBrushStyle(Qt::BrushStyle brushStyle)
Sets brush style.
Definition: qgsgeometryrubberband.cpp:158
QgsGeometryRubberBand::setGeometryType
void setGeometryType(const QgsWkbTypes::GeometryType &geometryType)
Sets which geometry is handled by the rubber band, polygon or line.
Definition: qgsgeometryrubberband.cpp:78
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
c
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Definition: porting_processing.dox:1
QgsVertexId
Utility class for identifying a unique vertex within a geometry.
Definition: qgsabstractgeometry.h:1059
QgsGeometryRubberBand::moveVertex
void moveVertex(QgsVertexId id, const QgsPoint &newPos)
Moves vertex to new position (in map coordinates)
Definition: qgsgeometryrubberband.cpp:129
QgsGeometryRubberBand::ICON_NONE
@ ICON_NONE
No icon is used.
Definition: qgsgeometryrubberband.h:67
QgsGeometryRubberBand::setVertexDrawingEnabled
void setVertexDrawingEnabled(bool isVerticesDrawn)
Sets whether the vertices are drawn.
Definition: qgsgeometryrubberband.cpp:163
QgsGeometryRubberBand::setStrokeWidth
void setStrokeWidth(int width)
Sets stroke width.
Definition: qgsgeometryrubberband.cpp:148
QgsGeometryRubberBand::setStrokeColor
void setStrokeColor(const QColor &c)
Sets stroke color for vertex markers.
Definition: qgsgeometryrubberband.cpp:143
QgsMapCanvas::mapUnitsPerPixel
double mapUnitsPerPixel() const
Returns the mapUnitsPerPixel (map units per pixel) for the canvas.
Definition: qgsmapcanvas.cpp:2160
qgsabstractgeometry.h
QgsGeometryRubberBand::ICON_X
@ ICON_X
A cross is used to highlight points (x)
Definition: qgsgeometryrubberband.h:77
QgsGeometryRubberBand::~QgsGeometryRubberBand
~QgsGeometryRubberBand() override
Definition: qgsgeometryrubberband.cpp:31