QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 }
Abstract base class for all geometries.
void setStrokeColor(const QColor &c)
Sets stroke color for vertex markers.
@ ICON_X
A cross is used to highlight points (x)
@ ICON_NONE
No icon is used.
@ ICON_FULL_BOX
A full box is used to highlight points (■)
@ ICON_CIRCLE
A circle is used to highlight points (○)
@ ICON_BOX
A box is used to highlight points (□)
@ ICON_CROSS
A cross is used to highlight points (+)
QgsWkbTypes::GeometryType geometryType() const
Returns which geometry is handled by the rubber band, polygon or line.
void setBrushStyle(Qt::BrushStyle brushStyle)
Sets brush style.
void setGeometryType(const QgsWkbTypes::GeometryType &geometryType)
Sets which geometry is handled by the rubber band, polygon or line.
void setLineStyle(Qt::PenStyle penStyle)
Sets pen style.
virtual void setGeometry(QgsAbstractGeometry *geom)
Sets geometry (takes ownership). Geometry is expected to be in map coordinates.
void paint(QPainter *painter) override
function to be implemented by derived classes
void moveVertex(QgsVertexId id, const QgsPoint &newPos)
Moves vertex to new position (in map coordinates)
void setStrokeWidth(int width)
Sets stroke width.
void setVertexDrawingEnabled(bool isVerticesDrawn)
Sets whether the vertices are drawn.
QgsGeometryRubberBand(QgsMapCanvas *mapCanvas, QgsWkbTypes::GeometryType geomType=QgsWkbTypes::LineGeometry)
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
An abstract class for items that can be placed on the map canvas.
QgsMapCanvas * mMapCanvas
pointer to map canvas
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:86
double mapUnitsPerPixel() const
Returns the mapUnitsPerPixel (map units per pixel) for the canvas.
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
QgsPointXY transform(const QgsPointXY &p) const
Transform the point p from map (world) coordinates to device coordinates.
Definition: qgsmaptopixel.h:82
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
Q_GADGET double x
Definition: qgspoint.h:52
double y
Definition: qgspoint.h:53
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Scoped object for saving and restoring a QPainter object's state.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
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
Utility class for identifying a unique vertex within a geometry.