QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 "qgspoint.h"
23#include "qgsrendercontext.h"
24
25#include <QPainter>
26
28 : QgsMapCanvasItem( mapCanvas ), mGeometryType( geomType )
29{
30 mPen = QPen( QColor( 255, 0, 0 ) );
31 mBrush = QBrush( QColor( 255, 0, 0 ) );
32}
33
37
38void QgsGeometryRubberBand::paint( QPainter *painter )
39{
40 if ( !mGeometry || !painter )
41 {
42 return;
43 }
44
45 const QgsScopedQPainterState painterState( painter );
46 painter->translate( -pos() );
47
48 if ( mGeometryType == Qgis::GeometryType::Polygon )
49 {
50 painter->setBrush( mBrush );
51 }
52 else
53 {
54 painter->setBrush( Qt::NoBrush );
55 }
56 painter->setPen( mPen );
57
58
59 std::unique_ptr<QgsAbstractGeometry> paintGeom( mGeometry->clone() );
60
61 paintGeom->transform( mMapCanvas->getCoordinateTransform()->transform() );
62 paintGeom->draw( *painter );
63
64 if ( !mDrawVertices )
65 return;
66
67 //draw vertices
68 QgsVertexId vertexId;
69 QgsPoint vertex;
70 while ( paintGeom->nextVertex( vertexId, vertex ) )
71 {
72 drawVertex( painter, vertex.x(), vertex.y() );
73 }
74}
75
77{
78 return mGeometryType;
79}
80
85
86void QgsGeometryRubberBand::drawVertex( QPainter *p, double x, double y )
87{
88 const qreal s = ( mIconSize - 1 ) / 2.0;
89
90 switch ( mIconType )
91 {
92 case ICON_NONE:
93 break;
94
95 case ICON_CROSS:
96 p->drawLine( QLineF( x - s, y, x + s, y ) );
97 p->drawLine( QLineF( x, y - s, x, y + s ) );
98 break;
99
100 case ICON_X:
101 p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
102 p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
103 break;
104
105 case ICON_BOX:
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 p->drawLine( QLineF( x + s, y + s, x - s, y + s ) );
109 p->drawLine( QLineF( x - s, y + s, x - s, y - s ) );
110 break;
111
112 case ICON_FULL_BOX:
113 p->drawRect( x - s, y - s, mIconSize, mIconSize );
114 break;
115
116 case ICON_CIRCLE:
117 p->drawEllipse( x - s, y - s, mIconSize, mIconSize );
118 break;
119 }
120}
121
123{
124 mGeometry.reset( geom );
125
126 if ( mGeometry )
127 {
128 setRect( rubberBandRectangle() );
129 }
130}
131
133{
134 if ( mGeometry )
135 {
136 mGeometry->moveVertex( id, newPos );
137 setRect( rubberBandRectangle() );
138 }
139}
140
142{
143 mBrush.setColor( c );
144}
145
147{
148 mPen.setColor( c );
149}
150
152{
153 mPen.setWidth( width );
154}
155
156void QgsGeometryRubberBand::setLineStyle( Qt::PenStyle penStyle )
157{
158 mPen.setStyle( penStyle );
159}
160
161void QgsGeometryRubberBand::setBrushStyle( Qt::BrushStyle brushStyle )
162{
163 mBrush.setStyle( brushStyle );
164}
165
167{
168 mDrawVertices = isVerticesDrawn;
169}
170
171QgsRectangle QgsGeometryRubberBand::rubberBandRectangle() const
172{
173 if ( !mGeometry || mGeometry->isEmpty() )
174 {
175 return QgsRectangle();
176 }
177 const QgsMapToPixel &m2p = *( mMapCanvas->getCoordinateTransform() );
178
179 qreal w = ( ( mIconSize - 1 ) / 2 + mPen.width() ); // in canvas units
180
181 QgsRectangle r; // in canvas units
182 QgsRectangle rectMap = mGeometry->boundingBox(); // in map units
183 QList<QgsPointXY> pl;
184 pl << QgsPointXY( rectMap.xMinimum(), rectMap.yMinimum() )
185 << QgsPointXY( rectMap.xMinimum(), rectMap.yMaximum() )
186 << QgsPointXY( rectMap.xMaximum(), rectMap.yMaximum() )
187 << QgsPointXY( rectMap.xMaximum(), rectMap.yMinimum() );
188
189 for ( QgsPointXY &p : pl )
190 {
191 p = toCanvasCoordinates( p );
192 // no need to normalize the rectangle -- we know it is already normal
193 QgsRectangle rect( p.x() - w, p.y() - w, p.x() + w, p.y() + w, false );
195 }
196
197 // This is an hack to pass QgsMapCanvasItem::setRect what it
198 // expects (encoding of position and size of the item)
199 qreal res = m2p.mapUnitsPerPixel();
200 QgsPointXY topLeft = m2p.toMapCoordinates( r.xMinimum(), r.yMinimum() );
201 QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + r.width() * res, topLeft.y() - r.height() * res );
202
203 return rect;
204}
205
207{
208 // re-compute rectangle
209 // See https://github.com/qgis/QGIS/issues/20566
210 // NOTE: could be optimized by saving map-extent
211 // of rubberband and simply re-projecting
212 // that to device-rectangle on "updatePosition"
213 setRect( rubberBandRectangle() );
214}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:358
@ Polygon
Polygons.
Definition qgis.h:361
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:64
double x
Definition qgspointxy.h:63
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
double x
Definition qgspoint.h:52
double y
Definition qgspoint.h:53
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:30