Skip to content

Commit

Permalink
[feature] Support horizontal alignment in HTML labels
Browse files Browse the repository at this point in the history
For multi-line labels, this allows use of either:

- HTML <p align="xxx"> attributes
- CSS "text-align: xxx"
- HTML <center>some text</center> tags

Supported alignments are left, right, center and justify

Horizontal alignment can be used in all contexts where
HTML text is rendered, EXCEPT for curved labels (since they
are restricted to single-line text)

Sponsored by City of Freiburg im Breisgau
  • Loading branch information
nyalldawson committed Sep 11, 2024
1 parent 89c27fe commit ce8ce0a
Show file tree
Hide file tree
Showing 23 changed files with 650 additions and 8 deletions.
17 changes: 17 additions & 0 deletions python/PyQt6/core/auto_additions/qgstextblockformat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# The following has been generated automatically from src/core/textrenderer/qgstextblockformat.h
# monkey patching scoped based enum
QgsTextBlockFormat.BooleanValue.NotSet.__doc__ = "Property is not set"
QgsTextBlockFormat.BooleanValue.SetTrue.__doc__ = "Property is set and ``True``"
QgsTextBlockFormat.BooleanValue.SetFalse.__doc__ = "Property is set and ``False``"
QgsTextBlockFormat.BooleanValue.__doc__ = """Status values for boolean format properties
* ``NotSet``: Property is not set
* ``SetTrue``: Property is set and ``True``
* ``SetFalse``: Property is set and ``False``
"""
# --
try:
QgsTextBlockFormat.__group__ = ['textrenderer']
except NameError:
pass
18 changes: 18 additions & 0 deletions python/PyQt6/core/auto_generated/textrenderer/qgstextblock.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ Returns ``True`` if the block is empty.
int size() const;
%Docstring
Returns the number of fragments in the block.
%End

const QgsTextBlockFormat &blockFormat() const;
%Docstring
Returns the block formatting for the fragment.

.. seealso:: :py:func:`setBlockFormat`

.. versionadded:: 3.40
%End

void setBlockFormat( const QgsTextBlockFormat &format );
%Docstring
Sets the block ``format`` for the fragment.

.. seealso:: :py:func:`blockFormat`

.. versionadded:: 3.40
%End

void applyCapitalization( Qgis::Capitalization capitalization );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/textrenderer/qgstextblockformat.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.py again *
************************************************************************/





