QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsmaprenderer.h"
20 #include "qgsmaptopixel.h"
21 #include "qgsrendercontext.h"
22 #include <QGraphicsScene>
23 #include <QRect>
24 #include <QPen>
25 #include <QBrush>
26 #include <QPainter>
27 #include "qgslogger.h"
28 
30  : QGraphicsItem()
31  , mMapCanvas( mapCanvas )
32  , mRectRotation( 0.0 )
33  , mPanningOffset( 0, 0 )
34  , mItemSize( 0, 0 )
35 {
36  Q_ASSERT( mapCanvas && mapCanvas->scene() );
37  mapCanvas->scene()->addItem( this );
38 }
39 
41 {
42  update(); // schedule redraw of canvas
43 }
44 
46  const QStyleOptionGraphicsItem * option,
47  QWidget * widget )
48 {
49  Q_UNUSED( option );
50  Q_UNUSED( widget );
52  {
53  painter->setRenderHint( QPainter::Antialiasing );
54  }
55  paint( painter ); // call the derived item's drawing routines
56 }
57 
59 {
61 }
62 
63 
65 {
66  qreal x = point.x(), y = point.y();
68  return QPointF( x, y ) + mPanningOffset;
69 }
70 
72 {
73  return mRect;
74 }
75 
76 
77 void QgsMapCanvasItem::setRect( const QgsRectangle& rect, bool resetRotation )
78 {
79  mRect = rect;
80  //updatePosition();
81 
82  QRectF r; // empty rect by default
83  if ( !mRect.isEmpty() )
84  {
85  // rect encodes origin of the item (xMin,yMax from map to canvas units)
86  // and size (rect size / map units per pixel)
89  double res = m2p->mapUnitsPerPixel();
90  r.setSize( QSizeF( mRect.width() / res, mRect.height() / res ) );
91  }
92 
93  // set position in canvas where the item will have coordinate (0,0)
95  setPos( r.topLeft() );
96  mItemSize = QSizeF( r.width() + 2, r.height() + 2 );
97 
98  if ( resetRotation )
99  {
101  setRotation( 0 );
102  }
103 
104  // QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height()));
105 
106  update();
107 }
108 
110 {
111  return QRectF( QPointF( -1, -1 ), mItemSize );
112 }
113 
114 
116 {
117  update();
118  // porting: update below should not be needed anymore
119  //mMapCanvas->scene()->update(); //Contents();
120 }
121 
123 {
124  if ( !mMapCanvas || !p )
125  {
126  return false;
127  }
128  const QgsMapSettings& ms = mMapCanvas->mapSettings();
129 
130  context.setPainter( p );
131  context.setRendererScale( mMapCanvas->scale() );
132  context.setScaleFactor( ms.outputDpi() / 25.4 );
133  context.setRasterScaleFactor( 1.0 );
134 
135  context.setForceVectorOutput( true );
136  return true;
137 }
138 
140 {
141  // default implementation: recalculate position of the item
142  setRect( mRect, false );
144 }
145 
146 
148 {
149  mPanningOffset = point;
150 }
void setForceVectorOutput(bool force)
qreal x() const
qreal y() const
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QgsRectangle rect() const
returns canvas item rectangle in map units
void setRenderHint(RenderHint hint, bool on)
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
QPoint mPanningOffset
offset from normal position due current panning operation, used when converting map coordinates to mo...
QGraphicsScene * scene() const
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
void update(const QRectF &rect)
double y() const
Get the y value of the point.
Definition: qgspoint.h:193
The QgsMapSettings class contains configuration for rendering of the map.
Q_DECL_DEPRECATED void setPanningOffset(QPoint point)
sets current offset, to be called from QgsMapCanvas
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:34
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.
void setPos(const QPointF &pos)
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:207
QPointF topLeft() const
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:117
void prepareGeometryChange()
QgsMapCanvasItem(QgsMapCanvas *mapCanvas)
protected constructor: cannot be constructed directly
void setTopLeft(const QPointF &position)
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.
qreal width() const
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:372
QgsMapCanvas * mMapCanvas
pointer to map canvas
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:192
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
void setRotation(qreal angle)
qreal height() const
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:197
void setSize(const QSizeF &size)
void setRasterScaleFactor(double factor)
virtual ~QgsMapCanvasItem()
void addItem(QGraphicsItem *item)
virtual void updatePosition()
called on changed extent or resize event to update position of the item
double x() const
Get the x value of the point.
Definition: qgspoint.h:185
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:212
bool setRenderContextVariables(QPainter *p, QgsRenderContext &context) const
Sets render context parameters.