Skip to content

Commit

Permalink
Make paletted raster widget behave the same as categorized widget
Browse files Browse the repository at this point in the history
when changing current band

Now the widget asks users if they want to delete existing categories
when they switch a band, just like how the categorized renderer
widget asks when the categorized field is changed.
  • Loading branch information
nyalldawson committed Jun 4, 2017
1 parent c040469 commit e5b8b32
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
7 changes: 7 additions & 0 deletions python/core/raster/qgspalettedrasterrenderer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ Returns number of colors
.. versionadded:: 2.1
%End

int band() const;
%Docstring
Returns the raster band used for rendering the raster.
.. versionadded:: 3.0
:rtype: int
%End

virtual void writeXml( QDomDocument &doc, QDomElement &parentElem ) const;


Expand Down
6 changes: 6 additions & 0 deletions src/core/raster/qgspalettedrasterrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ class CORE_EXPORT QgsPalettedRasterRenderer: public QgsRasterRenderer
* \since QGIS 2.1 */
void setLabel( int idx, const QString &label );

/**
* Returns the raster band used for rendering the raster.
* \since QGIS 3.0
*/
int band() const { return mBand; }

void writeXml( QDomDocument &doc, QDomElement &parentElem ) const override;

void legendSymbologyItems( QList< QPair< QString, QColor > > &symbolItems SIP_OUT ) const override;
Expand Down
31 changes: 30 additions & 1 deletion src/gui/raster/qgspalettedrendererwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ QgsPalettedRendererWidget::QgsPalettedRendererWidget( QgsRasterLayer *layer, con
}

connect( QgsProject::instance(), static_cast < void ( QgsProject::* )( QgsMapLayer * ) >( &QgsProject::layerWillBeRemoved ), this, &QgsPalettedRendererWidget::layerWillBeRemoved );
connect( mBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsPalettedRendererWidget::loadFromLayer );
connect( mBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsPalettedRendererWidget::bandChanged );
}

QgsPalettedRendererWidget::~QgsPalettedRendererWidget()
Expand Down Expand Up @@ -143,6 +143,9 @@ void QgsPalettedRendererWidget::setFromRenderer( const QgsRasterRenderer *r )
const QgsPalettedRasterRenderer *pr = dynamic_cast<const QgsPalettedRasterRenderer *>( r );
if ( pr )
{
mBand = pr->band();
whileBlocking( mBandComboBox )->setBand( mBand );

//read values and colors and fill into tree widget
mModel->setClassData( pr->classes() );

Expand Down Expand Up @@ -440,6 +443,32 @@ void QgsPalettedRendererWidget::loadFromLayer()
}
}

void QgsPalettedRendererWidget::bandChanged( int band )
{
if ( band == mBand )
return;

bool deleteExisting = false;
if ( !mModel->classData().isEmpty() )
{
int res = QMessageBox::question( this,
tr( "Confirm Delete" ),
tr( "The classification band was changed from %1 to %2.\n"
"Should the existing classes be deleted?" ).arg( mBand ).arg( band ),
QMessageBox::Yes | QMessageBox::No );

deleteExisting = ( res == QMessageBox::Yes );
}

mBand = band;
mModel->blockSignals( true );
if ( deleteExisting )
mModel->deleteAll();

mModel->blockSignals( false );
emit widgetChanged();
}

void QgsPalettedRendererWidget::gatheredClasses()
{
if ( !mGatherer || mGatherer->wasCanceled() )
Expand Down
3 changes: 3 additions & 0 deletions src/gui/raster/qgspalettedrendererwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ class GUI_EXPORT QgsPalettedRendererWidget: public QgsRasterRendererWidget, priv
//! Background class gatherer thread
QgsPalettedRendererClassGatherer *mGatherer = nullptr;

int mBand = -1;

void setSelectionColor( const QItemSelection &selection, const QColor &color );

private slots:
Expand All @@ -221,6 +223,7 @@ class GUI_EXPORT QgsPalettedRendererWidget: public QgsRasterRendererWidget, priv
void saveColorTable();
void classify();
void loadFromLayer();
void bandChanged( int band );

void gatheredClasses();
void gathererThreadFinished();
Expand Down

0 comments on commit e5b8b32

Please sign in to comment.