Skip to content
Permalink
Browse files

More fixes for raster transparency

  • Loading branch information
mhugent committed Jul 5, 2012
1 parent 7a8b9b7 commit ba0f5da677e810b0582a157fb0a3ffb07fdf7688
@@ -178,9 +178,6 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
.arg( pyramidSentence2 ).arg( pyramidSentence3 )
.arg( pyramidSentence4 ).arg( pyramidSentence5 ) );

// update based on lyr's current state
sync();

QSettings settings;
restoreGeometry( settings.value( "/Windows/RasterLayerProperties/geometry" ).toByteArray() );
tabBar->setCurrentIndex( settings.value( "/Windows/RasterLayerProperties/row" ).toInt() );
@@ -292,6 +289,9 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
}
}
on_mRenderTypeComboBox_currentIndexChanged( mRenderTypeComboBox->currentIndex() );

// update based on lyr's current state
sync();
} // QgsRasterLayerProperties ctor


@@ -306,20 +306,22 @@ QgsRasterLayerProperties::~QgsRasterLayerProperties()
}
}

void QgsRasterLayerProperties::populateTransparencyTable()
void QgsRasterLayerProperties::populateTransparencyTable( QgsRasterRenderer* renderer )
{
QgsDebugMsg( "entering." );
if ( !mRasterLayer )
{
return;
}
QgsRasterRenderer* renderer = mRasterLayer->renderer();

if ( !renderer )
{
return;
}

tableTransparency->clear();
tableTransparency->setColumnCount( 0 );
tableTransparency->setRowCount( 0 );

QList<int> bandList = renderer->usesBands();
tableTransparency->setColumnCount( bandList.size() + 1 );
@@ -371,20 +373,32 @@ void QgsRasterLayerProperties::populateTransparencyTable()

void QgsRasterLayerProperties::setRendererWidget( const QString& rendererName )
{
delete mRendererWidget;
mRendererWidget = 0;
QgsRasterRendererWidget* oldWidget = mRendererWidget;

QgsRasterRendererRegistryEntry rendererEntry;
if ( QgsRasterRendererRegistry::instance()->rendererData( rendererName, rendererEntry ) )
{
if ( rendererEntry.widgetCreateFunction ) //single band color data renderer e.g. has no widget
{
mRendererWidget = ( *rendererEntry.widgetCreateFunction )( mRasterLayer );
QWidget* oldWidget = mRendererStackedWidget->currentWidget();
mRendererStackedWidget->addWidget( mRendererWidget );
delete oldWidget;
if ( oldWidget )
{
//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;
}
}
}
delete oldWidget;
}

/**
@@ -460,7 +474,7 @@ void QgsRasterLayerProperties::sync()
leNoDataValue->insert( "" );
}

populateTransparencyTable();
populateTransparencyTable( mRasterLayer->renderer() );

QgsDebugMsg( "populate colormap tab" );
/*
@@ -578,7 +592,7 @@ void QgsRasterLayerProperties::apply()

//Walk through each row in table and test value. If not valid set to 0.0 and continue building transparency list
QgsRasterTransparency* rasterTransparency = new QgsRasterTransparency();
if ( mRasterLayer->bandCount() == 3 )
if ( tableTransparency->columnCount() == 4 )
{
double myTransparentValue;
double myPercentTransparent;
@@ -678,7 +692,7 @@ void QgsRasterLayerProperties::apply()
}
rasterTransparency->setTransparentThreeValuePixelList( myTransparentThreeValuePixelList );
}
else if ( mRasterLayer->bandCount() == 1 )
else if ( tableTransparency->columnCount() == 2 )
{
double myTransparentValue;
double myPercentTransparent;
@@ -967,9 +981,21 @@ void QgsRasterLayerProperties::on_pbnChangeSpatialRefSys_clicked()

void QgsRasterLayerProperties::on_pbnDefaultValues_clicked()
{
if ( !mRendererWidget )
{
return;
}

if ( /*rbtnThreeBand->isChecked() &&*/ QgsRasterLayer::PalettedColor != mRasterLayer->drawingStyle() &&
QgsRasterLayer::PalettedMultiBandColor != mRasterLayer->drawingStyle() )
QgsRasterRenderer* r = mRendererWidget->renderer();
if ( !r )
{
return;
}

