QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 "qgsrendercontext.h"
22 #include "qgspoint.h"
23 #include <QPainter>
24 
26  mIconSize( 5 ), mIconType( ICON_BOX ), mGeometryType( geomType )
27 {
28  mPen = QPen( QColor( 255, 0, 0 ) );
29  mBrush = QBrush( QColor( 255, 0, 0 ) );
30 }
31 
33 {
34 }
35 
36 void QgsGeometryRubberBand::paint( QPainter *painter )
37 {
38  if ( !mGeometry || !painter )
39  {
40  return;
41  }
42 
43  const QgsScopedQPainterState painterState( painter );
44  painter->translate( -pos() );
45 
46  if ( mGeometryType == QgsWkbTypes::PolygonGeometry )
47  {
48  painter->setBrush( mBrush );
49  }
50  else
51  {
52  painter->setBrush( Qt::NoBrush );
53  }
54  painter->setPen( mPen );
55 
56 
57  std::unique_ptr< QgsAbstractGeometry > paintGeom( mGeometry->clone() );
58 
59  paintGeom->transform( mMapCanvas->getCoordinateTransform()->transform() );
60  paintGeom->draw( *painter );
61 
62  if ( !mDrawVertices )
63  return;
64 
65  //draw vertices
66  QgsVertexId vertexId;
67  QgsPoint vertex;
68  while ( paintGeom->nextVertex( vertexId, vertex ) )
69  {
70  drawVertex( painter, vertex.x(), vertex.y() );
71  }
72 }
73 
75 {
76  return mGeometryType;
77 }
78 
80 {
81  mGeometryType = geometryType;
82 }
83 
84 void QgsGeometryRubberBand::drawVertex( QPainter *p, double x, double y )
85 {
86  const qreal s = ( mIconSize - 1 ) / 2.0;
87 
88  switch ( mIconType )
89  {
90  case ICON_NONE:
91  break;
92 
93  case ICON_CROSS:
94  p->drawLine( QLineF( x - s, y, x + s, y ) );
95  p->drawLine( QLineF( x, y - s, x, y + s ) );
96  break;
97 
98  case ICON_X:
99  p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
100  p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
101  break;
102 
103  case ICON_BOX:
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  p->drawLine( QLineF( x - s, y + s, x - s, y - s ) );
108  break;
109 
110  case ICON_FULL_BOX:
111  p->drawRect( x - s, y - s, mIconSize, mIconSize );
112  break;
113 
114  case ICON_CIRCLE:
115  p->drawEllipse( x - s, y - s, mIconSize, mIconSize );
116  break;
117  }
118 }
119 
121 {
122  mGeometry.reset( geom );
123 
124  if ( mGeometry )
125  {
126  setRect( rubberBandRectangle() );
127  }
128 }
129 
131 {
132  if ( mGeometry )
133  {
134  mGeometry->moveVertex( id, newPos );
135  setRect( rubberBandRectangle() );
136  }
137 }
138 
140 {
141  mBrush.setColor( c );
142 }
143 
145 {
146  mPen.setColor( c );
147 }
148 
150 {
151  mPen.setWidth( width );
152 }
153 
154 void QgsGeometryRubberBand::setLineStyle( Qt::PenStyle penStyle )
155 {
156  mPen.setStyle( penStyle );
157 }
158 
159 void QgsGeometryRubberBand::setBrushStyle( Qt::BrushStyle brushStyle )
160 {
161  mBrush.setStyle( brushStyle );
162 }
163 
165 {
166  mDrawVertices = isVerticesDrawn;
167 }
168 
169 QgsRectangle QgsGeometryRubberBand::rubberBandRectangle() const
170 {
171  const qreal scale = mMapCanvas->mapUnitsPerPixel();
172  const qreal s = ( mIconSize - 1 ) / 2.0 * scale;
173  const qreal p = mPen.width() * scale;
174  return mGeometry->boundingBox().buffered( s + p );
175 }
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:89
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
Transforms a point p from map (world) coordinates to device coordinates.
Definition: qgsmaptopixel.h:90
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.
Definition: qgsvertexid.h:31