QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  delete mGeometry;
34 }
35 
36 void QgsGeometryRubberBand::paint( QPainter *painter )
37 {
38  if ( !mGeometry || !painter )
39  {
40  return;
41  }
42 
43  painter->save();
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  QgsAbstractGeometry *paintGeom = mGeometry->clone();
58 
60  paintGeom->draw( *painter );
61 
62  //draw vertices
63  QgsVertexId vertexId;
64  QgsPoint vertex;
65  while ( paintGeom->nextVertex( vertexId, vertex ) )
66  {
67  drawVertex( painter, vertex.x(), vertex.y() );
68  }
69 
70  delete paintGeom;
71  painter->restore();
72 }
73 
74 void QgsGeometryRubberBand::drawVertex( QPainter *p, double x, double y )
75 {
76  qreal s = ( mIconSize - 1 ) / 2.0;
77 
78  switch ( mIconType )
79  {
80  case ICON_NONE:
81  break;
82 
83  case ICON_CROSS:
84  p->drawLine( QLineF( x - s, y, x + s, y ) );
85  p->drawLine( QLineF( x, y - s, x, y + s ) );
86  break;
87 
88  case ICON_X:
89  p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
90  p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
91  break;
92 
93  case ICON_BOX:
94  p->drawLine( QLineF( x - s, y - s, x + s, y - s ) );
95  p->drawLine( QLineF( x + s, y - s, x + s, y + s ) );
96  p->drawLine( QLineF( x + s, y + s, x - s, y + s ) );
97  p->drawLine( QLineF( x - s, y + s, x - s, y - s ) );
98  break;
99 
100  case ICON_FULL_BOX:
101  p->drawRect( x - s, y - s, mIconSize, mIconSize );
102  break;
103 
104  case ICON_CIRCLE:
105  p->drawEllipse( x - s, y - s, mIconSize, mIconSize );
106  break;
107  }
108 }
109 
111 {
112  delete mGeometry;
113  mGeometry = geom;
114 
115  if ( mGeometry )
116  {
117  setRect( rubberBandRectangle() );
118  }
119 }
120 
122 {
123  if ( mGeometry )
124  {
125  mGeometry->moveVertex( id, newPos );
126  setRect( rubberBandRectangle() );
127  }
128 }
129 
131 {
132  mBrush.setColor( c );
133 }
134 
136 {
137  mPen.setColor( c );
138 }
139 
141 {
142  mPen.setWidth( width );
143 }
144 
145 void QgsGeometryRubberBand::setLineStyle( Qt::PenStyle penStyle )
146 {
147  mPen.setStyle( penStyle );
148 }
149 
150 void QgsGeometryRubberBand::setBrushStyle( Qt::BrushStyle brushStyle )
151 {
152  mBrush.setStyle( brushStyle );
153 }
154 
155 QgsRectangle QgsGeometryRubberBand::rubberBandRectangle() const
156 {
157  qreal scale = mMapCanvas->mapUnitsPerPixel();
158  qreal s = ( mIconSize - 1 ) / 2.0 * scale;
159  qreal p = mPen.width() * scale;
160  return mGeometry->boundingBox().buffered( s + p );
161 }
A circle is used to highlight points (○)
A rectangle specified with double values.
Definition: qgsrectangle.h:41
double y
Definition: qgspoint.h:42
virtual void transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)=0
Transforms the geometry using a coordinate transform.
void setGeometry(QgsAbstractGeometry *geom)
Sets geometry (takes ownership). Geometry is expected to be in map coordinates.
A cross is used to highlight points (+)
A box is used to highlight points (□)
An abstract class for items that can be placed on the map canvas.
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
void setStrokeWidth(int width)
Sets stroke width.
virtual bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const =0
Returns next vertex id and coordinates.
virtual QgsRectangle boundingBox() const =0
Returns the minimal bounding box for the geometry.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:75
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
void moveVertex(QgsVertexId id, const QgsPoint &newPos)
Moves vertex to new position (in map coordinates)
QgsPointXY transform(const QgsPointXY &p) const
Transform the point from map (world) coordinates to device coordinates.
QgsRectangle buffered(double width) const
Gets rectangle enlarged by buffer.
Definition: qgsrectangle.h:304
A full box is used to highlight points (■)
double mapUnitsPerPixel() const
Returns the mapUnitsPerPixel (map units per pixel) for the canvas.
Utility class for identifying a unique vertex within a geometry.
void setStrokeColor(const QColor &c)
Sets stroke color for vertex markers.
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
QgsGeometryRubberBand(QgsMapCanvas *mapCanvas, QgsWkbTypes::GeometryType geomType=QgsWkbTypes::LineGeometry)
Abstract base class for all geometries.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
void setBrushStyle(Qt::BrushStyle brushStyle)
Sets brush style.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:139
virtual bool moveVertex(QgsVertexId position, const QgsPoint &newPos)=0
Moves a vertex within the geometry.
virtual void draw(QPainter &p) const =0
Draws the geometry using the specified QPainter.
QgsMapCanvas * mMapCanvas
pointer to map canvas
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
void paint(QPainter *painter) override
function to be implemented by derived classes
void setLineStyle(Qt::PenStyle penStyle)
Sets pen style.
A cross is used to highlight points (x)
double x
Definition: qgspoint.h:41