Skip to content

Commit

Permalink
Add option to render color ramp legend items as horizontal bars
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 18, 2020
1 parent eaea2aa commit 2ec4480
Show file tree
Hide file tree
Showing 14 changed files with 415 additions and 42 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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

};
Expand Down
217 changes: 177 additions & 40 deletions src/core/layertree/qgscolorramplegendnode.cpp

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/core/layertree/qgscolorramplegendnode.h
Expand Up @@ -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;

Expand Down
14 changes: 14 additions & 0 deletions src/core/layertree/qgscolorramplegendnodesettings.cpp
Expand Up @@ -33,6 +33,7 @@ QgsColorRampLegendNodeSettings::QgsColorRampLegendNodeSettings( const QgsColorRa
, mDirection( other.mDirection )
, mNumericFormat( other.numericFormat()->clone() )
, mTextFormat( other.textFormat() )
, mOrientation( other.mOrientation )
{

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

Expand Down Expand Up @@ -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 );
Expand All @@ -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() )
Expand Down Expand Up @@ -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;
}
19 changes: 19 additions & 0 deletions src/core/layertree/qgscolorramplegendnodesettings.h
Expand Up @@ -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 );

Expand Down Expand Up @@ -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;
Expand All @@ -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
25 changes: 25 additions & 0 deletions src/gui/qgscolorramplegendnodewidget.cpp
Expand Up @@ -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" ) );

Expand All @@ -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 );
}
Expand All @@ -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() );
Expand All @@ -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;
}

Expand All @@ -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 )
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgscolorramplegendnodewidget.h
Expand Up @@ -67,6 +67,7 @@ class GUI_EXPORT QgsColorRampLegendNodeWidget: public QgsPanelWidget, private Ui

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

private:

Expand Down
16 changes: 14 additions & 2 deletions src/ui/qgscolorramplegendnodewidgetbase.ui
Expand Up @@ -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>
Expand Down Expand Up @@ -183,6 +193,8 @@
<tabstop>mMinLabelLineEdit</tabstop>
<tabstop>mMaxLabelLineEdit</tabstop>
<tabstop>mNumberFormatPushButton</tabstop>
<tabstop>mFontButton</tabstop>
<tabstop>mOrientationComboBox</tabstop>
<tabstop>mDirectionComboBox</tabstop>
</tabstops>
<resources/>
Expand Down

0 comments on commit 2ec4480

Please sign in to comment.