Skip to content

Commit

Permalink
Add equality operator for QgsTextFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 14, 2020
1 parent dfb6223 commit ac5a722
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/core/auto_generated/textrenderer/qgstextformat.sip.in
Expand Up @@ -50,6 +50,9 @@ Copy constructor.

~QgsTextFormat();

bool operator==( const QgsTextFormat &other ) const;
bool operator!=( const QgsTextFormat &other ) const;

bool isValid() const;
%Docstring
Returns ``True`` if the format is valid.
Expand Down
30 changes: 30 additions & 0 deletions src/core/textrenderer/qgstextformat.cpp
Expand Up @@ -59,6 +59,36 @@ QgsTextFormat::~QgsTextFormat() //NOLINT

}

bool QgsTextFormat::operator==( const QgsTextFormat &other ) const
{
if ( d->isValid != other.isValid()
|| d->textFont != other.font()
|| d->textNamedStyle != other.namedStyle()
|| d->fontSizeUnits != other.sizeUnit()
|| d->fontSizeMapUnitScale != other.sizeMapUnitScale()
|| d->fontSize != other.size()
|| d->textColor != other.color()
|| d->opacity != other.opacity()
|| d->blendMode != other.blendMode()
|| d->multilineHeight != other.lineHeight()
|| d->orientation != other.orientation()
|| d->previewBackgroundColor != other.previewBackgroundColor()
|| d->allowHtmlFormatting != other.allowHtmlFormatting()
|| mBufferSettings != other.mBufferSettings
|| mBackgroundSettings != other.mBackgroundSettings
|| mShadowSettings != other.mShadowSettings
|| mMaskSettings != other.mMaskSettings
|| d->mDataDefinedProperties != other.dataDefinedProperties() )
return false;

return true;
}

bool QgsTextFormat::operator!=( const QgsTextFormat &other ) const
{
return !( *this == other );
}

bool QgsTextFormat::isValid() const
{
return d->isValid;
Expand Down
3 changes: 3 additions & 0 deletions src/core/textrenderer/qgstextformat.h
Expand Up @@ -63,6 +63,9 @@ class CORE_EXPORT QgsTextFormat

~QgsTextFormat();

bool operator==( const QgsTextFormat &other ) const;
bool operator!=( const QgsTextFormat &other ) const;

/**
* Returns TRUE if the format is valid.
*
Expand Down
90 changes: 90 additions & 0 deletions tests/src/python/test_qgstextrenderer.py
Expand Up @@ -695,6 +695,96 @@ def createFormatSettings(self):
s.dataDefinedProperties().setProperty(QgsPalLayerSettings.Bold, QgsProperty.fromExpression('1>2'))
return s

def testFormatEquality(self):
s = self.createFormatSettings()
s2 = self.createFormatSettings()
self.assertEqual(s, s2)

s.buffer().setEnabled(False)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.buffer().setSize(12)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.mask().setEnabled(False)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.mask().setSize(12)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.background().setEnabled(False)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.background().setSvgFile('test2.svg')
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.shadow().setEnabled(False)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.shadow().setOffsetAngle(123)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

font = getTestFont()
font.setKerning(True)
s.setFont(font)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.setNamedStyle('Bold')
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.setSize(15)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.setSizeUnit(QgsUnitTypes.RenderPixels)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.setSizeMapUnitScale(QgsMapUnitScale(11, 12))
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.setColor(QColor(255, 255, 0))
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.setOpacity(0.6)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.setBlendMode(QPainter.CompositionMode_Darken)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.setLineHeight(15)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.setPreviewBackgroundColor(QColor(100, 250, 200))
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.setOrientation(QgsTextFormat.HorizontalOrientation)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.setAllowHtmlFormatting(False)
self.assertNotEqual(s, s2)
s = self.createFormatSettings()

s.dataDefinedProperties().setProperty(QgsPalLayerSettings.Bold, QgsProperty.fromExpression('1>3'))
self.assertNotEqual(s, s2)

def checkTextFormat(self, s):
""" test QgsTextFormat """
self.assertTrue(s.buffer().enabled())
Expand Down

0 comments on commit ac5a722

Please sign in to comment.