Drop units API for auto wrap distance

This isn't exposed, and needs more thought before it is (should
we use RenderUnits or LayoutUnits?)
This commit is contained in:
Nyall Dawson 2025-04-08 08:52:38 +10:00
parent 61009bdf95
commit bbdc235c61
9 changed files with 22 additions and 213 deletions

View File

@ -578,56 +578,25 @@ The stroke is only drawn if
double autoWrapLinesAfter() const;
%Docstring
Returns the maximum line length allowed before lines of text in the
legend will be automatically word wrapped.
Units are retrieved via
:py:func:`~QgsLayoutItemLegend.autoWrapLinesUnit`.
Returns the maximum line length (in millimeters) allowed before lines of
text in the legend will be automatically word wrapped.
If the returned value is 0, then no automatic wrapping will occur.
.. seealso:: :py:func:`setAutoWrapLinesAfter`
.. seealso:: :py:func:`autoWrapLinesUnit`
.. versionadded:: 3.44
%End
void setAutoWrapLinesAfter( double length );
%Docstring
Sets the maximum line ``length`` allowed before lines of text in the
legend will be automatically word wrapped.
Units are set via :py:func:`~QgsLayoutItemLegend.setAutoWrapLinesUnit`.
Sets the maximum line ``length`` (in millimeters) allowed before lines
of text in the legend will be automatically word wrapped.
If ``length`` is 0, then no automatic wrapping will occur.
.. seealso:: :py:func:`autoWrapLinesAfter`
.. seealso:: :py:func:`setAutoWrapLinesUnit`
.. versionadded:: 3.44
%End
Qgis::RenderUnit autoWrapLinesUnit() const;
%Docstring
Returns the units used for the automatic maximum line length wrapping.
.. seealso:: :py:func:`autoWrapLinesAfter`
.. seealso:: :py:func:`setAutoWrapLinesUnit`
.. versionadded:: 3.44
%End
void setAutoWrapLinesUnit( Qgis::RenderUnit unit );
%Docstring
Sets the ``unit`` used for the automatic maximum line length wrapping.
.. seealso:: :py:func:`setAutoWrapLinesAfter`
.. seealso:: :py:func:`autoWrapLinesUnit`
.. versionadded:: 3.44
%End

View File

@ -628,55 +628,25 @@ Sets the JSON export flags to ``jsonRenderFlags``.
double autoWrapLinesAfter() const;
%Docstring
Returns the maximum line length allowed before lines of text in the
legend will be automatically word wrapped.
Units are retrieved via :py:func:`~QgsLegendSettings.autoWrapLinesUnit`.
Returns the maximum line length (in millimeters) allowed before lines of
text in the legend will be automatically word wrapped.
If the returned value is 0, then no automatic wrapping will occur.
.. seealso:: :py:func:`setAutoWrapLinesAfter`
.. seealso:: :py:func:`autoWrapLinesUnit`
.. versionadded:: 3.44
%End
void setAutoWrapLinesAfter( double length );
%Docstring
Sets the maximum line ``length`` allowed before lines of text in the
legend will be automatically word wrapped.
Units are set via :py:func:`~QgsLegendSettings.setAutoWrapLinesUnit`.
Sets the maximum line ``length`` (in millimeters) allowed before lines
of text in the legend will be automatically word wrapped.
If ``length`` is 0, then no automatic wrapping will occur.
.. seealso:: :py:func:`autoWrapLinesAfter`
.. seealso:: :py:func:`setAutoWrapLinesUnit`
.. versionadded:: 3.44
%End
Qgis::RenderUnit autoWrapLinesUnit() const;
%Docstring
Returns the units used for the automatic maximum line length wrapping.
.. seealso:: :py:func:`autoWrapLinesAfter`
.. seealso:: :py:func:`setAutoWrapLinesUnit`
.. versionadded:: 3.44
%End
void setAutoWrapLinesUnit( Qgis::RenderUnit unit );
%Docstring
Sets the ``unit`` used for the automatic maximum line length wrapping.
.. seealso:: :py:func:`setAutoWrapLinesAfter`
.. seealso:: :py:func:`autoWrapLinesUnit`
.. versionadded:: 3.44
%End

View File

@ -578,56 +578,25 @@ The stroke is only drawn if
double autoWrapLinesAfter() const;
%Docstring
Returns the maximum line length allowed before lines of text in the
legend will be automatically word wrapped.
Units are retrieved via
:py:func:`~QgsLayoutItemLegend.autoWrapLinesUnit`.
Returns the maximum line length (in millimeters) allowed before lines of
text in the legend will be automatically word wrapped.
If the returned value is 0, then no automatic wrapping will occur.
.. seealso:: :py:func:`setAutoWrapLinesAfter`
.. seealso:: :py:func:`autoWrapLinesUnit`
.. versionadded:: 3.44
%End
void setAutoWrapLinesAfter( double length );
%Docstring
Sets the maximum line ``length`` allowed before lines of text in the
legend will be automatically word wrapped.
Units are set via :py:func:`~QgsLayoutItemLegend.setAutoWrapLinesUnit`.
Sets the maximum line ``length`` (in millimeters) allowed before lines
of text in the legend will be automatically word wrapped.
If ``length`` is 0, then no automatic wrapping will occur.
.. seealso:: :py:func:`autoWrapLinesAfter`
.. seealso:: :py:func:`setAutoWrapLinesUnit`
.. versionadded:: 3.44
%End
Qgis::RenderUnit autoWrapLinesUnit() const;
%Docstring
Returns the units used for the automatic maximum line length wrapping.
.. seealso:: :py:func:`autoWrapLinesAfter`
.. seealso:: :py:func:`setAutoWrapLinesUnit`
.. versionadded:: 3.44
%End
void setAutoWrapLinesUnit( Qgis::RenderUnit unit );
%Docstring
Sets the ``unit`` used for the automatic maximum line length wrapping.
.. seealso:: :py:func:`setAutoWrapLinesAfter`
.. seealso:: :py:func:`autoWrapLinesUnit`
.. versionadded:: 3.44
%End

