QGIS API Documentation  2.8.2-Wien
 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()
31  , mMapCanvas( mapCanvas )
32  , mRectRotation( 0.0 )
33  , mPanningOffset( 0, 0 )
34  , mItemSize( 0, 0 )
35 {
36  Q_ASSERT( mapCanvas && mapCanvas->scene() );
37  mapCanvas->scene()->addItem( this );
38 }
39 
41 {
42  update(); // schedule redraw of canvas
43 }
44 
45 void QgsMapCanvasItem::paint( QPainter * painter,
46  const QStyleOptionGraphicsItem * option,
47  QWidget * widget )
48 {
49  Q_UNUSED( option );
50  Q_UNUSED( widget );
52  {
53  painter->setRenderHint( QPainter::Antialiasing );
54  }
55  paint( painter ); // call the derived item's drawing routines
56 }
57 
58 QgsPoint QgsMapCanvasItem::toMapCoordinates( const QPoint& point ) const
59 {
61 }
62 
63 
64 QPointF QgsMapCanvasItem::toCanvasCoordinates( const QgsPoint& point ) const
65 {
66  double x = point.x(), y = point.y();
68  return QPointF( x, y ) + mPanningOffset;
69 }
70 
72 {
73  return mRect;
74 }
75 
76 
77 void QgsMapCanvasItem::setRect( const QgsRectangle& rect, bool resetRotation )
78 {
79  mRect = rect;
80  //updatePosition();
81 
82  QRectF r; // empty rect by default
83  if ( !mRect.isEmpty() )
84  {
85  // rect encodes origin of the item (xMin,yMax from map to canvas units)
86  // and size (rect size / map units per pixel)
87  r.setTopLeft( toCanvasCoordinates( QPointF( mRect.xMinimum(), mRect.yMaximum() ) ) );
89  double res = m2p->mapUnitsPerPixel();
90  r.setSize( QSizeF( mRect.width() / res, mRect.height() / res ) );
91  }
92 
93  // set position in canvas where the item will have coordinate (0,0)
94  prepareGeometryChange();
95  setPos( r.topLeft() );
96  mItemSize = QSizeF( r.width() + 2, r.height() + 2 );
97 
98  if ( resetRotation )
99  {
101  setRotation( 0 );
102  }
103 
104  // QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height()));
105 
106  update();
107 }
108 
110 {
111  return QRectF( QPointF( -1, -1 ), mItemSize );
112 }
113 
114 
116 {
117  update();
118  // porting: update below should not be needed anymore
119  //mMapCanvas->scene()->update(); //Contents();
120 }
121 
123 {
124  if ( !mMapCanvas || !p )
125  {
126  return false;
127  }
128  const QgsMapSettings& ms = mMapCanvas->mapSettings();
129 
130  context.setPainter( p );
131  context.setRendererScale( mMapCanvas->scale() );
132  context.setScaleFactor( ms.outputDpi() / 25.4 );
133  context.setRasterScaleFactor( 1.0 );
134 
135  context.setForceVectorOutput( true );
136  return true;
137 }
138 
140 {
141  // default implementation: recalculate position of the item
142  setRect( mRect, false );
143  setRotation( mMapCanvas->rotation() - mRectRotation );
144 }
145 
146 
147 void QgsMapCanvasItem::setPanningOffset( const QPoint& point )
148 {
149  mPanningOffset = point;
150 }