Skip to content
Permalink
Browse files

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 authored and github-actions committed Mar 12, 2021
1 parent 6eeedbb commit acadc4062128a12b836c33cfd36be92a25423523
@@ -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

};
@@ -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

};
@@ -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

};
@@ -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 )
@@ -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;
@@ -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 );
@@ -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" ) );
@@ -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;
}
@@ -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;
@@ -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:
{
@@ -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:
{
@@ -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 );
@@ -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() );
@@ -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() );
@@ -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 );
@@ -148,3 +162,8 @@ QDialogButtonBox *QgsColorRampLegendNodeDialog::buttonBox() const
{
return mButtonBox;
}

void QgsColorRampLegendNodeDialog::setUseContinuousRampCheckBoxVisibility( bool visible )
{
mWidget->setUseContinuousRampCheckBoxVisibility( visible );
}
@@ -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();
@@ -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;
@@ -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, [ = ]
@@ -241,6 +242,7 @@ void QgsSingleBandGrayRendererWidget::showLegendSettings()
else
{
QgsColorRampLegendNodeDialog dialog( mLegendSettings, this );
dialog.setUseContinuousRampCheckBoxVisibility( false );
dialog.setWindowTitle( tr( "Legend Settings" ) );
if ( dialog.exec() )
{

0 comments on commit acadc40

Please sign in to comment.