QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 "qgsmaprenderer.h"
20 #include "qgsmapsettings.h"
21 #include "qgsmaplayer.h"
22 
23 #include <QPainter>
24 
26  : QgsMapCanvasItem( canvas )
27 {
28  setZValue( -10 );
29 }
30 
32 {
33 }
34 
35 void QgsMapCanvasMap::setContent( const QImage& image, const QgsRectangle& rect )
36 {
37  mImage = image;
38 
39  // For true retro fans: this is approximately how the graphics looked like in 1990
40  if ( mMapCanvas->property( "retro" ).toBool() )
41  mImage = mImage.scaled( mImage.width() / 3, mImage.height() / 3 )
42  .convertToFormat( QImage::Format_Indexed8, Qt::OrderedDither | Qt::OrderedAlphaDither );
43 
44  setRect( rect );
45 }
46 
47 void QgsMapCanvasMap::paint( QPainter* painter )
48 {
49  int w = qRound( boundingRect().width() ) - 2, h = qRound( boundingRect().height() ) - 2; // setRect() makes the size +2 :-(
50  if ( mImage.size() != QSize( w, h ) )
51  {
52  QgsDebugMsg( QString( "map paint DIFFERENT SIZE: img %1,%2 item %3,%4" ).arg( mImage.width() ).arg( mImage.height() ).arg( w ).arg( h ) );
53  // This happens on zoom events when ::paint is called before
54  // the renderer has completed
55  }
56 
57  painter->drawImage( QRect( 0, 0, w, h ), mImage );
58 
59  // For debugging:
60 #if 0
61  QRectF br = boundingRect();
62  QPointF c = br.center();
63  double rad = std::max( br.width(), br.height() ) / 10;
64  painter->drawRoundedRect( br, rad, rad );
65  painter->drawLine( QLineF( 0, 0, br.width(), br.height() ) );
66  painter->drawLine( QLineF( br.width(), 0, 0, br.height() ) );
67 
68  double nw = br.width() * 0.5; double nh = br.height() * 0.5;
69  br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
70  painter->drawRoundedRect( br, rad, rad );
71 
72  nw = br.width() * 0.5; nh = br.height() * 0.5;
73  br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
74  painter->drawRoundedRect( br, rad, rad );
75 #endif
76 }
77 
79 {
80  return mImage;
81 }
A rectangle specified with double values.
Definition: qgsrectangle.h:35
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
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:105
virtual QRectF boundingRect() const override
default implementation for canvas items
Q_DECL_DEPRECATED QPaintDevice & paintDevice()
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
virtual void paint(QPainter *painter) override
function to be implemented by derived classes
void setContent(const QImage &image, const QgsRectangle &rect)
QgsMapCanvas * mMapCanvas
pointer to map canvas
QgsMapCanvasMap(QgsMapCanvas *canvas)
constructor
int max(int a, int b)
Definition: util.h:87