QGIS API Documentation  2.99.0-Master (0a63d1f)
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 "qgslogger.h"
17 #include "qgsmapcanvas.h"
18 #include "qgsmapcanvasmap.h"
19 #include "qgsmapsettings.h"
20 
21 #include <QPainter>
22 
24 
25 QgsMapCanvasMap::QgsMapCanvasMap( QgsMapCanvas* canvas )
26  : QgsMapCanvasItem( canvas )
27 {
28  setZValue( -10 );
29 }
30 
31 void QgsMapCanvasMap::setContent( const QImage& image, const QgsRectangle& rect )
32 {
33  mImage = image;
34 
35  // For true retro fans: this is approximately how the graphics looked like in 1990
36  if ( mMapCanvas->property( "retro" ).toBool() )
37  mImage = mImage.scaled( mImage.width() / 3, mImage.height() / 3 )
38  .convertToFormat( QImage::Format_Indexed8, Qt::OrderedDither | Qt::OrderedAlphaDither );
39 
40  setRect( rect );
41 }
42 
43 void QgsMapCanvasMap::paint( QPainter* painter )
44 {
45  int w = qRound( boundingRect().width() ) - 2, h = qRound( boundingRect().height() ) - 2; // setRect() makes the size +2 :-(
46  if ( mImage.size() != QSize( w, h ) )
47  {
48  QgsDebugMsg( QString( "map paint DIFFERENT SIZE: img %1,%2 item %3,%4" ).arg( mImage.width() ).arg( mImage.height() ).arg( w ).arg( h ) );
49  // This happens on zoom events when ::paint is called before
50  // the renderer has completed
51  }
52 
53  painter->drawImage( QRect( 0, 0, w, h ), mImage );
54 
55  // For debugging:
56 #if 0
57  QRectF br = boundingRect();
58  QPointF c = br.center();
59  double rad = std::max( br.width(), br.height() ) / 10;
60  painter->drawRoundedRect( br, rad, rad );
61  painter->drawLine( QLineF( 0, 0, br.width(), br.height() ) );
62  painter->drawLine( QLineF( br.width(), 0, 0, br.height() ) );
63 
64  double nw = br.width() * 0.5;
65  double nh = br.height() * 0.5;
66  br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
67  painter->drawRoundedRect( br, rad, rad );
68 
69  nw = br.width() * 0.5;
70  nh = br.height() * 0.5;
71  br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
72  painter->drawRoundedRect( br, rad, rad );
73 #endif
74 }
75 
A rectangle specified with double values.
Definition: qgsrectangle.h:36
QgsRectangle rect() const
returns canvas item rectangle in map units
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
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.
Definition: qgsmapcanvas.h:74
virtual QRectF boundingRect() const override
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
double ANALYSIS_EXPORT max(double x, double y)
Returns the maximum of two doubles or the first argument if both are equal.
Definition: MathUtils.cc:437
QgsMapCanvas * mMapCanvas
pointer to map canvas