QGIS API Documentation  2.99.0-Master (8ec3eaf)
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  : QGraphicsItem()
30  , mMapCanvas( mapCanvas )
31  , mRectRotation( 0.0 )
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 
43 void QgsMapCanvasItem::paint( QPainter * painter,
44  const QStyleOptionGraphicsItem * option,
45  QWidget * widget )
46 {
47  Q_UNUSED( option );
48  Q_UNUSED( widget );
50  {
51  painter->setRenderHint( QPainter::Antialiasing );
52  }
53  paint( painter ); // call the derived item's drawing routines
54 }
55 
57 {
59 }
60 
61 
62 QPointF QgsMapCanvasItem::toCanvasCoordinates( const QgsPoint& point ) const
63 {
64  qreal x = point.x(), y = point.y();
66  return QPointF( x, y );
67 }
68 
70 {
71  return mRect;
72 }
73 
74 
75 void QgsMapCanvasItem::setRect( const QgsRectangle& rect, bool resetRotation )
76 {
77  mRect = rect;
78  //updatePosition();
79 
80  QRectF r; // empty rect by default
81  if ( !mRect.isEmpty() )
82  {
83  // rect encodes origin of the item (xMin,yMax from map to canvas units)
84  // and size (rect size / map units per pixel)
85  r.setTopLeft( toCanvasCoordinates( QPointF( mRect.xMinimum(), mRect.yMaximum() ) ) );
87  double res = m2p->mapUnitsPerPixel();
88  r.setSize( QSizeF( mRect.width() / res, mRect.height() / res ) );
89  }
90 
91  // set position in canvas where the item will have coordinate (0,0)
92  prepareGeometryChange();
93  setPos( r.topLeft() );
94  mItemSize = QSizeF( r.width() + 2, r.height() + 2 );
95 
96  if ( resetRotation )
97  {
99  setRotation( 0 );
100  }
101 
102  // QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height()));
103 
104  update();
105 }
106 
108 {
109  return QRectF( QPointF( -1, -1 ), mItemSize );
110 }
111 
112 
114 {
115  update();
116  // porting: update below should not be needed anymore
117  //mMapCanvas->scene()->update(); //Contents();
118 }
119 
121 {
122  if ( !mMapCanvas || !p )
123  {
124  return false;
125  }
126  const QgsMapSettings& ms = mMapCanvas->mapSettings();
127 
128  context.setPainter( p );
129  context.setRendererScale( mMapCanvas->scale() );
130  context.setScaleFactor( ms.outputDpi() / 25.4 );
131  context.setRasterScaleFactor( 1.0 );
132 
133  context.setForceVectorOutput( true );
134  return true;
135 }
136 
138 {
139  // default implementation: recalculate position of the item
140  setRect( mRect, false );
141  setRotation( mMapCanvas->rotation() - mRectRotation );
142 }
void setForceVectorOutput(bool force)
A rectangle specified with double values.
Definition: qgsrectangle.h:35
double y
Definition: qgspoint.h:147
QgsRectangle rect() const
returns canvas item rectangle in map units
QgsPoint toMapCoordinates(QPoint point) const
transformation from screen coordinates to map coordinates
double rotation() const
Get the current map canvas rotation in clockwise degrees.
void setRendererScale(double scale)
QgsPoint toMapCoordinates(int x, int y) const
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:106
The QgsMapSettings class contains configuration for rendering of the map.
QPointF toCanvasCoordinates(const QgsPoint &point) const
transformation from map coordinates to screen coordinates
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
QSizeF mItemSize
cached size of the item (to return in boundingRect())
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:33
virtual QRectF boundingRect() const override
default implementation for canvas items
void setScaleFactor(double factor)
double scale()
Get the last reported scale of the canvas.
bool isEmpty() const
test if rectangle is empty.
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:211
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
QgsRectangle mRect
cached canvas item rectangle in map coordinates encodes position (xmin,ymax) and size (width/height) ...
void setPainter(QPainter *p)
double mapUnitsPerPixel() const
Return current map units per pixel.
A class to represent a point.
Definition: qgspoint.h:142
QgsMapCanvasItem(QgsMapCanvas *mapCanvas)
protected constructor: cannot be constructed directly
void updateCanvas()
schedules map canvas for repaint
virtual void paint(QPainter *painter)=0
function to be implemented by derived classes
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
double outputDpi() const
Return DPI used for conversion between real world units (e.g.
Contains information about the context of a rendering operation.
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:337
QgsMapCanvas * mMapCanvas
pointer to map canvas
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:196
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:201
void setRasterScaleFactor(double factor)
virtual ~QgsMapCanvasItem()
virtual void updatePosition()
called on changed extent or resize event to update position of the item
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:216
bool setRenderContextVariables(QPainter *p, QgsRenderContext &context) const
Sets render context parameters.
double x
Definition: qgspoint.h:146