QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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()
31  , mMapCanvas( mapCanvas )
32  , mPanningOffset( 0, 0 )
33  , mItemSize( 0, 0 )
34 {
35  Q_ASSERT( mapCanvas && mapCanvas->scene() );
36  mapCanvas->scene()->addItem( this );
37 }
38 
40 {
41  update(); // schedule redraw of canvas
42 }
43 
44 void QgsMapCanvasItem::paint( QPainter * painter,
45  const QStyleOptionGraphicsItem * option,
46  QWidget * widget )
47 {
48  Q_UNUSED( option );
49  Q_UNUSED( widget );
51  {
52  painter->setRenderHint( QPainter::Antialiasing );
53  }
54  paint( painter ); // call the derived item's drawing routines
55 }
56 
58 {
60 }
61 
62 
64 {
65  double x = point.x(), y = point.y();
67  return QPointF( x, y ) + mPanningOffset;
68 }
69 
70 // private
71 QRectF QgsMapCanvasItem::toCanvasCoordinates( const QRectF& rect )
72 {
73  QPointF tl( toCanvasCoordinates( rect.topLeft() ) );
74  QPointF bl( toCanvasCoordinates( rect.bottomLeft() ) );
75  QPointF br( toCanvasCoordinates( rect.bottomRight() ) );
76  QPointF tr( toCanvasCoordinates( rect.topRight() ) );
77  double xmin = std::min( tl.x(), std::min( bl.x(), std::min( br.x(), tr.x() ) ) );
78  double ymin = std::min( tl.y(), std::min( bl.y(), std::min( br.y(), tr.y() ) ) );
79  double xmax = std::max( tl.x(), std::max( bl.x(), std::max( br.x(), tr.x() ) ) );
80  double ymax = std::max( tl.y(), std::max( bl.y(), std::max( br.y(), tr.y() ) ) );
81  return QRectF( QPointF( xmin, ymin ), QPointF( xmax, ymax ) );
82 }
83 
84 
86 {
87  return mRect;
88 }
89 
90 
92 {
93  mRect = rect;
94  //updatePosition();
95 
96  QRectF r; // empty rect by default
97  if ( !mRect.isEmpty() )
98  {
100  r = r.normalized();
101  }
102 
103  // set position in canvas where the item will have coordinate (0,0)
104  prepareGeometryChange();
105  setPos( r.topLeft() ); // TODO: compute from (0,0) using toMapCoordinates ?
106  mItemSize = QSizeF( r.width() + 2, r.height() + 2 );
107 
108  // QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height()));
109 
110  update();
111 }
112 
114 {
115  return QRectF( QPointF( -1, -1 ), mItemSize );
116 }
117 
118 
120 {
121  update();
122  // porting: update below should not be needed anymore
123  //mMapCanvas->scene()->update(); //Contents();
124 }
125 
127 {
128  if ( !mMapCanvas || !p )
129  {
130  return false;
131  }
132  const QgsMapSettings& ms = mMapCanvas->mapSettings();
133 
134  context.setPainter( p );
135  context.setRendererScale( mMapCanvas->scale() );
136  context.setScaleFactor( ms.outputDpi() / 25.4 );
137  context.setRasterScaleFactor( 1.0 );
138 
139  context.setForceVectorOutput( true );
140  return true;
141 }
142 
144 {
145  // default implementation: recalculate position of the item
146  setRect( mRect );
147 }
148 
149 
150 void QgsMapCanvasItem::setPanningOffset( const QPoint& point )
151 {
152  mPanningOffset = point;
153 }
void setForceVectorOutput(bool force)
A rectangle specified with double values.
Definition: qgsrectangle.h:35
bool isEmpty() const
test if rectangle is empty.
QRectF toRectF() const
returns a QRectF with same coordinates.
virtual QRectF boundingRect() const
default implementation for canvas items
void transformInPlace(qreal &x, qreal &y) const
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:334
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:104
double x() const
Definition: qgspoint.h:126
The QgsMapSettings class contains configuration for rendering of the map.
QSizeF mItemSize
cached size of the item (to return in boundingRect())
int outputDpi() const
Return DPI used for conversion between real world units (e.g.
void setScaleFactor(double factor)
double scale()
Get the last reported scale of the canvas.
QgsRectangle mRect
canvas item rectangle (in map coordinates)
void setPainter(QPainter *p)
A class to represent a point.
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
int min(int a, int b)
Definition: util.h:93
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:134
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
int max(int a, int b)
Definition: util.h:87
bool setRenderContextVariables(QPainter *p, QgsRenderContext &context) const
Sets render context parameters.
#define tr(sourceText)