QGIS API Documentation  3.9.0-Master (224899f119)
qgslayoutitemlabel.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutitemlabel.h
3  -------------------
4  begin : October 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #ifndef QGSLAYOUTITEMLABEL_H
18 #define QGSLAYOUTITEMLABEL_H
19 
20 #include "qgis_core.h"
21 #include "qgslayoutitem.h"
22 #include "qgswebpage.h"
23 #include <QFont>
24 
25 class QgsVectorLayer;
26 class QgsFeature;
27 class QgsDistanceArea;
28 
34 class CORE_EXPORT QgsLayoutItemLabel: public QgsLayoutItem
35 {
36  Q_OBJECT
37 
38  public:
39 
41  enum Mode
42  {
45  };
46 
50  QgsLayoutItemLabel( QgsLayout *layout );
51 
57  static QgsLayoutItemLabel *create( QgsLayout *layout ) SIP_FACTORY;
58 
59  int type() const override;
60  QIcon icon() const override;
61  //Overridden to contain part of label's text
62  QString displayName() const override;
63 
68  void adjustSizeToText();
69 
74  QSizeF sizeForText() const;
75 
81  QString text() const { return mText; }
82 
87  void setText( const QString &text );
88 
94  QString currentText() const;
95 
100  Mode mode() const { return mMode; }
101 
107  void setMode( Mode mode );
108 
113  QFont font() const;
114 
119  void setFont( const QFont &font );
120 
126  Qt::AlignmentFlag vAlign() const { return mVAlignment; }
127 
133  Qt::AlignmentFlag hAlign() const { return mHAlignment; }
134 
140  void setHAlign( Qt::AlignmentFlag alignment ) { mHAlignment = alignment; }
141 
147  void setVAlign( Qt::AlignmentFlag alignment ) { mVAlignment = alignment; }
148 
155  double marginX() const { return mMarginX; }
156 
163  double marginY() const { return mMarginY; }
164 
176  void setMargin( double margin );
177 
184  void setMarginX( double margin );
185 
192  void setMarginY( double margin );
193 
198  void setFontColor( const QColor &color ) { mFontColor = color; }
199 
204  QColor fontColor() const { return mFontColor; }
205 
206  // In case of negative margins, the bounding rect may be larger than the
207  // label's frame
208  QRectF boundingRect() const override;
209 
210  // Reimplemented to call prepareGeometryChange after toggling frame
211  void setFrameEnabled( bool drawFrame ) override;
212 
213  // Reimplemented to call prepareGeometryChange after changing stroke width
214  void setFrameStrokeWidth( QgsLayoutMeasurement strokeWidth ) override;
215 
216  public slots:
217 
218  void refresh() override;
219 
220  protected:
221  void draw( QgsLayoutItemRenderContext &context ) override;
222  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
223  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
224 
225  private slots:
226 
228  void loadingHtmlFinished( bool );
229 
230  void refreshExpressionContext();
231 
232  private:
233  bool mFirstRender = true;
234 
235  // Text
236  QString mText;
237 
238  Mode mMode = ModeFont;
239  double mHtmlUnitsToLayoutUnits = 1.0;
240  double htmlUnitsToLayoutUnits(); //calculate scale factor
241  bool mHtmlLoaded = false;
242 
244  void itemShiftAdjustSize( double newWidth, double newHeight, double &xShift, double &yShift ) const;
245 
247  void contentChanged();
248 
250  QFont mFont;
251 
253  double mMarginX = 0.0;
255  double mMarginY = 0.0;
256 
258  QColor mFontColor = QColor( 0, 0, 0 );
259 
261  Qt::AlignmentFlag mHAlignment = Qt::AlignJustify;
262 
264  Qt::AlignmentFlag mVAlignment = Qt::AlignTop;
265 
267  void replaceDateText( QString &text ) const;
268 
270  QUrl createStylesheetUrl() const;
271 
272  std::unique_ptr< QgsDistanceArea > mDistanceArea;
273 
274  std::unique_ptr< QgsWebPage > mWebPage;
275 };
276 
277 #endif //QGSLAYOUTITEMLABEL_H
The class is used as a container of context for various read/write operations on other objects...
virtual QIcon icon() const
Returns the item&#39;s icon.
double marginX() const
Returns the horizontal margin between the edge of the frame and the label contents, in layout units.
Base class for graphical items within a QgsLayout.
void setVAlign(Qt::AlignmentFlag alignment)
Sets for the vertical alignment of the label.
int type() const override
Returns a unique graphics item type identifier.
Mode mode() const
Returns the label&#39;s current mode.
A layout item subclass for text labels.
Label displays rendered HTML content.
QColor fontColor() const
Returns the label font color.
virtual void setFrameStrokeWidth(QgsLayoutMeasurement width)
Sets the frame stroke width.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
QString text() const
Returns the label&#39;s preset text.
void setHAlign(Qt::AlignmentFlag alignment)
Sets the horizontal alignment of the label.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
Qt::AlignmentFlag hAlign() const
Returns the horizontal alignment of the label.
Qt::AlignmentFlag vAlign() const
Returns for the vertical alignment of the label.
#define SIP_FACTORY
Definition: qgis_sip.h:76
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:44
Label displays text rendered using a single font.
void setFontColor(const QColor &color)
Sets the label font color.
virtual QString displayName() const
Gets item display name.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
double marginY() const
Returns the vertical margin between the edge of the frame and the label contents, in layout units...
virtual void setFrameEnabled(bool drawFrame)
Sets whether this item has a frame drawn around it or not.
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item&#39;s contents using the specified item render context.
Represents a vector layer which manages a vector based data sets.
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...