class QgsTextBlockFormat
{
%Docstring(signature="appended")
Stores information relating to individual block formatting.

These options encapsulate formatting options which override the default
settings from a :py:class:`QgsTextFormat` for individual text blocks.

.. warning::

This API is not considered stable and may change in future QGIS versions.

.. versionadded:: 3.40
%End

%TypeHeaderCode
#include "qgstextblockformat.h"
%End
public:

QgsTextBlockFormat();

QgsTextBlockFormat( const QTextBlockFormat &format );
%Docstring
Constructor for QgsTextBlockFormat, based on the specified QTextBlockFormat ``format``.
%End

enum class BooleanValue
{
NotSet,
SetTrue,
SetFalse,
};

void overrideWith( const QgsTextBlockFormat &other );
%Docstring
Override all the default/unset properties of the current block format
with the settings from another format.

This will replace any default/unset existing settings with the
settings from ``other``.

Any settings which are default/unset in ``other`` will be left unchanged.

:param other: The format to override with.
%End

bool hasHorizontalAlignmentSet() const;
%Docstring
Returns ``True`` if the format has an explicit horizontal alignment set.

If ``False`` is returned then the horizontal alignment will be inherited.

.. seealso:: :py:func:`setHasHorizontalAlignmentSet`

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

void setHasHorizontalAlignmentSet( bool set );
%Docstring
Sets whether the format has an explicit horizontal alignment ``set``.

If ``set`` is ``False`` then the horizontal alignment will be inherited.

.. seealso:: :py:func:`hasHorizontalAlignmentSet`

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

Qgis::TextHorizontalAlignment horizontalAlignment() const;
%Docstring
Returns the format horizontal alignment.

This property is only respected if :py:func:`~QgsTextBlockFormat.hasHorizontalAlignmentSet` is ``True``.

.. seealso:: :py:func:`hasHorizontalAlignmentSet`

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

void setHorizontalAlignment( Qgis::TextHorizontalAlignment alignment );
%Docstring
Sets the format horizontal ``alignment``.

This property is only respected if :py:func:`~QgsTextBlockFormat.hasHorizontalAlignmentSet` is ``True``.

.. seealso:: :py:func:`hasHorizontalAlignmentSet`

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

void updateFontForFormat( QFont &font, const QgsRenderContext &context, double scaleFactor = 1.0 ) const;
%Docstring
Updates the specified ``font`` in place, applying block formatting options which
are applicable on a font level when rendered in the given ``context``.

The optional ``scaleFactor`` parameter can specify a font size scaling factor. It is recommended to set this to
:py:func:`QgsTextRenderer.calculateScaleFactorForFormat()` and then manually calculations
based on the resultant font metrics. Failure to do so will result in poor quality text rendering
at small font sizes.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/textrenderer/qgstextblockformat.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.py again *
************************************************************************/
1 change: 1 addition & 0 deletions python/PyQt6/core/core_auto.sip
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@
%Include auto_generated/textrenderer/qgsfontmanager.sip
%Include auto_generated/textrenderer/qgstextbackgroundsettings.sip
%Include auto_generated/textrenderer/qgstextblock.sip
%Include auto_generated/textrenderer/qgstextblockformat.sip
%Include auto_generated/textrenderer/qgstextbuffersettings.sip
%Include auto_generated/textrenderer/qgstextcharacterformat.sip
%Include auto_generated/textrenderer/qgstextdocument.sip
Expand Down
17 changes: 17 additions & 0 deletions python/core/auto_additions/qgstextblockformat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# The following has been generated automatically from src/core/textrenderer/qgstextblockformat.h
# monkey patching scoped based enum
QgsTextBlockFormat.BooleanValue.NotSet.__doc__ = "Property is not set"
QgsTextBlockFormat.BooleanValue.SetTrue.__doc__ = "Property is set and ``True``"
QgsTextBlockFormat.BooleanValue.SetFalse.__doc__ = "Property is set and ``False``"
QgsTextBlockFormat.BooleanValue.__doc__ = """Status values for boolean format properties
* ``NotSet``: Property is not set
* ``SetTrue``: Property is set and ``True``
* ``SetFalse``: Property is set and ``False``
"""
# --
try:
QgsTextBlockFormat.__group__ = ['textrenderer']
except NameError:
pass
18 changes: 18 additions & 0 deletions python/core/auto_generated/textrenderer/qgstextblock.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ Returns ``True`` if the block is empty.
int size() const;
%Docstring
Returns the number of fragments in the block.
%End

const QgsTextBlockFormat &blockFormat() const;
%Docstring
Returns the block formatting for the fragment.

.. seealso:: :py:func:`setBlockFormat`

.. versionadded:: 3.40
%End

void setBlockFormat( const QgsTextBlockFormat &format );
%Docstring
Sets the block ``format`` for the fragment.

.. seealso:: :py:func:`blockFormat`

.. versionadded:: 3.40
%End

void applyCapitalization( Qgis::Capitalization capitalization );
Expand Down
123 changes: 123 additions & 0 deletions python/core/auto_generated/textrenderer/qgstextblockformat.sip.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/textrenderer/qgstextblockformat.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.py again *
************************************************************************/





class QgsTextBlockFormat
{
%Docstring(signature="appended")
Stores information relating to individual block formatting.

These options encapsulate formatting options which override the default
settings from a :py:class:`QgsTextFormat` for individual text blocks.

.. warning::

This API is not considered stable and may change in future QGIS versions.

.. versionadded:: 3.40
%End

%TypeHeaderCode
#include "qgstextblockformat.h"
%End
public:

QgsTextBlockFormat();

QgsTextBlockFormat( const QTextBlockFormat &format );
%Docstring
Constructor for QgsTextBlockFormat, based on the specified QTextBlockFormat ``format``.
%End

enum class BooleanValue
{
NotSet,
SetTrue,
SetFalse,
};

void overrideWith( const QgsTextBlockFormat &other );
%Docstring
Override all the default/unset properties of the current block format
with the settings from another format.

This will replace any default/unset existing settings with the
settings from ``other``.

Any settings which are default/unset in ``other`` will be left unchanged.

:param other: The format to override with.
%End

bool hasHorizontalAlignmentSet() const;
%Docstring
Returns ``True`` if the format has an explicit horizontal alignment set.

If ``False`` is returned then the horizontal alignment will be inherited.

.. seealso:: :py:func:`setHasHorizontalAlignmentSet`

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

void setHasHorizontalAlignmentSet( bool set );
%Docstring
Sets whether the format has an explicit horizontal alignment ``set``.

If ``set`` is ``False`` then the horizontal alignment will be inherited.

.. seealso:: :py:func:`hasHorizontalAlignmentSet`

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

Qgis::TextHorizontalAlignment horizontalAlignment() const;
%Docstring
Returns the format horizontal alignment.

This property is only respected if :py:func:`~QgsTextBlockFormat.hasHorizontalAlignmentSet` is ``True``.

.. seealso:: :py:func:`hasHorizontalAlignmentSet`

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

void setHorizontalAlignment( Qgis::TextHorizontalAlignment alignment );
%Docstring
Sets the format horizontal ``alignment``.

This property is only respected if :py:func:`~QgsTextBlockFormat.hasHorizontalAlignmentSet` is ``True``.

.. seealso:: :py:func:`hasHorizontalAlignmentSet`

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

void updateFontForFormat( QFont &font, const QgsRenderContext &context, double scaleFactor = 1.0 ) const;
%Docstring
Updates the specified ``font`` in place, applying block formatting options which
are applicable on a font level when rendered in the given ``context``.

The optional ``scaleFactor`` parameter can specify a font size scaling factor. It is recommended to set this to
:py:func:`QgsTextRenderer.calculateScaleFactorForFormat()` and then manually calculations
based on the resultant font metrics. Failure to do so will result in poor quality text rendering
at small font sizes.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/textrenderer/qgstextblockformat.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.py again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@
%Include auto_generated/textrenderer/qgsfontmanager.sip
%Include auto_generated/textrenderer/qgstextbackgroundsettings.sip
%Include auto_generated/textrenderer/qgstextblock.sip
%Include auto_generated/textrenderer/qgstextblockformat.sip
%Include auto_generated/textrenderer/qgstextbuffersettings.sip
%Include auto_generated/textrenderer/qgstextcharacterformat.sip
%Include auto_generated/textrenderer/qgstextdocument.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ set(QGIS_CORE_SRCS
textrenderer/qgsfontmanager.cpp
textrenderer/qgstextbackgroundsettings.cpp
textrenderer/qgstextblock.cpp
textrenderer/qgstextblockformat.cpp
textrenderer/qgstextbuffersettings.cpp
textrenderer/qgstextcharacterformat.cpp
textrenderer/qgstextdocument.cpp
Expand Down Expand Up @@ -1981,6 +1982,7 @@ set(QGIS_CORE_HDRS
textrenderer/qgsfontmanager.h
textrenderer/qgstextbackgroundsettings.h
textrenderer/qgstextblock.h
textrenderer/qgstextblockformat.h
textrenderer/qgstextbuffersettings.h
textrenderer/qgstextcharacterformat.h
textrenderer/qgstextdocument.h
Expand Down
5 changes: 5 additions & 0 deletions src/core/textrenderer/qgstextblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ int QgsTextBlock::size() const
return mFragments.size();
}

void QgsTextBlock::setBlockFormat( const QgsTextBlockFormat &format )
{
mBlockFormat = format;
}

void QgsTextBlock::applyCapitalization( Qgis::Capitalization capitalization )
{
for ( QgsTextFragment &fragment : mFragments )
Expand Down
Loading

0 comments on commit ce8ce0a

Please sign in to comment.