QGIS API Documentation  3.0.2-Girona (307d082)
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 
1059  QFont font() const;
1060 
1069  QFont scaledFont( const QgsRenderContext &context ) const;
1070 
1079  void setFont( const QFont &font );
1080 
1086  QString namedStyle() const;
1087 
1094  void setNamedStyle( const QString &style );
1095 
1101  double size() const;
1102 
1109  void setSize( double size );
1110 
1117  QgsUnitTypes::RenderUnit sizeUnit() const;
1118 
1126  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
1127 
1134  QgsMapUnitScale sizeMapUnitScale() const;
1135 
1142  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
1143 
1148  QColor color() const;
1149 
1155  void setColor( const QColor &color );
1156 
1162  double opacity() const;
1163 
1170  void setOpacity( double opacity );
1171 
1176  QPainter::CompositionMode blendMode() const;
1177 
1183  void setBlendMode( QPainter::CompositionMode mode );
1184 
1191  double lineHeight() const;
1192 
1200  void setLineHeight( double height );
1201 
1206  void readFromLayer( QgsVectorLayer *layer );
1207 
1212  void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
1213 
1218  QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
1219 
1225  QMimeData *toMimeData() const SIP_FACTORY;
1226 
1232  static QgsTextFormat fromMimeData( const QMimeData *data, bool *ok SIP_OUT = nullptr );
1233 
1238  bool containsAdvancedEffects() const;
1239 
1245  bool fontFound() const { return mTextFontFound; }
1246 
1253  QString resolvedFontFamily() const { return mTextFontFamily; }
1254 
1255  private:
1256 
1257  QgsTextBufferSettings mBufferSettings;
1258  QgsTextBackgroundSettings mBackgroundSettings;
1259  QgsTextShadowSettings mShadowSettings;
1260 
1261  QString mTextFontFamily;
1262  bool mTextFontFound = true;
1263 
1264  QSharedDataPointer<QgsTextSettingsPrivate> d;
1265 
1266 };
1267 
1276 class CORE_EXPORT QgsTextRenderer
1277 {
1278  public:
1279 
1282  {
1283  Text = 0,
1287  };
1288 
1291  {
1292  AlignLeft = 0,
1295  };
1296 
1305  static int sizeToPixel( double size, const QgsRenderContext &c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
1306 
1319  static void drawText( const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines,
1320  QgsRenderContext &context, const QgsTextFormat &format,
1321  bool drawAsOutlines = true );
1322 
1335  static void drawText( QPointF point, double rotation, HAlignment alignment, const QStringList &textLines,
1336  QgsRenderContext &context, const QgsTextFormat &format,
1337  bool drawAsOutlines = true );
1338 
1354  static void drawPart( const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines,
1355  QgsRenderContext &context, const QgsTextFormat &format,
1356  TextPart part, bool drawAsOutlines = true );
1357 
1373  static void drawPart( QPointF origin, double rotation, HAlignment alignment, const QStringList &textLines,
1374  QgsRenderContext &context, const QgsTextFormat &format,
1375  TextPart part, bool drawAsOutlines = true );
1376 
1377  private:
1378 
1379  enum DrawMode
1380  {
1381  Rect = 0,
1382  Point,
1383  Label,
1384  };
1385 
1386  struct Component
1387  {
1389  QString text;
1391  QPointF origin;
1393  bool useOrigin = false;
1395  double rotation = 0.0;
1397  double rotationOffset = 0.0;
1399  QPointF center;
1401  QSizeF size;
1403  QPointF offset;
1405  QPicture picture;
1406 
1411  double pictureBuffer = 0.0;
1413  double dpiRatio = 1.0;
1415  HAlignment hAlign = AlignLeft;
1416  };
1417 
1418  static void drawBuffer( QgsRenderContext &context,
1419  const Component &component,
1420  const QgsTextFormat &format );
1421 
1422  static void drawBackground( QgsRenderContext &context,
1423  Component component,
1424  const QgsTextFormat &format,
1425  const QStringList &textLines,
1426  DrawMode mode = Rect );
1427 
1428  static void drawShadow( QgsRenderContext &context,
1429  const Component &component,
1430  const QgsTextFormat &format );
1431 
1432  static void drawText( QgsRenderContext &context,
1433  const Component &component,
1434  const QgsTextFormat &format );
1435 
1436  static void drawTextInternal( TextPart drawType,
1437  QgsRenderContext &context,
1438  const QgsTextFormat &format,
1439  const Component &component,
1440  const QStringList &textLines,
1441  const QFontMetricsF *fontMetrics,
1442  HAlignment alignment,
1443  bool drawAsOutlines,
1444  DrawMode mode = Rect );
1445 
1447  friend class QgsLabelPreview;
1448 
1449  static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
1450 
1451  static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
1452  QFontMetricsF *fm = nullptr );
1453  static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode,
1454  QFontMetricsF *fm = nullptr );
1455 
1456 
1457 };
1458 
1459 #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.
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.
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
#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.
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...