QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 #include <QUrl>
25 
26 class QgsVectorLayer;
27 class QgsFeature;
28 class QgsDistanceArea;
29 
35 class CORE_EXPORT QgsLayoutItemLabel: public QgsLayoutItem
36 {
37  Q_OBJECT
38 
39  public:
40 
42  enum Mode
43  {
46  };
47 
51  QgsLayoutItemLabel( QgsLayout *layout );
52 
58  static QgsLayoutItemLabel *create( QgsLayout *layout ) SIP_FACTORY;
59 
60  int type() const override;
61  QIcon icon() const override;
62  //Overridden to contain part of label's text
63  QString displayName() const override;
64 
69  void adjustSizeToText();
70 
75  QSizeF sizeForText() const;
76 
82  QString text() const { return mText; }
83 
88  void setText( const QString &text );
89 
95  QString currentText() const;
96 
101  Mode mode() const { return mMode; }
102 
108  void setMode( Mode mode );
109 
114  QFont font() const;
115 
120  void setFont( const QFont &font );
121 
127  Qt::AlignmentFlag vAlign() const { return mVAlignment; }
128 
134  Qt::AlignmentFlag hAlign() const { return mHAlignment; }
135 
141  void setHAlign( Qt::AlignmentFlag alignment ) { mHAlignment = alignment; }
142 
148  void setVAlign( Qt::AlignmentFlag alignment ) { mVAlignment = alignment; }
149 
156  double marginX() const { return mMarginX; }
157 
164  double marginY() const { return mMarginY; }
165 
177  void setMargin( double margin );
178 
185  void setMarginX( double margin );
186 
193  void setMarginY( double margin );
194 
199  void setFontColor( const QColor &color ) { mFontColor = color; }
200 
205  QColor fontColor() const { return mFontColor; }
206 
207  // In case of negative margins, the bounding rect may be larger than the
208  // label's frame
209  QRectF boundingRect() const override;
210 
211  // Reimplemented to call prepareGeometryChange after toggling frame
212  void setFrameEnabled( bool drawFrame ) override;
213 
214  // Reimplemented to call prepareGeometryChange after changing stroke width
215  void setFrameStrokeWidth( QgsLayoutMeasurement strokeWidth ) override;
216 
217  public slots:
218 
219  void refresh() override;
220 
227  void convertToStaticText();
228 
229  protected:
230  void draw( QgsLayoutItemRenderContext &context ) override;
231  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
232  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
233 
234  private slots:
235 
237  void loadingHtmlFinished( bool );
238 
239  void refreshExpressionContext();
240 
241  private:
242  bool mFirstRender = true;
243 
244  // Text
245  QString mText;
246 
247  Mode mMode = ModeFont;
248  double mHtmlUnitsToLayoutUnits = 1.0;
249  double htmlUnitsToLayoutUnits(); //calculate scale factor
250  bool mHtmlLoaded = false;
251 
253  void itemShiftAdjustSize( double newWidth, double newHeight, double &xShift, double &yShift ) const;
254 
256  void contentChanged();
257 
259  QFont mFont;
260 
262  double mMarginX = 0.0;
264  double mMarginY = 0.0;
265 
267  QColor mFontColor = QColor( 0, 0, 0 );
268 
270  Qt::AlignmentFlag mHAlignment = Qt::AlignJustify;
271 
273  Qt::AlignmentFlag mVAlignment = Qt::AlignTop;
274 
276  void replaceDateText( QString &text ) const;
277 
279  QUrl createStylesheetUrl() const;
280 
281  std::unique_ptr< QgsDistanceArea > mDistanceArea;
282 
283  std::unique_ptr< QgsWebPage > mWebPage;
284 };
285 
286 #endif //QGSLAYOUTITEMLABEL_H
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
A layout item subclass for text labels.
Mode mode() const
Returns the label's current mode.
void setHAlign(Qt::AlignmentFlag alignment)
Sets the horizontal alignment of the label.
double marginX() const
Returns the horizontal margin between the edge of the frame and the label contents,...
Qt::AlignmentFlag vAlign() const
Returns for the vertical alignment of the label.
double marginY() const
Returns the vertical margin between the edge of the frame and the label contents, in layout units.
QColor fontColor() const
Returns the label font color.
QString text() const
Returns the label's preset text.
void setVAlign(Qt::AlignmentFlag alignment)
Sets for the vertical alignment of the label.
Qt::AlignmentFlag hAlign() const
Returns the horizontal alignment of the label.
void setFontColor(const QColor &color)
Sets the label font color.
@ ModeHtml
Label displays rendered HTML content.
@ ModeFont
Label displays text rendered using a single font.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:45
Base class for graphical items within a QgsLayout.
virtual void setFrameStrokeWidth(QgsLayoutMeasurement width)
Sets the frame stroke width.
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
virtual void setFrameEnabled(bool drawFrame)
Sets whether this item has a frame drawn around it or not.
int type() const override
Returns a unique graphics item type identifier.
virtual QString displayName() const
Gets item display name.
virtual QIcon icon() const
Returns the item's icon.
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item's contents using the specified item render context.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:51
The class is used as a container of context for various read/write operations on other objects.
Represents a vector layer which manages a vector based data sets.
#define SIP_FACTORY
Definition: qgis_sip.h:76