QGIS API Documentation  3.2.0-Bonn (bc43194)
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.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 
46 class CORE_EXPORT QgsTextBufferSettings
47 {
48  public:
49 
51 
57 
62  QgsTextBufferSettings &operator=( const QgsTextBufferSettings &other );
63 
65 
70  bool enabled() const;
71 
77  void setEnabled( bool enabled );
78 
84  double size() const;
85 
92  void setSize( double size );
93 
99  QgsUnitTypes::RenderUnit sizeUnit() const;
100 
107  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
108 
115  QgsMapUnitScale sizeMapUnitScale() const;
116 
124  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
125 
130  QColor color() const;
131 
137  void setColor( const QColor &color );
138 
145  bool fillBufferInterior() const;
146 
153  void setFillBufferInterior( bool fill );
154 
160  double opacity() const;
161 
168  void setOpacity( double opacity );
169 
174  Qt::PenJoinStyle joinStyle() const;
175 
181  void setJoinStyle( Qt::PenJoinStyle style );
182 
187  QPainter::CompositionMode blendMode() const;
188 
194  void setBlendMode( QPainter::CompositionMode mode );
195 
200  void readFromLayer( QgsVectorLayer *layer );
201 
206  void readXml( const QDomElement &elem );
207 
212  QDomElement writeXml( QDomDocument &doc ) const;
213 
219  QgsPaintEffect *paintEffect() const;
220 
226  void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
227 
228  private:
229 
230  QSharedDataPointer<QgsTextBufferSettingsPrivate> d;
231 
232 };
233 
242 class CORE_EXPORT QgsTextBackgroundSettings
243 {
244  public:
245 
250  {
251  ShapeRectangle = 0,
255  ShapeSVG
256  };
257 
261  enum SizeType
262  {
263  SizeBuffer = 0,
265  SizePercent
266  };
267 
272  {
273  RotationSync = 0,
275  RotationFixed
276  };
277 
279 
285 
286  QgsTextBackgroundSettings &operator=( const QgsTextBackgroundSettings &other );
287 
289 
294  bool enabled() const;
295 
301  void setEnabled( bool enabled );
302 
307  ShapeType type() const;
308 
314  void setType( ShapeType type );
315 
320  QString svgFile() const;
321 
328  void setSvgFile( const QString &file );
329 
336  SizeType sizeType() const;
337 
345  void setSizeType( SizeType type );
346 
355  QSizeF size() const;
356 
366  void setSize( QSizeF size );
367 
375  QgsUnitTypes::RenderUnit sizeUnit() const;
376 
385  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
386 
393  QgsMapUnitScale sizeMapUnitScale() const;
394 
402  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
403 
409  RotationType rotationType() const;
410 
417  void setRotationType( RotationType type );
418 
424  double rotation() const;
425 
431  void setRotation( double rotation );
432 
439  QPointF offset() const;
440 
448  void setOffset( QPointF offset );
449 
455  QgsUnitTypes::RenderUnit offsetUnit() const;
456 
463  void setOffsetUnit( QgsUnitTypes::RenderUnit units );
464 
471  QgsMapUnitScale offsetMapUnitScale() const;
472 
480  void setOffsetMapUnitScale( const QgsMapUnitScale &scale );
481 
488  QSizeF radii() const;
489 
498  void setRadii( QSizeF radii );
499 
505  QgsUnitTypes::RenderUnit radiiUnit() const;
506 
513  void setRadiiUnit( QgsUnitTypes::RenderUnit units );
514 
521  QgsMapUnitScale radiiMapUnitScale() const;
522 
530  void setRadiiMapUnitScale( const QgsMapUnitScale &scale );
531 
537  double opacity() const;
538 
545  void setOpacity( double opacity );
546 
551  QPainter::CompositionMode blendMode() const;
552 
558  void setBlendMode( QPainter::CompositionMode mode );
559 
565  QColor fillColor() const;
566 
573  void setFillColor( const QColor &color );
574 
580  QColor strokeColor() const;
581 
588  void setStrokeColor( const QColor &color );
589 
596  double strokeWidth() const;
597 
604  void setStrokeWidth( double width );
605 
611  QgsUnitTypes::RenderUnit strokeWidthUnit() const;
612 
619  void setStrokeWidthUnit( QgsUnitTypes::RenderUnit units );
620 
627  QgsMapUnitScale strokeWidthMapUnitScale() const;
628 
636  void setStrokeWidthMapUnitScale( const QgsMapUnitScale &scale );
637 
642  Qt::PenJoinStyle joinStyle() const;
643 
649  void setJoinStyle( Qt::PenJoinStyle style );
650 
656  QgsPaintEffect *paintEffect() const;
657 
663  void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
664 
669  void readFromLayer( QgsVectorLayer *layer );
670 
675  void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
676 
681  QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
682 
683  private:
684 
685  QSharedDataPointer<QgsTextBackgroundSettingsPrivate> d;
686 
687 };
688 
697 class CORE_EXPORT QgsTextShadowSettings
698 {
699  public:
700 
705  {
706  ShadowLowest = 0,
709  ShadowShape
710  };
711 
713 
719 
720  QgsTextShadowSettings &operator=( const QgsTextShadowSettings &other );
721 
723 
728  bool enabled() const;
729 
735  void setEnabled( bool enabled );
736 
743  QgsTextShadowSettings::ShadowPlacement shadowPlacement() const;
744 
752  void setShadowPlacement( QgsTextShadowSettings::ShadowPlacement placement );
753 
759  int offsetAngle() const;
760 
767  void setOffsetAngle( int angle );
768 
775  double offsetDistance() const;
776 
784  void setOffsetDistance( double distance );
785 
791  QgsUnitTypes::RenderUnit offsetUnit() const;
792 
799  void setOffsetUnit( QgsUnitTypes::RenderUnit units );
800 
807  QgsMapUnitScale offsetMapUnitScale() const;
808 
816  void setOffsetMapUnitScale( const QgsMapUnitScale &scale );
817 
822  bool offsetGlobal() const;
823 
828  void setOffsetGlobal( bool global );
829 
835  double blurRadius() const;
836 
843  void setBlurRadius( double blurRadius );
844 
850  QgsUnitTypes::RenderUnit blurRadiusUnit() const;
851 
858  void setBlurRadiusUnit( QgsUnitTypes::RenderUnit units );
859 
866  QgsMapUnitScale blurRadiusMapUnitScale() const;
867 
875  void setBlurRadiusMapUnitScale( const QgsMapUnitScale &scale );
876 
881  bool blurAlphaOnly() const;
882 
889  void setBlurAlphaOnly( bool alphaOnly );
890 
896  double opacity() const;
897 
904  void setOpacity( double opacity );
905 
910  int scale() const;
911 
917  void setScale( int scale );
918 
923  QColor color() const;
924 
930  void setColor( const QColor &color );
931 
936  QPainter::CompositionMode blendMode() const;
937 
943  void setBlendMode( QPainter::CompositionMode mode );
944 
949  void readFromLayer( QgsVectorLayer *layer );
950 
955  void readXml( const QDomElement &elem );
956 
961  QDomElement writeXml( QDomDocument &doc ) const;
962 
963  private:
964 
965  QSharedDataPointer<QgsTextShadowSettingsPrivate> d;
966 
967 };
968 
969 
978 class CORE_EXPORT QgsTextFormat
979 {
980  public:
981 
982  QgsTextFormat();
983 
988  QgsTextFormat( const QgsTextFormat &other );
989 
990  QgsTextFormat &operator=( const QgsTextFormat &other );
991 
992  ~QgsTextFormat();
993 
998  QgsTextBufferSettings &buffer() { return mBufferSettings; }
999 
1004  SIP_SKIP QgsTextBufferSettings buffer() const { return mBufferSettings; }
1005 
1011  void setBuffer( const QgsTextBufferSettings &bufferSettings ) { mBufferSettings = bufferSettings; }
1012 
1017  QgsTextBackgroundSettings &background() { return mBackgroundSettings; }
1018 
1023  SIP_SKIP QgsTextBackgroundSettings background() const { return mBackgroundSettings; }
1024 
1030  void setBackground( const QgsTextBackgroundSettings &backgroundSettings ) { mBackgroundSettings = backgroundSettings; }
1031 
1036  QgsTextShadowSettings &shadow() { return mShadowSettings; }
1037 
1042  SIP_SKIP QgsTextShadowSettings shadow() const { return mShadowSettings; }
1043 
1049  void setShadow( const QgsTextShadowSettings &shadowSettings ) { mShadowSettings = shadowSettings; }
1050 
1060  QFont font() const;
1061 
1070  QFont scaledFont( const QgsRenderContext &context ) const;
1071 
1081  void setFont( const QFont &font );
1082 
1088  QString namedStyle() const;
1089 
1096  void setNamedStyle( const QString &style );
1097 
1103  double size() const;
1104 
1111  void setSize( double size );
1112 
1119  QgsUnitTypes::RenderUnit sizeUnit() const;
1120 
1128  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
1129 
1136  QgsMapUnitScale sizeMapUnitScale() const;
1137 
1144  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
1145 
1150  QColor color() const;
1151 
1157  void setColor( const QColor &color );
1158 
1164  double opacity() const;
1165 
1172  void setOpacity( double opacity );
1173 
1178  QPainter::CompositionMode blendMode() const;
1179 
1185  void setBlendMode( QPainter::CompositionMode mode );
1186 
1193  double lineHeight() const;
1194 
1202  void setLineHeight( double height );
1203 
1208  void readFromLayer( QgsVectorLayer *layer );
1209 
1214  void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
1215 
1220  QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
1221 
1227  QMimeData *toMimeData() const SIP_FACTORY;
1228 
1236  static QgsTextFormat fromQFont( const QFont &font );
1237 
1245  QFont toQFont() const;
1246 
1252  static QgsTextFormat fromMimeData( const QMimeData *data, bool *ok SIP_OUT = nullptr );
1253 
1258  bool containsAdvancedEffects() const;
1259 
1265  bool fontFound() const { return mTextFontFound; }
1266 
1273  QString resolvedFontFamily() const { return mTextFontFamily; }
1274 
1275  private:
1276 
1277  QgsTextBufferSettings mBufferSettings;
1278  QgsTextBackgroundSettings mBackgroundSettings;
1279  QgsTextShadowSettings mShadowSettings;
1280 
1281  QString mTextFontFamily;
1282  bool mTextFontFound = true;
1283 
1284  QSharedDataPointer<QgsTextSettingsPrivate> d;
1285 
1286 };
1287 
1296 class CORE_EXPORT QgsTextRenderer
1297 {
1298  public:
1299 
1302  {
1303  Rect = 0,
1306  };
1307 
1310  {
1311  Text = 0,
1315  };
1316 
1319  {
1320  AlignLeft = 0,
1323  };
1324 
1333  static int sizeToPixel( double size, const QgsRenderContext &c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
1334 
1347  static void drawText( const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines,
1348  QgsRenderContext &context, const QgsTextFormat &format,
1349  bool drawAsOutlines = true );
1350 
1363  static void drawText( QPointF point, double rotation, HAlignment alignment, const QStringList &textLines,
1364  QgsRenderContext &context, const QgsTextFormat &format,
1365  bool drawAsOutlines = true );
1366 
1382  static void drawPart( const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines,
1383  QgsRenderContext &context, const QgsTextFormat &format,
1384  TextPart part, bool drawAsOutlines = true );
1385 
1401  static void drawPart( QPointF origin, double rotation, HAlignment alignment, const QStringList &textLines,
1402  QgsRenderContext &context, const QgsTextFormat &format,
1403  TextPart part, bool drawAsOutlines = true );
1404 
1411  static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format );
1412 
1420  static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
1421  QFontMetricsF *fontMetrics = nullptr );
1422 
1431  static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode,
1432  QFontMetricsF *fontMetrics = nullptr );
1433 
1434  private:
1435 
1436  struct Component
1437  {
1439  QString text;
1441  QPointF origin;
1443  bool useOrigin = false;
1445  double rotation = 0.0;
1447  double rotationOffset = 0.0;
1449  QPointF center;
1451  QSizeF size;
1453  QPointF offset;
1455  QPicture picture;
1456 
1461  double pictureBuffer = 0.0;
1463  double dpiRatio = 1.0;
1465  HAlignment hAlign = AlignLeft;
1466  };
1467 
1468  static void drawBuffer( QgsRenderContext &context,
1469  const Component &component,
1470  const QgsTextFormat &format );
1471 
1472  static void drawBackground( QgsRenderContext &context,
1473  Component component,
1474  const QgsTextFormat &format,
1475  const QStringList &textLines,
1476  DrawMode mode = Rect );
1477 
1478  static void drawShadow( QgsRenderContext &context,
1479  const Component &component,
1480  const QgsTextFormat &format );
1481 
1482  static void drawText( QgsRenderContext &context,
1483  const Component &component,
1484  const QgsTextFormat &format );
1485 
1486  static void drawTextInternal( TextPart drawType,
1487  QgsRenderContext &context,
1488  const QgsTextFormat &format,
1489  const Component &component,
1490  const QStringList &textLines,
1491  const QFontMetricsF *fontMetrics,
1492  HAlignment alignment,
1493  bool drawAsOutlines,
1494  DrawMode mode = Rect );
1495 
1497  friend class QgsLabelPreview;
1498 
1499  static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
1500 
1501 
1502 };
1503 
1504 #endif // QGSTEXTRENDERER_H
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.
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 ...
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
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:119
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:69
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:51
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:100
QString resolvedFontFamily() const
Returns the family for the resolved font, ie if the specified font was not found on the system this w...