Skip to content

Commit 777ce59

Browse files
committed
replace QgsRasterLayer::thumbnailAsPixmap() with previewAsPixmap() : return QPixmap and add size and background color parameters ; remove QgsRasterLayer::thumbnailAsImage() as it's not used
1 parent 3d326cc commit 777ce59

File tree

5 files changed

+84
-25
lines changed

5 files changed

+84
-25
lines changed

python/core/raster/qgsrasterlayer.sip

+4-1
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,13 @@ class QgsRasterLayer : QgsMapLayer
475475
/** \brief Draws a thumbnail of the rasterlayer into the supplied pixmap pointer */
476476
void thumbnailAsPixmap( QPixmap * theQPixmap );
477477

478+
/** \brief Draws a preview of the rasterlayer into a pixmap */
479+
QPixmap previewAsPixmap( QSize size, QColor bgColor = QColor( 255, 255, 255 ) );
480+
478481
/** \brief Draws a thumbnail of the rasterlayer into the supplied QImage pointer
479482
* @note added in QGIS 1.6
480483
* */
481-
void thumbnailAsImage( QImage * thepImage );
484+
/* void thumbnailAsImage( QImage * thepImage ); */
482485

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

src/app/legend/qgslegendlayer.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,7 @@ QPixmap QgsLegendLayer::getOriginalPixmap()
388388
if ( s.value( "/qgis/createRasterLegendIcons", true ).toBool() )
389389
{
390390
QgsRasterLayer* rlayer = qobject_cast<QgsRasterLayer *>( theLayer );
391-
QPixmap myPixmap( 32, 32 );
392-
rlayer->thumbnailAsPixmap( &myPixmap );
393-
return myPixmap;
391+
return rlayer->previewAsPixmap( QSize( 32, 32 ) );
394392
}
395393
else
396394
{

src/app/qgsrasterlayerproperties.cpp

+16-17
Original file line numberDiff line numberDiff line change
@@ -626,19 +626,19 @@ void QgsRasterLayerProperties::sync()
626626
}
627627

628628
//get the thumbnail for the layer
629-
QPixmap myQPixmap = QPixmap( pixmapThumbnail->width(), pixmapThumbnail->height() );
630-
mRasterLayer->thumbnailAsPixmap( &myQPixmap );
631-
pixmapThumbnail->setPixmap( myQPixmap );
629+
pixmapThumbnail->setPixmap( mRasterLayer->previewAsPixmap( pixmapThumbnail->size() ) );
630+
631+
// TODO fix legend + palette pixmap
632632

633633
//update the legend pixmap on this dialog
634634
//pixmapLegend->setPixmap( mRasterLayer->legendAsPixmap() );
635-
pixmapLegend->setScaledContents( true );
636-
pixmapLegend->repaint();
635+
// pixmapLegend->setScaledContents( true );
636+
// pixmapLegend->repaint();
637637

638638
//set the palette pixmap
639-
//pixmapPalette->setPixmap( mRasterLayer->paletteAsPixmap( mRasterLayer->bandNumber( mRasterLayer->grayBandName() ) ) );
640-
pixmapPalette->setScaledContents( true );
641-
pixmapPalette->repaint();
639+
// pixmapPalette->setPixmap( mRasterLayer->paletteAsPixmap( mRasterLayer->bandNumber( mRasterLayer->grayBandName() ) ) );
640+
// pixmapPalette->setScaledContents( true );
641+
// pixmapPalette->repaint();
642642

