Skip to content
Permalink
Browse files

Add an invalid state to QgsTextFormat

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 c575b827bf31fba785278d9e2b715728afc3e7cd
@@ -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
@@ -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.
@@ -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;
@@ -87,6 +145,7 @@ QFont QgsTextFormat::scaledFont( const QgsRenderContext &context, double scaleFa

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

@@ -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;
}
@@ -112,6 +172,7 @@ QgsUnitTypes::RenderUnit QgsTextFormat::sizeUnit() const

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@@ -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;
@@ -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;
@@ -580,6 +652,7 @@ bool QgsTextFormat::containsAdvancedEffects() const

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

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

@@ -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();

/**
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -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
@@ -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 )
@@ -274,6 +275,7 @@ class QgsTextSettingsPrivate : public QSharedData
{
}

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

0 comments on commit c575b82

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