Quantum GIS API Documentation
1.7.4
|
00001 /*************************************************************************** 00002 qgsmapcanvasmap.cpp - draws the map in map canvas 00003 ---------------------- 00004 begin : February 2006 00005 copyright : (C) 2006 by Martin Dobias 00006 email : wonder.sk at gmail dot com 00007 *************************************************************************** 00008 * * 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; either version 2 of the License, or * 00012 * (at your option) any later version. * 00013 * * 00014 ***************************************************************************/ 00015 /* $Id$ */ 00016 00017 #include "qgslogger.h" 00018 #include "qgsmapcanvas.h" 00019 #include "qgsmapcanvasmap.h" 00020 #include "qgsmaprenderer.h" 00021 00022 #include <QPainter> 00023 00024 QgsMapCanvasMap::QgsMapCanvasMap( QgsMapCanvas* canvas ) 00025 : mCanvas( canvas ) 00026 { 00027 setZValue( -10 ); 00028 setPos( 0, 0 ); 00029 resize( QSize( 1, 1 ) ); 00030 mUseQImageToRender = false; 00031 } 00032 00033 void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidget* ) 00034 { 00035 //refreshes the canvas map with the current offscreen image 00036 p->drawPixmap( 0, 0, mPixmap ); 00037 } 00038 00039 QRectF QgsMapCanvasMap::boundingRect() const 00040 { 00041 return QRectF( 0, 0, mPixmap.width(), mPixmap.height() ); 00042 } 00043 00044 00045 void QgsMapCanvasMap::resize( QSize size ) 00046 { 00047 QgsDebugMsg( QString( "resizing to %1x%2" ).arg( size.width() ).arg( size.height() ) ); 00048 prepareGeometryChange(); // to keep QGraphicsScene indexes up to date on size change 00049 00050 mPixmap = QPixmap( size ); 00051 mPixmap.fill( mBgColor.rgb() ); 00052 mImage = QImage( size, QImage::Format_RGB32 ); // temporary image - build it here so it is available when switching from QPixmap to QImage rendering 00053 mCanvas->mapRenderer()->setOutputSize( size, mPixmap.logicalDpiX() ); 00054 } 00055 00056 void QgsMapCanvasMap::setPanningOffset( const QPoint& point ) 00057 { 00058 mOffset = point; 00059 setPos( mOffset ); 00060 } 00061 00062 void QgsMapCanvasMap::render() 00063 { 00064 if ( mUseQImageToRender ) 00065 { 00066 // use temporary image for rendering 00067 mImage.fill( mBgColor.rgb() ); 00068 00069 // clear the pixmap so that old map won't be displayed while rendering 00070 // TODO: do the canvas updates wisely -> this wouldn't be needed 00071 mPixmap = QPixmap( mImage.size() ); 00072 mPixmap.fill( mBgColor.rgb() ); 00073 00074 QPainter paint; 00075 paint.begin( &mImage ); 00076 // Clip drawing to the QImage 00077 paint.setClipRect( mImage.rect() ); 00078 00079 // antialiasing 00080 if ( mAntiAliasing ) 00081 paint.setRenderHint( QPainter::Antialiasing ); 00082 00083 mCanvas->mapRenderer()->render( &paint ); 00084 00085 paint.end(); 00086 00087 // convert QImage to QPixmap to acheive faster drawing on screen 00088 mPixmap = QPixmap::fromImage( mImage ); 00089 } 00090 else 00091 { 00092 mPixmap.fill( mBgColor.rgb() ); 00093 QPainter paint; 00094 paint.begin( &mPixmap ); 00095 // Clip our drawing to the QPixmap 00096 paint.setClipRect( mPixmap.rect() ); 00097 00098 // antialiasing 00099 if ( mAntiAliasing ) 00100 paint.setRenderHint( QPainter::Antialiasing ); 00101 00102 mCanvas->mapRenderer()->render( &paint ); 00103 paint.end(); 00104 } 00105 update(); 00106 } 00107 00108 QPaintDevice& QgsMapCanvasMap::paintDevice() 00109 { 00110 return mPixmap; 00111 } 00112 00113 void QgsMapCanvasMap::updateContents() 00114 { 00115 // make sure we're using current contents 00116 if ( mUseQImageToRender ) 00117 mPixmap = QPixmap::fromImage( mImage ); 00118 00119 // trigger update of this item 00120 update(); 00121 }