Quantum GIS API Documentation  master-693a1fe
src/core/raster/qgsrasterdrawer.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                          qgsrasterdrawer.cpp
00003                          ---------------------
00004     begin                : June 2012
00005     copyright            : (C) 2012 by Radim Blazek
00006     email                : radim dot blazek at gmail.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "qgslogger.h"
00019 #include "qgsrasterdrawer.h"
00020 #include "qgsrasteriterator.h"
00021 #include "qgsrasterviewport.h"
00022 #include "qgsmaptopixel.h"
00023 #include <QImage>
00024 #include <QPainter>
00025 
00026 QgsRasterDrawer::QgsRasterDrawer( QgsRasterIterator* iterator ): mIterator( iterator )
00027 {
00028 }
00029 
00030 QgsRasterDrawer::~QgsRasterDrawer()
00031 {
00032 }
00033 
00034 void QgsRasterDrawer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel )
00035 {
00036   QgsDebugMsg( "Entered" );
00037   if ( !p || !mIterator || !viewPort || !theQgsMapToPixel )
00038   {
00039     return;
00040   }
00041 
00042   // last pipe filter has only 1 band
00043   int bandNumber = 1;
00044   mIterator->startRasterRead( bandNumber, viewPort->mWidth, viewPort->mHeight, viewPort->mDrawnExtent );
00045 
00046   //number of cols/rows in output pixels
00047   int nCols = 0;
00048   int nRows = 0;
00049   //shift to top left point for the raster part
00050   int topLeftCol = 0;
00051   int topLeftRow = 0;
00052 
00053   // We know that the output data type of last pipe filter is QImage data
00054 
00055   QgsRasterBlock *block;
00056 
00057   // readNextRasterPart calcs and resets  nCols, nRows, topLeftCol, topLeftRow
00058   while ( mIterator->readNextRasterPart( bandNumber, nCols, nRows,
00059                                          &block, topLeftCol, topLeftRow ) )
00060   {
00061     if ( !block )
00062     {
00063       QgsDebugMsg( "Cannot get block" );
00064       continue;
00065     }
00066 
00067     QImage img = block->image();
00068 
00069     drawImage( p, viewPort, img, topLeftCol, topLeftRow );
00070 
00071     delete block;
00072   }
00073 }
00074 
00075 void QgsRasterDrawer::drawImage( QPainter* p, QgsRasterViewPort* viewPort, const QImage& img, int topLeftCol, int topLeftRow ) const
00076 {
00077   if ( !p || !viewPort )
00078   {
00079     return;
00080   }
00081 
00082   //top left position in device coords
00083   QPoint tlPoint = QPoint( viewPort->mTopLeftPoint.x() + topLeftCol, viewPort->mTopLeftPoint.y() + topLeftRow );
00084   p->save();
00085   p->setRenderHint( QPainter::Antialiasing, false );
00086   p->drawImage( tlPoint, img );
00087   p->restore();
00088 }
00089 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines