Skip to content

Commit c575b82

Browse files
committedJul 14, 2020
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.
1 parent d176f1d commit c575b82

File tree

5 files changed

+231
-8
lines changed

5 files changed

+231
-8
lines changed
 

‎python/core/auto_generated/textrenderer/qgstextformat.sip.in

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Container for all settings relating to text rendering.
3535
};
3636

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

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

4751
~QgsTextFormat();
4852

53+
bool isValid() const;
54+
%Docstring
55+
Returns ``True`` if the format is valid.
56+
57+
A default constructed QgsTextFormat is invalid, until at least one or more properties
58+
have been set on the format. An invalid state can be used as a representation of a "not set"
59+
text format, e.g. for indicating that a default text format should be used.
60+
61+
.. note::
62+
63+
Calling any setter on a QgsTextFormat object will automatically set the format as valid.
64+
65+
.. seealso:: :py:func:`setValid`
66+
67+
.. versionadded:: 3.16
68+
%End
69+
70+
void setValid();
71+
%Docstring
72+
Sets the format to a valid state, without changing any of the default format settings.
73+
74+
.. seealso:: :py:func:`isValid`
75+
76+
.. versionadded:: 3.16
77+
%End
78+
4979
QgsTextBufferSettings &buffer();
5080
%Docstring
5181
Returns a reference to the text buffer settings.

‎src/core/textrenderer/qgstextformat.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,64 @@ QgsTextFormat::~QgsTextFormat() //NOLINT
5959

6060
}
6161

