Quantum GIS API Documentation  1.8
src/gui/qgsmapcanvasmap.cpp
Go to the documentation of this file.
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 
00016 #include "qgslogger.h"
00017 #include "qgsmapcanvas.h"
00018 #include "qgsmapcanvasmap.h"
00019 #include "qgsmaprenderer.h"
00020 
00021 #include <QPainter>
00022 
00023 QgsMapCanvasMap::QgsMapCanvasMap( QgsMapCanvas* canvas )
00024     : mCanvas( canvas )
00025 {
00026   setZValue( -10 );
00027   setPos( 0, 0 );
00028   resize( QSize( 1, 1 ) );
00029   mUseQImageToRender = true;
00030 }
00031 
00032 void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidget* )
00033 {
00034   //refreshes the canvas map with the current offscreen image
00035   p->drawPixmap( 0, 0, mPixmap );
00036 }
00037 
00038 QRectF QgsMapCanvasMap::boundingRect() const
00039 {
00040   return QRectF( 0, 0, mPixmap.width(), mPixmap.height() );
00041 }
00042 
00043 
00044 void QgsMapCanvasMap::resize( QSize size )
00045 {
00046   QgsDebugMsg( QString( "resizing to %1x%2" ).arg( size.width() ).arg( size.height() ) );
00047   prepareGeometryChange(); // to keep QGraphicsScene indexes up to date on size change
00048 
00049   mPixmap = QPixmap( size );
00050   mPixmap.fill( mBgColor.rgb() );
00051   mImage = QImage( size, QImage::Format_RGB32 ); // temporary image - build it here so it is available when switching from QPixmap to QImage rendering
00052   mCanvas->mapRenderer()->setOutputSize( size, mPixmap.logicalDpiX() );
00053 }
00054 
00055 void QgsMapCanvasMap::setPanningOffset( const QPoint& point )
00056 {
00057   mOffset = point;
00058   setPos( mOffset );
00059 }
00060 
00061 void QgsMapCanvasMap::render()
00062 {
00063   QgsDebugMsg( QString( "mUseQImageToRender = %1" ).arg( mUseQImageToRender ) );
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 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines