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 }