QGIS API Documentation 3.99.0-Master (09f76ad7019)
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 )
46 .convertToFormat( QImage::Format_Indexed8, Qt::OrderedDither | Qt::OrderedAlphaDither );
47
48 setRect( rect );
49}
50
51void QgsMapCanvasMap::addPreviewImage( const QImage &image, const QPolygonF &visiblePolygon )
52{
53 mPreviewImages.append( qMakePair( image, visiblePolygon ) );
54 update();
55}
56
57QRectF QgsMapCanvasMap::boundingRect() const
58{
59 const double width = mItemSize.width();
60 const double height = mItemSize.height();
61
62 return QRectF( -width, -height, 3 * width, 3 * height );
63}
64
65void QgsMapCanvasMap::paint( QPainter *painter )
66{
67 // setRect() makes the size +2 :-(
68 const int w = std::round( mItemSize.width() ) - 2;
69 const int h = std::round( mItemSize.height() ) - 2;
70
71 bool scale = false;
72 if ( mImage.size() != QSize( w, h ) * mImage.devicePixelRatioF() )
73 {
74 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 );
75 // This happens on zoom events when ::paint is called before
76 // the renderer has completed
77 scale = true;
78 }
79
80 /*Offset between 0/0 and mRect.xMinimum/mRect.yMinimum.
81 We need to consider the offset, because mRect is not updated yet and there might be an offset*/
82 const QgsPointXY pt = toMapCoordinates( QPoint( 0, 0 ) );
83 const double offsetX = pt.x() - mRect.xMinimum();
84 const double offsetY = pt.y() - mRect.yMaximum();
85
86 //draw preview images first
87 QList<QPair<QImage, QPolygonF>>::const_iterator imIt = mPreviewImages.constBegin();
88 for ( ; imIt != mPreviewImages.constEnd(); ++imIt )
89 {
90 const QPointF mapTopLeft = imIt->second.at( 0 );
91 const QPointF mapBottomRight = imIt->second.at( 2 );
92 const QPointF canvasTopLeft = toCanvasCoordinates( QgsPoint( mapTopLeft.x() + offsetX, mapTopLeft.y() + offsetY ) );
93 const QPointF canvasBottomRight = toCanvasCoordinates( QgsPoint( mapBottomRight.x() + offsetX, mapBottomRight.y() + offsetY ) );
94 painter->drawImage( QRectF( canvasTopLeft, canvasBottomRight ), imIt->first, QRect( 0, 0, imIt->first.width(), imIt->first.height() ) );
95 }
96
97 if ( scale )
98 painter->drawImage( QRect( 0, 0, w, h ), mImage );
99 else
100 painter->drawImage( 0, 0, mImage );
101
102 // For debugging:
103#if 0
104 QRectF br = boundingRect();
105 QPointF c = br.center();
106 double rad = std::max( br.width(), br.height() ) / 10;
107 painter->drawRoundedRect( br, rad, rad );
108 painter->drawLine( QLineF( 0, 0, br.width(), br.height() ) );
109 painter->drawLine( QLineF( br.width(), 0, 0, br.height() ) );
110
111 double nw = br.width() * 0.5;
112 double nh = br.height() * 0.5;
113 br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
114 painter->drawRoundedRect( br, rad, rad );
115
116 nw = br.width() * 0.5;
117 nh = br.height() * 0.5;
118 br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
119 painter->drawRoundedRect( br, rad, rad );
120#endif
121}
122
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