Skip to content
Permalink
Browse files
Enable load style for raster renderers
  • Loading branch information
mhugent committed Apr 18, 2012
1 parent 61ceec5 commit 4f4a3ba68878d7bc5bf4964f42104aa9f042af6f
@@ -399,6 +399,22 @@ void QgsRasterLayerProperties::populateTransparencyTable()
tableTransparency->resizeRowsToContents();
}

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

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 );
mRendererStackedWidget->addWidget( mRendererWidget );
}
}
}

/**
@note moved from ctor
@@ -940,30 +956,13 @@ void QgsRasterLayerProperties::on_buttonBuildPyramids_clicked()

void QgsRasterLayerProperties::on_mRenderTypeComboBox_currentIndexChanged( int index )
{
delete mRendererWidget;
mRendererWidget = 0;

if ( index < 0 )
{
return;
}

QString rendererName = mRenderTypeComboBox->itemData( index ).toString();

QgsRasterRendererRegistryEntry rendererEntry;
if ( QgsRasterRendererRegistry::instance()->rendererData( rendererName, rendererEntry ) )
{
if ( rendererEntry.widgetCreateFunction )
{
mRendererWidget = ( *rendererEntry.widgetCreateFunction )( mRasterLayer );
mRendererStackedWidget->addWidget( mRendererWidget );
}
else //single band color data renderer e.g. has no widget
{
delete mRendererWidget;
mRendererWidget = 0;
}
}
setRendererWidget( rendererName );
}

void QgsRasterLayerProperties::on_pbnAddValuesFromDisplay_clicked()
@@ -1525,6 +1524,12 @@ void QgsRasterLayerProperties::on_pbnLoadStyle_clicked()
}

settings.setValue( "style/lastStyleDir", QFileInfo( fileName ).absolutePath() );
QgsRasterRenderer* renderer = mRasterLayer->renderer();
if ( renderer )
{
setRendererWidget( renderer->type() );
}
mRasterLayer->triggerRepaint();
}


@@ -139,8 +139,7 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope
/** \brief Clear the current transparency table and populate the table with the correct types for current drawing mode and data type*/
void populateTransparencyTable();

/**Restores the state of the colormap tab*/
void syncColormapTab();
void setRendererWidget( const QString& rendererName );

//@TODO we should move these gradient generators somewhere more generic
//so they can be used generically throughut the app
@@ -65,7 +65,7 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
QList<QgsColorRampShader::ColorRampItem> colorRampItemList() const {return mColorRampItemList;}

/** \brief Get the color ramp type */
QgsColorRampShader::ColorRamp_TYPE colorRampType() {return mColorRampType;}
QgsColorRampShader::ColorRamp_TYPE colorRampType() const {return mColorRampType;}

/** \brief Get the color ramp type as a string */
QString colorRampTypeAsQString();
@@ -47,6 +47,7 @@ class CORE_EXPORT QgsRasterShader
double minimumValue() { return mMinimumValue; }

QgsRasterShaderFunction* rasterShaderFunction() { return mRasterShaderFunction; }
const QgsRasterShaderFunction* rasterShaderFunction() const { return mRasterShaderFunction; }

/*
*
@@ -37,6 +37,7 @@ class QgsSingleBandPseudoColorRenderer: public QgsRasterRenderer
/**Takes ownership of the shader*/
void setShader( QgsRasterShader* shader );
QgsRasterShader* shader() { return mShader; }
const QgsRasterShader* shader() const { return mShader; }

void writeXML( QDomDocument& doc, QDomElement& parentElem ) const;

@@ -51,23 +51,7 @@ QgsMultiBandColorRendererWidget::QgsMultiBandColorRendererWidget( QgsRasterLayer
mBlueBandComboBox->addItem( provider->colorInterpretationName( i ), i );
}

