Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #42193 from elpaso/raster-legend-add-option-to-use…
…-separate-items

Add option to use the old legend style
  • Loading branch information
elpaso committed Mar 12, 2021
2 parents 6c6fd06 + d6047d3 commit 5dc2028
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 53 deletions.
Expand Up @@ -197,6 +197,26 @@ Sets the ramp ``orientation`` (i.e. horizontal or vertical).
.. seealso:: :py:func:`orientation`

.. seealso:: :py:func:`setDirection`
%End

bool useContinuousLegend() const;
%Docstring
Returns ``True`` if a continuous gradient legend will be used.

.. seealso:: :py:func:`setUseContinuousLegend`
%End

void setUseContinuousLegend( bool useContinuousLegend );
%Docstring
Sets the flag to use a continuos gradient legend to ``useContinuousLegend``.

When this flag is set the legend will be rendered using a continuous color ramp with
min and max values, when it is not set the legend will be rendered using separate
items for each entry.

.. seealso:: :py:func:`setOrientation`

.. seealso:: :py:func:`direction`
%End

};
Expand Down
18 changes: 18 additions & 0 deletions python/gui/auto_generated/qgscolorramplegendnodewidget.sip.in
Expand Up @@ -47,6 +47,15 @@ Returns the legend node settings as defined by the widget.
Sets the settings to show in the widget.

.. seealso:: :py:func:`settings`
%End

void setUseContinuousRampCheckBoxVisibility( bool visible );
%Docstring
Sets visibility for the "Use Continuous Legend" checkbox to ``visible``.

This widget is visible and checked by default but in a few cases it does not
need to be visible because disabling it would not make sense (for instance
when using single band gray renderer).
%End

};
Expand Down Expand Up @@ -77,6 +86,15 @@ Returns the legend node settings as defined by the dialog.
QDialogButtonBox *buttonBox() const;
%Docstring
Returns a reference to the dialog's button box.
%End

void setUseContinuousRampCheckBoxVisibility( bool visible );
%Docstring
Sets visibility for the "Use Continuous Legend" checkbox in the legend settings dialog to ``visible``.

This widget is visible and checked by default but in a few cases it does not
need to be visible because disabling it would not make sense (for instance
when using single band gray renderer).
%End

};
Expand Down
16 changes: 15 additions & 1 deletion src/core/layertree/qgscolorramplegendnodesettings.cpp
Expand Up @@ -26,7 +26,8 @@ QgsColorRampLegendNodeSettings::QgsColorRampLegendNodeSettings()
}

