QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 }
QgsGeometryRubberBand::paint
void paint(QPainter *painter) override
function to be implemented by derived classes
Definition: qgsgeometryrubberband.cpp:36
qgsmapcanvas.h
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
QgsMapCanvasItem::mMapCanvas
QgsMapCanvas * mMapCanvas
pointer to map canvas
Definition: qgsmapcanvasitem.h:82
QgsAbstractGeometry::moveVertex
virtual bool moveVertex(QgsVertexId position, const QgsPoint &newPos)=0
Moves a vertex within the geometry.
QgsGeometryRubberBand::ICON_CROSS
@ ICON_CROSS
A cross is used to highlight points (+)
Definition: qgsgeometryrubberband.h:65
qgsgeometryrubberband.h
QgsMapCanvas
Definition: qgsmapcanvas.h:83
QgsGeometryRubberBand::ICON_CIRCLE
@ ICON_CIRCLE
A circle is used to highlight points (○)
Definition: qgsgeometryrubberband.h:80
qgspoint.h
QgsRectangle
Definition: qgsrectangle.h:41
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:143
QgsGeometryRubberBand::ICON_BOX
@ ICON_BOX
A box is used to highlight points (□)
Definition: qgsgeometryrubberband.h:75
QgsMapCanvasItem::setRect
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
Definition: qgsmapcanvasitem.cpp:74
QgsGeometryRubberBand::setFillColor
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
Definition: qgsgeometryrubberband.cpp:130
QgsPoint::y
double y
Definition: qgspoint.h:59
QgsMapCanvasItem
Definition: qgsmapcanvasitem.h:33
QgsGeometryRubberBand::setLineStyle
void setLineStyle(Qt::PenStyle penStyle)
Sets pen style.
Definition: qgsgeometryrubberband.cpp:145
QgsGeometryRubberBand::QgsGeometryRubberBand
QgsGeometryRubberBand(QgsMapCanvas *mapCanvas, QgsWkbTypes::GeometryType geomType=QgsWkbTypes::LineGeometry)
Definition: qgsgeometryrubberband.cpp:24
QgsAbstractGeometry::clone
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
QgsRectangle::buffered
QgsRectangle buffered(double width) const
Gets rectangle enlarged by buffer.
Definition: qgsrectangle.h:304
QgsMapCanvas::getCoordinateTransform
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
Definition: qgsmapcanvas.cpp:334
QgsGeometryRubberBand::setGeometry
void setGeometry(QgsAbstractGeometry *geom)
Sets geometry (takes ownership). Geometry is expected to be in map coordinates.
Definition: qgsgeometryrubberband.cpp:110
QgsGeometryRubberBand::ICON_FULL_BOX
@ ICON_FULL_BOX
A full box is used to highlight points (■)
Definition: qgsgeometryrubberband.h:85
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:71
QgsGeometryRubberBand::setBrushStyle
void setBrushStyle(Qt::BrushStyle brushStyle)
Sets brush style.
Definition: qgsgeometryrubberband.cpp:150
QgsAbstractGeometry::boundingBox
virtual QgsRectangle boundingBox() const =0
Returns the minimal bounding box for the geometry.
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:139
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
QgsAbstractGeometry::draw
virtual void draw(QPainter &p) const =0
Draws the geometry using the specified QPainter.
QgsVertexId
Utility class for identifying a unique vertex within a geometry.
Definition: qgsabstractgeometry.h:1033
QgsGeometryRubberBand::moveVertex
void moveVertex(QgsVertexId id, const QgsPoint &newPos)
Moves vertex to new position (in map coordinates)
Definition: qgsgeometryrubberband.cpp:121
QgsGeometryRubberBand::ICON_NONE
@ ICON_NONE
No icon is used.
Definition: qgsgeometryrubberband.h:60
QgsGeometryRubberBand::setStrokeWidth
void setStrokeWidth(int width)
Sets stroke width.
Definition: qgsgeometryrubberband.cpp:140
QgsGeometryRubberBand::setStrokeColor
void setStrokeColor(const QColor &c)
Sets stroke color for vertex markers.
Definition: qgsgeometryrubberband.cpp:135
QgsMapCanvas::mapUnitsPerPixel
double mapUnitsPerPixel() const
Returns the mapUnitsPerPixel (map units per pixel) for the canvas.
Definition: qgsmapcanvas.cpp:2124
QgsAbstractGeometry::transform
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.
qgsabstractgeometry.h
QgsAbstractGeometry::nextVertex
virtual bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const =0
Returns next vertex id and coordinates.
QgsPoint::x
double x
Definition: qgspoint.h:58
QgsGeometryRubberBand::ICON_X
@ ICON_X
A cross is used to highlight points (x)
Definition: qgsgeometryrubberband.h:70
QgsGeometryRubberBand::~QgsGeometryRubberBand
~QgsGeometryRubberBand() override
Definition: qgsgeometryrubberband.cpp:31