QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsrubberband.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrubberband.cpp - Rubberband widget for drawing multilines and polygons
3 --------------------------------------
4 Date : 07-Jan-2006
5 Copyright : (C) 2006 by Tom Elwertowski
6 Email : telwertowski at users dot sourceforge dot net
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsrubberband.h"
17
18#include "qgsfillsymbol.h"
19#include "qgsgeometry.h"
20#include "qgsguiutils.h"
21#include "qgslinesymbol.h"
22#include "qgslogger.h"
23#include "qgsmapcanvas.h"
24#include "qgsproject.h"
25#include "qgsrectangle.h"
26#include "qgsrendercontext.h"
27#include "qgssymbol.h"
28#include "qgsvectorlayer.h"
29
30#include <QPainter>
31#include <QString>
32
33#include "moc_qgsrubberband.cpp"
34
35using namespace Qt::StringLiterals;
36
38 : QObject( nullptr )
39 , QgsMapCanvasItem( mapCanvas )
40 , mGeometryType( geometryType )
41{
42 reset( geometryType );
43 QColor color( Qt::lightGray );
44 color.setAlpha( 63 );
45 setColor( color );
46 setWidth( 1 );
47 setLineStyle( Qt::SolidLine );
48 setBrushStyle( Qt::SolidPattern );
49 setSecondaryStrokeColor( QColor() );
50}
51
53 : QObject( nullptr )
54 , QgsMapCanvasItem( nullptr )
55{
56}
57
59
60void QgsRubberBand::setColor( const QColor &color )
61{
62 setStrokeColor( color );
63 setFillColor( color );
64}
65
66void QgsRubberBand::setFillColor( const QColor &color )
67{
68 if ( mBrush.color() == color )
69 return;
70
71 mBrush.setColor( color );
72}
73
74void QgsRubberBand::setStrokeColor( const QColor &color )
75{
76 mPen.setColor( color );
77}
78
79void QgsRubberBand::setSecondaryStrokeColor( const QColor &color )
80{
81 mSecondaryPen.setColor( color );
82}
83
85{
86 mPen.setWidthF( width );
87}
88
90{
91 mIconType = icon;
92}
93
94void QgsRubberBand::setSvgIcon( const QString &path, QPoint drawOffset )
95{
97 mSvgRenderer = std::make_unique<QSvgRenderer>( path );
98 mSvgOffset = drawOffset;
99}
100
102{
103 mIconSize = iconSize;
104}
105
106void QgsRubberBand::setLineStyle( Qt::PenStyle penStyle )
107{
108 mPen.setStyle( penStyle );
109}
110
111void QgsRubberBand::setBrushStyle( Qt::BrushStyle brushStyle )
112{
113 mBrush.setStyle( brushStyle );
114}
115
117{
118 mPoints.clear();
119 mGeometryType = geometryType;
120 updateRect();
121 update();
122}
123
124void QgsRubberBand::addPoint( const QgsPointXY &p, bool doUpdate /* = true */, int geometryIndex, int ringIndex )
125{
126 if ( geometryIndex < 0 )
127 {
128 geometryIndex = mPoints.size() - 1;
129 }
130
131 if ( geometryIndex < 0 || geometryIndex > mPoints.size() )
132 {
133 return;
134 }
135
136 if ( geometryIndex == mPoints.size() )
137 {
138 // since we're adding a geometry, ringIndex must be 0 or negative for last ring
139 if ( ringIndex > 0 )
140 return;
141 mPoints.append( QgsPolygonXY() );
142 }
143
144 // negative ringIndex means last ring
145 if ( ringIndex < 0 )
146 {
147 if ( mPoints.at( geometryIndex ).isEmpty() )
148 ringIndex = 0;
149 else
150 ringIndex = mPoints.at( geometryIndex ).size() - 1;
151 }
152
153 if ( ringIndex > mPoints.at( geometryIndex ).size() )
154 return;
155
156 if ( ringIndex == mPoints.at( geometryIndex ).size() )
157 {
158 mPoints[geometryIndex].append( QgsPolylineXY() );
159 if ( mGeometryType != Qgis::GeometryType::Point )
160 mPoints[geometryIndex][ringIndex].append( p );
161 }
162
163 if ( mPoints.at( geometryIndex ).at( ringIndex ).size() == 2 && mPoints.at( geometryIndex ).at( ringIndex ).at( 0 ) == mPoints.at( geometryIndex ).at( ringIndex ).at( 1 ) )
164 {
165 mPoints[geometryIndex][ringIndex].last() = p;
166 }
167 else
168 {
169 mPoints[geometryIndex][ringIndex].append( p );
170 }
171
172
173 if ( doUpdate )
174 {
175 setVisible( true );
176 updateRect();
177 update();
178 }
179}
180
181void QgsRubberBand::closePoints( bool doUpdate, int geometryIndex, int ringIndex )
182{
183 if ( geometryIndex < 0 || ringIndex < 0 || mPoints.size() <= geometryIndex || mPoints.at( geometryIndex ).size() <= ringIndex || mPoints.at( geometryIndex ).at( ringIndex ).isEmpty() )
184 {
185 return;
186 }
187
188 if ( mPoints.at( geometryIndex ).at( ringIndex ).constFirst() != mPoints.at( geometryIndex ).at( ringIndex ).constLast() )
189 {
190 mPoints[geometryIndex][ringIndex].append( mPoints.at( geometryIndex ).at( ringIndex ).constFirst() );
191 }
192
193 if ( doUpdate )
194 {
195 setVisible( true );
196 updateRect();
197 update();
198 }
199}
200
201
202void QgsRubberBand::removePoint( int index, bool doUpdate /* = true*/, int geometryIndex /* = 0*/, int ringIndex /* = 0*/ )
203{
204 if ( geometryIndex < 0 || ringIndex < 0 || mPoints.size() <= geometryIndex || mPoints.at( geometryIndex ).size() <= ringIndex || mPoints.at( geometryIndex ).at( ringIndex ).size() <= index || mPoints.at( geometryIndex ).at( ringIndex ).size() < -index || mPoints.at( geometryIndex ).at( ringIndex ).isEmpty() )
205 {
206 return;
207 }
208
209 // negative index removes from end, e.g., -1 removes last one
210 if ( index < 0 )
211 {
212 index = mPoints.at( geometryIndex ).at( ringIndex ).size() + index;
213 }
214 mPoints[geometryIndex][ringIndex].removeAt( index );
215
216 if ( doUpdate )
217 {
218 updateRect();
219 update();
220 }
221}
222
223void QgsRubberBand::removeLastPoint( int geometryIndex, bool doUpdate /* = true*/, int ringIndex /* = 0*/ )
224{
225 removePoint( -1, doUpdate, geometryIndex, ringIndex );
226}
227
228void QgsRubberBand::movePoint( const QgsPointXY &p, int geometryIndex, int ringIndex )
229{
230 if ( geometryIndex < 0 || ringIndex < 0 || mPoints.size() <= geometryIndex || mPoints.at( geometryIndex ).size() <= ringIndex || mPoints.at( geometryIndex ).at( ringIndex ).isEmpty() )
231 {
232 return;
233 }
234
235 mPoints[geometryIndex][ringIndex].last() = p;
236
237 updateRect();
238 update();
239}
240
241void QgsRubberBand::movePoint( int index, const QgsPointXY &p, int geometryIndex, int ringIndex )
242{
243 if ( geometryIndex < 0 || ringIndex < 0 || index < 0 || mPoints.size() <= geometryIndex || mPoints.at( geometryIndex ).size() <= ringIndex || mPoints.at( geometryIndex ).at( ringIndex ).size() <= index )
244 {
245 return;
246 }
247
248 mPoints[geometryIndex][ringIndex][index] = p;
249
250 updateRect();
251 update();
252}
253
255{
256 if ( geom.isNull() )
257 {
258 reset( mGeometryType );
259 return;
260 }
261
262 reset( geom.type() );
263 addGeometry( geom, layer );
264}
265
267{
268 if ( geom.isNull() )
269 {
270 reset( mGeometryType );
271 return;
272 }
273
274 reset( geom.type() );
275 addGeometry( geom, crs );
276}
277
278void QgsRubberBand::addGeometry( const QgsGeometry &geometry, QgsMapLayer *layer, bool doUpdate )
279{
280 QgsGeometry geom = geometry;
281 if ( layer )
282 {
283 QgsCoordinateTransform ct = mMapCanvas->mapSettings().layerTransform( layer );
284 try
285 {
286 geom.transform( ct );
287 }
288 catch ( QgsCsException & )
289 {
290 return;
291 }
292 }
293
294 addGeometry( geom, QgsCoordinateReferenceSystem(), doUpdate );
295}
296
297void QgsRubberBand::addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs, bool doUpdate )
298{
299 if ( geometry.isEmpty() )
300 {
301 return;
302 }
303
304 //maprender object of canvas
305 const QgsMapSettings &ms = mMapCanvas->mapSettings();
306
307 int idx = mPoints.size();
308
309 QgsGeometry geom = geometry;
310 if ( crs.isValid() )
311 {
313 try
314 {
315 geom.transform( ct );
316 }
317 catch ( QgsCsException & )
318 {
319 QgsDebugError( u"Could not transform rubber band geometry to map CRS"_s );
320 return;
321 }
322 }
323
324 Qgis::WkbType geomType = geom.wkbType();
326 {
327 QgsPointXY pt = geom.asPoint();
328 addPoint( pt, false, idx );
329 }
331 {
332 const QgsMultiPointXY mpt = geom.asMultiPoint();
333 for ( const QgsPointXY &pt : mpt )
334 {
335 addPoint( pt, false, idx );
336 idx++;
337 }
338 }
339 else if ( QgsWkbTypes::geometryType( geomType ) == Qgis::GeometryType::Line && !QgsWkbTypes::isMultiType( geomType ) )
340 {
341 const QgsPolylineXY line = geom.asPolyline();
342 for ( const QgsPointXY &pt : line )
343 {
344 addPoint( pt, false, idx );
345 }
346 }
347 else if ( QgsWkbTypes::geometryType( geomType ) == Qgis::GeometryType::Line && QgsWkbTypes::isMultiType( geomType ) )
348 {
349 const QgsMultiPolylineXY mline = geom.asMultiPolyline();
350 for ( const QgsPolylineXY &line : mline )
351 {
352 if ( line.isEmpty() )
353 {
354 continue;
355 }
356 for ( const QgsPointXY &pt : line )
357 {
358 addPoint( pt, false, idx );
359 }
360 idx++;
361 }
362 }
364 {
365 const QgsPolygonXY poly = geom.asPolygon();
366 int ringIdx = 0;
367 for ( const QgsPolylineXY &ring : poly )
368 {
369 for ( const QgsPointXY &pt : ring )
370 {
371 addPoint( pt, false, idx, ringIdx );
372 }
373 ringIdx++;
374 }
375 }
377 {
378 const QgsMultiPolygonXY multipoly = geom.asMultiPolygon();
379 for ( const QgsPolygonXY &poly : multipoly )
380 {
381 if ( poly.isEmpty() )
382 continue;
383
384 int ringIdx = 0;
385 for ( const QgsPolylineXY &ring : poly )
386 {
387 for ( const QgsPointXY &pt : ring )
388 {
389 addPoint( pt, false, idx, ringIdx );
390 }
391 ringIdx++;
392 }
393 idx++;
394 }
395 }
396 else
397 {
398 return;
399 }
400
401 setVisible( true );
402 if ( doUpdate )
403 {
404 updateRect();
405 update();
406 }
407}
408
410{
411 if ( !mMapCanvas )
412 {
413 return;
414 }
415
416 const QgsMapToPixel *transform = mMapCanvas->getCoordinateTransform();
417 QgsPointXY ll = transform->toMapCoordinates( rect.left(), rect.bottom() );
418 QgsPointXY lr = transform->toMapCoordinates( rect.right(), rect.bottom() );
419 QgsPointXY ul = transform->toMapCoordinates( rect.left(), rect.top() );
420 QgsPointXY ur = transform->toMapCoordinates( rect.right(), rect.top() );
421
423 addPoint( ll, false );
424 addPoint( lr, false );
425 addPoint( ur, false );
426 addPoint( ul, true );
427}
428
430{
431 reset( other->mGeometryType );
432 mPoints = other->mPoints;
433 updateRect();
434 update();
435}
436
437void QgsRubberBand::paint( QPainter *p )
438{
439 if ( mPoints.isEmpty() )
440 return;
441
442 QVector<QVector<QPolygonF>> shapes;
443 shapes.reserve( mPoints.size() );
444 for ( const QgsPolygonXY &poly : std::as_const( mPoints ) )
445 {
446 QVector<QPolygonF> rings;
447 rings.reserve( poly.size() );
448 for ( const QgsPolylineXY &line : poly )
449 {
450 QVector<QPointF> pts;
451 pts.reserve( line.size() );
452 for ( const QgsPointXY &pt : line )
453 {
454 const QPointF cur = toCanvasCoordinates( QgsPointXY( pt.x() + mTranslationOffsetX, pt.y() + mTranslationOffsetY ) ) - pos();
455 if ( pts.isEmpty() || std::abs( pts.last().x() - cur.x() ) > 1 || std::abs( pts.last().y() - cur.y() ) > 1 )
456 pts.append( cur );
457 }
458 rings.append( pts );
459 }
460 shapes.append( rings );
461 }
462
463 if ( QgsLineSymbol *lineSymbol = dynamic_cast<QgsLineSymbol *>( mSymbol.get() ) )
464 {
467
468 lineSymbol->startRender( context );
469 for ( const QVector<QPolygonF> &shape : std::as_const( shapes ) )
470 {
471 for ( const QPolygonF &ring : shape )
472 {
473 lineSymbol->renderPolyline( ring, nullptr, context );
474 }
475 }
476 lineSymbol->stopRender( context );
477 }
478 else if ( QgsFillSymbol *fillSymbol = dynamic_cast<QgsFillSymbol *>( mSymbol.get() ) )
479 {
482
483 fillSymbol->startRender( context );
484 for ( const QVector<QPolygonF> &shape : std::as_const( shapes ) )
485 {
486 for ( const QPolygonF &ring : shape )
487 {
488 fillSymbol->renderPolygon( ring, nullptr, nullptr, context );
489 }
490 }
491 fillSymbol->stopRender( context );
492 }
493 else
494 {
495 int iterations = mSecondaryPen.color().isValid() ? 2 : 1;
496 for ( int i = 0; i < iterations; ++i )
497 {
498 if ( i == 0 && iterations > 1 )
499 {
500 // first iteration with multi-pen painting, so use secondary pen
501 mSecondaryPen.setWidthF( mPen.widthF() + QgsGuiUtils::scaleIconSize( 2 ) );
502 p->setBrush( Qt::NoBrush );
503 p->setPen( mSecondaryPen );
504 }
505 else
506 {
507 // "top" layer, use primary pen/brush
508 p->setBrush( mBrush );
509 p->setPen( mPen );
510 }
511
512 for ( const QVector<QPolygonF> &shape : std::as_const( shapes ) )
513 {
514 drawShape( p, shape );
515 }
516 }
517 }
518}
519
520void QgsRubberBand::drawShape( QPainter *p, const QVector<QPolygonF> &rings )
521{
522 if ( rings.size() == 1 )
523 {
524 drawShape( p, rings.at( 0 ) );
525 }
526 else
527 {
528 QPainterPath path;
529 for ( const QPolygonF &poly : rings )
530 {
531 path.addPolygon( poly );
532 }
533 p->drawPath( path );
534 }
535}
536
537void QgsRubberBand::drawShape( QPainter *p, const QVector<QPointF> &pts )
538{
539 switch ( mGeometryType )
540 {
542 {
543 p->drawPolygon( pts );
544 }
545 break;
546
548 {
549 const auto constPts = pts;
550 for ( QPointF pt : constPts )
551 {
552 double x = pt.x();
553 double y = pt.y();
554
555 qreal s = ( mIconSize - 1 ) / 2.0;
556
557 switch ( mIconType )
558 {
559 case ICON_NONE:
560 break;
561
562 case ICON_CROSS:
563 p->drawLine( QLineF( x - s, y, x + s, y ) );
564 p->drawLine( QLineF( x, y - s, x, y + s ) );
565 break;
566
567 case ICON_X:
568 p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
569 p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
570 break;
571
572 case ICON_BOX:
573 p->drawLine( QLineF( x - s, y - s, x + s, y - s ) );
574 p->drawLine( QLineF( x + s, y - s, x + s, y + s ) );
575 p->drawLine( QLineF( x + s, y + s, x - s, y + s ) );
576 p->drawLine( QLineF( x - s, y + s, x - s, y - s ) );
577 break;
578
579 case ICON_FULL_BOX:
580 p->drawRect( QRectF( static_cast<int>( x - s ), static_cast<int>( y - s ), mIconSize, mIconSize ) );
581 break;
582
583 case ICON_CIRCLE:
584 p->drawEllipse( QRectF( static_cast<int>( x - s ), static_cast<int>( y - s ), mIconSize, mIconSize ) );
585 break;
586
587 case ICON_DIAMOND:
589 {
590 QPointF pts[] = {
591 QPointF( x, y - s ),
592 QPointF( x + s, y ),
593 QPointF( x, y + s ),
594 QPointF( x - s, y )
595 };
596 if ( mIconType == ICON_FULL_DIAMOND )
597 p->drawPolygon( pts, 4 );
598 else
599 p->drawPolyline( pts, 4 );
600 break;
601 }
602
603 case ICON_SVG:
604 {
605 QRectF viewBox = mSvgRenderer->viewBoxF();
606 QRectF r( mSvgOffset.x(), mSvgOffset.y(), viewBox.width(), viewBox.height() );
607 QgsScopedQPainterState painterState( p );
608 p->translate( pt );
609 mSvgRenderer->render( p, r );
610 break;
611 }
612 }
613 }
614 }
615 break;
616
618 default:
619 {
620 p->drawPolyline( pts );
621 }
622 break;
623 }
624}
625
627{
628 if ( mPoints.isEmpty() )
629 {
631 setVisible( false );
632 return;
633 }
634
635 const QgsMapToPixel &m2p = *( mMapCanvas->getCoordinateTransform() );
636
637#if 0 // unused?
638 double iconSize = ( mIconSize + 1 ) / 2.;
639 if ( mSvgRenderer )
640 {
641 QRectF viewBox = mSvgRenderer->viewBoxF();
642 iconSize = std::max( std::fabs( mSvgOffset.x() ) + .5 * viewBox.width(), std::fabs( mSvgOffset.y() ) + .5 * viewBox.height() );
643 }
644#endif
645
646 qreal w = ( ( mIconSize - 1 ) / 2 + mPen.widthF() ); // in canvas units
647
648 QgsRectangle r; // in canvas units
649 for ( const QgsPolygonXY &poly : std::as_const( mPoints ) )
650 {
651 for ( const QgsPointXY &point : poly.at( 0 ) )
652 {
653 QgsPointXY p( point.x() + mTranslationOffsetX, point.y() + mTranslationOffsetY );
654 p = m2p.transform( p );
655 // no need to normalize the rectangle -- we know it is already normal
656 QgsRectangle rect( p.x() - w, p.y() - w, p.x() + w, p.y() + w, false );
658 }
659 }
660
661 // This is an hack to pass QgsMapCanvasItem::setRect what it
662 // expects (encoding of position and size of the item)
663 qreal res = m2p.mapUnitsPerPixel();
664 QgsPointXY topLeft = m2p.toMapCoordinates( r.xMinimum(), r.yMinimum() );
665 QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + r.width() * res, topLeft.y() - r.height() * res );
666
667 setRect( rect );
668}
669
671{
672 return mSymbol.get();
673}
674
676{
677 mSymbol.reset( symbol );
678}
679
681{
682 // re-compute rectangle
683 // See https://github.com/qgis/QGIS/issues/20566
684 // NOTE: could be optimized by saving map-extent
685 // of rubberband and simply re-projecting
686 // that to device-rectangle on "updatePosition"
687 updateRect();
688}
689
690void QgsRubberBand::setTranslationOffset( double dx, double dy )
691{
692 mTranslationOffsetX = dx;
693 mTranslationOffsetY = dy;
694 updateRect();
695}
696
698{
699 return mPoints.size();
700}
701
702int QgsRubberBand::partSize( int geometryIndex ) const
703{
704 if ( geometryIndex < 0 || geometryIndex >= mPoints.size() || mPoints.at( geometryIndex ).isEmpty() )
705 return 0;
706 return mPoints.at( geometryIndex ).at( 0 ).size();
707}
708
710{
711 int count = 0;
712 for ( const QgsPolygonXY &poly : std::as_const( mPoints ) )
713 {
714 for ( const QgsPolylineXY &ring : poly )
715 {
716 count += ring.size();
717 }
718 }
719 return count;
720}
721
722const QgsPointXY *QgsRubberBand::getPoint( int i, int j, int ringIndex ) const
723{
724 if ( i < 0 || ringIndex < 0 || j < 0 || mPoints.size() <= i || mPoints.at( i ).size() <= ringIndex || mPoints.at( i ).at( ringIndex ).size() <= j )
725 return nullptr;
726 else
727 return &mPoints[i][ringIndex][j];
728}
729
731{
732 QgsGeometry geom;
733
734 switch ( mGeometryType )
735 {
737 {
738 geom = QgsGeometry::fromMultiPolygonXY( mPoints );
739 break;
740 }
741
743 {
744 QgsMultiPointXY multiPoint;
745
746 for ( const QgsPolygonXY &poly : std::as_const( mPoints ) )
747 {
748 if ( poly.isEmpty() )
749 continue;
750 multiPoint.append( poly.at( 0 ) );
751 }
752 geom = QgsGeometry::fromMultiPointXY( multiPoint );
753 break;
754 }
755
757 default:
758 {
759 if ( !mPoints.isEmpty() )
760 {
761 if ( mPoints.size() > 1 )
762 {
763 QgsMultiPolylineXY multiPolyline;
764 for ( const QgsPolygonXY &poly : std::as_const( mPoints ) )
765 {
766 if ( poly.isEmpty() )
767 continue;
768 multiPolyline.append( poly.at( 0 ) );
769 }
770 geom = QgsGeometry::fromMultiPolylineXY( multiPolyline );
771 }
772 else
773 {
774 if ( !mPoints.at( 0 ).isEmpty() )
775 geom = QgsGeometry::fromPolylineXY( mPoints.at( 0 ).at( 0 ) );
776 else
778 }
779 }
780 break;
781 }
782 }
783 return geom;
784}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:365
@ Point
Points.
Definition qgis.h:366
@ Line
Lines.
Definition qgis.h:367
@ Polygon
Polygons.
Definition qgis.h:368
@ Antialiasing
Use antialiasing while drawing.
Definition qgis.h:2814
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Handles coordinate transforms between two coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
A geometry is the spatial representation of a feature.
QgsMultiPolygonXY asMultiPolygon() const
Returns the contents of the geometry as a multi-polygon.
static QgsGeometry fromMultiPolylineXY(const QgsMultiPolylineXY &multiline)
Creates a new geometry from a QgsMultiPolylineXY object.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
static QgsGeometry fromPolylineXY(const QgsPolylineXY &polyline)
Creates a new LineString geometry from a list of QgsPointXY points.
QgsMultiPointXY asMultiPoint() const
Returns the contents of the geometry as a multi-point.
QgsPolygonXY asPolygon() const
Returns the contents of the geometry as a polygon.
static QgsGeometry fromMultiPointXY(const QgsMultiPointXY &multipoint)
Creates a new geometry from a QgsMultiPointXY object.
QgsPolylineXY asPolyline() const
Returns the contents of the geometry as a polyline.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
Qgis::GeometryType type
QgsMultiPolylineXY asMultiPolyline() const
Returns the contents of the geometry as a multi-linestring.
static QgsGeometry fromMultiPolygonXY(const QgsMultiPolygonXY &multipoly)
Creates a new geometry from a QgsMultiPolygonXY.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
A line symbol type, for rendering LineString and MultiLineString geometries.
An abstract class for items that can be placed on the map canvas.
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.
Base class for all map layer types.
Definition qgsmaplayer.h:83
Contains configuration for rendering maps.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
Perform transforms between map coordinates and device coordinates.
double mapUnitsPerPixel() const
Returns the current map units per pixel.
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
QgsPointXY transform(const QgsPointXY &p) const
Transforms a point p from map (world) coordinates to device coordinates.
Represents a 2D point.
Definition qgspointxy.h:62
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
static QgsProject * instance()
Returns the QgsProject singleton instance.
A rectangle specified with double values.
double xMinimum
double yMinimum
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
Contains information about the context of a rendering operation.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
static QgsRenderContext fromQPainter(QPainter *painter)
Creates a default render context given a pixel based QPainter destination.
void setIconSize(double iconSize)
Sets the size of the point icons.
void closePoints(bool doUpdate=true, int geometryIndex=0, int ringIndex=0)
Ensures that a polygon geometry is closed and that the last vertex equals the first vertex.
~QgsRubberBand() override
void setSymbol(QgsSymbol *symbol)
Sets the symbol used for rendering the rubberband.
void removeLastPoint(int geometryIndex=0, bool doUpdate=true, int ringIndex=0)
Removes the last point.
void movePoint(const QgsPointXY &p, int geometryIndex=0, int ringIndex=0)
Moves the rubber band point specified by index.
QgsRubberBand(QgsMapCanvas *mapCanvas, Qgis::GeometryType geometryType=Qgis::GeometryType::Line)
Creates a new RubberBand.
QgsGeometry asGeometry() const
Returns the rubberband as a Geometry.
int size() const
Returns number of geometries.
void paint(QPainter *p) override
Paints the rubber band in response to an update event.
IconType icon() const
Returns the current icon type to highlight point geometries.
int partSize(int geometryIndex) const
Returns number of vertices in feature part.
void setSvgIcon(const QString &path, QPoint drawOffset)
Set the path to the svg file to use to draw points.
void setWidth(double width)
Sets the width of the line.
void reset(Qgis::GeometryType geometryType=Qgis::GeometryType::Line)
Clears all the geometries in this rubberband.
void setSecondaryStrokeColor(const QColor &color)
Sets a secondary stroke color for the rubberband which will be drawn under the main stroke color.
@ ICON_X
A cross is used to highlight points (x).
@ ICON_FULL_DIAMOND
A diamond is used to highlight points (◆).
@ ICON_FULL_BOX
A full box is used to highlight points (■).
@ ICON_NONE
No icon is used.
@ ICON_CROSS
A cross is used to highlight points (+).
@ ICON_SVG
An svg image is used to highlight points.
@ ICON_CIRCLE
A circle is used to highlight points (○).
@ ICON_DIAMOND
A diamond is used to highlight points (◇).
@ ICON_BOX
A box is used to highlight points (□).
const QgsPointXY * getPoint(int i, int j=0, int ringIndex=0) const
Returns a vertex.
void setColor(const QColor &color)
Sets the color for the rubberband.
void setToGeometry(const QgsGeometry &geom, QgsVectorLayer *layer)
Sets this rubber band to geom.
void setStrokeColor(const QColor &color)
Sets the stroke color for the rubberband.
void setLineStyle(Qt::PenStyle penStyle)
Sets the style of the line.
void updateRect()
Recalculates needed rectangle.
QgsSymbol * symbol() const
Returns the symbol used for rendering the rubberband, if set.
void setToCanvasRectangle(QRect rect)
Sets this rubber band to a map canvas rectangle.
void setIcon(IconType icon)
Sets the icon type to highlight point geometries.
void setBrushStyle(Qt::BrushStyle brushStyle)
Sets the style of the brush.
void updatePosition() override
called on changed extent or resize event to update position of the item
void copyPointsFrom(const QgsRubberBand *other)
Copies the points from another rubber band.
int numberOfVertices() const
Returns count of vertices in all lists of mPoint.
void addGeometry(const QgsGeometry &geometry, QgsMapLayer *layer, bool doUpdate=true)
Adds the geometry of an existing feature to a rubberband This is useful for multi feature highlightin...
void removePoint(int index=0, bool doUpdate=true, int geometryIndex=0, int ringIndex=0)
Removes a vertex from the rubberband and (optionally) updates canvas.
void setTranslationOffset(double dx, double dy)
Adds translation to original coordinates (all in map coordinates).
void setFillColor(const QColor &color)
Sets the fill color for the rubberband.
void drawShape(QPainter *p, const QVector< QPointF > &pts)
Draws shape of the rubber band.
void addPoint(const QgsPointXY &p, bool doUpdate=true, int geometryIndex=0, int ringIndex=0)
Adds a vertex to the rubberband and update canvas.
Scoped object for saving and restoring a QPainter object's state.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
Represents a vector layer which manages a vector based dataset.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
QVector< QgsPolylineXY > QgsPolygonXY
Polygon: first item of the list is outer ring, inner rings (if any) start from second item.
Definition qgsgeometry.h:92
QVector< QgsPolylineXY > QgsMultiPolylineXY
A collection of QgsPolylines that share a common collection of attributes.
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
Definition qgsgeometry.h:98
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition qgsgeometry.h:63
QVector< QgsPolygonXY > QgsMultiPolygonXY
A collection of QgsPolygons that share a common collection of attributes.
#define QgsDebugError(str)
Definition qgslogger.h:59