Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 /* $Id$ */
16 
17 #include "qgslogger.h"
18 #include "qgsmapcanvas.h"
19 #include "qgsmapcanvasmap.h"
20 #include "qgsmaprenderer.h"
21 
22 #include <QPainter>
23 
25  : mCanvas( canvas )
26 {
27  setZValue( -10 );
28  setPos( 0, 0 );
29  resize( QSize( 1, 1 ) );
30  mUseQImageToRender = false;
31 }
32 
33 void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidget* )
34 {
35  //refreshes the canvas map with the current offscreen image
36  p->drawPixmap( 0, 0, mPixmap );
37 }
38 
40 {
41  return QRectF( 0, 0, mPixmap.width(), mPixmap.height() );
42 }
43 
44 
45 void QgsMapCanvasMap::resize( QSize size )
46 {
47  QgsDebugMsg( QString( "resizing to %1x%2" ).arg( size.width() ).arg( size.height() ) );
48  prepareGeometryChange(); // to keep QGraphicsScene indexes up to date on size change
49 
50  mPixmap = QPixmap( size );
51  mPixmap.fill( mBgColor.rgb() );
52  mImage = QImage( size, QImage::Format_RGB32 ); // temporary image - build it here so it is available when switching from QPixmap to QImage rendering
53  mCanvas->mapRenderer()->setOutputSize( size, mPixmap.logicalDpiX() );
54 }
55 
56 void QgsMapCanvasMap::setPanningOffset( const QPoint& point )
57 {
58  mOffset = point;
59  setPos( mOffset );
60 }
61 
63 {
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 acheive 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 }