QGIS API Documentation  2.5.0-Master
 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  const QgsMapSettings& ms = mMapCanvas->mapSettings();
116 
117  context.setPainter( p );
118  context.setRendererScale( mMapCanvas->scale() );
119  context.setScaleFactor( ms.outputDpi() / 25.4 );
120  context.setRasterScaleFactor( 1.0 );
121 
122  context.setForceVectorOutput( true );
123  return true;
124 }
125 
127 {
128  // default implementation: recalculate position of the item
129  setRect( mRect );
130 }
131 
132 
133 void QgsMapCanvasItem::setPanningOffset( const QPoint& point )
134 {
135  mPanningOffset = point;
136 }
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
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:194
void setRendererScale(double scale)
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
Q_DECL_DEPRECATED 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...
QgsRectangle rect() const
returns canvas item rectangle
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:319
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:104
double x() const
Definition: qgspoint.h:110
The QgsMapSettings class contains configuration for rendering of the map.
QSizeF mItemSize
cached size of the item (to return in boundingRect())
void transformInPlace(double &x, double &y) const
int outputDpi() const
Return DPI used for conversion between real world units (e.g.
void setScaleFactor(double factor)
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:199
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:184
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
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
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:189
bool setRenderContextVariables(QPainter *p, QgsRenderContext &context) const
Sets render context parameters.