QgsColorRampLegendNodeSettings::QgsColorRampLegendNodeSettings( const QgsColorRampLegendNodeSettings &other )
: mMinimumLabel( other.mMinimumLabel )
: mUseContinuousLegend( other.mUseContinuousLegend )
, mMinimumLabel( other.mMinimumLabel )
, mMaximumLabel( other.mMaximumLabel )
, mPrefix( other.mPrefix )
, mSuffix( other.mSuffix )
Expand All @@ -40,6 +41,7 @@ QgsColorRampLegendNodeSettings::QgsColorRampLegendNodeSettings( const QgsColorRa

QgsColorRampLegendNodeSettings &QgsColorRampLegendNodeSettings::operator=( const QgsColorRampLegendNodeSettings &other )
{
mUseContinuousLegend = other.mUseContinuousLegend;
mMinimumLabel = other.mMinimumLabel;
mMaximumLabel = other.mMaximumLabel;
mPrefix = other.mPrefix;
Expand Down Expand Up @@ -97,6 +99,7 @@ void QgsColorRampLegendNodeSettings::writeXml( QDomDocument &doc, QDomElement &e
{
QDomElement settingsElement = doc.createElement( QStringLiteral( "rampLegendSettings" ) );

settingsElement.setAttribute( QStringLiteral( "useContinuousLegend" ), mUseContinuousLegend );
settingsElement.setAttribute( QStringLiteral( "minimumLabel" ), mMinimumLabel );
settingsElement.setAttribute( QStringLiteral( "maximumLabel" ), mMaximumLabel );
settingsElement.setAttribute( QStringLiteral( "prefix" ), mPrefix );
Expand All @@ -121,6 +124,7 @@ void QgsColorRampLegendNodeSettings::readXml( const QDomElement &element, const
const QDomElement settingsElement = element.firstChildElement( QStringLiteral( "rampLegendSettings" ) );
if ( !settingsElement.isNull() )
{
mUseContinuousLegend = settingsElement.attribute( QStringLiteral( "useContinuousLegend" ), QStringLiteral( "1" ) ).toInt( );
mMinimumLabel = settingsElement.attribute( QStringLiteral( "minimumLabel" ) );
mMaximumLabel = settingsElement.attribute( QStringLiteral( "maximumLabel" ) );
mPrefix = settingsElement.attribute( QStringLiteral( "prefix" ) );
Expand Down Expand Up @@ -185,3 +189,13 @@ void QgsColorRampLegendNodeSettings::setOrientation( Qt::Orientation orientation
{
mOrientation = orientation;
}

bool QgsColorRampLegendNodeSettings::useContinuousLegend() const
{
return mUseContinuousLegend;
}

void QgsColorRampLegendNodeSettings::setUseContinuousLegend( bool useContinuousLegend )
{
mUseContinuousLegend = useContinuousLegend;
}
20 changes: 20 additions & 0 deletions src/core/layertree/qgscolorramplegendnodesettings.h
Expand Up @@ -202,7 +202,27 @@ class CORE_EXPORT QgsColorRampLegendNodeSettings
*/
void setOrientation( Qt::Orientation orientation );

/**
* Returns TRUE if a continuous gradient legend will be used.
*
* \see setUseContinuousLegend()
*/
bool useContinuousLegend() const;

/**
* Sets the flag to use a continuos gradient legend to \a useContinuousLegend.
*
* When this flag is set the legend will be rendered using a continuous color ramp with
* min and max values, when it is not set the legend will be rendered using separate
* items for each entry.
*
* \see setOrientation()
* \see direction()
*/
void setUseContinuousLegend( bool useContinuousLegend );

private:
bool mUseContinuousLegend = true;
QString mMinimumLabel;
QString mMaximumLabel;
QString mPrefix;
Expand Down
18 changes: 11 additions & 7 deletions src/core/pointcloud/qgspointcloudattributebyramprenderer.cpp
Expand Up @@ -179,13 +179,17 @@ QList<QgsLayerTreeModelLegendNode *> QgsPointCloudAttributeByRampRenderer::creat
switch ( mColorRampShader.colorRampType() )
{
case QgsColorRampShader::Interpolated:
// for interpolated shaders we use a ramp legend node
res << new QgsColorRampLegendNode( nodeLayer, mColorRampShader.sourceColorRamp()->clone(),
mColorRampShader.legendSettings() ? *mColorRampShader.legendSettings() : QgsColorRampLegendNodeSettings(),
mColorRampShader.minimumValue(),
mColorRampShader.maximumValue() );
break;

// for interpolated shaders we use a ramp legend node unless the settings flag
// to use the continuous legend is not set, in that case we fall through
if ( ! mColorRampShader.legendSettings() || mColorRampShader.legendSettings()->useContinuousLegend() )
{
res << new QgsColorRampLegendNode( nodeLayer, mColorRampShader.sourceColorRamp()->clone(),
mColorRampShader.legendSettings() ? *mColorRampShader.legendSettings() : QgsColorRampLegendNodeSettings(),
mColorRampShader.minimumValue(),
mColorRampShader.maximumValue() );
break;
}
Q_FALLTHROUGH();
case QgsColorRampShader::Discrete:
case QgsColorRampShader::Exact:
{
Expand Down
18 changes: 11 additions & 7 deletions src/core/raster/qgssinglebandpseudocolorrenderer.cpp
Expand Up @@ -444,15 +444,19 @@ QList<QgsLayerTreeModelLegendNode *> QgsSingleBandPseudoColorRenderer::createLeg
switch ( rampShader->colorRampType() )
{
case QgsColorRampShader::Interpolated:
// for interpolated shaders we use a ramp legend node
if ( !rampShader->colorRampItemList().isEmpty() )
// for interpolated shaders we use a ramp legend node unless the settings flag
// to use the continuous legend is not set, in that case we fall through
if ( ! rampShader->legendSettings() || rampShader->legendSettings()->useContinuousLegend() )
{
res << new QgsColorRampLegendNode( nodeLayer, rampShader->createColorRamp(),
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;
}
break;

Q_FALLTHROUGH();
case QgsColorRampShader::Discrete:
case QgsColorRampShader::Exact:
{
Expand Down
19 changes: 19 additions & 0 deletions src/gui/qgscolorramplegendnodewidget.cpp
Expand Up @@ -38,6 +38,13 @@ QgsColorRampLegendNodeWidget::QgsColorRampLegendNodeWidget( QWidget *parent )
mFontButton->setShowNullFormat( true );
mFontButton->setNoFormatString( tr( "Default" ) );

connect( mUseContinuousLegendCheckBox, &QCheckBox::stateChanged, this, [ = ]( bool checked )
{
mLayoutGroup->setEnabled( checked );
mLabelsGroup->setEnabled( checked );
onChanged();
} );

connect( mMinLabelLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
connect( mMaxLabelLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
connect( mPrefixLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
Expand All @@ -51,6 +58,7 @@ QgsColorRampLegendNodeWidget::QgsColorRampLegendNodeWidget( QWidget *parent )
QgsColorRampLegendNodeSettings QgsColorRampLegendNodeWidget::settings() const
{
QgsColorRampLegendNodeSettings settings;
settings.setUseContinuousLegend( mUseContinuousLegendCheckBox->isChecked() );
settings.setDirection( static_cast< QgsColorRampLegendNodeSettings::Direction >( mDirectionComboBox->currentData().toInt() ) );
settings.setOrientation( static_cast< Qt::Orientation >( mOrientationComboBox->currentData().toInt() ) );
settings.setMinimumLabel( mMinLabelLineEdit->text() );
Expand All @@ -67,6 +75,7 @@ void QgsColorRampLegendNodeWidget::setSettings( const QgsColorRampLegendNodeSett
mBlockSignals = true;

mSettings = settings;
mUseContinuousLegendCheckBox->setChecked( settings.useContinuousLegend() );
mMinLabelLineEdit->setText( settings.minimumLabel() );
mMaxLabelLineEdit->setText( settings.maximumLabel() );
mPrefixLineEdit->setText( settings.prefix() );
Expand All @@ -78,6 +87,11 @@ void QgsColorRampLegendNodeWidget::setSettings( const QgsColorRampLegendNodeSett
mBlockSignals = false;
}

void QgsColorRampLegendNodeWidget::setUseContinuousRampCheckBoxVisibility( bool visible )
{
mUseContinuousLegendCheckBox->setVisible( visible );
}

void QgsColorRampLegendNodeWidget::changeNumberFormat()
{
QgsNumericFormatSelectorWidget *widget = new QgsNumericFormatSelectorWidget( this );
Expand Down Expand Up @@ -148,3 +162,8 @@ QDialogButtonBox *QgsColorRampLegendNodeDialog::buttonBox() const
{
return mButtonBox;
}

void QgsColorRampLegendNodeDialog::setUseContinuousRampCheckBoxVisibility( bool visible )
{
mWidget->setUseContinuousRampCheckBoxVisibility( visible );
}
18 changes: 18 additions & 0 deletions src/gui/qgscolorramplegendnodewidget.h
Expand Up @@ -63,6 +63,15 @@ class GUI_EXPORT QgsColorRampLegendNodeWidget: public QgsPanelWidget, private Ui
*/
void setSettings( const QgsColorRampLegendNodeSettings &settings );

/**
* Sets visibility for the "Use Continuous Legend" checkbox to \a visible.
*
* This widget is visible and checked by default but in a few cases it does not
* need to be visible because disabling it would not make sense (for instance
* when using single band gray renderer).
*/
void setUseContinuousRampCheckBoxVisibility( bool visible );

private slots:

void onChanged();
Expand Down Expand Up @@ -102,6 +111,15 @@ class GUI_EXPORT QgsColorRampLegendNodeDialog : public QDialog
*/
QDialogButtonBox *buttonBox() const;

/**
* Sets visibility for the "Use Continuous Legend" checkbox in the legend settings dialog to \a visible.
*
* This widget is visible and checked by default but in a few cases it does not
* need to be visible because disabling it would not make sense (for instance
* when using single band gray renderer).
*/
void setUseContinuousRampCheckBoxVisibility( bool visible );

private:

QgsColorRampLegendNodeWidget *mWidget = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions src/gui/raster/qgssinglebandgrayrendererwidget.cpp
Expand Up @@ -229,6 +229,7 @@ void QgsSingleBandGrayRendererWidget::showLegendSettings()
if ( panel && panel->dockMode() )
{
QgsColorRampLegendNodeWidget *legendPanel = new QgsColorRampLegendNodeWidget();
legendPanel->setUseContinuousRampCheckBoxVisibility( false );
legendPanel->setPanelTitle( tr( "Legend Settings" ) );
legendPanel->setSettings( mLegendSettings );
connect( legendPanel, &QgsColorRampLegendNodeWidget::widgetChanged, this, [ = ]
Expand All @@ -241,6 +242,7 @@ void QgsSingleBandGrayRendererWidget::showLegendSettings()
else
{
QgsColorRampLegendNodeDialog dialog( mLegendSettings, this );
dialog.setUseContinuousRampCheckBoxVisibility( false );
dialog.setWindowTitle( tr( "Legend Settings" ) );
if ( dialog.exec() )
{
Expand Down

0 comments on commit 5dc2028

Please sign in to comment.