643643
QgsDebugMsg( "populate metadata tab" );
644644
/*
@@ -761,9 +761,9 @@ void QgsRasterLayerProperties::apply()
761761
mRasterLayer->setMaximumScale( 1.0 / cbMaximumScale->scale() );
762762

763763
//update the legend pixmap
764-
pixmapLegend->setPixmap( mRasterLayer->legendAsPixmap() );
765-
pixmapLegend->setScaledContents( true );
766-
pixmapLegend->repaint();
764+
// pixmapLegend->setPixmap( mRasterLayer->legendAsPixmap() );
765+
// pixmapLegend->setScaledContents( true );
766+
// pixmapLegend->repaint();
767767

768768
QgsRasterResampleFilter* resampleFilter = mRasterLayer->resampleFilter();
769769

@@ -803,9 +803,7 @@ void QgsRasterLayerProperties::apply()
803803

804804

805805
//get the thumbnail for the layer
806-
QPixmap myQPixmap = QPixmap( pixmapThumbnail->width(), pixmapThumbnail->height() );
807-
mRasterLayer->thumbnailAsPixmap( &myQPixmap );
808-
pixmapThumbnail->setPixmap( myQPixmap );
806+
pixmapThumbnail->setPixmap( mRasterLayer->previewAsPixmap( pixmapThumbnail->size() ) );
809807

810808
mRasterLayer->setTitle( mLayerTitleLineEdit->text() );
811809
mRasterLayer->setAbstract( mLayerAbstractTextEdit->toPlainText() );
@@ -914,9 +912,10 @@ void QgsRasterLayerProperties::on_buttonBuildPyramids_clicked()
914912
}
915913
}
916914
//update the legend pixmap
917-
pixmapLegend->setPixmap( mRasterLayer->legendAsPixmap() );
918-
pixmapLegend->setScaledContents( true );
919-
pixmapLegend->repaint();
915+
// pixmapLegend->setPixmap( mRasterLayer->legendAsPixmap() );
916+
// pixmapLegend->setScaledContents( true );
917+
// pixmapLegend->repaint();
918+
920919
//populate the metadata tab's text browser widget with gdal metadata info
921920
QString myStyle = QgsApplication::reportStyleSheet();
922921
txtbMetadata->setHtml( mRasterLayer->metadata() );

src/core/raster/qgsrasterlayer.cpp

+57-1
Original file line numberDiff line numberDiff line change
@@ -2321,7 +2321,7 @@ QStringList QgsRasterLayer::subLayers() const
23212321

23222322
void QgsRasterLayer::thumbnailAsPixmap( QPixmap * theQPixmap )
23232323
{
2324-
//TODO: This should be depreciated and a new function written that just returns a new QPixmap, it will be safer
2324+
//deprecated, use previewAsPixmap() instead
23252325
if ( !theQPixmap )
23262326
return;
23272327

@@ -2372,9 +2372,64 @@ void QgsRasterLayer::thumbnailAsPixmap( QPixmap * theQPixmap )
23722372
delete myQPainter;
23732373
}
23742374

2375+
QPixmap QgsRasterLayer::previewAsPixmap( QSize size, QColor bgColor )
2376+
{
2377+
QPixmap myQPixmap( size );
2378+
2379+
myQPixmap.fill( bgColor ); //defaults to white, set to transparent for rendering on a map
2380+
2381+
QgsRasterViewPort *myRasterViewPort = new QgsRasterViewPort();
2382+
2383+
double myMapUnitsPerPixel;
2384+
double myX = 0.0;
2385+
double myY = 0.0;
2386+
QgsRectangle myExtent = mDataProvider->extent();
2387+
if ( myExtent.width() / myExtent.height() >= myQPixmap.width() / myQPixmap.height() )
2388+
{
2389+
myMapUnitsPerPixel = myExtent.width() / myQPixmap.width();
2390+
myY = ( myQPixmap.height() - myExtent.height() / myMapUnitsPerPixel ) / 2;
2391+
}
2392+
else
2393+
{
2394+
myMapUnitsPerPixel = myExtent.height() / myQPixmap.height();
2395+
myX = ( myQPixmap.width() - myExtent.width() / myMapUnitsPerPixel ) / 2;
2396+
}
2397+
2398+
double myPixelWidth = myExtent.width() / myMapUnitsPerPixel;
2399+
double myPixelHeight = myExtent.height() / myMapUnitsPerPixel;
2400+
2401+
//myRasterViewPort->topLeftPoint = QgsPoint( 0, 0 );
2402+
myRasterViewPort->topLeftPoint = QgsPoint( myX, myY );
2403+
2404+
//myRasterViewPort->bottomRightPoint = QgsPoint( myQPixmap.width(), myQPixmap.height() );
2405+
2406+
myRasterViewPort->bottomRightPoint = QgsPoint( myPixelWidth, myPixelHeight );
2407+
myRasterViewPort->drawableAreaXDim = myQPixmap.width();
2408+
myRasterViewPort->drawableAreaYDim = myQPixmap.height();
2409+
//myRasterViewPort->drawableAreaXDim = myPixelWidth;
2410+
//myRasterViewPort->drawableAreaYDim = myPixelHeight;
2411+
2412+
myRasterViewPort->mDrawnExtent = myExtent;
2413+
myRasterViewPort->mSrcCRS = QgsCoordinateReferenceSystem(); // will be invalid
2414+
myRasterViewPort->mDestCRS = QgsCoordinateReferenceSystem(); // will be invalid
2415+
2416+
QgsMapToPixel *myMapToPixel = new QgsMapToPixel( myMapUnitsPerPixel );
2417+
2418+
QPainter * myQPainter = new QPainter( &myQPixmap );
2419+
draw( myQPainter, myRasterViewPort, myMapToPixel );
2420+
delete myRasterViewPort;
2421+
delete myMapToPixel;
2422+
myQPainter->end();
2423+
delete myQPainter;
2424+
2425+
return myQPixmap;
2426+
}
2427+
2428+
#if 0
23752429
void QgsRasterLayer::thumbnailAsImage( QImage * thepImage )
23762430
{
23772431
//TODO: This should be depreciated and a new function written that just returns a new QImage, it will be safer
2432+
// removed as it's not used anywhere, use previewAsPixmap() instead
23782433
if ( !thepImage )
23792434
return;
23802435

@@ -2398,6 +2453,7 @@ void QgsRasterLayer::thumbnailAsImage( QImage * thepImage )
23982453
}
23992454

24002455
}
2456+
#endif
24012457

24022458
void QgsRasterLayer::triggerRepaint()
24032459
{

src/core/raster/qgsrasterlayer.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
501501
QList< QPair< QString, QColor > > legendSymbologyItems() const;
502502

503503
/** \brief Get a legend image for this layer */
504-
QPixmap legendAsPixmap();
504+
Q_DECL_DEPRECATED QPixmap legendAsPixmap();
505505

506506
/** \brief Overloaded version of above function that can print layer name onto legend */
507507
//! @deprecated
@@ -639,12 +639,15 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
639639
virtual QStringList subLayers() const;
640640

641641
/** \brief Draws a thumbnail of the rasterlayer into the supplied pixmap pointer */
642-
void thumbnailAsPixmap( QPixmap * theQPixmap );
642+
Q_DECL_DEPRECATED void thumbnailAsPixmap( QPixmap * theQPixmap );
643+
644+
/** \brief Draws a preview of the rasterlayer into a pixmap */
645+
QPixmap previewAsPixmap( QSize size, QColor bgColor = Qt::white );
643646

644647
/** \brief Draws a thumbnail of the rasterlayer into the supplied QImage pointer
645648
* @note added in QGIS 1.6
646649
* */
647-
void thumbnailAsImage( QImage * thepImage );
650+
/* Q_DECL_DEPRECATED void thumbnailAsImage( QImage * thepImage ); */
648651

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

0 commit comments

Comments
 (0)