Skip to content
Permalink
Browse files

add QgsRasterLayer::previewAsImage() for MTR rendering (see bug 9626)

  • Loading branch information
etiennesky committed Feb 24, 2014
1 parent 115f980 commit 75a2edb3e934df4c9f925dd2d0045f98828fe569
Showing with 65 additions and 2 deletions.
  1. +7 −1 python/core/raster/qgsrasterlayer.sip
  2. +51 −0 src/core/raster/qgsrasterlayer.cpp
  3. +7 −1 src/core/raster/qgsrasterlayer.h
@@ -188,9 +188,15 @@ class QgsRasterLayer : QgsMapLayer
/** \brief Returns the sublayers of this layer - Useful for providers that manage their own layers, such as WMS */
virtual QStringList subLayers() const;

/** \brief Draws a preview of the rasterlayer into a pixmap */
/** \brief Draws a preview of the rasterlayer into a pixmap
@note - use previewAsImage() for rendering with QGIS>=2.4 */
QPixmap previewAsPixmap( QSize size, QColor bgColor = QColor( 255, 255, 255 ) );

/** \brief Draws a preview of the rasterlayer into a QImage
@note added in 2.4 */
QImage previewAsImage( QSize size, QColor bgColor = QColor( 255, 255, 255 ),
QImage::Format format = QImage::Format_ARGB32_Premultiplied );

/** \brief Emit a signal asking for a repaint. (inherited from maplayer) */
void triggerRepaint();

@@ -1144,6 +1144,57 @@ QPixmap QgsRasterLayer::previewAsPixmap( QSize size, QColor bgColor )
return myQPixmap;
}

// this function should be used when rendering with the MTR engine introduced in 2.3, as QPixmap is not thread safe (see bug #9626)
// note: previewAsImage and previewAsPixmap should use a common low-level fct QgsRasterLayer::previewOnPaintDevice( QSize size, QColor bgColor, QPaintDevice &device )
QImage QgsRasterLayer::previewAsImage( QSize size, QColor bgColor, QImage::Format format )
{
QImage myQImage( size, format );

myQImage.fill( bgColor ); //defaults to white, set to transparent for rendering on a map

QgsRasterViewPort *myRasterViewPort = new QgsRasterViewPort();

double myMapUnitsPerPixel;
double myX = 0.0;
double myY = 0.0;
QgsRectangle myExtent = mDataProvider->extent();
if ( myExtent.width() / myExtent.height() >= myQImage.width() / myQImage.height() )
{
myMapUnitsPerPixel = myExtent.width() / myQImage.width();
myY = ( myQImage.height() - myExtent.height() / myMapUnitsPerPixel ) / 2;
}
else
{
myMapUnitsPerPixel = myExtent.height() / myQImage.height();
myX = ( myQImage.width() - myExtent.width() / myMapUnitsPerPixel ) / 2;
}

double myPixelWidth = myExtent.width() / myMapUnitsPerPixel;
double myPixelHeight = myExtent.height() / myMapUnitsPerPixel;

myRasterViewPort->mTopLeftPoint = QgsPoint( myX, myY );
myRasterViewPort->mBottomRightPoint = QgsPoint( myPixelWidth, myPixelHeight );
myRasterViewPort->mWidth = myQImage.width();
myRasterViewPort->mHeight = myQImage.height();

myRasterViewPort->mDrawnExtent = myExtent;
myRasterViewPort->mSrcCRS = QgsCoordinateReferenceSystem(); // will be invalid
myRasterViewPort->mDestCRS = QgsCoordinateReferenceSystem(); // will be invalid
myRasterViewPort->mSrcDatumTransform = -1;
myRasterViewPort->mDestDatumTransform = -1;

QgsMapToPixel *myMapToPixel = new QgsMapToPixel( myMapUnitsPerPixel );

QPainter * myQPainter = new QPainter( &myQImage );
draw( myQPainter, myRasterViewPort, myMapToPixel );
delete myRasterViewPort;
delete myMapToPixel;
myQPainter->end();
delete myQPainter;

return myQImage;
}

void QgsRasterLayer::triggerRepaint()
{
emit repaintRequested();
@@ -346,9 +346,15 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
/** \brief Returns the sublayers of this layer - Useful for providers that manage their own layers, such as WMS */
virtual QStringList subLayers() const;

/** \brief Draws a preview of the rasterlayer into a pixmap */
/** \brief Draws a preview of the rasterlayer into a pixmap
@note - use previewAsImage() for rendering with QGIS>=2.4 */
QPixmap previewAsPixmap( QSize size, QColor bgColor = Qt::white );

/** \brief Draws a preview of the rasterlayer into a QImage
@note added in 2.4 */
QImage previewAsImage( QSize size, QColor bgColor = Qt::white,
QImage::Format format = QImage::Format_ARGB32_Premultiplied );

/** \brief Emit a signal asking for a repaint. (inherited from maplayer) */
void triggerRepaint();

0 comments on commit 75a2edb

Please sign in to comment.
You can’t perform that action at this time.