QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgsmapcanvas.h"
19 #include "qgsmaptopixel.h"
20 #include "qgsrendercontext.h"
21 #include <QGraphicsScene>
22 #include <QRect>
23 #include <QPen>
24 #include <QBrush>
25 #include <QPainter>
26 #include "qgslogger.h"
27 
29  : mMapCanvas( mapCanvas )
30  , mRectRotation( 0.0 )
31  , mItemSize( 0, 0 )
32 {
33  Q_ASSERT( mapCanvas && mapCanvas->scene() );
34  mapCanvas->scene()->addItem( this );
35 }
36 
38 {
39  update(); // schedule redraw of canvas
40 }
41 
42 void QgsMapCanvasItem::paint( QPainter *painter,
43  const QStyleOptionGraphicsItem *option,
44  QWidget *widget )
45 {
46  Q_UNUSED( option )
47  Q_UNUSED( widget )
49  {
50  painter->setRenderHint( QPainter::Antialiasing );
51  }
52  paint( painter ); // call the derived item's drawing routines
53 }
54 
56 {
58 }
59 
60 
62 {
63  qreal x = point.x(), y = point.y();
65  return QPointF( x, y );
66 }
67 
69 {
70  return mRect;
71 }
72 
73 
74 void QgsMapCanvasItem::setRect( const QgsRectangle &rect, bool resetRotation )
75 {
76  mRect = rect;
77  //updatePosition();
78 
79  QRectF r; // empty rect by default
80  if ( !mRect.isEmpty() )
81  {
82  // rect encodes origin of the item (xMin,yMax from map to canvas units)
83  // and size (rect size / map units per pixel)
84  r.setTopLeft( toCanvasCoordinates( QPointF( mRect.xMinimum(), mRect.yMaximum() ) ) );
86  const double res = m2p->mapUnitsPerPixel();
87  r.setSize( QSizeF( mRect.width() / res, mRect.height() / res ) );
88  }
89 
90  // set position in canvas where the item will have coordinate (0,0)
91  prepareGeometryChange();
92  setPos( r.topLeft() );
93  mItemSize = QSizeF( r.width() + 2, r.height() + 2 );
94 
95  if ( resetRotation )
96  {
98  setRotation( 0 );
99  }
100 
101  // QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height()));
102 
103  update();
104 }
105 
107 {
108  return QRectF( QPointF( -1, -1 ), mItemSize );
109 }
110 
111 
113 {
114  update();
115  // porting: update below should not be needed anymore
116  //mMapCanvas->scene()->update(); //Contents();
117 }
118 
120 {
121  if ( !mMapCanvas || !p )
122  {
123  return false;
124  }
125  const QgsMapSettings &ms = mMapCanvas->mapSettings();
126 
127  context.setPainter( p );
128  context.setRendererScale( mMapCanvas->scale() );
129  context.setScaleFactor( ms.outputDpi() / 25.4 );
130 
131  context.setForceVectorOutput( true );
132  return true;
133 }
134 
136 {
137  // default implementation: recalculate position of the item
138  setRect( mRect, false );
139  setRotation( mMapCanvas->rotation() - mRectRotation );
140 }
QgsRectangle::height
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
Definition: qgsrectangle.h:230
QgsPointXY::y
double y
Definition: qgspointxy.h:63
QgsMapToPixel::mapUnitsPerPixel
double mapUnitsPerPixel() const
Returns the current map units per pixel.
Definition: qgsmaptopixel.h:229
qgsmapcanvas.h
QgsMapCanvasItem::mMapCanvas
QgsMapCanvas * mMapCanvas
pointer to map canvas
Definition: qgsmapcanvasitem.h:82
qgsmaptopixel.h
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:437
QgsMapCanvas::antiAliasingEnabled
bool antiAliasingEnabled() const
true if antialiasing is enabled
Definition: qgsmapcanvas.cpp:324
QgsMapCanvasItem::setRenderContextVariables
bool setRenderContextVariables(QPainter *p, QgsRenderContext &context) const
Sets render context parameters.
Definition: qgsmapcanvasitem.cpp:119
QgsRenderContext::setPainter
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
Definition: qgsrendercontext.h:512
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsMapCanvas::scale
double scale() const
Returns the last reported scale of the canvas.
Definition: qgsmapcanvas.cpp:367
QgsMapCanvasItem::mRect
QgsRectangle mRect
cached canvas item rectangle in map coordinates encodes position (xmin,ymax) and size (width/height) ...
Definition: qgsmapcanvasitem.h:94
QgsMapToPixel::toMapCoordinates
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
Definition: qgsmaptopixel.h:173
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsMapCanvasItem::mRectRotation
double mRectRotation
Definition: qgsmapcanvasitem.h:96
QgsMapCanvasItem::setRect
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
Definition: qgsmapcanvasitem.cpp:74
QgsMapCanvasItem::updatePosition
virtual void updatePosition()
called on changed extent or resize event to update position of the item
Definition: qgsmapcanvasitem.cpp:135
QgsMapCanvas::rotation
double rotation() const
Gets the current map canvas rotation in clockwise degrees.
Definition: qgsmapcanvas.cpp:1472
QgsMapCanvasItem::paint
virtual void paint(QPainter *painter)=0
function to be implemented by derived classes
qgsmapcanvasitem.h
QgsMapCanvasItem::toCanvasCoordinates
QPointF toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
Definition: qgsmapcanvasitem.cpp:61
QgsMapCanvasItem::QgsMapCanvasItem
QgsMapCanvasItem(QgsMapCanvas *mapCanvas)
protected constructor: cannot be constructed directly
Definition: qgsmapcanvasitem.cpp:28
qgsrendercontext.h
QgsRectangle::xMinimum
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
QgsMapCanvas::getCoordinateTransform
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
Definition: qgsmapcanvas.cpp:379
QgsRenderContext::setRendererScale
void setRendererScale(double scale)
Sets the renderer map scale.
Definition: qgsrendercontext.h:487
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsRenderContext::setForceVectorOutput
void setForceVectorOutput(bool force)
Sets whether rendering operations should use vector operations instead of any faster raster shortcuts...
Definition: qgsrendercontext.cpp:330
QgsMapToPixel::transformInPlace
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
Definition: qgsmaptopixel.h:128
QgsRectangle::yMaximum
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:193
QgsMapCanvasItem::~QgsMapCanvasItem
~QgsMapCanvasItem() override
Definition: qgsmapcanvasitem.cpp:37
QgsMapCanvasItem::rect
QgsRectangle rect() const
returns canvas item rectangle in map units
Definition: qgsmapcanvasitem.cpp:68
QgsMapCanvasItem::mItemSize
QSizeF mItemSize
cached size of the item (to return in boundingRect())
Definition: qgsmapcanvasitem.h:99
QgsMapToPixel
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:38
QgsRectangle::width
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Definition: qgsrectangle.h:223
QgsPointXY::x
double x
Definition: qgspointxy.h:62
QgsMapCanvasItem::boundingRect
QRectF boundingRect() const override
Definition: qgsmapcanvasitem.cpp:106
qgslogger.h
QgsMapSettings::outputDpi
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
Definition: qgsmapsettings.cpp:267
QgsMapCanvasItem::toMapCoordinates
QgsPointXY toMapCoordinates(QPoint point) const
transformation from screen coordinates to map coordinates
Definition: qgsmapcanvasitem.cpp:55
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
QgsMapCanvasItem::updateCanvas
void updateCanvas()
schedules map canvas for repaint
Definition: qgsmapcanvasitem.cpp:112
QgsRectangle::isEmpty
bool isEmpty() const
Returns true if the rectangle is empty.
Definition: qgsrectangle.h:469
QgsRenderContext::setScaleFactor
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
Definition: qgsrendercontext.h:472