QGIS API Documentation 4.3.0-Master (35968c35f7e)
Loading...
Searching...
No Matches
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
19
20#include "qgsabstractgeometry.h"
21#include "qgsmapcanvas.h"
22#include "qgspainting.h"
23#include "qgspoint.h"
24#include "qgsrendercontext.h"
25
26#include <QPainter>
27
29 : QgsMapCanvasItem( mapCanvas )
30 , mGeometryType( geomType )
31{
32 mPen = QPen( QColor( 255, 0, 0 ) );
33 mBrush = QBrush( QColor( 255, 0, 0 ) );
34}
35
38
39void QgsGeometryRubberBand::paint( QPainter *painter )
40{
41 if ( !mGeometry || !painter )
42 {
43 return;
44 }
45
46 const QgsScopedQPainterState painterState( painter );
47 painter->translate( -pos() );
48
49 if ( mGeometryType == Qgis::GeometryType::Polygon )
50 {
51 painter->setBrush( mBrush );
52 }
53 else
54 {
55 painter->setBrush( Qt::NoBrush );
56 }
57 painter->setPen( mPen );
58
59
60 std::unique_ptr<QgsAbstractGeometry> paintGeom( mGeometry->clone() );
61
62 paintGeom->transform( mMapCanvas->getCoordinateTransform()->transform() );
63 paintGeom->draw( *painter );
64
65 if ( !mDrawVertices )
66 return;
67
68 //draw vertices
69 QgsVertexId vertexId;
70 QgsPoint vertex;
71 while ( paintGeom->nextVertex( vertexId, vertex ) )
72 {
73 drawVertex( painter, vertex.x(), vertex.y() );
74 }
75}
76
78{
79 return mGeometryType;
80}
81
86
87void QgsGeometryRubberBand::drawVertex( QPainter *p, double x, double y )
88{
89 const qreal s = ( mIconSize - 1 ) / 2.0;
90
91 switch ( mIconType )
92 {
93 case ICON_NONE:
94 break;
95
96 case ICON_CROSS:
97 p->drawLine( QLineF( x - s, y, x + s, y ) );
98 p->drawLine( QLineF( x, y - s, x, y + s ) );
99 break;
100
101 case ICON_X:
102 p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
103 p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
104 break;
105
106 case ICON_BOX:
107 p->drawLine( QLineF( x - s, y - s, x + s, y - s ) );
108 p->drawLine( QLineF( x + s, y - s, x + s, y + s ) );
109 p->drawLine( QLineF( x + s, y + s, x - s, y + s ) );
110 p->drawLine( QLineF( x - s, y + s, x - s, y - s ) );
111 break;
112
113 case ICON_FULL_BOX:
114 p->drawRect( x - s, y - s, mIconSize, mIconSize );
115 break;
116
117 case ICON_CIRCLE:
118 p->drawEllipse( x - s, y - s, mIconSize, mIconSize );
119 break;
120 }
121}
122
124{
125 mGeometry.reset( geom );
126
127 if ( mGeometry )
128 {
129 setRect( rubberBandRectangle() );
130 }
131}
132
134{
135 if ( mGeometry )
136 {
137 mGeometry->moveVertex( id, newPos );
138 setRect( rubberBandRectangle() );
139 }
140}
141
143{
144 mBrush.setColor( c );
145}
146
148{
149 mPen.setColor( c );
150}
151
153{
154 mPen.setWidth( width );
155}
156
157void QgsGeometryRubberBand::setLineStyle( Qt::PenStyle penStyle )
158{
159 mPen.setStyle( penStyle );
160}
161
162void QgsGeometryRubberBand::setBrushStyle( Qt::BrushStyle brushStyle )
163{
164 mBrush.setStyle( brushStyle );
165}
166
168{
169 mDrawVertices = isVerticesDrawn;
170}
171
172QgsRectangle QgsGeometryRubberBand::rubberBandRectangle() const
173{
174 if ( !mGeometry || mGeometry->isEmpty() )
175 {
176 return QgsRectangle();
177 }
178 const QgsMapToPixel &m2p = *( mMapCanvas->getCoordinateTransform() );
179
180 qreal w = ( ( mIconSize - 1 ) / 2 + mPen.width() ); // in canvas units
181
182 QgsRectangle r; // in canvas units
183 QgsRectangle rectMap = mGeometry->boundingBox(); // in map units
184 QList<QgsPointXY> pl;
185 pl
186 << QgsPointXY( rectMap.xMinimum(), rectMap.yMinimum() )
187 << QgsPointXY( rectMap.xMinimum(), rectMap.yMaximum() )
188 << QgsPointXY( rectMap.xMaximum(), rectMap.yMaximum() )
189 << QgsPointXY( rectMap.xMaximum(), rectMap.yMinimum() );
190
191 for ( QgsPointXY &p : pl )
192 {
193 p = toCanvasCoordinates( p );
194 // no need to normalize the rectangle -- we know it is already normal
195 QgsRectangle rect( p.x() - w, p.y() - w, p.x() + w, p.y() + w, false );
197 }
198
199 // This is an hack to pass QgsMapCanvasItem::setRect what it
200 // expects (encoding of position and size of the item)
201 qreal res = m2p.mapUnitsPerPixel();
202 QgsPointXY topLeft = m2p.toMapCoordinates( r.xMinimum(), r.yMinimum() );
203 QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + r.width() * res, topLeft.y() - r.height() * res );
204
205 return rect;
206}
207
209{
210 // re-compute rectangle
211 // See https://github.com/qgis/QGIS/issues/20566
212 // NOTE: could be optimized by saving map-extent
213 // of rubberband and simply re-projecting
214 // that to device-rectangle on "updatePosition"
215 setRect( rubberBandRectangle() );
216}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:399
@ Polygon
Polygons.
Definition qgis.h:402
Abstract base class for all geometries.
void setStrokeColor(const QColor &c)
Sets stroke color for vertex markers.
QgsGeometryRubberBand(QgsMapCanvas *mapCanvas, Qgis::GeometryType geomType=Qgis::GeometryType::Line)
Constructor for QgsGeometryRubberBand of the given geomType, shown in the specified mapCanvas.
@ ICON_X
A cross is used to highlight points (x).
@ 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 (+).
Qgis::GeometryType geometryType() const
Returns which geometry is handled by the rubber band, polygon or line.
void setGeometryType(Qgis::GeometryType geometryType)
Sets which geometry is handled by the rubber band, polygon or line.
void setBrushStyle(Qt::BrushStyle brushStyle)
Sets brush style.
void updatePosition() override
called on changed extent or resize event to update position of the item
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.
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
QgsRectangle rect() const
returns canvas item rectangle in map units
QPointF toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
QgsMapCanvas * mMapCanvas
pointer to map canvas
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
QgsMapCanvasItem(QgsMapCanvas *mapCanvas)
protected constructor: cannot be constructed directly
Map canvas is a class for displaying all GIS data types on a canvas.
double mapUnitsPerPixel() const
Returns the current map units per pixel.
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
double x
Definition qgspoint.h:56
double y
Definition qgspoint.h:57
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
Scoped object for saving and restoring a QPainter object's state.
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:35