QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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
QgsLayoutItem::writePropertiesToElement
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
Definition: qgslayoutitem.cpp:1325
QgsLayoutItem::setFrameEnabled
virtual void setFrameEnabled(bool drawFrame)
Sets whether this item has a frame drawn around it or not.
Definition: qgslayoutitem.cpp:833
QgsLayoutItemLabel::marginY
double marginY() const
Returns the vertical margin between the edge of the frame and the label contents, in layout units.
Definition: qgslayoutitemlabel.h:163
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsLayoutItem::setFrameStrokeWidth
virtual void setFrameStrokeWidth(QgsLayoutMeasurement width)
Sets the frame stroke width.
Definition: qgslayoutitem.cpp:859
QgsLayoutItem::icon
virtual QIcon icon() const
Returns the item's icon.
Definition: qgslayoutitem.h:334
QgsLayoutItemLabel::setFontColor
void setFontColor(const QColor &color)
Sets the label font color.
Definition: qgslayoutitemlabel.h:198
QgsLayoutItemRenderContext
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:45
QgsLayoutItemLabel::ModeFont
@ ModeFont
Label displays text rendered using a single font.
Definition: qgslayoutitemlabel.h:43
QgsLayoutItemLabel::setHAlign
void setHAlign(Qt::AlignmentFlag alignment)
Sets the horizontal alignment of the label.
Definition: qgslayoutitemlabel.h:140
QgsLayoutItemLabel::setVAlign
void setVAlign(Qt::AlignmentFlag alignment)
Sets for the vertical alignment of the label.
Definition: qgslayoutitemlabel.h:147
QgsLayoutItemLabel::mode
Mode mode() const
Returns the label's current mode.
Definition: qgslayoutitemlabel.h:100
QgsLayoutItem::readPropertiesFromElement
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
Definition: qgslayoutitem.cpp:1330
QgsLayoutItem::draw
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item's contents using the specified item render context.
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsLayoutItem::type
int type() const override
Returns a unique graphics item type identifier.
Definition: qgslayoutitem.cpp:124
qgswebpage.h
qgslayoutitem.h
QgsLayoutItemLabel::marginX
double marginX() const
Returns the horizontal margin between the edge of the frame and the label contents,...
Definition: qgslayoutitemlabel.h:155
QgsLayoutItemLabel::hAlign
Qt::AlignmentFlag hAlign() const
Returns the horizontal alignment of the label.
Definition: qgslayoutitemlabel.h:133
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:113
QgsLayoutItemLabel::ModeHtml
@ ModeHtml
Label displays rendered HTML content.
Definition: qgslayoutitemlabel.h:44
QgsLayoutItem::refresh
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
Definition: qgslayoutitem.cpp:1176
QgsLayoutItemLabel::text
QString text() const
Returns the label's preset text.
Definition: qgslayoutitemlabel.h:81
QgsLayoutItemLabel::Mode
Mode
Label modes.
Definition: qgslayoutitemlabel.h:42
QgsLayoutItemLabel::fontColor
QColor fontColor() const
Returns the label font color.
Definition: qgslayoutitemlabel.h:204
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsLayoutItem::displayName
virtual QString displayName() const
Gets item display name.
Definition: qgslayoutitem.cpp:107
QgsDistanceArea
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Definition: qgsdistancearea.h:50
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsLayoutItemLabel
A layout item subclass for text labels.
Definition: qgslayoutitemlabel.h:35
QgsLayoutItemLabel::vAlign
Qt::AlignmentFlag vAlign() const
Returns for the vertical alignment of the label.
Definition: qgslayoutitemlabel.h:126
QgsLayoutMeasurement
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
Definition: qgslayoutmeasurement.h:34