/
qgstextformat.h
564 lines (487 loc) · 17.2 KB
/
qgstextformat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/***************************************************************************
qgstextformat.h
---------------
begin : May 2020
copyright : (C) Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSTEXTFORMAT_H
#define QGSTEXTFORMAT_H
#include "qgis_sip.h"
#include "qgis_core.h"
#include "qgsunittypes.h"
#include "qgstextbuffersettings.h"
#include "qgstextbackgroundsettings.h"
#include "qgstextshadowsettings.h"
#include "qgstextmasksettings.h"
#include "qgsstringutils.h"
#include <QSharedDataPointer>
class QMimeData;
class QgsTextSettingsPrivate;
/**
* \class QgsTextFormat
* \ingroup core
* \brief Container for all settings relating to text rendering.
* \note QgsTextFormat objects are implicitly shared.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsTextFormat
{
public:
//! Text orientation
enum TextOrientation
{
HorizontalOrientation, //!< Vertically oriented text
VerticalOrientation, //!< Horizontally oriented text
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();
/**
* Copy constructor.
* \param other source QgsTextFormat
*/
QgsTextFormat( const QgsTextFormat &other );
QgsTextFormat &operator=( const QgsTextFormat &other );
~QgsTextFormat();
bool operator==( const QgsTextFormat &other ) const;
bool operator!=( const QgsTextFormat &other ) const;
/**
* 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();
/**
* Returns a reference to the text buffer settings.
* \see setBuffer()
*/
SIP_SKIP QgsTextBufferSettings buffer() const { return mBufferSettings; }
/**
* Sets the text's buffer settings.
* \param bufferSettings buffer settings
* \see buffer()
*/
void setBuffer( const QgsTextBufferSettings &bufferSettings );
/**
* Returns a reference to the text background settings.
* \see setBackground()
*/
QgsTextBackgroundSettings &background();
/**
* Returns a reference to the text background settings.
* \see setBackground()
*/
SIP_SKIP QgsTextBackgroundSettings background() const { return mBackgroundSettings; }
/**
* Sets the text's background settings.q
* \param backgroundSettings background settings
* \see background()
*/
void setBackground( const QgsTextBackgroundSettings &backgroundSettings );
/**
* Returns a reference to the text drop shadow settings.
* \see setShadow()
*/
QgsTextShadowSettings &shadow();
/**
* Returns a reference to the text drop shadow settings.
* \see setShadow()
*/
SIP_SKIP QgsTextShadowSettings shadow() const { return mShadowSettings; }
/**
* Sets the text's drop shadow settings.
* \param shadowSettings shadow settings
* \see shadow()
*/
void setShadow( const QgsTextShadowSettings &shadowSettings );
/**
* Returns a reference to the masking settings.
* \see setMask()
*/
QgsTextMaskSettings &mask();
/**
* Returns a reference to the masking settings.
* Masks may be defined in contexts where the text is rendered over some map layers, for labeling especially.
* \see setMask()
* \since QGIS 3.12
*/
SIP_SKIP QgsTextMaskSettings mask() const { return mMaskSettings; }
/**
* Sets the text's masking settings.
* Masks may be defined in contexts where the text is rendered over some map layers, for labeling especially.
* \param maskSettings mask settings
* \see mask()
* \since QGIS 3.12
*/
void setMask( const QgsTextMaskSettings &maskSettings );
/**
* Returns the font used for rendering text. Note that the size of the font
* is not used, and size() should be called instead to determine the size
* of rendered text.
* \see scaledFont()
* \see setFont()
* \see namedStyle()
* \see toQFont()
*/
QFont font() const;
/**
* Returns a font with the size scaled to match the format's size settings (including
* units and map unit scale) for a specified render context.
* \param context destination render context
* \param scaleFactor optional font size scaling factor. It is recommended to set this to
* QgsTextRenderer::FONT_WORKAROUND_SCALE and then manually scale painter devices or calculations
* based on the resultant font metrics. Failure to do so will result in poor quality text rendering
* at small font sizes.
* \returns font with scaled size
* \see font()
* \see size()
*/
QFont scaledFont( const QgsRenderContext &context, double scaleFactor = 1.0 ) const;
/**
* Sets the font used for rendering text. Note that the size of the font
* is not used, and setSize() should be called instead to explicitly set the size
* of rendered text.
* \param font desired font
* \see font()
* \see setNamedStyle()
* \see fromQFont()
*/
void setFont( const QFont &font );
/**
* Returns the named style for the font used for rendering text (e.g., "bold").
* \see setNamedStyle()
* \see font()
*/
QString namedStyle() const;
/**
* Sets the named style for the font used for rendering text.
* \param style named style, e.g., "bold"
* \see namedStyle()
* \see setFont()
*/
void setNamedStyle( const QString &style );
/**
* Returns the list of font families to use when restoring the text format, in order of precedence.
*
* \warning The list of families returned by this method is ONLY used when restoring the text format
* from serialized versions, and will not affect the current font() familily used by the format.
*
* \see setFamilies()
* \since QGIS 3.20
*/
QStringList families() const;
/**
* Sets a list of font \a families to use for the text format, in order of precedence.
*
* When restoring serialized versions of the text format then the first matching font family
* from this list will be used for the text format. This provides a way to specify a list of possible
* font families which are used as fallbacks if a family isn't available on a particular QGIS install (CSS style).
*
* \warning The list of families set by calling this method is ONLY used when restoring the text format
* from serialized versions, and will not affect the current font() familily used by the format.
*
* \see families()
* \since QGIS 3.20
*/
void setFamilies( const QStringList &families );
/**
* Returns the size for rendered text. Units are retrieved using sizeUnit().
* \see setSize()
* \see sizeUnit()
*/
double size() const;
/**
* Sets the size for rendered text.
* \param size size of rendered text. Units are set using setSizeUnit()
* \see size()
* \see setSizeUnit()
*/
void setSize( double size );
/**
* Returns the units for the size of rendered text.
* \see size()
* \see setSizeUnit()
* \see sizeMapUnitScale()
*/
QgsUnitTypes::RenderUnit sizeUnit() const;
/**
* Sets the units for the size of rendered text.
* \param unit size units
* \see setSize()
* \see sizeUnit()
* \see setSizeMapUnitScale()
*/
void setSizeUnit( QgsUnitTypes::RenderUnit unit );
/**
* Returns the map unit scale object for the size. This is only used if the
* sizeUnit() is set to QgsUnitTypes::RenderMapUnit.
* \see setSizeMapUnitScale()
* \see sizeUnit()
*/
QgsMapUnitScale sizeMapUnitScale() const;
/**
* Sets the map unit scale object for the size. This is only used if the
* sizeUnit() is set to QgsUnitTypes::RenderMapUnit.
* \see sizeMapUnitScale()
* \see setSizeUnit()
*/
void setSizeMapUnitScale( const QgsMapUnitScale &scale );
/**
* Returns the color that text will be rendered in.
* \see setColor()
*/
QColor color() const;
/**
* Sets the color that text will be rendered in.
* \param color text color
* \see color()
*/
void setColor( const QColor &color );
/**
* Returns the text's opacity. The opacity is a double value between 0 (fully transparent) and 1 (totally
* opaque).
* \see setOpacity()
*/
double opacity() const;
/**
* Sets the text's opacity.
* \param opacity opacity as a double value between 0 (fully transparent) and 1 (totally
* opaque)
* \see opacity()
*/
void setOpacity( double opacity );
/**
* Returns the blending mode used for drawing the text.
* \see setBlendMode()
*/
QPainter::CompositionMode blendMode() const;
/**
* Sets the blending mode used for drawing the text.
* \param mode blending mode
* \see blendMode()
*/
void setBlendMode( QPainter::CompositionMode mode );
/**
* Returns the line height for text. This is a number between
* 0.0 and 10.0 representing the leading between lines as a
* multiplier of line height.
* \see setLineHeight()
*/
double lineHeight() const;
/**
* Sets the line height for text.
* \param height a number between
* 0.0 and 10.0 representing the leading between lines as a
* multiplier of line height.
* \see lineHeight()
*/
void setLineHeight( double height );
/**
* Returns the orientation of the text.
* \see setOrientation()
* \since QGIS 3.10
*/
TextOrientation orientation() const;
/**
* Sets the \a orientation for the text.
* \see orientation()
* \since QGIS 3.10
*/
void setOrientation( TextOrientation orientation );
/**
* Returns the text capitalization style.
*
* \see setCapitalization()
* \since QGIS 3.16
*/
QgsStringUtils::Capitalization capitalization() const;
/**
* Sets the text \a capitalization style.
*
* \see capitalization()
* \since QGIS 3.16
*/
void setCapitalization( QgsStringUtils::Capitalization capitalization );
/**
* 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
* - strikethrough
* - underline
* - overline
*
* \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
* - strikethrough
* - underline
* - overline
*
* \see allowHtmlFormatting()
* \since QGIS 3.14
*/
void setAllowHtmlFormatting( bool allow );
/**
* Returns the background color for text previews.
* \see setPreviewBackgroundColor()
* \since QGIS 3.10
*/
QColor previewBackgroundColor() const;
/**
* Sets the background \a color that text will be rendered on for previews.
* \see previewBackgroundColor()
* \since QGIS 3.10
*/
void setPreviewBackgroundColor( const QColor &color );
/**
* Reads settings from a layer's custom properties (for QGIS 2.x projects).
* \param layer source vector layer
*/
void readFromLayer( QgsVectorLayer *layer );
/**
* Read settings from a DOM element.
* \see writeXml()
*/
void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
/**
* Write settings into a DOM element.
* \see readXml()
*/
QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
/**
* Returns new mime data representing the text format settings.
* Caller takes responsibility for deleting the returned object.
* \see fromMimeData()
*/
QMimeData *toMimeData() const SIP_FACTORY;
/**
* Returns a text format matching the settings from an input \a font.
* Unlike setFont(), this method also handles the size and size units
* from \a font.
* \see toQFont()
* \since QGIS 3.2
*/
static QgsTextFormat fromQFont( const QFont &font );
/**
* Returns a QFont matching the relevant settings from this text format.
* Unlike font(), this method also handles the size and size units
* from the text format.
* \see fromQFont()
* \since QGIS 3.2
*/
QFont toQFont() const;
/**
* Attempts to parse the provided mime \a data as a QgsTextFormat.
* If data can be parsed as a text format, \a ok will be set to TRUE.
* \see toMimeData()
*/
static QgsTextFormat fromMimeData( const QMimeData *data, bool *ok SIP_OUT = nullptr );
/**
* Returns TRUE if any component of the font format requires advanced effects
* such as blend modes, which require output in raster formats to be fully respected.
*/
bool containsAdvancedEffects() const;
/**
* Returns TRUE if the specified font was found on the system, or FALSE
* if the font was not found and a replacement was used instead.
* \see resolvedFontFamily()
*/
bool fontFound() const { return mTextFontFound; }
/**
* Returns the family for the resolved font, ie if the specified font
* was not found on the system this will return the name of the replacement
* font.
* \see fontFound()
*/
QString resolvedFontFamily() const { return mTextFontFamily; }
/**
* Returns a reference to the format's property collection, used for data defined overrides.
* \see setDataDefinedProperties()
* \since QGIS 3.10
*/
QgsPropertyCollection &dataDefinedProperties();
/**
* Returns a reference to the format's property collection, used for data defined overrides.
* \see setDataDefinedProperties()
* \note not available in Python bindings
* \since QGIS 3.10
*/
const QgsPropertyCollection &dataDefinedProperties() const SIP_SKIP;
/**
* Returns all field names referenced by the configuration (e.g. from data defined properties).
* \since QGIS 3.14
*/
QSet<QString> referencedFields( const QgsRenderContext &context ) const;
/**
* Sets the format's property collection, used for data defined overrides.
* \param collection property collection. Existing properties will be replaced.
* \see dataDefinedProperties()
* \since QGIS 3.10
*/
void setDataDefinedProperties( const QgsPropertyCollection &collection );
/**
* Updates the format by evaluating current values of data defined properties.
* \since QGIS 3.10
*/
void updateDataDefinedProperties( QgsRenderContext &context );
/**
* Returns a pixmap preview for a text \a format.
* \param format text format
* \param size target pixmap size
* \param previewText text to render in preview, or empty for default text
* \param padding space between icon edge and color ramp
* \since QGIS 3.10
*/
static QPixmap textFormatPreviewPixmap( const QgsTextFormat &format, QSize size, const QString &previewText = QString(), int padding = 0 );
private:
QgsTextBufferSettings mBufferSettings;
QgsTextBackgroundSettings mBackgroundSettings;
QgsTextShadowSettings mShadowSettings;
QgsTextMaskSettings mMaskSettings;
QString mTextFontFamily;
bool mTextFontFound = true;
QSharedDataPointer<QgsTextSettingsPrivate> d;
};
Q_DECLARE_METATYPE( QgsTextFormat )
#endif // QGSTEXTFORMAT_H