62+
bool QgsTextFormat::isValid() const
63+
{
64+
return d->isValid;
65+
}
66+
67+
void QgsTextFormat::setValid()
68+
{
69+
d->isValid = true;
70+
}
71+
72+
QgsTextBufferSettings &QgsTextFormat::buffer()
73+
{
74+
d->isValid = true;
75+
return mBufferSettings;
76+
}
77+
78+
void QgsTextFormat::setBuffer( const QgsTextBufferSettings &bufferSettings )
79+
{
80+
d->isValid = true;
81+
mBufferSettings = bufferSettings;
82+
}
83+
84+
QgsTextBackgroundSettings &QgsTextFormat::background()
85+
{
86+
d->isValid = true;
87+
return mBackgroundSettings;
88+
}
89+
90+
void QgsTextFormat::setBackground( const QgsTextBackgroundSettings &backgroundSettings )
91+
{
92+
d->isValid = true;
93+
mBackgroundSettings = backgroundSettings;
94+
}
95+
96+
QgsTextShadowSettings &QgsTextFormat::shadow()
97+
{
98+
d->isValid = true;
99+
return mShadowSettings;
100+
}
101+
102+
void QgsTextFormat::setShadow( const QgsTextShadowSettings &shadowSettings )
103+
{
104+
d->isValid = true;
105+
mShadowSettings = shadowSettings;
106+
}
107+
108+
QgsTextMaskSettings &QgsTextFormat::mask()
109+
{
110+
d->isValid = true;
111+
return mMaskSettings;
112+
}
113+
114+
void QgsTextFormat::setMask( const QgsTextMaskSettings &maskSettings )
115+
{
116+
d->isValid = true;
117+
mMaskSettings = maskSettings;
118+
}
119+
62120
QFont QgsTextFormat::font() const
63121
{
64122
return d->textFont;
@@ -87,6 +145,7 @@ QFont QgsTextFormat::scaledFont( const QgsRenderContext &context, double scaleFa
87145

88146
void QgsTextFormat::setFont( const QFont &font )
89147
{
148+
d->isValid = true;
90149
d->textFont = font;
91150
}
92151

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

102161
void QgsTextFormat::setNamedStyle( const QString &style )
103162
{
163+
d->isValid = true;
104164
QgsFontUtils::updateFontViaStyle( d->textFont, style );
105165
d->textNamedStyle = style;
106166
}
@@ -112,6 +172,7 @@ QgsUnitTypes::RenderUnit QgsTextFormat::sizeUnit() const
112172

113173
void QgsTextFormat::setSizeUnit( QgsUnitTypes::RenderUnit unit )
114174
{
175+
d->isValid = true;
115176
d->fontSizeUnits = unit;
116177
}
117178

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

123184
void QgsTextFormat::setSizeMapUnitScale( const QgsMapUnitScale &scale )
124185
{
186+
d->isValid = true;
125187
d->fontSizeMapUnitScale = scale;
126188
}
127189

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

133195
void QgsTextFormat::setSize( double size )
134196
{
197+
d->isValid = true;
135198
d->fontSize = size;
136199
}
137200

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

143206
void QgsTextFormat::setColor( const QColor &color )
144207
{
208+
d->isValid = true;
145209
d->textColor = color;
146210
}
147211

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

153217
void QgsTextFormat::setOpacity( double opacity )
154218
{
219+
d->isValid = true;
155220
d->opacity = opacity;
156221
}
157222

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

163228
void QgsTextFormat::setBlendMode( QPainter::CompositionMode mode )
164229
{
230+
d->isValid = true;
165231
d->blendMode = mode;
166232
}
167233

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

173239
void QgsTextFormat::setLineHeight( double height )
174240
{
241+
d->isValid = true;
175242
d->multilineHeight = height;
176243
}
177244

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

183250
void QgsTextFormat::setOrientation( TextOrientation orientation )
184251
{
252+
d->isValid = true;
185253
d->orientation = orientation;
186254
}
187255

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

193261
void QgsTextFormat::setAllowHtmlFormatting( bool allow )
194262
{
263+
d->isValid = true;
195264
d->allowHtmlFormatting = allow;
196265
}
197266

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

203272
void QgsTextFormat::setPreviewBackgroundColor( const QColor &color )
204273
{
274+
d->isValid = true;
205275
d->previewBackgroundColor = color;
206276
}
207277

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

291362
void QgsTextFormat::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
292363
{
364+
d->isValid = true;
293365
QDomElement textStyleElem;
294366
if ( elem.nodeName() == QStringLiteral( "text-style" ) )
295367
textStyleElem = elem;
@@ -580,6 +652,7 @@ bool QgsTextFormat::containsAdvancedEffects() const
580652

581653
QgsPropertyCollection &QgsTextFormat::dataDefinedProperties()
582654
{
655+
d->isValid = true;
583656
return d->mDataDefinedProperties;
584657
}
585658

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

601674
void QgsTextFormat::setDataDefinedProperties( const QgsPropertyCollection &collection )
602675
{
676+
d->isValid = true;
603677
d->mDataDefinedProperties = collection;
604678
}
605679

606680
void QgsTextFormat::updateDataDefinedProperties( QgsRenderContext &context )
607681
{
682+
d->isValid = true;
608683
if ( !d->mDataDefinedProperties.hasActiveProperties() )
609684
return;
610685

‎src/core/textrenderer/qgstextformat.h

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class CORE_EXPORT QgsTextFormat
4747
RotationBasedOrientation, //!< Horizontally or vertically oriented text based on rotation (only available for map labeling)
4848
};
4949

50+
/**
51+
* Default constructor for QgsTextFormat. Creates a text format initially
52+
* set to an invalid state (see isValid()).
53+
*/
5054
QgsTextFormat();
5155

5256
/**
@@ -59,11 +63,33 @@ class CORE_EXPORT QgsTextFormat
5963

6064
~QgsTextFormat();
6165

66+
/**
67+
* Returns TRUE if the format is valid.
68+
*
69+
* A default constructed QgsTextFormat is invalid, until at least one or more properties
70+
* have been set on the format. An invalid state can be used as a representation of a "not set"
71+
* text format, e.g. for indicating that a default text format should be used.
72+
*
73+
* \note Calling any setter on a QgsTextFormat object will automatically set the format as valid.
74+
*
75+
* \see setValid()
76+
* \since QGIS 3.16
77+
*/
78+
bool isValid() const;
79+
80+
/**
81+
* Sets the format to a valid state, without changing any of the default format settings.
82+
*
83+
* \see isValid()
84+
* \since QGIS 3.16
85+
*/
86+
void setValid();
87+
6288
/**
6389
* Returns a reference to the text buffer settings.
6490
* \see setBuffer()
6591
*/
66-
QgsTextBufferSettings &buffer() { return mBufferSettings; }
92+
QgsTextBufferSettings &buffer();
6793

6894
/**
6995
* Returns a reference to the text buffer settings.
@@ -76,13 +102,13 @@ class CORE_EXPORT QgsTextFormat
76102
* \param bufferSettings buffer settings
77103
* \see buffer()
78104
*/
79-
void setBuffer( const QgsTextBufferSettings &bufferSettings ) { mBufferSettings = bufferSettings; }
105+
void setBuffer( const QgsTextBufferSettings &bufferSettings );
80106

81107
/**
82108
* Returns a reference to the text background settings.
83109
* \see setBackground()
84110
*/
85-
QgsTextBackgroundSettings &background() { return mBackgroundSettings; }
111+
QgsTextBackgroundSettings &background();
86112

87113
/**
88114
* Returns a reference to the text background settings.
@@ -95,13 +121,13 @@ class CORE_EXPORT QgsTextFormat
95121
* \param backgroundSettings background settings
96122
* \see background()
97123
*/
98-
void setBackground( const QgsTextBackgroundSettings &backgroundSettings ) { mBackgroundSettings = backgroundSettings; }
124+
void setBackground( const QgsTextBackgroundSettings &backgroundSettings );
99125

100126
/**
101127
* Returns a reference to the text drop shadow settings.
102128
* \see setShadow()
103129
*/
104-
QgsTextShadowSettings &shadow() { return mShadowSettings; }
130+
QgsTextShadowSettings &shadow();
105131

106132
/**
107133
* Returns a reference to the text drop shadow settings.
@@ -114,13 +140,13 @@ class CORE_EXPORT QgsTextFormat
114140
* \param shadowSettings shadow settings
115141
* \see shadow()
116142
*/
117-
void setShadow( const QgsTextShadowSettings &shadowSettings ) { mShadowSettings = shadowSettings; }
143+
void setShadow( const QgsTextShadowSettings &shadowSettings );
118144

119145
/**
120146
* Returns a reference to the masking settings.
121147
* \see setMask()
122148
*/
123-
QgsTextMaskSettings &mask() { return mMaskSettings; }
149+
QgsTextMaskSettings &mask();
124150

125151
/**
126152
* Returns a reference to the masking settings.
@@ -137,7 +163,7 @@ class CORE_EXPORT QgsTextFormat
137163
* \see mask()
138164
* \since QGIS 3.12
139165
*/
140-
void setMask( const QgsTextMaskSettings &maskSettings ) { mMaskSettings = maskSettings; }
166+
void setMask( const QgsTextMaskSettings &maskSettings );
141167

142168
/**
143169
* Returns the font used for rendering text. Note that the size of the font

‎src/core/textrenderer/qgstextrenderer_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ class QgsTextSettingsPrivate : public QSharedData
258258

259259
QgsTextSettingsPrivate( const QgsTextSettingsPrivate &other )
260260
: QSharedData( other )
261+
, isValid( other.isValid )
261262
, textFont( other.textFont )
262263
, textNamedStyle( other.textNamedStyle )
263264
, fontSizeUnits( other.fontSizeUnits )
@@ -274,6 +275,7 @@ class QgsTextSettingsPrivate : public QSharedData
274275
{
275276
}
276277

278+
bool isValid = false;
277279
QFont textFont;
278280
QString textNamedStyle;
279281
QgsUnitTypes::RenderUnit fontSizeUnits = QgsUnitTypes::RenderPoints;

‎tests/src/python/test_qgstextrenderer.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,96 @@ def tearDownClass(cls):
6363
with open(report_file_path, 'a') as report_file:
6464
report_file.write(cls.report)
6565

66+
def testValid(self):
67+
t = QgsTextFormat()
68+
self.assertFalse(t.isValid())
69+
70+
tt = QgsTextFormat(t)
71+
self.assertFalse(tt.isValid())
72+
73+
t.setValid()
74+
self.assertTrue(t.isValid())
75+
tt = QgsTextFormat(t)
76+
self.assertTrue(tt.isValid())
77+
78+
doc = QDomDocument()
79+
elem = t.writeXml(doc, QgsReadWriteContext())
80+
parent = doc.createElement("settings")
81+
parent.appendChild(elem)
82+
t3 = QgsTextFormat()
83+
t3.readXml(parent, QgsReadWriteContext())
84+
self.assertTrue(t3.isValid())
85+
86+
t = QgsTextFormat()
87+
t.buffer().setEnabled(True)
88+
self.assertTrue(t.isValid())
89+
90+
t = QgsTextFormat()
91+
t.background().setEnabled(True)
92+
self.assertTrue(t.isValid())
93+
94+
t = QgsTextFormat()
95+
t.shadow().setEnabled(True)
96+
self.assertTrue(t.isValid())
97+
98+
t = QgsTextFormat()
99+
t.mask().setEnabled(True)
100+
self.assertTrue(t.isValid())
101+
102+
t = QgsTextFormat()
103+
t.font()
104+
self.assertFalse(t.isValid())
105+
t.setFont(QFont())
106+
self.assertTrue(t.isValid())
107+
108+
t = QgsTextFormat()
109+
t.setNamedStyle('Bold')
110+
self.assertTrue(t.isValid())
111+
112+
t = QgsTextFormat()
113+
t.setSize(20)
114+
self.assertTrue(t.isValid())
115+
116+
t = QgsTextFormat()
117+
t.setSizeUnit(QgsUnitTypes.RenderPixels)
118+
self.assertTrue(t.isValid())
119+
120+
t = QgsTextFormat()
121+
t.setSizeMapUnitScale(QgsMapUnitScale(5, 10))
122+
self.assertTrue(t.isValid())
123+
124+
t = QgsTextFormat()
125+
t.setColor(QColor(255, 0, 0))
126+
self.assertTrue(t.isValid())
127+
128+
t = QgsTextFormat()
129+
t.setOpacity(0.2)
130+
self.assertTrue(t.isValid())
131+
132+
t = QgsTextFormat()
133+
t.setBlendMode(QPainter.CompositionMode_Darken)
134+
self.assertTrue(t.isValid())
135+
136+
t = QgsTextFormat()
137+
t.setLineHeight(20)
138+
self.assertTrue(t.isValid())
139+
140+
t = QgsTextFormat()
141+
t.setOrientation(QgsTextFormat.VerticalOrientation)
142+
self.assertTrue(t.isValid())
143+
144+
t = QgsTextFormat()
145+
t.setAllowHtmlFormatting(True)
146+
self.assertTrue(t.isValid())
147+
148+
t = QgsTextFormat()
149+
t.setPreviewBackgroundColor(QColor(255, 0, 0))
150+
self.assertTrue(t.isValid())
151+
152+
t = QgsTextFormat()
153+
t.dataDefinedProperties().setProperty(QgsPalLayerSettings.Bold, QgsProperty.fromValue(True))
154+
self.assertTrue(t.isValid())
155+
66156
def testAlignmentConversion(self):
67157
self.assertEqual(QgsTextRenderer.convertQtHAlignment(Qt.AlignLeft), QgsTextRenderer.AlignLeft)
68158
self.assertEqual(QgsTextRenderer.convertQtHAlignment(Qt.AlignRight), QgsTextRenderer.AlignRight)

0 commit comments

Comments
 (0)
Please sign in to comment.