Skip to content
Permalink
Browse files

Add html flag

  • Loading branch information
nyalldawson committed May 8, 2020
1 parent 59e1dce commit df88d42eb3291a2aacb9fff1a593c6d52d2fbc6b
@@ -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;
@@ -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;
@@ -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 );
@@ -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() );
@@ -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()
@@ -255,6 +255,7 @@ class QgsTextSettingsPrivate : public QSharedData
, multilineHeight( other.multilineHeight )
, orientation( other.orientation )
, previewBackgroundColor( other.previewBackgroundColor )
, allowHtmlFormatting( other.allowHtmlFormatting )
, mDataDefinedProperties( other.mDataDefinedProperties )
{
}
@@ -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;

@@ -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

@@ -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):

0 comments on commit df88d42

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