QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgstextrenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgstextrenderer.h
3  -----------------
4  begin : September 2015
5  copyright : (C) Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7 
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #ifndef QGSTEXTRENDERER_H
18 #define QGSTEXTRENDERER_H
19 
20 #include "qgis_sip.h"
21 #include "qgis_core.h"
22 #include "qgsmapunitscale.h"
23 #include "qgsunittypes.h"
24 
25 #include <QSharedData>
26 #include <QPainter>
27 #include <QPicture>
28 #include <QDomElement>
29 
31 class QgsTextBufferSettingsPrivate;
32 class QgsTextBackgroundSettingsPrivate;
33 class QgsTextShadowSettingsPrivate;
34 class QgsTextSettingsPrivate;
35 class QgsVectorLayer;
36 class QgsPaintEffect;
37 class QgsMarkerSymbol;
39 
47 class CORE_EXPORT QgsTextBufferSettings
48 {
49  public:
50 
52 
58 
63  QgsTextBufferSettings &operator=( const QgsTextBufferSettings &other );
64 
66 
71  bool enabled() const;
72 
78  void setEnabled( bool enabled );
79 
85  double size() const;
86 
93  void setSize( double size );
94 
100  QgsUnitTypes::RenderUnit sizeUnit() const;
101 
108  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
109 
116  QgsMapUnitScale sizeMapUnitScale() const;
117 
125  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
126 
131  QColor color() const;
132 
138  void setColor( const QColor &color );
139 
146  bool fillBufferInterior() const;
147 
154  void setFillBufferInterior( bool fill );
155 
161  double opacity() const;
162 
169  void setOpacity( double opacity );
170 
175  Qt::PenJoinStyle joinStyle() const;
176 
182  void setJoinStyle( Qt::PenJoinStyle style );
183 
188  QPainter::CompositionMode blendMode() const;
189 
195  void setBlendMode( QPainter::CompositionMode mode );
196 
201  void readFromLayer( QgsVectorLayer *layer );
202 
207  void readXml( const QDomElement &elem );
208 
213  QDomElement writeXml( QDomDocument &doc ) const;
214 
220  QgsPaintEffect *paintEffect() const;
221 
227  void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
228 
233  void updateDataDefinedProperties( QgsRenderContext &context, const QgsPropertyCollection &properties );
234 
235  private:
236 
237  QSharedDataPointer<QgsTextBufferSettingsPrivate> d;
238 
239 };
240 
249 class CORE_EXPORT QgsTextBackgroundSettings
250 {
251  public:
252 
257  {
258  ShapeRectangle = 0,
264  };
265 
269  enum SizeType
270  {
271  SizeBuffer = 0,
273  SizePercent
274  };
275 
280  {
281  RotationSync = 0,
283  RotationFixed
284  };
285 
287 
293 
294  QgsTextBackgroundSettings &operator=( const QgsTextBackgroundSettings &other );
295 
297 
302  bool enabled() const;
303 
309  void setEnabled( bool enabled );
310 
315  ShapeType type() const;
316 
322  void setType( ShapeType type );
323 
328  QString svgFile() const;
329 
336  void setSvgFile( const QString &file );
337 
345  QgsMarkerSymbol *markerSymbol() const;
346 
354  void setMarkerSymbol( QgsMarkerSymbol *symbol SIP_TRANSFER );
355 
362  SizeType sizeType() const;
363 
371  void setSizeType( SizeType type );
372 
381  QSizeF size() const;
382 
392  void setSize( QSizeF size );
393 
401  QgsUnitTypes::RenderUnit sizeUnit() const;
402 
411  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
412 
419  QgsMapUnitScale sizeMapUnitScale() const;
420 
428  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
429 
435  RotationType rotationType() const;
436 
443  void setRotationType( RotationType type );
444 
450  double rotation() const;
451 
457  void setRotation( double rotation );
458 
465  QPointF offset() const;
466 
474  void setOffset( QPointF offset );
475 
481  QgsUnitTypes::RenderUnit offsetUnit() const;
482 
489  void setOffsetUnit( QgsUnitTypes::RenderUnit units );
490 
497  QgsMapUnitScale offsetMapUnitScale() const;
498 
506  void setOffsetMapUnitScale( const QgsMapUnitScale &scale );
507 
514  QSizeF radii() const;
515 
524  void setRadii( QSizeF radii );
525 
531  QgsUnitTypes::RenderUnit radiiUnit() const;
532 
539  void setRadiiUnit( QgsUnitTypes::RenderUnit units );
540 
547  QgsMapUnitScale radiiMapUnitScale() const;
548 
556  void setRadiiMapUnitScale( const QgsMapUnitScale &scale );
557 
563  double opacity() const;
564 
571  void setOpacity( double opacity );
572 
577  QPainter::CompositionMode blendMode() const;
578 
584  void setBlendMode( QPainter::CompositionMode mode );
585 
591  QColor fillColor() const;
592 
599  void setFillColor( const QColor &color );
600 
606  QColor strokeColor() const;
607 
614  void setStrokeColor( const QColor &color );
615 
622  double strokeWidth() const;
623 
630  void setStrokeWidth( double width );
631 
637  QgsUnitTypes::RenderUnit strokeWidthUnit() const;
638 
645  void setStrokeWidthUnit( QgsUnitTypes::RenderUnit units );
646 
653  QgsMapUnitScale strokeWidthMapUnitScale() const;
654 
662  void setStrokeWidthMapUnitScale( const QgsMapUnitScale &scale );
663 
668  Qt::PenJoinStyle joinStyle() const;
669 
675  void setJoinStyle( Qt::PenJoinStyle style );
676 
682  QgsPaintEffect *paintEffect() const;
683 
689  void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
690 
695  void readFromLayer( QgsVectorLayer *layer );
696 
701  void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
702 
707  QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
708 
713  void updateDataDefinedProperties( QgsRenderContext &context, const QgsPropertyCollection &properties );
714 
715  private:
716 
717  QSharedDataPointer<QgsTextBackgroundSettingsPrivate> d;
718 
719 };
720 
729 class CORE_EXPORT QgsTextShadowSettings
730 {
731  public:
732 
737  {
738  ShadowLowest = 0,
741  ShadowShape
742  };
743 
745 
751 
752  QgsTextShadowSettings &operator=( const QgsTextShadowSettings &other );
753 
755 
760  bool enabled() const;
761 
767  void setEnabled( bool enabled );
768 
775  QgsTextShadowSettings::ShadowPlacement shadowPlacement() const;
776 
784  void setShadowPlacement( QgsTextShadowSettings::ShadowPlacement placement );
785 
791  int offsetAngle() const;
792 
799  void setOffsetAngle( int angle );
800 
807  double offsetDistance() const;
808 
816  void setOffsetDistance( double distance );
817 
823  QgsUnitTypes::RenderUnit offsetUnit() const;
824 
831  void setOffsetUnit( QgsUnitTypes::RenderUnit units );
832 
839  QgsMapUnitScale offsetMapUnitScale() const;
840 
848  void setOffsetMapUnitScale( const QgsMapUnitScale &scale );
849 
854  bool offsetGlobal() const;
855 
860  void setOffsetGlobal( bool global );
861 
867  double blurRadius() const;
868 
875  void setBlurRadius( double blurRadius );
876 
882  QgsUnitTypes::RenderUnit blurRadiusUnit() const;
883 
890  void setBlurRadiusUnit( QgsUnitTypes::RenderUnit units );
891 
898  QgsMapUnitScale blurRadiusMapUnitScale() const;
899 
907  void setBlurRadiusMapUnitScale( const QgsMapUnitScale &scale );
908 
913  bool blurAlphaOnly() const;
914 
921  void setBlurAlphaOnly( bool alphaOnly );
922 
928  double opacity() const;
929 
936  void setOpacity( double opacity );
937 
942  int scale() const;
943 
949  void setScale( int scale );
950 
955  QColor color() const;
956 
962  void setColor( const QColor &color );
963 
968  QPainter::CompositionMode blendMode() const;
969 
975  void setBlendMode( QPainter::CompositionMode mode );
976 
981  void readFromLayer( QgsVectorLayer *layer );
982 
987  void readXml( const QDomElement &elem );
988 
993  QDomElement writeXml( QDomDocument &doc ) const;
994 
999  void updateDataDefinedProperties( QgsRenderContext &context, const QgsPropertyCollection &properties );
1000 
1001  private:
1002 
1003  QSharedDataPointer<QgsTextShadowSettingsPrivate> d;
1004 
1005 };
1006 
1007 
1016 class CORE_EXPORT QgsTextFormat
1017 {
1018  public:
1019 
1022  {
1026  };
1027 
1028  QgsTextFormat();
1029 
1034  QgsTextFormat( const QgsTextFormat &other );
1035 
1036  QgsTextFormat &operator=( const QgsTextFormat &other );
1037 
1038  ~QgsTextFormat();
1039 
1044  QgsTextBufferSettings &buffer() { return mBufferSettings; }
1045 
1050  SIP_SKIP QgsTextBufferSettings buffer() const { return mBufferSettings; }
1051 
1057  void setBuffer( const QgsTextBufferSettings &bufferSettings ) { mBufferSettings = bufferSettings; }
1058 
1063  QgsTextBackgroundSettings &background() { return mBackgroundSettings; }
1064 
1069  SIP_SKIP QgsTextBackgroundSettings background() const { return mBackgroundSettings; }
1070 
1076  void setBackground( const QgsTextBackgroundSettings &backgroundSettings ) { mBackgroundSettings = backgroundSettings; }
1077 
1082  QgsTextShadowSettings &shadow() { return mShadowSettings; }
1083 
1088  SIP_SKIP QgsTextShadowSettings shadow() const { return mShadowSettings; }
1089 
1095  void setShadow( const QgsTextShadowSettings &shadowSettings ) { mShadowSettings = shadowSettings; }
1096 
1106  QFont font() const;
1107 
1116  QFont scaledFont( const QgsRenderContext &context ) const;
1117 
1127  void setFont( const QFont &font );
1128 
1134  QString namedStyle() const;
1135 
1142  void setNamedStyle( const QString &style );
1143 
1149  double size() const;
1150 
1157  void setSize( double size );
1158 
1165  QgsUnitTypes::RenderUnit sizeUnit() const;
1166 
1174  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
1175 
1182  QgsMapUnitScale sizeMapUnitScale() const;
1183 
1190  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
1191 
1196  QColor color() const;
1197 
1203  void setColor( const QColor &color );
1204 
1210  double opacity() const;
1211 
1218  void setOpacity( double opacity );
1219 
1224  QPainter::CompositionMode blendMode() const;
1225 
1231  void setBlendMode( QPainter::CompositionMode mode );
1232 
1239  double lineHeight() const;
1240 
1248  void setLineHeight( double height );
1249 
1255  TextOrientation orientation() const;
1256 
1262  void setOrientation( TextOrientation orientation );
1263 
1269  QColor previewBackgroundColor() const;
1270 
1276  void setPreviewBackgroundColor( const QColor &color );
1277 
1282  void readFromLayer( QgsVectorLayer *layer );
1283 
1288  void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
1289 
1294  QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
1295 
1301  QMimeData *toMimeData() const SIP_FACTORY;
1302 
1310  static QgsTextFormat fromQFont( const QFont &font );
1311 
1319  QFont toQFont() const;
1320 
1326  static QgsTextFormat fromMimeData( const QMimeData *data, bool *ok SIP_OUT = nullptr );
1327 
1332  bool containsAdvancedEffects() const;
1333 
1339  bool fontFound() const { return mTextFontFound; }
1340 
1347  QString resolvedFontFamily() const { return mTextFontFamily; }
1348 
1354  QgsPropertyCollection &dataDefinedProperties();
1355 
1362  const QgsPropertyCollection &dataDefinedProperties() const SIP_SKIP;
1363 
1370  void setDataDefinedProperties( const QgsPropertyCollection &collection );
1371 
1376  void updateDataDefinedProperties( QgsRenderContext &context );
1377 
1386  static QPixmap textFormatPreviewPixmap( const QgsTextFormat &format, QSize size, const QString &previewText = QString(), int padding = 0 );
1387 
1388  private:
1389 
1390  QgsTextBufferSettings mBufferSettings;
1391  QgsTextBackgroundSettings mBackgroundSettings;
1392  QgsTextShadowSettings mShadowSettings;
1393 
1394  QString mTextFontFamily;
1395  bool mTextFontFound = true;
1396 
1397  QSharedDataPointer<QgsTextSettingsPrivate> d;
1398 
1399 };
1400 
1409 class CORE_EXPORT QgsTextRenderer
1410 {
1411  public:
1412 
1415  {
1416  Rect = 0,
1419  };
1420 
1423  {
1424  Text = 0,
1428  };
1429 
1432  {
1433  AlignLeft = 0,
1436  };
1437 
1446  static int sizeToPixel( double size, const QgsRenderContext &c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
1447 
1448  // TODO QGIS 4.0 -- remove drawAsOutlines from below methods!
1449 
1463  static void drawText( const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines,
1464  QgsRenderContext &context, const QgsTextFormat &format,
1465  bool drawAsOutlines = true );
1466 
1480  static void drawText( QPointF point, double rotation, HAlignment alignment, const QStringList &textLines,
1481  QgsRenderContext &context, const QgsTextFormat &format,
1482  bool drawAsOutlines = true );
1483 
1500  static void drawPart( const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines,
1501  QgsRenderContext &context, const QgsTextFormat &format,
1502  TextPart part, bool drawAsOutlines = true );
1503 
1520  static void drawPart( QPointF origin, double rotation, HAlignment alignment, const QStringList &textLines,
1521  QgsRenderContext &context, const QgsTextFormat &format,
1522  TextPart part, bool drawAsOutlines = true );
1523 
1530  static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format );
1531 
1539  static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
1540  QFontMetricsF *fontMetrics = nullptr );
1541 
1550  static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode,
1551  QFontMetricsF *fontMetrics = nullptr );
1552 
1553  private:
1554 
1555  struct Component
1556  {
1558  QString text;
1560  QPointF origin;
1562  bool useOrigin = false;
1564  double rotation = 0.0;
1566  double rotationOffset = 0.0;
1568  QPointF center;
1570  QSizeF size;
1572  QPointF offset;
1574  QPicture picture;
1575 
1580  double pictureBuffer = 0.0;
1582  double dpiRatio = 1.0;
1584  HAlignment hAlign = AlignLeft;
1585  };
1586 
1587  static void drawBuffer( QgsRenderContext &context,
1588  const Component &component,
1589  const QgsTextFormat &format,
1590  const QFontMetricsF *fontMetrics );
1591 
1592  static void drawBackground( QgsRenderContext &context,
1593  Component component,
1594  const QgsTextFormat &format,
1595  const QStringList &textLines,
1596  DrawMode mode = Rect );
1597 
1598  static void drawShadow( QgsRenderContext &context,
1599  const Component &component,
1600  const QgsTextFormat &format );
1601 
1602  static void drawText( QgsRenderContext &context,
1603  const Component &component,
1604  const QgsTextFormat &format );
1605 
1606  static void drawTextInternal( TextPart drawType,
1607  QgsRenderContext &context,
1608  const QgsTextFormat &format,
1609  const Component &component,
1610  const QStringList &textLines,
1611  const QFontMetricsF *fontMetrics,
1612  HAlignment alignment,
1613  DrawMode mode = Rect );
1614 
1616  friend class QgsLabelPreview;
1617 
1618  static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
1619 
1620 
1621 };
1622 
1629 class CORE_EXPORT QgsTextRendererUtils
1630 {
1631  public:
1632 
1636  static QgsTextBackgroundSettings::ShapeType decodeShapeType( const QString &string );
1637 
1641  static QgsTextBackgroundSettings::SizeType decodeBackgroundSizeType( const QString &string );
1642 
1646  static QgsTextBackgroundSettings::RotationType decodeBackgroundRotationType( const QString &string );
1647 
1651  static QgsTextShadowSettings::ShadowPlacement decodeShadowPlacementType( const QString &string );
1652 
1658  static QString encodeTextOrientation( QgsTextFormat::TextOrientation orientation );
1659 
1667  static QgsTextFormat::TextOrientation decodeTextOrientation( const QString &name, bool *ok = nullptr );
1668 };
1669 
1670 
1671 #endif // QGSTEXTRENDERER_H
TextOrientation
Text orientation.
The class is used as a container of context for various read/write operations on other objects...
RotationType
Methods for determining the rotation of the background shape.
Horizontally or vertically oriented text based on rotation (only available for map labeling) ...
Text at point of origin draw mode.
bool fontFound() const
Returns true if the specified font was found on the system, or false if the font was not found and a ...
Utility functions for text rendering.
QgsTextBackgroundSettings background() const
Returns a reference to the text background settings.
Draw shadow under buffer.
ShadowPlacement
Placement positions for text shadow.
QgsTextShadowSettings & shadow()
Returns a reference to the text drop shadow settings.
HAlignment
Horizontal alignment.
Base class for visual effects which can be applied to QPicture drawings.
The QgsVectorLayerLabelProvider class implements a label provider for vector layers.
void setBackground(const QgsTextBackgroundSettings &backgroundSettings)
Sets the text&#39;s background settings.q.
Label-specific draw mode.
Container for settings relating to a text background object.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgssymbol.h:860
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define SIP_SKIP
Definition: qgis_sip.h:126
Buffer component.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Draw shadow under text.
QgsTextBackgroundSettings & background()
Returns a reference to the text background settings.
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
SizeType
Methods for determining the background shape size.
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsTextShadowSettings shadow() const
Returns a reference to the text drop shadow settings.
Handles rendering text using rich formatting options, including drop shadows, buffers and background ...
ShapeType
Background shape types.
TextPart
Components of text.
Contains information about the context of a rendering operation.
Shape rotation is offset from text rotation.
Struct for storing maximum and minimum scales for measurements in map units.
Container for settings relating to a text shadow.
Container for settings relating to a text buffer.
#define SIP_OUT
Definition: qgis_sip.h:58
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
void setBuffer(const QgsTextBufferSettings &bufferSettings)
Sets the text&#39;s buffer settings.
void setShadow(const QgsTextShadowSettings &shadowSettings)
Sets the text&#39;s drop shadow settings.
Container for all settings relating to text rendering.
DrawMode
Draw mode to calculate width and height.
Represents a vector layer which manages a vector based data sets.
Square - buffered sizes only.
QgsTextBufferSettings buffer() const
Returns a reference to the text buffer settings.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:145
Horizontally oriented text.
QString resolvedFontFamily() const
Returns the family for the resolved font, ie if the specified font was not found on the system this w...