QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
21 #include <QPainter>
22 
24  : mCanvas( canvas )
25 {
26  setZValue( -10 );
27  setPos( 0, 0 );
28  resize( QSize( 1, 1 ) );
29  mUseQImageToRender = true;
30 }
31 
32 void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidget* )
33 {
34  //refreshes the canvas map with the current offscreen image
35  p->drawPixmap( 0, 0, mPixmap );
36 }
37 
39 {
40  return QRectF( 0, 0, mPixmap.width(), mPixmap.height() );
41 }
42 
43 
45 {
46  QgsDebugMsg( QString( "resizing to %1x%2" ).arg( size.width() ).arg( size.height() ) );
47  prepareGeometryChange(); // to keep QGraphicsScene indexes up to date on size change
48 
49  mPixmap = QPixmap( size );
50  mPixmap.fill( mBgColor.rgb() );
51  mImage = QImage( size, QImage::Format_RGB32 ); // temporary image - build it here so it is available when switching from QPixmap to QImage rendering
52  mCanvas->mapRenderer()->setOutputSize( size, mPixmap.logicalDpiX() );
53 }
54 
55 void QgsMapCanvasMap::setPanningOffset( const QPoint& point )
56 {
57  mOffset = point;
58  setPos( mOffset );
59 }
60 
62 {
63  QgsDebugMsg( QString( "mUseQImageToRender = %1" ).arg( mUseQImageToRender ) );
64  if ( mUseQImageToRender )
65  {
66  // use temporary image for rendering
67  mImage.fill( mBgColor.rgb() );
68 
69  // clear the pixmap so that old map won't be displayed while rendering
70  // TODO: do the canvas updates wisely -> this wouldn't be needed
71  mPixmap = QPixmap( mImage.size() );
72  mPixmap.fill( mBgColor.rgb() );
73 
74  QPainter paint;
75  paint.begin( &mImage );
76  // Clip drawing to the QImage
77  paint.setClipRect( mImage.rect() );
78 
79  // antialiasing
80  if ( mAntiAliasing )
81  paint.setRenderHint( QPainter::Antialiasing );
82 
83  mCanvas->mapRenderer()->render( &paint );
84 
85  paint.end();
86 
87  // convert QImage to QPixmap to achieve faster drawing on screen
88  mPixmap = QPixmap::fromImage( mImage );
89  }
90  else
91  {
92  mPixmap.fill( mBgColor.rgb() );
93  QPainter paint;
94  paint.begin( &mPixmap );
95  // Clip our drawing to the QPixmap
96  paint.setClipRect( mPixmap.rect() );
97 
98  // antialiasing
99  if ( mAntiAliasing )
100  paint.setRenderHint( QPainter::Antialiasing );
101 
102  mCanvas->mapRenderer()->render( &paint );
103  paint.end();
104  }
105  update();
106 }
107 
109 {
110  return mPixmap;
111 }
112 
114 {
115  // make sure we're using current contents
116  if ( mUseQImageToRender )
117  mPixmap = QPixmap::fromImage( mImage );
118 
119  // trigger update of this item
120  update();
121 }