Skip to content
Permalink
Browse files

Allow users to set text format for ramp legend labels

  • Loading branch information
nyalldawson committed Dec 18, 2020
1 parent 13e4969 commit eaea2aaf10cd9a6e6cd9b7950c6d5d984a1a309e
@@ -161,6 +161,20 @@ Sets the ``suffix`` to show after legend text.
.. seealso:: :py:func:`suffix`

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

QgsTextFormat textFormat() const;
%Docstring
Returns the text format used to render text in the legend item.

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

void setTextFormat( const QgsTextFormat &format );
%Docstring
Sets the text ``format`` used to render text in the legend item.

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

};
@@ -172,8 +172,7 @@ QSizeF QgsColorRampLegendNode::drawSymbol( const QgsLegendSettings &settings, It
}

QFont symbolLabelFont = settings.style( QgsLegendStyle::SymbolLabel ).font();
QgsTextFormat format = QgsTextFormat::fromQFont( symbolLabelFont );
format.setColor( settings.fontColor() );
const QgsTextFormat format = mSettings.textFormat().isValid() ? mSettings.textFormat() : QgsTextFormat::fromQFont( symbolLabelFont );

const QString minLabel = labelForMinimum();
const QString maxLabel = labelForMaximum();
@@ -279,8 +278,9 @@ QSizeF QgsColorRampLegendNode::drawSymbolText( const QgsLegendSettings &settings
}

QFont symbolLabelFont = settings.style( QgsLegendStyle::SymbolLabel ).font();
QgsTextFormat format = QgsTextFormat::fromQFont( symbolLabelFont );
format.setColor( settings.fontColor() );
QgsTextFormat format = mSettings.textFormat().isValid() ? mSettings.textFormat() : QgsTextFormat::fromQFont( symbolLabelFont );
if ( !mSettings.textFormat().isValid() )
format.setColor( settings.fontColor() );

const QString minLabel = labelForMinimum();
const QString maxLabel = labelForMaximum();
@@ -32,6 +32,7 @@ QgsColorRampLegendNodeSettings::QgsColorRampLegendNodeSettings( const QgsColorRa
, mSuffix( other.mSuffix )
, mDirection( other.mDirection )
, mNumericFormat( other.numericFormat()->clone() )
, mTextFormat( other.textFormat() )
{

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

@@ -103,6 +105,11 @@ void QgsColorRampLegendNodeSettings::writeXml( QDomDocument &doc, QDomElement &e
mNumericFormat->writeXml( numericFormatElem, doc, context );
settingsElement.appendChild( numericFormatElem );

if ( mTextFormat.isValid() )
{
settingsElement.appendChild( mTextFormat.writeXml( doc, context ) );
}

element.appendChild( settingsElement );
}

@@ -123,6 +130,15 @@ void QgsColorRampLegendNodeSettings::readXml( const QDomElement &element, const
QDomElement numericFormatElem = numericFormatNodeList.at( 0 ).toElement();
mNumericFormat.reset( QgsApplication::numericFormatRegistry()->createFromXml( numericFormatElem, context ) );
}

if ( !settingsElement.firstChildElement( QStringLiteral( "text-style" ) ).isNull() )
{
mTextFormat.readXml( settingsElement, context );
}
else
{
mTextFormat = QgsTextFormat();
}
}
}

@@ -145,3 +161,13 @@ void QgsColorRampLegendNodeSettings::setSuffix( const QString &suffix )
{
mSuffix = suffix;
}

QgsTextFormat QgsColorRampLegendNodeSettings::textFormat() const
{
return mTextFormat;
}

void QgsColorRampLegendNodeSettings::setTextFormat( const QgsTextFormat &format )
{
mTextFormat = format;
}
@@ -19,6 +19,7 @@
#include "qgis_core.h"
#include "qgis_sip.h"

#include "qgstextformat.h"
#include <QString>
#include <memory>

@@ -169,13 +170,28 @@ class CORE_EXPORT QgsColorRampLegendNodeSettings
*/
void setSuffix( const QString &suffix );

/**
* Returns the text format used to render text in the legend item.
*
* \see setTextFormat()
*/
QgsTextFormat textFormat() const;

/**
* Sets the text \a format used to render text in the legend item.
*
* \see textFormat()
*/
void setTextFormat( const QgsTextFormat &format );

