Skip to content
Permalink
Browse files

Add option to render color ramp legend items as horizontal bars

  • Loading branch information
nyalldawson committed Dec 18, 2020
1 parent eaea2aa commit 2ec448053f1d2716fefb0faa103a7e578dd7f3ff
@@ -44,13 +44,17 @@ Copy constructor
Returns the direction of the ramp.

.. seealso:: :py:func:`setDirection`

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

void setDirection( QgsColorRampLegendNodeSettings::Direction direction );
%Docstring
Sets the ``direction`` of the ramp.

.. seealso:: :py:func:`direction`

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

QString minimumLabel() const;
@@ -175,6 +179,24 @@ Returns the text format used to render text in the legend item.
Sets the text ``format`` used to render text in the legend item.

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

Qt::Orientation orientation() const;
%Docstring
Returns the ramp orientation (i.e. horizontal or vertical).

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

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

void setOrientation( Qt::Orientation orientation );
%Docstring
Sets the ramp ``orientation`` (i.e. horizontal or vertical).

.. seealso:: :py:func:`orientation`

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

};

Large diffs are not rendered by default.

@@ -99,6 +99,8 @@ class CORE_EXPORT QgsColorRampLegendNode : public QgsLayerTreeModelLegendNode
void setSettings( const QgsColorRampLegendNodeSettings &settings );

private:
void init( QgsLayerTreeLayer *nodeLayer );

QString labelForMinimum() const;
QString labelForMaximum() const;

