Navigation Menu

Skip to content

Commit

Permalink
add QgsRasterLayer::previewAsImage() for MTR rendering (see bug 9626)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennesky committed Feb 24, 2014
1 parent 115f980 commit 75a2edb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
8 changes: 7 additions & 1 deletion python/core/raster/qgsrasterlayer.sip
Expand Up @@ -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();

Expand Down
51 changes: 51 additions & 0 deletions src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -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();
Expand Down
8 changes: 7 additions & 1 deletion src/core/raster/qgsrasterlayer.h
Expand Up @@ -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();

Expand Down

0 comments on commit 75a2edb

Please sign in to comment.