QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsmapcanvasitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmapcanvasitem.h - base class for map canvas items
3 ----------------------
4 begin : February 2006
5 copyright : (C) 2006 by Martin Dobias
6 email : wonder.sk at gmail dot com
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
17#include "qgsmapcanvasitem.h"
18
19#include "qgslogger.h"
20#include "qgsmapcanvas.h"
21#include "qgsmaptopixel.h"
22#include "qgsrendercontext.h"
23
24#include <QBrush>
25#include <QGraphicsScene>
26#include <QPainter>
27#include <QPen>
28#include <QRect>
29
31 : mMapCanvas( mapCanvas )
32 , mItemSize( 0, 0 )
33{
34 Q_ASSERT( mapCanvas && mapCanvas->scene() );
35 mapCanvas->scene()->addItem( this );
36}
37
39{
40 update(); // schedule redraw of canvas
41}
42
43void QgsMapCanvasItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
44{
45 Q_UNUSED( option )
46 Q_UNUSED( widget )
47 if ( mMapCanvas->antiAliasingEnabled() )
48 {
49 painter->setRenderHint( QPainter::Antialiasing );
50 }
51 paint( painter ); // call the derived item's drawing routines
52}
53
55{
56 return mMapCanvas->getCoordinateTransform()->toMapCoordinates( point );
57}
58
59
61{
62 qreal x = point.x(), y = point.y();
63 mMapCanvas->getCoordinateTransform()->transformInPlace( x, y );
64 return QPointF( x, y );
65}
66
68{
69 return mRect;
70}
71
72
73void QgsMapCanvasItem::setRect( const QgsRectangle &rect, bool resetRotation )
74{
75 mRect = rect;
76 //updatePosition();
77
78 QRectF r; // empty rect by default
79 if ( !mRect.isEmpty() )
80 {
81 // rect encodes origin of the item (xMin,yMax from map to canvas units)
82 // and size (rect size / map units per pixel)
83 r.setTopLeft( toCanvasCoordinates( QPointF( mRect.xMinimum(), mRect.yMaximum() ) ) );
84 const QgsMapToPixel *m2p = mMapCanvas->getCoordinateTransform();
85 const double res = m2p->mapUnitsPerPixel();
86 r.setSize( QSizeF( mRect.width() / res, mRect.height() / res ) );
87 }
88
89 // set position in canvas where the item will have coordinate (0,0)
90 prepareGeometryChange();
91 setPos( r.topLeft() );
92 mItemSize = QSizeF( r.width() + 2, r.height() + 2 );
93
94 if ( resetRotation )
95 {
96 mRectRotation = mMapCanvas->rotation();
97 setRotation( 0 );
98 }
99
100 // QgsDebugMsgLevel(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height()), 2);
101
102 update();
103}
104
106{
107 return QRectF( QPointF( -1, -1 ), mItemSize );
108}
109
110
112{
113 update();
114 // porting: update below should not be needed anymore
115 //mMapCanvas->scene()->update(); //Contents();
116}
117
119{
120 if ( !mMapCanvas || !p )
121 {
122 return false;
123 }
124 const QgsMapSettings &ms = mMapCanvas->mapSettings();
125
126 context.setPainter( p );
127 context.setRendererScale( mMapCanvas->scale() );
128 context.setScaleFactor( ms.outputDpi() / 25.4 );
129
131 return true;
132}
133
135{
136 // default implementation: recalculate position of the item
137 setRect( mRect, false );
138 setRotation( mMapCanvas->rotation() - mRectRotation );
139}
@ PreferVector
Prefer vector-based rendering, when the result will still be visually near-identical to a raster-base...
Definition qgis.h:2705
QRectF boundingRect() const override
virtual void paint(QPainter *painter)=0
function to be implemented by derived classes
QSizeF mItemSize
cached size of the item (to return in boundingRect())
QgsRectangle rect() const
returns canvas item rectangle in map units
QgsRectangle mRect
cached canvas item rectangle in map coordinates encodes position (xmin,ymax) and size (width/height) ...
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
void updateCanvas()
schedules map canvas for repaint
~QgsMapCanvasItem() override
bool setRenderContextVariables(QPainter *p, QgsRenderContext &context) const
Sets render context parameters.
virtual void updatePosition()
called on changed extent or resize event to update position of the item
QgsPointXY toMapCoordinates(QPoint point) const
transformation from screen coordinates to map coordinates
Map canvas is a class for displaying all GIS data types on a canvas.
Contains configuration for rendering maps.
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
Perform transforms between map coordinates and device coordinates.
double mapUnitsPerPixel() const
Returns the current map units per pixel.
Represents a 2D point.
Definition qgspointxy.h:60
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
A rectangle specified with double values.
Contains information about the context of a rendering operation.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
void setRasterizedRenderingPolicy(Qgis::RasterizedRenderingPolicy policy)
Sets the policy controlling when rasterisation of content during renders is permitted.
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
void setRendererScale(double scale)
Sets the renderer map scale.