Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 /* $Id$ */
16 
17 
18 #include "qgsmapcanvasitem.h"
19 #include "qgsmapcanvas.h"
20 #include "qgsmaprenderer.h"
21 #include "qgsmaptopixel.h"
22 #include "qgsrendercontext.h"
23 #include <QGraphicsScene>
24 #include <QRect>
25 #include <QPen>
26 #include <QBrush>
27 #include <QPainter>
28 #include "qgslogger.h"
29 
31  : QGraphicsItem( 0, mapCanvas->scene() ), mMapCanvas( mapCanvas ),
32  mPanningOffset( 0, 0 ), mItemSize( 0, 0 )
33 {
34 }
35 
37 {
38  update(); // schedule redraw of canvas
39 }
40 
41 void QgsMapCanvasItem::paint( QPainter * painter,
42  const QStyleOptionGraphicsItem * option,
43  QWidget * widget )
44 {
46  {
47  painter->setRenderHint( QPainter::Antialiasing );
48  }
49  paint( painter ); // call the derived item's drawing routines
50 }
51 
53 {
55 }
56 
57 
59 {
60  double x = point.x(), y = point.y();
62  return QPointF( x, y ) + mPanningOffset;
63 }
64 
65 
67 {
68  return mRect;
69 }
70 
71 
73 {
74  mRect = rect;
75  //updatePosition();
76 
77  QRectF r; // empty rect by default
78  if ( !mRect.isEmpty() )
79  {
80  r.setTopLeft( toCanvasCoordinates( QgsPoint( mRect.xMinimum(), mRect.yMinimum() ) ) );
81  r.setBottomRight( toCanvasCoordinates( QgsPoint( mRect.xMaximum(), mRect.yMaximum() ) ) );
82  r = r.normalized();
83  }
84 
85  // set position in canvas where the item will have coordinate (0,0)
86  prepareGeometryChange();
87  setPos( r.topLeft() );
88  mItemSize = QSizeF( r.width() + 2, r.height() + 2 );
89 
90  // QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height()));
91 
92  update();
93 }
94 
96 {
97  return QRectF( QPointF( -1, -1 ), mItemSize );
98 }
99 
100 
102 {
103  update();
104  // porting: update below should not be needed anymore
105  //mMapCanvas->scene()->update(); //Contents();
106 }
107 
109 {
110  if ( !mMapCanvas || !p )
111  {
112  return false;
113  }
114  QgsMapRenderer* mapRenderer = mMapCanvas->mapRenderer();
115  if ( !mapRenderer )
116  {
117  return false;
118  }
119 
120  context.setPainter( p );
121  context.setRendererScale( mMapCanvas->scale() );
122 
123  int dpi = mapRenderer->outputDpi();
124  int painterDpi = p->device()->logicalDpiX();
125  double scaleFactor = 1.0;
126  double rasterScaleFactor = 1.0;
127 
128  //little trick to find out if painting origines from composer or main map canvas
129  if ( data( 0 ).toString() == "composer" )
130  {
131  rasterScaleFactor = painterDpi / 25.4;
132  scaleFactor = dpi / 25.4;
133  }
134  else
135  {
136  if ( mapRenderer->outputUnits() == QgsMapRenderer::Millimeters )
137  {
138  scaleFactor = dpi / 25.4;
139  }
140  }
141  context.setScaleFactor( scaleFactor );
142  context.setRasterScaleFactor( rasterScaleFactor );
143  return true;
144 }
145 
147 {
148  // default implementation: recalculate position of the item
149  setRect( mRect );
150 }
151 
152 
153 void QgsMapCanvasItem::setPanningOffset( const QPoint& point )
154 {
155  mPanningOffset = point;
156 }