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 }