Skip to content

Commit

Permalink
Add an invalid state to QgsTextFormat
Browse files Browse the repository at this point in the history
Allows determining whether the QgsTextFormat is a default constructed
object, which should represent a not set/default settings value.
  • Loading branch information
nyalldawson committed Jul 14, 2020
1 parent d176f1d commit c575b82
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 8 deletions.
30 changes: 30 additions & 0 deletions python/core/auto_generated/textrenderer/qgstextformat.sip.in
Expand Up @@ -35,6 +35,10 @@ Container for all settings relating to text rendering.
};

QgsTextFormat();
%Docstring
Default constructor for QgsTextFormat. Creates a text format initially
set to an invalid state (see :py:func:`~QgsTextFormat.isValid`).
%End

QgsTextFormat( const QgsTextFormat &other );
%Docstring
Expand All @@ -46,6 +50,32 @@ Copy constructor.

~QgsTextFormat();

bool isValid() const;
%Docstring
Returns ``True`` if the format is valid.

A default constructed QgsTextFormat is invalid, until at least one or more properties
have been set on the format. An invalid state can be used as a representation of a "not set"
text format, e.g. for indicating that a default text format should be used.

.. note::

Calling any setter on a QgsTextFormat object will automatically set the format as valid.

.. seealso:: :py:func:`setValid`

.. versionadded:: 3.16
%End

void setValid();
%Docstring
Sets the format to a valid state, without changing any of the default format settings.

.. seealso:: :py:func:`isValid`

.. versionadded:: 3.16
%End

QgsTextBufferSettings &buffer();
%Docstring
Returns a reference to the text buffer settings.
Expand Down
75 changes: 75 additions & 0 deletions src/core/textrenderer/qgstextformat.cpp
Expand Up @@ -59,6 +59,64 @@ QgsTextFormat::~QgsTextFormat() //NOLINT

}

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

void QgsTextFormat::setValid()
{
d->isValid = true;
}

QgsTextBufferSettings &QgsTextFormat::buffer()
{
d->isValid = true;
return mBufferSettings;
}

void QgsTextFormat::setBuffer( const QgsTextBufferSettings &bufferSettings )
{
d->isValid = true;
mBufferSettings = bufferSettings;
}

QgsTextBackgroundSettings &QgsTextFormat::background()
{
d->isValid = true;
return mBackgroundSettings;
}

void QgsTextFormat::setBackground( const QgsTextBackgroundSettings &backgroundSettings )
{
d->isValid = true;
mBackgroundSettings = backgroundSettings;
}

QgsTextShadowSettings &QgsTextFormat::shadow()
{
d->isValid = true;
return mShadowSettings;
}

void QgsTextFormat::setShadow( const QgsTextShadowSettings &shadowSettings )
{
d->isValid = true;
mShadowSettings = shadowSettings;
}

QgsTextMaskSettings &QgsTextFormat::mask()
{
d->isValid = true;
return mMaskSettings;
}

void QgsTextFormat::setMask( const QgsTextMaskSettings &maskSettings )
{
d->isValid = true;
mMaskSettings = maskSettings;
}

