QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgstextformat.h"
24 #include <QFont>
25 #include <QUrl>
26 
27 class QgsVectorLayer;
28 class QgsFeature;
29 class QgsDistanceArea;
30 
36 class CORE_EXPORT QgsLayoutItemLabel: public QgsLayoutItem
37 {
38  Q_OBJECT
39 
40  public:
41 
43  enum Mode
44  {
47  };
48 
52  QgsLayoutItemLabel( QgsLayout *layout );
53 
59  static QgsLayoutItemLabel *create( QgsLayout *layout ) SIP_FACTORY;
60 
61  int type() const override;
62  QIcon icon() const override;
63  //Overridden to contain part of label's text
64  QString displayName() const override;
65 
70  void adjustSizeToText();
71 
76  QSizeF sizeForText() const;
77 
83  QString text() const { return mText; }
84 
89  void setText( const QString &text );
90 
96  QString currentText() const;
97 
102  Mode mode() const { return mMode; }
103 
109  void setMode( Mode mode );
110 
116  Q_DECL_DEPRECATED QFont font() const SIP_DEPRECATED;
117 
123  Q_DECL_DEPRECATED void setFont( const QFont &font ) SIP_DEPRECATED;
124 
130  Qt::AlignmentFlag vAlign() const { return mVAlignment; }
131 
137  Qt::AlignmentFlag hAlign() const { return mHAlignment; }
138 
144  void setHAlign( Qt::AlignmentFlag alignment ) { mHAlignment = alignment; }
145 
151  void setVAlign( Qt::AlignmentFlag alignment ) { mVAlignment = alignment; }
152 
159  double marginX() const { return mMarginX; }
160 
167  double marginY() const { return mMarginY; }
168 
180  void setMargin( double margin );
181 
188  void setMarginX( double margin );
189 
196  void setMarginY( double margin );
197 
203  Q_DECL_DEPRECATED void setFontColor( const QColor &color ) SIP_DEPRECATED { mFormat.setColor( color ); }
204 
210  Q_DECL_DEPRECATED QColor fontColor() const SIP_DEPRECATED { return mFormat.color(); }
211 
212  // In case of negative margins, the bounding rect may be larger than the
213  // label's frame
214  QRectF boundingRect() const override;
215 
216  // Reimplemented to call prepareGeometryChange after toggling frame
217  void setFrameEnabled( bool drawFrame ) override;
218 
219  // Reimplemented to call prepareGeometryChange after changing stroke width
220  void setFrameStrokeWidth( QgsLayoutMeasurement strokeWidth ) override;
221 
222 
228  QgsTextFormat textFormat() const;
229 
235  void setTextFormat( const QgsTextFormat &format );
236 
237  public slots:
238 
239  void refresh() override;
240 
247  void convertToStaticText();
248 
249  protected:
250  void draw( QgsLayoutItemRenderContext &context ) override;
251  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
252  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
253 
254  private slots:
255 
257  void loadingHtmlFinished( bool );
258 
259  void refreshExpressionContext();
260 
261  private:
262  bool mFirstRender = true;
263 
264  // Text
265  QString mText;
266 
267  Mode mMode = ModeFont;
268  double mHtmlUnitsToLayoutUnits = 1.0;
269  double htmlUnitsToLayoutUnits(); //calculate scale factor
270  bool mHtmlLoaded = false;
271 
273  void itemShiftAdjustSize( double newWidth, double newHeight, double &xShift, double &yShift ) const;
274 
276  void contentChanged();
277 
278  QgsTextFormat mFormat;
279 
281  double mMarginX = 0.0;
283  double mMarginY = 0.0;
284 
286  Qt::AlignmentFlag mHAlignment = Qt::AlignJustify;
287 
289  Qt::AlignmentFlag mVAlignment = Qt::AlignTop;
290 
292  void replaceDateText( QString &text ) const;
293 
295  QUrl createStylesheetUrl() const;
296 
297  std::unique_ptr< QgsDistanceArea > mDistanceArea;
298 
299  std::unique_ptr< QgsWebPage > mWebPage;
300 };
301 
302 #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:1326
QgsLayoutItem::setFrameEnabled
virtual void setFrameEnabled(bool drawFrame)
Sets whether this item has a frame drawn around it or not.
Definition: qgslayoutitem.cpp:834
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:167
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsLayoutItem::setFrameStrokeWidth
virtual void setFrameStrokeWidth(QgsLayoutMeasurement width)
Sets the frame stroke width.
Definition: qgslayoutitem.cpp:860
QgsLayoutItem::icon
virtual QIcon icon() const
Returns the item's icon.
Definition: qgslayoutitem.h:336
QgsLayoutItemRenderContext
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:44
QgsLayoutItemLabel::ModeFont
@ ModeFont
Label displays text rendered using a single font.
Definition: qgslayoutitemlabel.h:45
QgsLayoutItemLabel::setHAlign
void setHAlign(Qt::AlignmentFlag alignment)
Sets the horizontal alignment of the label.
Definition: qgslayoutitemlabel.h:144
QgsLayoutItemLabel::setVAlign
void setVAlign(Qt::AlignmentFlag alignment)
Sets for the vertical alignment of the label.
Definition: qgslayoutitemlabel.h:151
QgsLayoutItemLabel::mode
Mode mode() const
Returns the label's current mode.
Definition: qgslayoutitemlabel.h:102
QgsLayoutItem::readPropertiesFromElement
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
Definition: qgslayoutitem.cpp:1331
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
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsLayoutItemLabel::setFontColor
Q_DECL_DEPRECATED void setFontColor(const QColor &color)
Sets the label font color.
Definition: qgslayoutitemlabel.h:203
qgswebpage.h
QgsTextFormat
Container for all settings relating to text rendering.
Definition: qgstextformat.h:40
qgslayoutitem.h
QgsLayoutItemLabel::marginX
double marginX() const
Returns the horizontal margin between the edge of the frame and the label contents,...
Definition: qgslayoutitemlabel.h:159
QgsLayoutItemLabel::hAlign
Qt::AlignmentFlag hAlign() const
Returns the horizontal alignment of the label.
Definition: qgslayoutitemlabel.h:137
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
QgsLayoutItemLabel::ModeHtml
@ ModeHtml
Label displays rendered HTML content.
Definition: qgslayoutitemlabel.h:46
QgsLayoutItem::refresh
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
Definition: qgslayoutitem.cpp:1177
QgsLayoutItemLabel::text
QString text() const
Returns the label's preset text.
Definition: qgslayoutitemlabel.h:83
QgsLayoutItemLabel::Mode
Mode
Label modes.
Definition: qgslayoutitemlabel.h:43
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:391
QgsLayoutItemLabel::fontColor
Q_DECL_DEPRECATED QColor fontColor() const
Returns the label font color.
Definition: qgslayoutitemlabel.h:210
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:52
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
qgstextformat.h
QgsLayoutItemLabel
A layout item subclass for text labels.
Definition: qgslayoutitemlabel.h:36
QgsLayoutMeasurement
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
Definition: qgslayoutmeasurement.h:33