QgsMultiBandColorRenderer* r = dynamic_cast<QgsMultiBandColorRenderer*>( mRasterLayer->renderer() );
if ( r )
{
mRedBandComboBox->setCurrentIndex( mRedBandComboBox->findData( r->redBand() ) );
mGreenBandComboBox->setCurrentIndex( mGreenBandComboBox->findData( r->greenBand() ) );
mBlueBandComboBox->setCurrentIndex( mBlueBandComboBox->findData( r->blueBand() ) );

setMinMaxValue( r->redContrastEnhancement(), mRedMinLineEdit, mRedMaxLineEdit );
setMinMaxValue( r->greenContrastEnhancement(), mGreenMinLineEdit, mGreenMaxLineEdit );
setMinMaxValue( r->blueContrastEnhancement(), mBlueMinLineEdit, mBlueMaxLineEdit );
}
else
{
mRedBandComboBox->setCurrentIndex( mRedBandComboBox->findText( tr( "Red" ) ) );
mGreenBandComboBox->setCurrentIndex( mGreenBandComboBox->findText( tr( "Green" ) ) );
mBlueBandComboBox->setCurrentIndex( mBlueBandComboBox->findText( tr( "Blue" ) ) );
}
setFromRenderer( mRasterLayer->renderer() );
}
}

@@ -256,3 +240,24 @@ void QgsMultiBandColorRendererWidget::loadMinMaxValueForBand( int band, QLineEdi
minEdit->setText( QString::number( minVal ) );
maxEdit->setText( QString::number( maxVal ) );
}

void QgsMultiBandColorRendererWidget::setFromRenderer( const QgsRasterRenderer* r )
{
const QgsMultiBandColorRenderer* mbcr = dynamic_cast<const QgsMultiBandColorRenderer*>( r );
if ( mbcr )
{
mRedBandComboBox->setCurrentIndex( mRedBandComboBox->findData( mbcr->redBand() ) );
mGreenBandComboBox->setCurrentIndex( mGreenBandComboBox->findData( mbcr->greenBand() ) );
mBlueBandComboBox->setCurrentIndex( mBlueBandComboBox->findData( mbcr->blueBand() ) );

setMinMaxValue( mbcr->redContrastEnhancement(), mRedMinLineEdit, mRedMaxLineEdit );
setMinMaxValue( mbcr->greenContrastEnhancement(), mGreenMinLineEdit, mGreenMaxLineEdit );
setMinMaxValue( mbcr->blueContrastEnhancement(), mBlueMinLineEdit, mBlueMaxLineEdit );
}
else
{
mRedBandComboBox->setCurrentIndex( mRedBandComboBox->findText( tr( "Red" ) ) );
mGreenBandComboBox->setCurrentIndex( mGreenBandComboBox->findText( tr( "Green" ) ) );
mBlueBandComboBox->setCurrentIndex( mBlueBandComboBox->findText( tr( "Blue" ) ) );
}
}
@@ -38,6 +38,8 @@ class QgsMultiBandColorRendererWidget: public QgsRasterRendererWidget, private U

QgsRasterRenderer* renderer();

void setFromRenderer( const QgsRasterRenderer* r );

private slots:
void on_mLoadPushButton_clicked();

@@ -39,35 +39,7 @@ QgsPalettedRendererWidget::QgsPalettedRendererWidget( QgsRasterLayer* layer ): Q
mBandComboBox->addItem( provider->colorInterpretationName( i ), i );
}

QgsPalettedRasterRenderer* r = dynamic_cast<QgsPalettedRasterRenderer*>( mRasterLayer->renderer() );
if ( r )
{
//read values and colors and fill into tree widget
int nColors = r->nColors();
QColor* colors = r->colors();
for ( int i = 0; i < nColors; ++i )
{
QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
item->setText( 0, QString::number( i ) );
item->setBackground( 1, QBrush( colors[i] ) );
}
delete[] colors;
}
else
{
//read default palette settings from layer
QList<QgsColorRampShader::ColorRampItem>* itemList =
mRasterLayer->colorTable( mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt() );
QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList->constBegin();
int index = 0;
for ( ; itemIt != itemList->constEnd(); ++itemIt )
{
QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
item->setText( 0, QString::number( index ) );
item->setBackground( 1, QBrush( itemIt->color ) );
++index;
}
}
setFromRenderer( mRasterLayer->renderer() );
}
}