private:
QString mMinimumLabel;
QString mMaximumLabel;
QString mPrefix;
QString mSuffix;
Direction mDirection = MinimumToMaximum;
std::unique_ptr< QgsNumericFormat > mNumericFormat;
QgsTextFormat mTextFormat;
};

#endif // QGSCOLORRAMPLEGENDNODESETTINGS_H
@@ -32,12 +32,16 @@ QgsColorRampLegendNodeWidget::QgsColorRampLegendNodeWidget( QWidget *parent )
mMinLabelLineEdit->setPlaceholderText( tr( "Default" ) );
mMaxLabelLineEdit->setPlaceholderText( tr( "Default" ) );

mFontButton->setShowNullFormat( true );
mFontButton->setNoFormatString( tr( "Default" ) );

connect( mMinLabelLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
connect( mMaxLabelLineEdit, &QLineEdit::textChanged, this, &QgsColorRampLegendNodeWidget::onChanged );
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( mNumberFormatPushButton, &QPushButton::clicked, this, &QgsColorRampLegendNodeWidget::changeNumberFormat );
connect( mFontButton, &QgsFontButton::changed, this, &QgsColorRampLegendNodeWidget::onChanged );
}

QgsColorRampLegendNodeSettings QgsColorRampLegendNodeWidget::settings() const
@@ -49,6 +53,7 @@ QgsColorRampLegendNodeSettings QgsColorRampLegendNodeWidget::settings() const
settings.setPrefix( mPrefixLineEdit->text() );
settings.setSuffix( mSuffixLineEdit->text() );
settings.setNumericFormat( mSettings.numericFormat()->clone() );
settings.setTextFormat( mFontButton->textFormat() );
return settings;
}

@@ -62,7 +67,7 @@ void QgsColorRampLegendNodeWidget::setSettings( const QgsColorRampLegendNodeSett
mPrefixLineEdit->setText( settings.prefix() );
mSuffixLineEdit->setText( settings.suffix() );
mDirectionComboBox->setCurrentIndex( mDirectionComboBox->findData( settings.direction() ) );

mFontButton->setTextFormat( settings.textFormat() );
mBlockSignals = false;
}

@@ -86,7 +86,11 @@
</widget>
</item>
<item row="1" column="1">
<widget class="QgsDoubleSpinBox" name="mWidthSpinBox"/>
<widget class="QgsDoubleSpinBox" name="mWidthSpinBox">
<property name="maximum">
<double>9999.989999999999782</double>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="mPatchHeightLabel">
@@ -96,7 +100,11 @@
</widget>
</item>
<item row="2" column="1">
<widget class="QgsDoubleSpinBox" name="mHeightSpinBox"/>
<widget class="QgsDoubleSpinBox" name="mHeightSpinBox">
<property name="maximum">
<double>9999.989999999999782</double>
</property>
</widget>
</item>
</layout>
</widget>
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>359</width>
<height>290</height>
<height>368</height>
</rect>
</property>
<property name="sizePolicy">
@@ -38,16 +38,6 @@
<string>Labels</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Maximum</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QgsFilterLineEdit" name="mMinLabelLineEdit"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
@@ -58,6 +48,26 @@
<item row="3" column="1">
<widget class="QgsFilterLineEdit" name="mMaxLabelLineEdit"/>
</item>
<item row="5" column="1">
<widget class="QgsFontButton" name="mFontButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Font</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Suffix</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
@@ -75,26 +85,43 @@
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QPushButton" name="mNumberFormatPushButton">
<item row="0" column="1">
<widget class="QgsFilterLineEdit" name="mPrefixLineEdit"/>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Customize</string>
<string>Text format</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_5">
<item row="4" column="1">
<widget class="QPushButton" name="mNumberFormatPushButton">
<property name="text">
<string>Suffix</string>
<string>Customize</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QgsFilterLineEdit" name="mPrefixLineEdit"/>
<item row="2" column="1">
<widget class="QgsFilterLineEdit" name="mMinLabelLineEdit"/>
</item>
<item row="1" column="1">
<widget class="QgsFilterLineEdit" name="mSuffixLineEdit"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Maximum</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLabel" name="label_6">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-style:italic;&quot;&gt;(Applies to print layout legends only)&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@@ -144,6 +171,11 @@
<extends>QLineEdit</extends>
<header>qgsfilterlineedit.h</header>
</customwidget>
<customwidget>
<class>QgsFontButton</class>
<extends>QToolButton</extends>
<header>qgsfontbutton.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mPrefixLineEdit</tabstop>

0 comments on commit eaea2aa

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