QFont QgsTextFormat::font() const
{
return d->textFont;
Expand Down Expand Up @@ -87,6 +145,7 @@ QFont QgsTextFormat::scaledFont( const QgsRenderContext &context, double scaleFa

void QgsTextFormat::setFont( const QFont &font )
{
d->isValid = true;
d->textFont = font;
}

Expand All @@ -101,6 +160,7 @@ QString QgsTextFormat::namedStyle() const

void QgsTextFormat::setNamedStyle( const QString &style )
{
d->isValid = true;
QgsFontUtils::updateFontViaStyle( d->textFont, style );
d->textNamedStyle = style;
}
Expand All @@ -112,6 +172,7 @@ QgsUnitTypes::RenderUnit QgsTextFormat::sizeUnit() const

void QgsTextFormat::setSizeUnit( QgsUnitTypes::RenderUnit unit )
{
d->isValid = true;
d->fontSizeUnits = unit;
}

Expand All @@ -122,6 +183,7 @@ QgsMapUnitScale QgsTextFormat::sizeMapUnitScale() const

void QgsTextFormat::setSizeMapUnitScale( const QgsMapUnitScale &scale )
{
d->isValid = true;
d->fontSizeMapUnitScale = scale;
}

Expand All @@ -132,6 +194,7 @@ double QgsTextFormat::size() const

void QgsTextFormat::setSize( double size )
{
d->isValid = true;
d->fontSize = size;
}

Expand All @@ -142,6 +205,7 @@ QColor QgsTextFormat::color() const

void QgsTextFormat::setColor( const QColor &color )
{
d->isValid = true;
d->textColor = color;
}

Expand All @@ -152,6 +216,7 @@ double QgsTextFormat::opacity() const

void QgsTextFormat::setOpacity( double opacity )
{
d->isValid = true;
d->opacity = opacity;
}

Expand All @@ -162,6 +227,7 @@ QPainter::CompositionMode QgsTextFormat::blendMode() const

void QgsTextFormat::setBlendMode( QPainter::CompositionMode mode )
{
d->isValid = true;
d->blendMode = mode;
}

Expand All @@ -172,6 +238,7 @@ double QgsTextFormat::lineHeight() const

void QgsTextFormat::setLineHeight( double height )
{
d->isValid = true;
d->multilineHeight = height;
}

Expand All @@ -182,6 +249,7 @@ QgsTextFormat::TextOrientation QgsTextFormat::orientation() const

void QgsTextFormat::setOrientation( TextOrientation orientation )
{
d->isValid = true;
d->orientation = orientation;
}

Expand All @@ -192,6 +260,7 @@ bool QgsTextFormat::allowHtmlFormatting() const

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

Expand All @@ -202,11 +271,13 @@ QColor QgsTextFormat::previewBackgroundColor() const

void QgsTextFormat::setPreviewBackgroundColor( const QColor &color )
{
d->isValid = true;
d->previewBackgroundColor = color;
}

void QgsTextFormat::readFromLayer( QgsVectorLayer *layer )
{
d->isValid = true;
QFont appFont = QApplication::font();
mTextFontFamily = layer->customProperty( QStringLiteral( "labeling/fontFamily" ), QVariant( appFont.family() ) ).toString();
QString fontFamily = mTextFontFamily;
Expand Down Expand Up @@ -290,6 +361,7 @@ void QgsTextFormat::readFromLayer( QgsVectorLayer *layer )

void QgsTextFormat::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
{
d->isValid = true;
QDomElement textStyleElem;
if ( elem.nodeName() == QStringLiteral( "text-style" ) )
textStyleElem = elem;
Expand Down Expand Up @@ -580,6 +652,7 @@ bool QgsTextFormat::containsAdvancedEffects() const

QgsPropertyCollection &QgsTextFormat::dataDefinedProperties()
{
d->isValid = true;
return d->mDataDefinedProperties;
}

Expand All @@ -600,11 +673,13 @@ QSet<QString> QgsTextFormat::referencedFields( const QgsRenderContext &context )

void QgsTextFormat::setDataDefinedProperties( const QgsPropertyCollection &collection )
{
d->isValid = true;
d->mDataDefinedProperties = collection;
}

void QgsTextFormat::updateDataDefinedProperties( QgsRenderContext &context )
{
d->isValid = true;
if ( !d->mDataDefinedProperties.hasActiveProperties() )
return;

Expand Down
42 changes: 34 additions & 8 deletions src/core/textrenderer/qgstextformat.h
Expand Up @@ -47,6 +47,10 @@ class CORE_EXPORT QgsTextFormat
RotationBasedOrientation, //!< Horizontally or vertically oriented text based on rotation (only available for map labeling)
};

/**
* Default constructor for QgsTextFormat. Creates a text format initially
* set to an invalid state (see isValid()).
*/
QgsTextFormat();

/**
Expand All @@ -59,11 +63,33 @@ class CORE_EXPORT QgsTextFormat

~QgsTextFormat();

/**
* Returns TRUE if the format is valid.
*
* A default constructed QgsTextFormat is invalid, until at least one or more properties
* have been set on the format. An invalid state can be used as a representation of a "not set"
* text format, e.g. for indicating that a default text format should be used.
*
* \note Calling any setter on a QgsTextFormat object will automatically set the format as valid.
*
* \see setValid()
* \since QGIS 3.16
*/
bool isValid() const;

/**
* Sets the format to a valid state, without changing any of the default format settings.
*
* \see isValid()
* \since QGIS 3.16
*/
void setValid();

/**
* Returns a reference to the text buffer settings.
* \see setBuffer()
*/
QgsTextBufferSettings &buffer() { return mBufferSettings; }
QgsTextBufferSettings &buffer();

/**
* Returns a reference to the text buffer settings.
Expand All @@ -76,13 +102,13 @@ class CORE_EXPORT QgsTextFormat
* \param bufferSettings buffer settings
* \see buffer()
*/
void setBuffer( const QgsTextBufferSettings &bufferSettings ) { mBufferSettings = bufferSettings; }
void setBuffer( const QgsTextBufferSettings &bufferSettings );

/**
* Returns a reference to the text background settings.
* \see setBackground()
*/
QgsTextBackgroundSettings &background() { return mBackgroundSettings; }
QgsTextBackgroundSettings &background();

/**
* Returns a reference to the text background settings.
Expand All @@ -95,13 +121,13 @@ class CORE_EXPORT QgsTextFormat
* \param backgroundSettings background settings
* \see background()
*/
void setBackground( const QgsTextBackgroundSettings &backgroundSettings ) { mBackgroundSettings = backgroundSettings; }
void setBackground( const QgsTextBackgroundSettings &backgroundSettings );

/**
* Returns a reference to the text drop shadow settings.
* \see setShadow()
*/
QgsTextShadowSettings &shadow() { return mShadowSettings; }
QgsTextShadowSettings &shadow();

/**
* Returns a reference to the text drop shadow settings.
Expand All @@ -114,13 +140,13 @@ class CORE_EXPORT QgsTextFormat
* \param shadowSettings shadow settings
* \see shadow()
*/
void setShadow( const QgsTextShadowSettings &shadowSettings ) { mShadowSettings = shadowSettings; }
void setShadow( const QgsTextShadowSettings &shadowSettings );

/**
* Returns a reference to the masking settings.
* \see setMask()
*/
QgsTextMaskSettings &mask() { return mMaskSettings; }
QgsTextMaskSettings &mask();

/**
* Returns a reference to the masking settings.
Expand All @@ -137,7 +163,7 @@ class CORE_EXPORT QgsTextFormat
* \see mask()
* \since QGIS 3.12
*/
void setMask( const QgsTextMaskSettings &maskSettings ) { mMaskSettings = maskSettings; }
void setMask( const QgsTextMaskSettings &maskSettings );

/**
* Returns the font used for rendering text. Note that the size of the font
Expand Down
2 changes: 2 additions & 0 deletions src/core/textrenderer/qgstextrenderer_p.h
Expand Up @@ -258,6 +258,7 @@ class QgsTextSettingsPrivate : public QSharedData

QgsTextSettingsPrivate( const QgsTextSettingsPrivate &other )
: QSharedData( other )
, isValid( other.isValid )
, textFont( other.textFont )
, textNamedStyle( other.textNamedStyle )
, fontSizeUnits( other.fontSizeUnits )
Expand All @@ -274,6 +275,7 @@ class QgsTextSettingsPrivate : public QSharedData
{
}

bool isValid = false;
QFont textFont;
QString textNamedStyle;
QgsUnitTypes::RenderUnit fontSizeUnits = QgsUnitTypes::RenderPoints;
Expand Down

0 comments on commit c575b82

Please sign in to comment.