/
qgsmapcanvasmap.cpp
92 lines (79 loc) · 3.06 KB
/
qgsmapcanvasmap.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
/***************************************************************************
qgsmapcanvasmap.cpp - draws the map in map canvas
----------------------
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 "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmapcanvasmap.h"
#include "qgsmaprenderer.h"
#include "qgsmapsettings.h"
#include "qgsmaplayer.h"
#include <QPainter>
QgsMapCanvasMap::QgsMapCanvasMap( QgsMapCanvas* canvas )
: QgsMapCanvasItem( canvas )
{
setZValue( -10 );
}
QgsMapCanvasMap::~QgsMapCanvasMap()
{
}
void QgsMapCanvasMap::setContent( const QImage& image, const QgsRectangle& rect )
{
mImage = image;
// For true retro fans: this is approximately how the graphics looked like in 1990
if ( mMapCanvas->property( "retro" ).toBool() )
mImage = mImage.scaled( mImage.width() / 3, mImage.height() / 3 )
.convertToFormat( QImage::Format_Indexed8, Qt::OrderedDither | Qt::OrderedAlphaDither );
setRect( rect );
}
void QgsMapCanvasMap::paint( QPainter* painter )
{
int w = qRound( boundingRect().width() ) - 2, h = qRound( boundingRect().height() ) - 2; // setRect() makes the size +2 :-(
if ( mImage.size() != QSize( w, h ) )
{
QgsDebugMsg( QString( "map paint DIFFERENT SIZE: img %1,%2 item %3,%4" ).arg( mImage.width() ).arg( mImage.height() ).arg( w ).arg( h ) );
}
if ( mMapCanvas->getCoordinateTransform()->mapRotation() )
{
int tX = ( w - mImage.width() ) / 2.0;
int tY = ( h - mImage.height() ) / 2.0;
int fX = 0;
int fY = 0;
int fW = w;
int fH = h;
painter->drawImage( tX, tY, mImage, fX, fY, fW, fH );
}
else
{
painter->drawImage( QRect( 0, 0, w, h ), mImage );
}
// For debugging:
#if 0
QRectF br = boundingRect();
QPointF c = br.center();
double rad = std::max( br.width(), br.height() ) / 10;
painter->drawRoundedRect( br, rad, rad );
painter->drawLine( QLineF( 0, 0, br.width(), br.height() ) );
painter->drawLine( QLineF( br.width(), 0, 0, br.height() ) );
double nw = br.width() * 0.5; double nh = br.height() * 0.5;
br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
painter->drawRoundedRect( br, rad, rad );
nw = br.width() * 0.5; nh = br.height() * 0.5;
br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
painter->drawRoundedRect( br, rad, rad );
#endif
}
QPaintDevice& QgsMapCanvasMap::paintDevice()
{
return mImage;
}