View File

@ -628,55 +628,25 @@ Sets the JSON export flags to ``jsonRenderFlags``.
double autoWrapLinesAfter() const;
%Docstring
Returns the maximum line length allowed before lines of text in the
legend will be automatically word wrapped.
Units are retrieved via :py:func:`~QgsLegendSettings.autoWrapLinesUnit`.
Returns the maximum line length (in millimeters) allowed before lines of
text in the legend will be automatically word wrapped.
If the returned value is 0, then no automatic wrapping will occur.
.. seealso:: :py:func:`setAutoWrapLinesAfter`
.. seealso:: :py:func:`autoWrapLinesUnit`
.. versionadded:: 3.44
%End
void setAutoWrapLinesAfter( double length );
%Docstring
Sets the maximum line ``length`` allowed before lines of text in the
legend will be automatically word wrapped.
Units are set via :py:func:`~QgsLegendSettings.setAutoWrapLinesUnit`.
Sets the maximum line ``length`` (in millimeters) allowed before lines
of text in the legend will be automatically word wrapped.
If ``length`` is 0, then no automatic wrapping will occur.
.. seealso:: :py:func:`autoWrapLinesAfter`
.. seealso:: :py:func:`setAutoWrapLinesUnit`
.. versionadded:: 3.44
%End
Qgis::RenderUnit autoWrapLinesUnit() const;
%Docstring
Returns the units used for the automatic maximum line length wrapping.
.. seealso:: :py:func:`autoWrapLinesAfter`
.. seealso:: :py:func:`setAutoWrapLinesUnit`
.. versionadded:: 3.44
%End
void setAutoWrapLinesUnit( Qgis::RenderUnit unit );
%Docstring
Sets the ``unit`` used for the automatic maximum line length wrapping.
.. seealso:: :py:func:`setAutoWrapLinesAfter`
.. seealso:: :py:func:`autoWrapLinesUnit`
.. versionadded:: 3.44
%End

View File

