Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix for alphaBand and opacity loss upon renderer switch (style dock &…
… properties window) (#3967)

* [styledock] maintain raster alpha band and opacity when switching renderer

* [raster] fix alphaBand/opacity loss when opening layer properties
(fixes #16044)
  • Loading branch information
nirvn committed Jan 9, 2017
1 parent 015fdf2 commit 632f9c5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/app/qgsrasterlayerproperties.cpp
Expand Up @@ -540,6 +540,16 @@ void QgsRasterLayerProperties::setRendererWidget( const QString& rendererName )
{
QgsDebugMsg( "rendererName = " + rendererName );
QgsRasterRendererWidget* oldWidget = mRendererWidget;
QgsRasterRenderer* oldRenderer = mRasterLayer->renderer();

int alphaBand = -1;
double opacity = 1;
if ( oldRenderer )
{
// Retain alpha band and opacity when switching renderer
alphaBand = oldRenderer->alphaBand();
opacity = oldRenderer->opacity();
}

QgsRasterRendererRegistryEntry rendererEntry;
if ( QgsApplication::rasterRendererRegistry()->rendererData( rendererName, rendererEntry ) )
Expand All @@ -562,6 +572,8 @@ void QgsRasterLayerProperties::setRendererWidget( const QString& rendererName )
whileBlocking( mRasterLayer )->setDefaultContrastEnhancement();
}
}
mRasterLayer->renderer()->setAlphaBand( alphaBand );
mRasterLayer->renderer()->setOpacity( opacity );
mRendererWidget = rendererEntry.widgetCreateFunction( mRasterLayer, myExtent );
mRendererWidget->setMapCanvas( mMapCanvas );
mRendererStackedWidget->addWidget( mRendererWidget );
Expand Down
6 changes: 5 additions & 1 deletion src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -1258,7 +1258,11 @@ QDateTime QgsRasterLayer::timestamp() const
void QgsRasterLayer::setRenderer( QgsRasterRenderer* theRenderer )
{
QgsDebugMsgLevel( "Entered", 4 );
if ( !theRenderer ) { return; }
if ( !theRenderer )
{
return;
}

mPipe.set( theRenderer );
emit rendererChanged();
emit styleChanged();
Expand Down
20 changes: 18 additions & 2 deletions src/gui/raster/qgsrendererrasterpropertieswidget.cpp
Expand Up @@ -33,6 +33,8 @@
#include "qgssinglebandgrayrenderer.h"


#include "qgsmessagelog.h"

static void _initRendererWidgetFunctions()
{
static bool initialized = false;
Expand Down Expand Up @@ -319,11 +321,21 @@ void QgsRendererRasterPropertiesWidget::setRendererWidget( const QString &render
{
QgsDebugMsg( "rendererName = " + rendererName );
QgsRasterRendererWidget* oldWidget = mRendererWidget;
QgsRasterRenderer* oldRenderer = mRasterLayer->renderer();

int alphaBand = -1;
double opacity = 1;
if ( oldRenderer )
{
// Retain alpha band and opacity when switching renderer
alphaBand = oldRenderer->alphaBand();
opacity = oldRenderer->opacity();
}

QgsRasterRendererRegistryEntry rendererEntry;
if ( QgsApplication::rasterRendererRegistry()->rendererData( rendererName, rendererEntry ) )
{
if ( rendererEntry.widgetCreateFunction ) //single band color data renderer e.g. has no widget
if ( rendererEntry.widgetCreateFunction ) // Single band color data renderer e.g. has no widget
{
QgsDebugMsg( "renderer has widgetCreateFunction" );
// Current canvas extent (used to calc min/max) in layer CRS
Expand All @@ -341,22 +353,26 @@ void QgsRendererRasterPropertiesWidget::setRendererWidget( const QString &render
whileBlocking( mRasterLayer )->setDefaultContrastEnhancement();
}
}
mRasterLayer->renderer()->setAlphaBand( alphaBand );
mRasterLayer->renderer()->setOpacity( opacity );
mRendererWidget = rendererEntry.widgetCreateFunction( mRasterLayer, myExtent );
mRendererWidget->setMapCanvas( mMapCanvas );
connect( mRendererWidget, SIGNAL( widgetChanged() ), this, SIGNAL( widgetChanged() ) );
stackedWidget->addWidget( mRendererWidget );
stackedWidget->setCurrentWidget( mRendererWidget );
if ( oldWidget )
{
//compare used bands in new and old renderer and reset transparency dialog if different
// Compare used bands in new and old renderer and reset transparency dialog if different
QgsRasterRenderer* oldRenderer = oldWidget->renderer();
QgsRasterRenderer* newRenderer = mRendererWidget->renderer();
QList<int> oldBands = oldRenderer->usesBands();
QList<int> newBands = newRenderer->usesBands();

// if ( oldBands != newBands )
// {
// populateTransparencyTable( newRenderer );
// }

delete oldRenderer;
delete newRenderer;
}
Expand Down

0 comments on commit 632f9c5

Please sign in to comment.