@@ -99,3 +71,36 @@ void QgsPalettedRendererWidget::on_mTreeWidget_itemDoubleClicked( QTreeWidgetIte
}
}
}

void QgsPalettedRendererWidget::setFromRenderer( const QgsRasterRenderer* r )
{
const QgsPalettedRasterRenderer* pr = dynamic_cast<const QgsPalettedRasterRenderer*>( r );
if ( pr )
{
//read values and colors and fill into tree widget
int nColors = pr->nColors();
QColor* colors = pr->colors();
for ( int i = 0; i < nColors; ++i )
{
QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
item->setText( 0, QString::number( i ) );
item->setBackground( 1, QBrush( colors[i] ) );
}
delete[] colors;
}
else
{
//read default palette settings from layer
QList<QgsColorRampShader::ColorRampItem>* itemList =
mRasterLayer->colorTable( mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt() );
QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList->constBegin();
int index = 0;
for ( ; itemIt != itemList->constEnd(); ++itemIt )
{
QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
item->setText( 0, QString::number( index ) );
item->setBackground( 1, QBrush( itemIt->color ) );
++index;
}
}
}
@@ -34,6 +34,8 @@ class QgsPalettedRendererWidget: public QgsRasterRendererWidget, private Ui::Qgs

QgsRasterRenderer* renderer();

void setFromRenderer( const QgsRasterRenderer* r );

private slots:
void on_mTreeWidget_itemDoubleClicked( QTreeWidgetItem * item, int column );
};
@@ -46,19 +46,7 @@ QgsSingleBandGrayRendererWidget::QgsSingleBandGrayRendererWidget( QgsRasterLayer
mContrastEnhancementComboBox->addItem( tr( "Stretch and clip to MinMax" ), 2 );
mContrastEnhancementComboBox->addItem( tr( "Clip to MinMax" ), 3 );

QgsSingleBandGrayRenderer* r = dynamic_cast<QgsSingleBandGrayRenderer*>( layer->renderer() );
if ( r )
{
//band
mGrayBandComboBox->setCurrentIndex( mGrayBandComboBox->findData( r->grayBand() ) );
const QgsContrastEnhancement* ce = r->contrastEnhancement();
//minmax
mMinLineEdit->setText( QString::number( ce->minimumValue() ) );
mMaxLineEdit->setText( QString::number( ce->maximumValue() ) );
//contrast enhancement algorithm
mContrastEnhancementComboBox->setCurrentIndex(
mContrastEnhancementComboBox->findData(( int )( ce->contrastEnhancementAlgorithm() ) ) );
}
setFromRenderer( layer->renderer() );
}
}

@@ -133,3 +121,20 @@ void QgsSingleBandGrayRendererWidget::on_mLoadPushButton_clicked()
mMinLineEdit->setText( QString::number( minVal ) );
mMaxLineEdit->setText( QString::number( maxVal ) );
}

void QgsSingleBandGrayRendererWidget::setFromRenderer( const QgsRasterRenderer* r )
{
const QgsSingleBandGrayRenderer* gr = dynamic_cast<const QgsSingleBandGrayRenderer*>( r );
if ( gr )
{
//band
mGrayBandComboBox->setCurrentIndex( mGrayBandComboBox->findData( gr->grayBand() ) );
const QgsContrastEnhancement* ce = gr->contrastEnhancement();
//minmax
mMinLineEdit->setText( QString::number( ce->minimumValue() ) );
mMaxLineEdit->setText( QString::number( ce->maximumValue() ) );
//contrast enhancement algorithm
mContrastEnhancementComboBox->setCurrentIndex(
mContrastEnhancementComboBox->findData(( int )( ce->contrastEnhancementAlgorithm() ) ) );
}
}
@@ -32,6 +32,8 @@ class QgsSingleBandGrayRendererWidget: public QgsRasterRendererWidget, private U

