/
qgsmapcanvasmap.cpp
114 lines (97 loc) · 3.01 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/***************************************************************************
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. *
* *
***************************************************************************/
/* $Id$ */
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmapcanvasmap.h"
#include "qgsmaprenderer.h"
#include <QPainter>
QgsMapCanvasMap::QgsMapCanvasMap(QgsMapCanvas* canvas)
: mCanvas(canvas)
{
setZValue(-10);
setPos(0,0);
resize(QSize(1,1));
mUseQImageToRender = false;
}
void QgsMapCanvasMap::paint(QPainter* p, const QStyleOptionGraphicsItem*, QWidget*)
{
//refreshes the canvas map with the current offscreen image
if (mUseQImageToRender)
{
mPixmap = QPixmap::fromImage(mImage);
}
p->drawPixmap(0,0, mPixmap);
}
QRectF QgsMapCanvasMap::boundingRect() const
{
return QRectF(0,0,mPixmap.width(),mPixmap.height());
}
void QgsMapCanvasMap::resize(QSize size)
{
mPixmap = QPixmap(size);
mCanvas->mapRenderer()->setOutputSize(size, mPixmap.logicalDpiX());
}
void QgsMapCanvasMap::setPanningOffset(const QPoint& point)
{
mOffset = point;
setPos(mOffset);
}
void QgsMapCanvasMap::render()
{
// Rendering to a QImage gives incorrectly filled polygons in some
// cases (as at Qt4.1.4), but it is the only renderer that supports
// anti-aliasing, so we provide the means to swap between QImage and
// QPixmap.
if (mUseQImageToRender)
{
// use temporary image for rendering
mImage = QImage(boundingRect().size().toSize(), QImage::Format_RGB32);
mImage.fill(mBgColor.rgb());
QPainter paint;
paint.begin(&mImage);
// Clip drawing to the QImage
paint.setClipRect(mImage.rect());
// antialiasing
if (mAntiAliasing)
paint.setRenderHint(QPainter::Antialiasing);
mCanvas->mapRenderer()->render(&paint);
paint.end();
// convert QImage to QPixmap to acheive faster drawing on screen
//mPixmap = QPixmap::fromImage(image);
}
else
{
mPixmap.fill(mBgColor.rgb());
QPainter paint;
paint.begin(&mPixmap);
// Clip our drawing to the QPixmap
paint.setClipRect(mPixmap.rect());
mCanvas->mapRenderer()->render(&paint);
paint.end();
}
update();
}
QPaintDevice& QgsMapCanvasMap::paintDevice()
{
if (mUseQImageToRender)
{
return mImage;
}
else
{
return mPixmap;
}
}