Skip to content
Permalink
Browse files

[ui] Insure color/value changes in the colormap tree are reflected in…

… the color ramp button of the color ramp shader widget (#40671)
  • Loading branch information
nirvn committed Dec 28, 2020
1 parent 8a3eefd commit 14136a15cbc577cd427df8d9d3dc825a59653ed2
@@ -115,6 +115,13 @@ Returns the source color ramp.
.. seealso:: :py:func:`setSourceColorRamp`

.. versionadded:: 3.0
%End

QgsColorRamp *createColorRamp() const /Factory/;
%Docstring
Creates a gradient color ramp from shader settings.

.. versionadded:: 3.18
%End

void setSourceColorRamp( QgsColorRamp *colorramp /Transfer/ );
@@ -135,6 +135,50 @@ QgsColorRamp *QgsColorRampShader::sourceColorRamp() const
return mSourceColorRamp.get();
}

QgsColorRamp *QgsColorRampShader::createColorRamp() const
{
std::unique_ptr<QgsGradientColorRamp> ramp = qgis::make_unique< QgsGradientColorRamp >();
int count = mColorRampItemList.size();
if ( count == 0 )
{
const QColor none( 0, 0, 0, 0 );
ramp->setColor1( none );
ramp->setColor2( none );
}
else if ( count == 1 )
{
ramp->setColor1( mColorRampItemList[0].color );
ramp->setColor2( mColorRampItemList[0].color );
}
else
{
QgsGradientStopsList stops;
// minimum and maximum values can fall outside the range of the item list
const double min = minimumValue();
const double max = maximumValue();
for ( int i = 0; i < count; i++ )
{
double offset = ( mColorRampItemList[i].value - min ) / ( max - min );
if ( i == 0 )
{
ramp->setColor1( mColorRampItemList[i].color );
if ( offset <= 0.0 )
continue;
}
else if ( i == count - 1 )
{
ramp->setColor2( mColorRampItemList[i].color );
if ( offset >= 1.0 )
continue;
}
stops << QgsGradientStop( offset, mColorRampItemList[i].color );
}
ramp->setStops( stops );
}

return ramp.release();
}

void QgsColorRampShader::setSourceColorRamp( QgsColorRamp *colorramp )
{
mSourceColorRamp.reset( colorramp );
@@ -151,12 +151,17 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction

/**
* Returns the source color ramp.
*
* \see setSourceColorRamp()
* \since QGIS 3.0
*/
QgsColorRamp *sourceColorRamp() const;

/**
* Creates a gradient color ramp from shader settings.
* \since QGIS 3.18
*/
QgsColorRamp *createColorRamp() const SIP_FACTORY;

/**
* Set the source color ramp. Ownership is transferred to the shader.
* \see sourceColorRamp()
@@ -441,24 +441,16 @@ QList<QgsLayerTreeModelLegendNode *> QgsSingleBandPseudoColorRenderer::createLeg
res << new QgsSimpleLegendNode( nodeLayer, name );
}

if ( !rampShader->sourceColorRamp() )
{
const QList< QPair< QString, QColor > > items = legendSymbologyItems();
res.reserve( items.size() );
for ( const QPair< QString, QColor > &item : items )
{
res << new QgsRasterSymbolLegendNode( nodeLayer, item.second, item.first );
}
return res;
}

switch ( rampShader->colorRampType() )
{
case QgsColorRampShader::Interpolated:
// for interpolated shaders we use a ramp legend node
res << new QgsColorRampLegendNode( nodeLayer, rampShader->sourceColorRamp()->clone(),
rampShader->legendSettings() ? *rampShader->legendSettings() : QgsColorRampLegendNodeSettings(),
rampShader->minimumValue(), rampShader->maximumValue() );
if ( !rampShader->colorRampItemList().isEmpty() )
{
res << new QgsColorRampLegendNode( nodeLayer, rampShader->createColorRamp(),
rampShader->legendSettings() ? *rampShader->legendSettings() : QgsColorRampLegendNodeSettings(),
rampShader->minimumValue(), rampShader->maximumValue() );
}
break;

case QgsColorRampShader::Discrete:
@@ -284,6 +284,7 @@ void QgsColorRampShaderWidget::mAddEntryButton_clicked()
autoLabel();

loadMinimumMaximumFromTree();
updateColorRamp();
emit widgetChanged();
}

@@ -303,6 +304,7 @@ void QgsColorRampShaderWidget::mDeleteEntryButton_clicked()
}

loadMinimumMaximumFromTree();
updateColorRamp();
emit widgetChanged();
}

@@ -356,6 +358,12 @@ void QgsColorRampShaderWidget::mClassificationModeComboBox_currentIndexChanged(
emit classificationModeChanged( mode );
}

void QgsColorRampShaderWidget::updateColorRamp()
{
std::unique_ptr< QgsColorRamp > ramp( shader().createColorRamp() );
whileBlocking( btnColorRamp )->setColorRamp( ramp.get() );
}

void QgsColorRampShaderWidget::applyColorRamp()
{
std::unique_ptr< QgsColorRamp > ramp( btnColorRamp->colorRamp() );
@@ -571,6 +579,7 @@ void QgsColorRampShaderWidget::mColormapTreeWidget_itemEdited( QTreeWidgetItem *
{
autoLabel();
loadMinimumMaximumFromTree();
updateColorRamp();
emit widgetChanged();
break;
}
@@ -586,6 +595,7 @@ void QgsColorRampShaderWidget::mColormapTreeWidget_itemEdited( QTreeWidgetItem *
case ColorColumn:
{
loadMinimumMaximumFromTree();
updateColorRamp();
emit widgetChanged();
break;
}
@@ -134,6 +134,7 @@ class GUI_EXPORT QgsColorRampShaderWidget: public QWidget, protected Ui::QgsColo
private slots:

void applyColorRamp();
void updateColorRamp();
void mAddEntryButton_clicked();
void mDeleteEntryButton_clicked();
void mLoadFromBandButton_clicked();
@@ -16,7 +16,7 @@

from qgis.PyQt.QtGui import QColor

from qgis.core import (QgsColorRampShader)
from qgis.core import (QgsColorRampShader, QgsGradientColorRamp, QgsGradientStop)
from qgis.testing import unittest


@@ -31,6 +31,21 @@ def testNan(self):
self.assertFalse(shader.shade(float('NaN'))[0])
self.assertFalse(shader.shade(float("inf"))[0])

def testCreateColorRamp(self):
shader = QgsColorRampShader(1, 3)

item1 = QgsColorRampShader.ColorRampItem(1, QColor(255, 0, 0))
item2 = QgsColorRampShader.ColorRampItem(2, QColor(255, 255, 0))
item3 = QgsColorRampShader.ColorRampItem(3, QColor(255, 255, 255))
shader.setColorRampItemList([item1, item2, item3])
shaderRamp = shader.createColorRamp()

gradientRamp = QgsGradientColorRamp(QColor(255, 0, 0), QColor(255, 255, 255), False, [QgsGradientStop(0.5, QColor(255, 255, 0))])

self.assertEqual(shaderRamp.color1(), gradientRamp.color1())
self.assertEqual(shaderRamp.color2(), gradientRamp.color2())
self.assertEqual(shaderRamp.stops(), gradientRamp.stops())


if __name__ == '__main__':
unittest.main()

0 comments on commit 14136a1

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