QgsRasterRenderer* renderer();

void setFromRenderer( const QgsRasterRenderer* r );

private slots:
void on_mLoadPushButton_clicked();
};
@@ -54,40 +54,7 @@ QgsSingleBandPseudoColorRendererWidget::QgsSingleBandPseudoColorRendererWidget(
mClassificationModeComboBox->addItem( tr( "Equal interval" ) );
//quantile would be nice as well

QgsSingleBandPseudoColorRenderer* r = dynamic_cast<QgsSingleBandPseudoColorRenderer*>( layer->renderer() );
if ( r )
{
QgsRasterShader* rasterShader = r->shader();
if ( rasterShader )
{
QgsColorRampShader* colorRampShader = dynamic_cast<QgsColorRampShader*>( rasterShader->rasterShaderFunction() );
if ( colorRampShader )
{
if ( colorRampShader->colorRampType() == QgsColorRampShader::INTERPOLATED )
{
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
}
else if ( colorRampShader->colorRampType() == QgsColorRampShader::DISCRETE )
{
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Discrete" ) ) );
}
else
{
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Exact" ) ) );
}

const QList<QgsColorRampShader::ColorRampItem> colorRampItemList = colorRampShader->colorRampItemList();
QList<QgsColorRampShader::ColorRampItem>::const_iterator it = colorRampItemList.constBegin();
for ( ; it != colorRampItemList.end(); ++it )
{
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
newItem->setText( 0, QString::number( it->value, 'f' ) );
newItem->setBackground( 1, QBrush( it->color ) );
newItem->setText( 2, it->label );
}
}
}
}
setFromRenderer( layer->renderer() );
}

QgsSingleBandPseudoColorRendererWidget::~QgsSingleBandPseudoColorRendererWidget()
@@ -479,3 +446,41 @@ void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemDoubleCl
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
}
}

void QgsSingleBandPseudoColorRendererWidget::setFromRenderer( const QgsRasterRenderer* r )
{
const QgsSingleBandPseudoColorRenderer* pr = dynamic_cast<const QgsSingleBandPseudoColorRenderer*>( r );
if ( pr )
{
const QgsRasterShader* rasterShader = pr->shader();
if ( rasterShader )
{
const QgsColorRampShader* colorRampShader = dynamic_cast<const QgsColorRampShader*>( rasterShader->rasterShaderFunction() );
if ( colorRampShader )
{
if ( colorRampShader->colorRampType() == QgsColorRampShader::INTERPOLATED )
{
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
}
else if ( colorRampShader->colorRampType() == QgsColorRampShader::DISCRETE )
{
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Discrete" ) ) );
}
else
{
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Exact" ) ) );
}

const QList<QgsColorRampShader::ColorRampItem> colorRampItemList = colorRampShader->colorRampItemList();
QList<QgsColorRampShader::ColorRampItem>::const_iterator it = colorRampItemList.constBegin();
for ( ; it != colorRampItemList.end(); ++it )
{
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
newItem->setText( 0, QString::number( it->value, 'f' ) );
newItem->setBackground( 1, QBrush( it->color ) );
newItem->setText( 2, it->label );
}
}
}
}
}
@@ -33,6 +33,8 @@ class QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendererWidget,
static QgsRasterRendererWidget* create( QgsRasterLayer* layer ) { return new QgsSingleBandPseudoColorRendererWidget( layer ); }
QgsRasterRenderer* renderer();

void setFromRenderer( const QgsRasterRenderer* r );

private:
void populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems );

0 comments on commit 4f4a3ba

Please sign in to comment.