@ -101,7 +101,7 @@ QgsLayerTreeModelLegendNode::ItemMetrics QgsLayerTreeModelLegendNode::draw( cons
if ( settings.autoWrapLinesAfter() > 0 )
{
documentContext.setMaximumWidth( ctx->context->convertToPainterUnits( settings.autoWrapLinesAfter(), settings.autoWrapLinesUnit() ) );
documentContext.setMaximumWidth( ctx->context->convertToPainterUnits( settings.autoWrapLinesAfter(), Qgis::RenderUnit::Millimeters ) );
documentContext.setFlags( Qgis::TextRendererFlag::WrapLines );
}

View File

@ -648,16 +648,6 @@ void QgsLayoutItemLegend::setAutoWrapLinesAfter( double length )
mSettings.setAutoWrapLinesAfter( length );
}
Qgis::RenderUnit QgsLayoutItemLegend::autoWrapLinesUnit() const
{
return mSettings.autoWrapLinesUnit();
}
void QgsLayoutItemLegend::setAutoWrapLinesUnit( Qgis::RenderUnit unit )
{
mSettings.setAutoWrapLinesUnit( unit );
}
void QgsLayoutItemLegend::updateLegend()
{
adjustBoxSize();
@ -694,10 +684,6 @@ bool QgsLayoutItemLegend::writePropertiesToElement( QDomElement &legendElem, QDo
{
legendElem.setAttribute( QStringLiteral( "autoWrapLinesAfter" ), mSettings.autoWrapLinesAfter() );
}
if ( mSettings.autoWrapLinesUnit() != Qgis::RenderUnit::Millimeters )
{
legendElem.setAttribute( QStringLiteral( "autoWrapLinesAfterUnit" ), QgsUnitTypes::encodeUnit( mSettings.autoWrapLinesUnit() ) );
}
legendElem.setAttribute( QStringLiteral( "wmsLegendWidth" ), QString::number( mSettings.wmsLegendSize().width() ) );
legendElem.setAttribute( QStringLiteral( "wmsLegendHeight" ), QString::number( mSettings.wmsLegendSize().height() ) );
@ -840,7 +826,6 @@ bool QgsLayoutItemLegend::readPropertiesFromElement( const QDomElement &itemElem
mSettings.setRasterStrokeWidth( itemElem.attribute( QStringLiteral( "rasterBorderWidth" ), QStringLiteral( "0" ) ).toDouble() );
mSettings.setAutoWrapLinesAfter( itemElem.attribute( QStringLiteral( "autoWrapLinesAfter" ), QStringLiteral( "0" ) ).toDouble() );
mSettings.setAutoWrapLinesUnit( QgsUnitTypes::decodeRenderUnit( itemElem.attribute( QStringLiteral( "autoWrapLinesAfterUnit" ), QStringLiteral( "MM" ) ) ) );
mSettings.setWrapChar( itemElem.attribute( QStringLiteral( "wrapChar" ) ) );

View File

@ -533,55 +533,29 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
void setRasterStrokeWidth( double width );
/**
* Returns the maximum line length allowed before lines of text in the legend
* Returns the maximum line length (in millimeters) allowed before lines of text in the legend
* will be automatically word wrapped.
*
* Units are retrieved via autoWrapLinesUnit().
*
* If the returned value is 0, then no automatic wrapping will occur.
*
* \see setAutoWrapLinesAfter()
* \see autoWrapLinesUnit()
*
* \since QGIS 3.44
*/
double autoWrapLinesAfter() const;
/**
* Sets the maximum line \a length allowed before lines of text in the legend
* Sets the maximum line \a length (in millimeters) allowed before lines of text in the legend
* will be automatically word wrapped.
*
* Units are set via setAutoWrapLinesUnit().
*
* If \a length is 0, then no automatic wrapping will occur.
*
* \see autoWrapLinesAfter()
* \see setAutoWrapLinesUnit()
*
* \since QGIS 3.44
*/
void setAutoWrapLinesAfter( double length );
/**
* Returns the units used for the automatic maximum line length wrapping.
*
* \see autoWrapLinesAfter()
* \see setAutoWrapLinesUnit()
*
* \since QGIS 3.44
*/
Qgis::RenderUnit autoWrapLinesUnit() const;
/**
* Sets the \a unit used for the automatic maximum line length wrapping.
*
* \see setAutoWrapLinesAfter()
* \see autoWrapLinesUnit()
*
* \since QGIS 3.44
*/
void setAutoWrapLinesUnit( Qgis::RenderUnit unit );
/**
* Sets the \a map to associate with the legend.
* \see linkedMap()

View File

@ -534,55 +534,29 @@ class CORE_EXPORT QgsLegendSettings
void setJsonRenderFlags( const Qgis::LegendJsonRenderFlags &jsonRenderFlags );
/**
* Returns the maximum line length allowed before lines of text in the legend
* Returns the maximum line length (in millimeters) allowed before lines of text in the legend
* will be automatically word wrapped.
*
* Units are retrieved via autoWrapLinesUnit().
*
* If the returned value is 0, then no automatic wrapping will occur.
*
* \see setAutoWrapLinesAfter()
* \see autoWrapLinesUnit()
*
* \since QGIS 3.44
*/
double autoWrapLinesAfter() const { return mAutoWrapLinesAfter; }
/**
* Sets the maximum line \a length allowed before lines of text in the legend
* Sets the maximum line \a length (in millimeters) allowed before lines of text in the legend
* will be automatically word wrapped.
*
* Units are set via setAutoWrapLinesUnit().
*
* If \a length is 0, then no automatic wrapping will occur.
*
* \see autoWrapLinesAfter()
* \see setAutoWrapLinesUnit()
*
* \since QGIS 3.44
*/
void setAutoWrapLinesAfter( double length ) { mAutoWrapLinesAfter = length; }
/**
* Returns the units used for the automatic maximum line length wrapping.
*
* \see autoWrapLinesAfter()
* \see setAutoWrapLinesUnit()
*
* \since QGIS 3.44
*/
Qgis::RenderUnit autoWrapLinesUnit() const { return mAutoWrapLinesUnit; }
/**
* Sets the \a unit used for the automatic maximum line length wrapping.
*
* \see setAutoWrapLinesAfter()
* \see autoWrapLinesUnit()
*
* \since QGIS 3.44
*/
void setAutoWrapLinesUnit( Qgis::RenderUnit unit ) { mAutoWrapLinesUnit = unit; }
private:
QString mTitle;
@ -593,7 +567,6 @@ class CORE_EXPORT QgsLegendSettings
QString mWrapChar;
double mAutoWrapLinesAfter = 0;
Qgis::RenderUnit mAutoWrapLinesUnit = Qgis::RenderUnit::Millimeters;
//! Space between item box and contents
qreal mBoxSpace = 2;

View File

@ -1954,8 +1954,7 @@ class TestQgsLayoutItemLegend(QgisTestCase, LayoutItemTestCase):
legend.setTitle("")
layout.addLayoutItem(legend)
legend.setAutoWrapLinesAfter(200)
legend.setAutoWrapLinesUnit(Qgis.RenderUnit.Points)
legend.setAutoWrapLinesAfter(70.5556)
legend.setStyleFont(
QgsLegendStyle.Style.Title, QgsFontUtils.getStandardTestFont("Bold", 16)