QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsrasterdrawer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterdrawer.cpp
3  ---------------------
4  begin : June 2012
5  copyright : (C) 2012 by Radim Blazek
6  email : radim dot blazek at gmail.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgslogger.h"
19 #include "qgsrasterdrawer.h"
20 #include "qgsrasteriterator.h"
21 #include "qgsrasterviewport.h"
22 #include "qgsmaptopixel.h"
23 #include <QImage>
24 #include <QPainter>
25 
26 QgsRasterDrawer::QgsRasterDrawer( QgsRasterIterator* iterator ): mIterator( iterator )
27 {
28 }
29 
31 {
32 }
33 
34 void QgsRasterDrawer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel )
35 {
36  QgsDebugMsg( "Entered" );
37  if ( !p || !mIterator || !viewPort || !theQgsMapToPixel )
38  {
39  return;
40  }
41 
42  // last pipe filter has only 1 band
43  int bandNumber = 1;
44  mIterator->startRasterRead( bandNumber, viewPort->mWidth, viewPort->mHeight, viewPort->mDrawnExtent );
45 
46  //number of cols/rows in output pixels
47  int nCols = 0;
48  int nRows = 0;
49  //shift to top left point for the raster part
50  int topLeftCol = 0;
51  int topLeftRow = 0;
52 
53  // We know that the output data type of last pipe filter is QImage data
54 
55  QgsRasterBlock *block;
56 
57  // readNextRasterPart calcs and resets nCols, nRows, topLeftCol, topLeftRow
58  while ( mIterator->readNextRasterPart( bandNumber, nCols, nRows,
59  &block, topLeftCol, topLeftRow ) )
60  {
61  if ( !block )
62  {
63  QgsDebugMsg( "Cannot get block" );
64  continue;
65  }
66 
67  QImage img = block->image();
68 
69  drawImage( p, viewPort, img, topLeftCol, topLeftRow );
70 
71  delete block;
72  }
73 }
74 
75 void QgsRasterDrawer::drawImage( QPainter* p, QgsRasterViewPort* viewPort, const QImage& img, int topLeftCol, int topLeftRow ) const
76 {
77  if ( !p || !viewPort )
78  {
79  return;
80  }
81 
82  //top left position in device coords
83  QPoint tlPoint = QPoint( viewPort->mTopLeftPoint.x() + topLeftCol, viewPort->mTopLeftPoint.y() + topLeftRow );
84  p->save();
85  p->setRenderHint( QPainter::Antialiasing, false );
86 
87  // Blending problem was reported with PDF output if background color has alpha < 255
88  // in #7766, it seems to be a bug in Qt, setting a brush with alpha 255 is a workaround
89  // which should not harm anything
90  p->setBrush( QBrush( QColor( Qt::white ), Qt::NoBrush ) );
91 
92  p->drawImage( tlPoint, img );
93  p->restore();
94 }
95