QGIS API Documentation  2.12.0-Lyon
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 "qgsabstractgeometryv2.h"
20 #include "qgsmapcanvas.h"
21 #include "qgspointv2.h"
22 #include <QPainter>
23 
25  mGeometry( 0 ), 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 
37 {
38  if ( !mGeometry || !painter )
39  {
40  return;
41  }
42 
43  painter->save();
44  painter->translate( -pos() );
45 
46  if ( mGeometryType == QGis::Polygon )
47  {
48  painter->setBrush( mBrush );
49  }
50  else
51  {
52  painter->setBrush( Qt::NoBrush );
53  }
54  painter->setPen( mPen );
55 
56 
57  QgsAbstractGeometryV2* paintGeom = mGeometry->clone();
58 
60  paintGeom->draw( *painter );
61 
62  //draw vertices
63  QgsVertexId vertexId;
64  QgsPointV2 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().buffer( s + p );
161 }
A circle is used to highlight points (○)
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void setStyle(Qt::PenStyle style)
double mapUnitsPerPixel() const
Returns the mapUnitsPerPixel (map units per pixel) for the canvas.
void setGeometry(QgsAbstractGeometryV2 *geom)
Sets geometry (takes ownership).
double x() const
Definition: qgspointv2.h:42
A cross is used to highlight points (+)
A box is used to highlight points (□)
QgsRectangle buffer(double width)
Get rectangle enlarged by buffer.
void setOutlineWidth(int width)
Sets outline width.
virtual bool moveVertex(const QgsVertexId &position, const QgsPointV2 &newPos)=0
Moves a vertex within the geometry.
QgsPoint transform(const QgsPoint &p) const
Transform the point from map (world) coordinates to device coordinates.
An abstract class for items that can be placed on the map canvas.
void setOutlineColor(const QColor &c)
Sets outline color for vertex markes.
void save()
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
Abstract base class for all geometries.
GeometryType
Definition: qgis.h:104
void drawLine(const QLineF &line)
void setStyle(Qt::BrushStyle style)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:107
double y() const
Definition: qgspointv2.h:43
void drawRect(const QRectF &rectangle)
QPointF pos() const
A full box is used to highlight points (■)
Utility class for identifying a unique vertex within a geometry.
void setPen(const QColor &color)
void drawEllipse(const QRectF &rectangle)
Point geometry type.
Definition: qgspointv2.h:29
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
void setBrush(const QBrush &brush)
virtual void transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d=QgsCoordinateTransform::ForwardTransform)=0
Transforms the geometry using a coordinate transform.
virtual bool nextVertex(QgsVertexId &id, QgsPointV2 &vertex) const =0
Returns next vertex id and coordinates.
void setColor(const QColor &color)
void setBrushStyle(Qt::BrushStyle brushStyle)
Sets brush style.
void restore()
int width() const
void setWidth(int width)
QgsMapCanvas * mMapCanvas
pointer to map canvas
void translate(const QPointF &offset)
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
virtual void draw(QPainter &p) const =0
Draws the geometry using the specified QPainter.
QgsRectangle boundingBox() const
Returns the minimal bounding box for the geometry.
virtual QgsAbstractGeometryV2 * clone() const =0
Clones the geometry by performing a deep copy.
virtual 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)
QgsGeometryRubberBand(QgsMapCanvas *mapCanvas, QGis::GeometryType geomType=QGis::Line)
void setColor(const QColor &color)
void moveVertex(const QgsVertexId &id, const QgsPointV2 &newPos)
Moves vertex to new position (in map coordinates)
qreal scale() const