Navigation Menu

Skip to content

Commit

Permalink
Add html flag
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 12, 2020
1 parent 59e1dce commit df88d42
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
30 changes: 30 additions & 0 deletions python/core/auto_generated/qgstextrenderer.sip.in
Expand Up @@ -1729,6 +1729,36 @@ Sets the ``orientation`` for the text.
.. seealso:: :py:func:`orientation`

.. versionadded:: 3.10
%End

bool allowHtmlFormatting() const;
%Docstring
Returns ``True`` if text should be treated as a HTML document and HTML tags should be used for formatting
the rendered text.

.. warning::

Only a small subset of HTML formatting is supported. Currently this is restricted to:
* text color formatting

.. seealso:: :py:func:`setAllowHtmlFormatting`

.. versionadded:: 3.14
%End

void setAllowHtmlFormatting( bool allow );
%Docstring
Sets whether text should be treated as a HTML document and HTML tags should be used for formatting
the rendered text.

.. warning::

Only a small subset of HTML formatting is supported. Currently this is restricted to:
* text color formatting

.. seealso:: :py:func:`allowHtmlFormatting`

.. versionadded:: 3.14
%End

QColor previewBackgroundColor() const;
Expand Down
13 changes: 13 additions & 0 deletions src/core/qgstextrenderer.cpp
Expand Up @@ -1919,6 +1919,16 @@ void QgsTextFormat::setOrientation( TextOrientation orientation )
d->orientation = orientation;
}

bool QgsTextFormat::allowHtmlFormatting() const
{
return d->allowHtmlFormatting;
}

void QgsTextFormat::setAllowHtmlFormatting( bool allow )
{
d->allowHtmlFormatting = allow;
}

QColor QgsTextFormat::previewBackgroundColor() const
{
return d->previewBackgroundColor;
Expand Down Expand Up @@ -2106,6 +2116,8 @@ void QgsTextFormat::readXml( const QDomElement &elem, const QgsReadWriteContext
d->multilineHeight = textStyleElem.attribute( QStringLiteral( "multilineHeight" ), QStringLiteral( "1" ) ).toDouble();
}

d->allowHtmlFormatting = textStyleElem.attribute( QStringLiteral( "allowHtml" ), QStringLiteral( "0" ) ).toInt();

if ( textStyleElem.firstChildElement( QStringLiteral( "text-buffer" ) ).isNull() )
{
mBufferSettings.readXml( elem );
Expand Down Expand Up @@ -2177,6 +2189,7 @@ QDomElement QgsTextFormat::writeXml( QDomDocument &doc, const QgsReadWriteContex
textStyleElem.setAttribute( QStringLiteral( "textOrientation" ), QgsTextRendererUtils::encodeTextOrientation( d->orientation ) );
textStyleElem.setAttribute( QStringLiteral( "blendMode" ), QgsPainting::getBlendModeEnum( d->blendMode ) );
textStyleElem.setAttribute( QStringLiteral( "multilineHeight" ), d->multilineHeight );
textStyleElem.setAttribute( QStringLiteral( "allowHtml" ), d->allowHtmlFormatting ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );

QDomElement ddElem = doc.createElement( QStringLiteral( "dd_properties" ) );
d->mDataDefinedProperties.writeXml( ddElem, QgsPalLayerSettings::propertyDefinitions() );
Expand Down
24 changes: 24 additions & 0 deletions src/core/qgstextrenderer.h
Expand Up @@ -1471,6 +1471,30 @@ class CORE_EXPORT QgsTextFormat
*/
void setOrientation( TextOrientation orientation );

/**
* Returns TRUE if text should be treated as a HTML document and HTML tags should be used for formatting
* the rendered text.
*
* \warning Only a small subset of HTML formatting is supported. Currently this is restricted to:
* * text color formatting
*
* \see setAllowHtmlFormatting()
* \since QGIS 3.14
*/
bool allowHtmlFormatting() const;

/**
* Sets whether text should be treated as a HTML document and HTML tags should be used for formatting
* the rendered text.
*
* \warning Only a small subset of HTML formatting is supported. Currently this is restricted to:
* * text color formatting
*
* \see allowHtmlFormatting()
* \since QGIS 3.14
*/
void setAllowHtmlFormatting( bool allow );

/**
* Returns the background color for text previews.
* \see setPreviewBackgroundColor()
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgstextrenderer_p.h
Expand Up @@ -255,6 +255,7 @@ class QgsTextSettingsPrivate : public QSharedData
, multilineHeight( other.multilineHeight )
, orientation( other.orientation )
, previewBackgroundColor( other.previewBackgroundColor )
, allowHtmlFormatting( other.allowHtmlFormatting )
, mDataDefinedProperties( other.mDataDefinedProperties )
{
}
Expand All @@ -271,6 +272,8 @@ class QgsTextSettingsPrivate : public QSharedData
QgsTextFormat::TextOrientation orientation = QgsTextFormat::HorizontalOrientation;
QColor previewBackgroundColor = Qt::white;

bool allowHtmlFormatting = false;

//! Property collection for data defined settings
QgsPropertyCollection mDataDefinedProperties;

Expand Down
4 changes: 4 additions & 0 deletions tests/src/python/test_qgstextrenderer.py
Expand Up @@ -351,6 +351,8 @@ def createFormatSettings(self):
s.setBlendMode(QPainter.CompositionMode_DestinationAtop)
s.setLineHeight(5)
s.setPreviewBackgroundColor(QColor(100, 150, 200))
s.setOrientation(QgsTextFormat.VerticalOrientation)
s.setAllowHtmlFormatting(True)
s.dataDefinedProperties().setProperty(QgsPalLayerSettings.Bold, QgsProperty.fromExpression('1>2'))
return s

Expand All @@ -375,6 +377,8 @@ def checkTextFormat(self, s):
self.assertEqual(s.blendMode(), QPainter.CompositionMode_DestinationAtop)
self.assertEqual(s.lineHeight(), 5)
self.assertEqual(s.previewBackgroundColor().name(), '#6496c8')
self.assertEqual(s.orientation(), QgsTextFormat.VerticalOrientation)
self.assertTrue(s.allowHtmlFormatting())
self.assertEqual(s.dataDefinedProperties().property(QgsPalLayerSettings.Bold).expressionString(), '1>2')

def testFormatGettersSetters(self):
Expand Down

0 comments on commit df88d42

Please sign in to comment.