QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsmapcanvasmap.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmapcanvasmap.cpp - draws the map in map canvas
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#include "qgsmapcanvasmap.h"
17
18#include "qgslogger.h"
19#include "qgsmapcanvas.h"
20#include "qgsmaplayer.h"
21#include "qgsmaprendererjob.h"
22#include "qgsmapsettings.h"
23
24#include <QPainter>
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
30
31QgsMapCanvasMap::QgsMapCanvasMap( QgsMapCanvas *canvas )
32 : QgsMapCanvasItem( canvas )
33{
34 setZValue( -10 );
35}
36
37void QgsMapCanvasMap::setContent( const QImage &image, const QgsRectangle &rect )
38{
39 mPreviewImages.clear();
40
41 mImage = image;
42
43 // For true retro fans: this is approximately how the graphics looked like in 1990
44 if ( mMapCanvas->property( "retro" ).toBool() )
45 mImage = mImage.scaled( mImage.width() / 3, mImage.height() / 3 ).convertToFormat( QImage::Format_Indexed8, Qt::OrderedDither | Qt::OrderedAlphaDither );
46
47 setRect( rect );
48}
49
50void QgsMapCanvasMap::addPreviewImage( const QImage &image, const QPolygonF &visiblePolygon )
51{
52 mPreviewImages.append( qMakePair( image, visiblePolygon ) );
53 update();
54}
55
56QRectF QgsMapCanvasMap::boundingRect() const
57{
58 const double width = mItemSize.width();
59 const double height = mItemSize.height();
60
61 return QRectF( -width, -height, 3 * width, 3 * height );
62}
63
64void QgsMapCanvasMap::paint( QPainter *painter )
65{
66 // setRect() makes the size +2 :-(
67 const int w = std::round( mItemSize.width() ) - 2;
68 const int h = std::round( mItemSize.height() ) - 2;
69
70 bool scale = false;
71 if ( mImage.size() != QSize( w, h ) * mImage.devicePixelRatioF() )
72 {
73 QgsDebugMsgLevel( u"map paint DIFFERENT SIZE: img %1,%2 item %3,%4"_s.arg( mImage.width() / mImage.devicePixelRatioF() ).arg( mImage.height() / mImage.devicePixelRatioF() ).arg( w ).arg( h ), 2 );
74 // This happens on zoom events when ::paint is called before
75 // the renderer has completed
76 scale = true;
77 }
78
79 /*Offset between 0/0 and mRect.xMinimum/mRect.yMinimum.
80 We need to consider the offset, because mRect is not updated yet and there might be an offset*/
81 const QgsPointXY pt = toMapCoordinates( QPoint( 0, 0 ) );
82 const double offsetX = pt.x() - mRect.xMinimum();
83 const double offsetY = pt.y() - mRect.yMaximum();
84
85 //draw preview images first
86 QList<QPair<QImage, QPolygonF>>::const_iterator imIt = mPreviewImages.constBegin();
87 for ( ; imIt != mPreviewImages.constEnd(); ++imIt )
88 {
89 const QPointF mapTopLeft = imIt->second.at( 0 );
90 const QPointF mapBottomRight = imIt->second.at( 2 );
91 const QPointF canvasTopLeft = toCanvasCoordinates( QgsPoint( mapTopLeft.x() + offsetX, mapTopLeft.y() + offsetY ) );
92 const QPointF canvasBottomRight = toCanvasCoordinates( QgsPoint( mapBottomRight.x() + offsetX, mapBottomRight.y() + offsetY ) );
93 painter->drawImage( QRectF( canvasTopLeft, canvasBottomRight ), imIt->first, QRect( 0, 0, imIt->first.width(), imIt->first.height() ) );
94 }
95
96 if ( scale )
97 painter->drawImage( QRect( 0, 0, w, h ), mImage );
98 else
99 painter->drawImage( 0, 0, mImage );
100
101 // For debugging:
102#if 0
103 QRectF br = boundingRect();
104 QPointF c = br.center();
105 double rad = std::max( br.width(), br.height() ) / 10;
106 painter->drawRoundedRect( br, rad, rad );
107 painter->drawLine( QLineF( 0, 0, br.width(), br.height() ) );
108 painter->drawLine( QLineF( br.width(), 0, 0, br.height() ) );
109
110 double nw = br.width() * 0.5;
111 double nh = br.height() * 0.5;
112 br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
113 painter->drawRoundedRect( br, rad, rad );
114
115 nw = br.width() * 0.5;
116 nh = br.height() * 0.5;
117 br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
118 painter->drawRoundedRect( br, rad, rad );
119#endif
120}
121
An abstract class for items that can be placed on the map canvas.
Map canvas is a class for displaying all GIS data types on a canvas.
Represents a 2D point.
Definition qgspointxy.h:62
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
A rectangle specified with double values.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63