QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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( 0, mapCanvas->scene() ), mMapCanvas( mapCanvas ),
31  mPanningOffset( 0, 0 ), mItemSize( 0, 0 )
32 {
33 }
34 
36 {
37  update(); // schedule redraw of canvas
38 }
39 
40 void QgsMapCanvasItem::paint( QPainter * painter,
41  const QStyleOptionGraphicsItem * option,
42  QWidget * widget )
43 {
44  Q_UNUSED( option );
45  Q_UNUSED( widget );
47  {
48  painter->setRenderHint( QPainter::Antialiasing );
49  }
50  paint( painter ); // call the derived item's drawing routines
51 }
52 
54 {
56 }
57 
58 
60 {
61  double x = point.x(), y = point.y();
63  return QPointF( x, y ) + mPanningOffset;
64 }
65 
66 
68 {
69  return mRect;
70 }
71 
72 
74 {
75  mRect = rect;
76  //updatePosition();
77 
78  QRectF r; // empty rect by default
79  if ( !mRect.isEmpty() )
80  {
81  r.setTopLeft( toCanvasCoordinates( QgsPoint( mRect.xMinimum(), mRect.yMinimum() ) ) );
82  r.setBottomRight( toCanvasCoordinates( QgsPoint( mRect.xMaximum(), mRect.yMaximum() ) ) );
83  r = r.normalized();
84  }
85 
86  // set position in canvas where the item will have coordinate (0,0)
87  prepareGeometryChange();
88  setPos( r.topLeft() );
89  mItemSize = QSizeF( r.width() + 2, r.height() + 2 );
90 
91  // QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height()));
92 
93  update();
94 }
95 
97 {
98  return QRectF( QPointF( -1, -1 ), mItemSize );
99 }
100 
101 
103 {
104  update();
105  // porting: update below should not be needed anymore
106  //mMapCanvas->scene()->update(); //Contents();
107 }
108 
110 {
111  if ( !mMapCanvas || !p )
112  {
113  return false;
114  }
115  QgsMapRenderer* mapRenderer = mMapCanvas->mapRenderer();
116  if ( !mapRenderer )
117  {
118  return false;
119  }
120 
121  context.setPainter( p );
122  context.setRendererScale( mMapCanvas->scale() );
123 
124  int dpi = mapRenderer->outputDpi();
125  int painterDpi = p->device()->logicalDpiX();
126  double scaleFactor = 1.0;
127  double rasterScaleFactor = 1.0;
128 
129  //little trick to find out if painting origines from composer or main map canvas
130  if ( data( 1 ).toString() == "composer" )
131  {
132  rasterScaleFactor = painterDpi / 25.4;
133  scaleFactor = dpi / 25.4;
134 
135  context.setForceVectorOutput( true );
136  }
137  else
138  {
139  if ( mapRenderer->outputUnits() == QgsMapRenderer::Millimeters )
140  {
141  scaleFactor = dpi / 25.4;
142  }
143  }
144  context.setScaleFactor( scaleFactor );
145  context.setRasterScaleFactor( rasterScaleFactor );
146  return true;
147 }
148 
150 {
151  // default implementation: recalculate position of the item
152  setRect( mRect );
153 }
154 
155 
156 void QgsMapCanvasItem::setPanningOffset( const QPoint& point )
157 {
158  mPanningOffset = point;
159 }
void setForceVectorOutput(bool force)
Added in QGIS v1.5.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
bool isEmpty() const
test if rectangle is empty
virtual QRectF boundingRect() const
default implementation for canvas items
OutputUnits outputUnits() const
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:189
void setRendererScale(double scale)
void setPanningOffset(const QPoint &point)
sets current offset, to be called from QgsMapCanvas
QPoint mPanningOffset
offset from normal position due current panning operation, used when converting map coordinates to mo...
double outputDpi()
accessor for output dpi
A non GUI class for rendering a map layer set onto a QPainter.
QgsRectangle rect() const
returns canvas item rectangle
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:253
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:99
double x() const
Definition: qgspoint.h:110
QSizeF mItemSize
cached size of the item (to return in boundingRect())
void transformInPlace(double &x, double &y) const
void setScaleFactor(double factor)
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:194
double scale()
Get the last reported scale of the canvas.
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:179
QgsRectangle mRect
canvas item rectangle (in map coordinates)
void setPainter(QPainter *p)
A class to represent a point geometry.
Definition: qgspoint.h:63
QPointF toCanvasCoordinates(const QgsPoint &point)
transformation from map coordinates to screen coordinates
double rasterScaleFactor
Definition: qgssvgcache.cpp:80
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
QgsPoint toMapCoordinates(int x, int y) const
Contains information about the context of a rendering operation.
QgsMapCanvas * mMapCanvas
pointer to map canvas
QgsMapRenderer * mapRenderer()
void setRect(const QgsRectangle &r)
sets canvas item rectangle
const QgsMapToPixel * getCoordinateTransform()
Get the current coordinate transform.
double y() const
Definition: qgspoint.h:118
void setRasterScaleFactor(double factor)
QgsPoint toMapCoordinates(const QPoint &point)
transformation from screen coordinates to map coordinates
virtual ~QgsMapCanvasItem()
virtual void updatePosition()
called on changed extent or resize event to update position of the item
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:184
bool setRenderContextVariables(QPainter *p, QgsRenderContext &context) const
Sets render context parameters.