@@ -33,6 +33,7 @@ QgsColorRampLegendNodeSettings::QgsColorRampLegendNodeSettings( const QgsColorRa
, mDirection( other.mDirection )
, mNumericFormat( other.numericFormat()->clone() )
, mTextFormat( other.textFormat() )
, mOrientation( other.mOrientation )
{

}
@@ -46,6 +47,7 @@ QgsColorRampLegendNodeSettings &QgsColorRampLegendNodeSettings::operator=( const
mDirection = other.mDirection;
mNumericFormat.reset( other.numericFormat()->clone() );
mTextFormat = other.mTextFormat;
mOrientation = other.mOrientation;
return *this;
}

@@ -100,6 +102,7 @@ void QgsColorRampLegendNodeSettings::writeXml( QDomDocument &doc, QDomElement &e
settingsElement.setAttribute( QStringLiteral( "prefix" ), mPrefix );
settingsElement.setAttribute( QStringLiteral( "suffix" ), mSuffix );
settingsElement.setAttribute( QStringLiteral( "direction" ), static_cast< int >( mDirection ) );
settingsElement.setAttribute( QStringLiteral( "orientation" ), static_cast< int >( mOrientation ) );

QDomElement numericFormatElem = doc.createElement( QStringLiteral( "numericFormat" ) );
mNumericFormat->writeXml( numericFormatElem, doc, context );
@@ -123,6 +126,7 @@ void QgsColorRampLegendNodeSettings::readXml( const QDomElement &element, const
mPrefix = settingsElement.attribute( QStringLiteral( "prefix" ) );
mSuffix = settingsElement.attribute( QStringLiteral( "suffix" ) );
mDirection = static_cast< QgsColorRampLegendNodeSettings::Direction >( settingsElement.attribute( QStringLiteral( "direction" ) ).toInt() );
mOrientation = static_cast< Qt::Orientation >( settingsElement.attribute( QStringLiteral( "orientation" ), QString::number( Qt::Vertical ) ).toInt() );

QDomNodeList numericFormatNodeList = settingsElement.elementsByTagName( QStringLiteral( "numericFormat" ) );
if ( !numericFormatNodeList.isEmpty() )
@@ -171,3 +175,13 @@ void QgsColorRampLegendNodeSettings::setTextFormat( const QgsTextFormat &format
{
mTextFormat = format;
}

Qt::Orientation QgsColorRampLegendNodeSettings::orientation() const
{
return mOrientation;
}

void QgsColorRampLegendNodeSettings::setOrientation( Qt::Orientation orientation )
{
mOrientation = orientation;
}
@@ -60,13 +60,15 @@ class CORE_EXPORT QgsColorRampLegendNodeSettings
* Returns the direction of the ramp.
*
* \see setDirection()
* \see orientation()
*/
QgsColorRampLegendNodeSettings::Direction direction() const;

/**
* Sets the \a direction of the ramp.
*
* \see direction()
* \see setOrientation()
*/
void setDirection( QgsColorRampLegendNodeSettings::Direction direction );

@@ -184,6 +186,22 @@ class CORE_EXPORT QgsColorRampLegendNodeSettings
*/
void setTextFormat( const QgsTextFormat &format );

/**
* Returns the ramp orientation (i.e. horizontal or vertical).
*
* \see setOrientation()
* \see direction()
*/
Qt::Orientation orientation() const;

/**
* Sets the ramp \a orientation (i.e. horizontal or vertical).
*
* \see orientation()
* \see setDirection()
*/
void setOrientation( Qt::Orientation orientation );

private:
QString mMinimumLabel;
QString mMaximumLabel;
@@ -192,6 +210,7 @@ class CORE_EXPORT QgsColorRampLegendNodeSettings
Direction mDirection = MinimumToMaximum;
std::unique_ptr< QgsNumericFormat > mNumericFormat;
QgsTextFormat mTextFormat;
Qt::Orientation mOrientation = Qt::Vertical;
};

#endif // QGSCOLORRAMPLEGENDNODESETTINGS_H
@@ -29,6 +29,9 @@ QgsColorRampLegendNodeWidget::QgsColorRampLegendNodeWidget( QWidget *parent )
mDirectionComboBox->addItem( tr( "Maximum on Top" ), QgsColorRampLegendNodeSettings::MinimumToMaximum );
mDirectionComboBox->addItem( tr( "Minimum on Top" ), QgsColorRampLegendNodeSettings::MaximumToMinimum );

mOrientationComboBox->addItem( tr( "Vertical" ), Qt::Vertical );
mOrientationComboBox->addItem( tr( "Horizontal" ), Qt::Horizontal );

mMinLabelLineEdit->setPlaceholderText( tr( "Default" ) );
mMaxLabelLineEdit->setPlaceholderText( tr( "Default" ) );

@@ -40,6 +43,7 @@ QgsColorRampLegendNodeWidget::QgsColorRampLegendNodeWidget( QWidget *parent )
connect( mPrefixLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
connect( mSuffixLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
connect( mDirectionComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, &QgsColorRampLegendNodeWidget::onChanged );
connect( mOrientationComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, &QgsColorRampLegendNodeWidget::onOrientationChanged );
connect( mNumberFormatPushButton, &QPushButton::clicked, this, &QgsColorRampLegendNodeWidget::changeNumberFormat );
connect( mFontButton, &QgsFontButton::changed, this, &QgsColorRampLegendNodeWidget::onChanged );
}
@@ -48,6 +52,7 @@ QgsColorRampLegendNodeSettings QgsColorRampLegendNodeWidget::settings() const
{
QgsColorRampLegendNodeSettings settings;
settings.setDirection( static_cast< QgsColorRampLegendNodeSettings::Direction >( mDirectionComboBox->currentData().toInt() ) );
settings.setOrientation( static_cast< Qt::Orientation >( mOrientationComboBox->currentData().toInt() ) );
settings.setMinimumLabel( mMinLabelLineEdit->text() );
settings.setMaximumLabel( mMaxLabelLineEdit->text() );
settings.setPrefix( mPrefixLineEdit->text() );
@@ -67,7 +72,9 @@ void QgsColorRampLegendNodeWidget::setSettings( const QgsColorRampLegendNodeSett
mPrefixLineEdit->setText( settings.prefix() );
mSuffixLineEdit->setText( settings.suffix() );
mDirectionComboBox->setCurrentIndex( mDirectionComboBox->findData( settings.direction() ) );
mOrientationComboBox->setCurrentIndex( mOrientationComboBox->findData( settings.orientation() ) );
mFontButton->setTextFormat( settings.textFormat() );
onOrientationChanged();
mBlockSignals = false;
}

@@ -85,6 +92,24 @@ void QgsColorRampLegendNodeWidget::changeNumberFormat()
return;
}

void QgsColorRampLegendNodeWidget::onOrientationChanged()
{
switch ( static_cast< Qt::Orientation >( mOrientationComboBox->currentData().toInt() ) )
{
case Qt::Vertical:
mDirectionComboBox->setItemText( 0, tr( "Maximum on Top" ) );
mDirectionComboBox->setItemText( 1, tr( "Minimum on Top" ) );
break;

case Qt::Horizontal:
mDirectionComboBox->setItemText( 0, tr( "Maximum on Right" ) );
mDirectionComboBox->setItemText( 1, tr( "Minimum on Right" ) );
break;
}

onChanged();
}

void QgsColorRampLegendNodeWidget::onChanged()
{
if ( mBlockSignals )
@@ -67,6 +67,7 @@ class GUI_EXPORT QgsColorRampLegendNodeWidget: public QgsPanelWidget, private Ui

void onChanged();
void changeNumberFormat();
void onOrientationChanged();

private:

@@ -131,15 +131,25 @@
<string>Layout</string>
</property>
<layout class="QGridLayout" name="gridLayout_2" columnstretch="0,0">
<item row="0" column="0">
<item row="1" column="1">
<widget class="QComboBox" name="mDirectionComboBox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Direction</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Orientation</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="mDirectionComboBox"/>
<widget class="QComboBox" name="mOrientationComboBox"/>
</item>
</layout>
</widget>
@@ -183,6 +193,8 @@
<tabstop>mMinLabelLineEdit</tabstop>
<tabstop>mMaxLabelLineEdit</tabstop>
<tabstop>mNumberFormatPushButton</tabstop>
<tabstop>mFontButton</tabstop>
<tabstop>mOrientationComboBox</tabstop>
<tabstop>mDirectionComboBox</tabstop>
</tabstops>
<resources/>

0 comments on commit 2ec4480

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