Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix calculating color map when min/max values are not set yet.
if mMin or mMax are not set yet for some reason, we should try our
best to apply a reasonable color ramp.
  • Loading branch information
iona5 committed Aug 1, 2020
1 parent 5b81714 commit 2d986f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/gui/raster/qgscolorrampshaderwidget.cpp
Expand Up @@ -376,6 +376,18 @@ void QgsColorRampShaderWidget::applyColorRamp()
int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
if ( topLevelItemCount > 0 )
{
// We need to have valid min/max values here. If we haven't, load from colormap
double min, max;
if ( std::isnan( mMin ) || std::isnan( mMax ) )
{
colormapMinMax( min, max );
}
else
{
min = mMin;
max = mMax;
}

// if the list values has been customized, maintain pre-existing values
QTreeWidgetItem *currentItem = nullptr;
for ( int i = 0; i < topLevelItemCount; ++i )
Expand All @@ -387,7 +399,7 @@ void QgsColorRampShaderWidget::applyColorRamp()
}

double value = QLocale().toDouble( currentItem->text( ValueColumn ) );
double position = ( value - mMin ) / ( mMax - mMin );
double position = ( value - min ) / ( max - min );
whileBlocking( static_cast<QgsTreeWidgetItemObject *>( currentItem ) )->setData( ColorColumn, Qt::EditRole, ramp->color( position ) );
}

Expand Down Expand Up @@ -727,19 +739,23 @@ double QgsColorRampShaderWidget::maximum() const
return mMax;
}



void QgsColorRampShaderWidget::loadMinimumMaximumFromTree()
void QgsColorRampShaderWidget::colormapMinMax( double &min, double &max ) const
{
QTreeWidgetItem *item = mColormapTreeWidget->topLevelItem( 0 );
if ( !item )
{
return;
}

double min = QLocale().toDouble( item->text( ValueColumn ) );
min = QLocale().toDouble( item->text( ValueColumn ) );
item = mColormapTreeWidget->topLevelItem( mColormapTreeWidget->topLevelItemCount() - 1 );
double max = QLocale().toDouble( item->text( ValueColumn ) );
max = QLocale().toDouble( item->text( ValueColumn ) );
}

void QgsColorRampShaderWidget::loadMinimumMaximumFromTree()
{
double min = 0, max = 0;
colormapMinMax( min, max );

if ( !qgsDoubleNear( mMin, min ) || !qgsDoubleNear( mMax, max ) )
{
Expand Down
2 changes: 2 additions & 0 deletions src/gui/raster/qgscolorrampshaderwidget.h
Expand Up @@ -102,6 +102,8 @@ class GUI_EXPORT QgsColorRampShaderWidget: public QWidget, protected Ui::QgsColo
protected:
//! Populates color ramp tree from ramp items
void populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem> &colorRampItems );
//! Extracts the minimal and maximal value from the colormapTreeWidget
void colormapMinMax( double &min, double &max ) const SIP_SKIP;

private:

Expand Down

0 comments on commit 2d986f6

Please sign in to comment.