int nBands = r->usesBands().size();
delete r;

if ( nBands == 3 )
{
tableTransparency->clear();
tableTransparency->setColumnCount( 4 );
@@ -986,7 +1012,7 @@ void QgsRasterLayerProperties::on_pbnDefaultValues_clicked()
tableTransparency->setItem( 0, 3, new QTableWidgetItem( "100.0" ) );
}
}
else
else //1 band
{
tableTransparency->clear();
tableTransparency->setColumnCount( 2 );
@@ -1009,7 +1035,6 @@ void QgsRasterLayerProperties::on_pbnDefaultValues_clicked()
tableTransparency->setItem( 0, 0, new QTableWidgetItem( QString::number( mRasterLayer->noDataValue(), 'f' ) ) );
tableTransparency->setItem( 0, 1, new QTableWidgetItem( "100.0" ) );
}

}
}

@@ -29,6 +29,7 @@ class QgsMapLayer;
class QgsMapCanvas;
class QgsRasterLayer;
class QgsMapToolEmitPoint;
class QgsRasterRenderer;
class QgsRasterRendererWidget;

/**Property sheet for a raster map layer
@@ -137,7 +138,7 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope
QgsRasterRendererWidget* mRendererWidget;

/** \brief Clear the current transparency table and populate the table with the correct types for current drawing mode and data type*/
void populateTransparencyTable();
void populateTransparencyTable( QgsRasterRenderer* renderer );

void setRendererWidget( const QString& rendererName );

@@ -221,6 +221,9 @@ void QgsMultiBandColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
int redVal = 0;
int greenVal = 0;
int blueVal = 0;
int redDataVal = 0;
int greenDataVal = 0;
int blueDataVal = 0;
QRgb defaultColor = qRgba( 255, 255, 255, 0 );
double currentOpacity = mOpacity; //opacity (between 0 and 1)

@@ -243,14 +246,17 @@ void QgsMultiBandColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
if ( mRedBand > 0 )
{
redVal = readValue( redData, redType, currentRasterPos );
redDataVal = redVal;
}
if ( mGreenBand > 0 )
{
greenVal = readValue( greenData, greenType, currentRasterPos );
greenDataVal = greenVal;
}
if ( mBlueBand > 0 )
{
blueVal = readValue( blueData, blueType, currentRasterPos );
blueDataVal = blueVal;
}

//apply default color if red, green or blue not in displayable range
@@ -288,7 +294,7 @@ void QgsMultiBandColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
currentOpacity = mOpacity;
if ( mRasterTransparency )
{
currentOpacity = mRasterTransparency->alphaValue( redVal, greenVal, blueVal, mOpacity * 255 ) / 255.0;
currentOpacity = mRasterTransparency->alphaValue( redDataVal, greenDataVal, blueDataVal, mOpacity * 255 ) / 255.0;
}
if ( mAlphaBand > 0 )
{
@@ -256,6 +256,29 @@ QgsRasterRenderer* QgsRasterRendererRegistry::defaultRendererForDrawingStyle( co
break;
}

QgsRasterTransparency* tr = new QgsRasterTransparency(); //renderer takes ownership
int bandCount = renderer->usesBands().size();
if ( bandCount == 1 )
{
QList<QgsRasterTransparency::TransparentSingleValuePixel> transparentSingleList;
QgsRasterTransparency::TransparentSingleValuePixel singleEntry;
singleEntry.pixelValue = provider->noDataValue();
singleEntry.percentTransparent = 100;
transparentSingleList.push_back( singleEntry );
tr->setTransparentSingleValuePixelList( transparentSingleList );
}
else if ( bandCount == 3 )
{
QList<QgsRasterTransparency::TransparentThreeValuePixel> transparentThreeValueList;
QgsRasterTransparency::TransparentThreeValuePixel threeValueEntry;
threeValueEntry.red = provider->noDataValue();
threeValueEntry.green = provider->noDataValue();
threeValueEntry.blue = provider->noDataValue();
threeValueEntry.percentTransparent = 100;
transparentThreeValueList.push_back( threeValueEntry );
tr->setTransparentThreeValuePixelList( transparentThreeValueList );
}
renderer->setRasterTransparency( tr );
#if 0
if ( !renderer )
{

0 comments on commit ba0f5da

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