/
qgsmapcanvasitem.cpp
142 lines (118 loc) · 4.08 KB
/
qgsmapcanvasitem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/***************************************************************************
qgsmapcanvasitem.h - base class for map canvas items
----------------------
begin : February 2006
copyright : (C) 2006 by Martin Dobias
email : wonder.sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsmapcanvasitem.h"
#include "qgsmapcanvas.h"
#include "qgsmaprenderer.h"
#include "qgsmaptopixel.h"
#include "qgsrendercontext.h"
#include <QGraphicsScene>
#include <QRect>
#include <QPen>
#include <QBrush>
#include <QPainter>
#include "qgslogger.h"
QgsMapCanvasItem::QgsMapCanvasItem( QgsMapCanvas* mapCanvas )
: QGraphicsItem()
, mMapCanvas( mapCanvas )
, mPanningOffset( 0, 0 )
, mItemSize( 0, 0 )
{
Q_ASSERT( mapCanvas && mapCanvas->scene() );
mapCanvas->scene()->addItem( this );
}
QgsMapCanvasItem::~QgsMapCanvasItem()
{
update(); // schedule redraw of canvas
}
void QgsMapCanvasItem::paint( QPainter * painter,
const QStyleOptionGraphicsItem * option,
QWidget * widget )
{
Q_UNUSED( option );
Q_UNUSED( widget );
if ( mMapCanvas->antiAliasingEnabled() )
{
painter->setRenderHint( QPainter::Antialiasing );
}
paint( painter ); // call the derived item's drawing routines
}
QgsPoint QgsMapCanvasItem::toMapCoordinates( const QPoint& point )
{
return mMapCanvas->getCoordinateTransform()->toMapCoordinates( point - mPanningOffset );
}
QPointF QgsMapCanvasItem::toCanvasCoordinates( const QgsPoint& point )
{
double x = point.x(), y = point.y();
mMapCanvas->getCoordinateTransform()->transformInPlace( x, y );
return QPointF( x, y ) + mPanningOffset;
}
QgsRectangle QgsMapCanvasItem::rect() const
{
return mRect;
}
void QgsMapCanvasItem::setRect( const QgsRectangle& rect )
{
mRect = rect;
//updatePosition();
QRectF r; // empty rect by default
if ( !mRect.isEmpty() )
{
// rect encodes origin of the item (xMin,yMax from map to canvas units)
// and size (rect size / map units per pixel)
r.setTopLeft( toCanvasCoordinates( QPointF(mRect.xMinimum(), mRect.yMaximum()) ) );
const QgsMapToPixel* m2p = mMapCanvas->getCoordinateTransform();
double res = m2p->mapUnitsPerPixel();
r.setSize( QSizeF(mRect.width()/res, mRect.height()/res) );
}
// set position in canvas where the item will have coordinate (0,0)
prepareGeometryChange();
setPos( r.topLeft() );
mItemSize = QSizeF( r.width() + 2, r.height() + 2 );
// QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height()));
update();
}
QRectF QgsMapCanvasItem::boundingRect() const
{
return QRectF( QPointF( -1, -1 ), mItemSize );
}
void QgsMapCanvasItem::updateCanvas()
{
update();
// porting: update below should not be needed anymore
//mMapCanvas->scene()->update(); //Contents();
}
bool QgsMapCanvasItem::setRenderContextVariables( QPainter* p, QgsRenderContext& context ) const
{
if ( !mMapCanvas || !p )
{
return false;
}
const QgsMapSettings& ms = mMapCanvas->mapSettings();
context.setPainter( p );
context.setRendererScale( mMapCanvas->scale() );
context.setScaleFactor( ms.outputDpi() / 25.4 );
context.setRasterScaleFactor( 1.0 );
context.setForceVectorOutput( true );
return true;
}
void QgsMapCanvasItem::updatePosition()
{
// default implementation: recalculate position of the item
setRect( mRect );
}
void QgsMapCanvasItem::setPanningOffset( const QPoint& point )
{
mPanningOffset = point;
}