QGIS API Documentation  2.7.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  int tX = ( w - mImage.width() ) / 2.0;
54  int tY = ( h - mImage.height() ) / 2.0;
55  int fX = 0;
56  int fY = 0;
57  int fW = w;
58  int fH = h;
59  painter->drawImage( tX, tY, mImage, fX, fY, fW, fH );
60  }
61  else
62  {
63  painter->drawImage( QRect( 0, 0, w, h ), mImage );
64  }
65 
66  // For debugging:
67 #if 0
68  QRectF br = boundingRect();
69  QPointF c = br.center();
70  double rad = std::max( br.width(), br.height() ) / 10;
71  painter->drawRoundedRect( br, rad, rad );
72  painter->drawLine( QLineF( 0, 0, br.width(), br.height() ) );
73  painter->drawLine( QLineF( br.width(), 0, 0, br.height() ) );
74 
75  double nw = br.width() * 0.5; double nh = br.height() * 0.5;
76  br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
77  painter->drawRoundedRect( br, rad, rad );
78 
79  nw = br.width() * 0.5; nh = br.height() * 0.5;
80  br = QRectF( c - QPointF( nw / 2, nh / 2 ), QSize( nw, nh ) );
81  painter->drawRoundedRect( br, rad, rad );
82 #endif
83 }
84 
86 {
87  return mImage;
88 }
A rectangle specified with double values.
Definition: qgsrectangle.h:35
virtual QRectF boundingRect() const
default implementation for canvas items
#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:104
Q_DECL_DEPRECATED QPaintDevice & paintDevice()
virtual void paint(QPainter *painter)
function to be implemented by derived classes
void setContent(const QImage &image, const QgsRectangle &rect)
QgsMapCanvas * mMapCanvas
pointer to map canvas
void setRect(const QgsRectangle &r)
sets canvas item rectangle
QgsMapCanvasMap(QgsMapCanvas *canvas)
constructor
int max(int a, int b)
Definition: util.h:87