QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 "qgstextformat.h"
23#include <QFont>
24#include <QUrl>
25
26class QgsVectorLayer;
27class QgsFeature;
28class QgsDistanceArea;
29
34class CORE_EXPORT QgsLayoutItemLabel: public QgsLayoutItem
35{
36 Q_OBJECT
37
38 public:
39
41 enum Mode
42 {
45 };
46
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
114 Q_DECL_DEPRECATED QFont font() const SIP_DEPRECATED;
115
121 Q_DECL_DEPRECATED void setFont( const QFont &font ) SIP_DEPRECATED;
122
128 Qt::AlignmentFlag vAlign() const { return mVAlignment; }
129
135 Qt::AlignmentFlag hAlign() const { return mHAlignment; }
136
142 void setHAlign( Qt::AlignmentFlag alignment ) { mHAlignment = alignment; invalidateCache(); }
143
149 void setVAlign( Qt::AlignmentFlag alignment ) { mVAlignment = alignment; invalidateCache(); }
150
157 double marginX() const { return mMarginX; }
158
165 double marginY() const { return mMarginY; }
166
178 void setMargin( double margin );
179
186 void setMarginX( double margin );
187
194 void setMarginY( double margin );
195
201 Q_DECL_DEPRECATED void setFontColor( const QColor &color ) SIP_DEPRECATED { mFormat.setColor( color ); }
202
208 Q_DECL_DEPRECATED QColor fontColor() const SIP_DEPRECATED { return mFormat.color(); }
209
210 // In case of negative margins, the bounding rect may be larger than the
211 // label's frame
212 QRectF boundingRect() const override;
213 void setFrameEnabled( bool drawFrame ) override;
214 void setFrameStrokeWidth( QgsLayoutMeasurement strokeWidth ) override;
215
221 QgsTextFormat textFormat() const;
222
228 void setTextFormat( const QgsTextFormat &format );
229
230 public slots:
231
232 void refresh() override;
233
240 void convertToStaticText();
241
242 protected:
243 void draw( QgsLayoutItemRenderContext &context ) override;
244 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
245 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
246
247 private slots:
248
249 void refreshExpressionContext();
251 void updateBoundingRect();
252
253 private:
254 bool mFirstRender = true;
255
256 // Text
257 QString mText;
258
259 Mode mMode = ModeFont;
260 double mHtmlUnitsToLayoutUnits = 1.0;
261 double htmlUnitsToLayoutUnits(); //calculate scale factor
262
264 void itemShiftAdjustSize( double newWidth, double newHeight, double &xShift, double &yShift ) const;
265
267 void contentChanged();
268
269 QgsTextFormat mFormat;
270
272 double mMarginX = 0.0;
274 double mMarginY = 0.0;
275
277 Qt::AlignmentFlag mHAlignment = Qt::AlignLeft;
278
280 Qt::AlignmentFlag mVAlignment = Qt::AlignTop;
281
283 void replaceDateText( QString &text ) const;
284
286 QFont createDefaultFont() const;
287
289 QUrl createStylesheetUrl() const;
290
292 QString createStylesheet() const;
293
294 std::unique_ptr< QgsDistanceArea > mDistanceArea;
295
296 QRectF mCurrentRectangle;
297
298 friend class QgsLayoutItemHtml;
299};
300
301#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 multiframe subclass for HTML content.
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.
Q_DECL_DEPRECATED QColor fontColor() const
Returns the label font color.
double marginX() const
Returns the horizontal margin between the edge of the frame and the label contents,...
double marginY() const
Returns the vertical margin between the edge of the frame and the label contents, in layout units.
QString text() const
Returns the label's preset text.
Q_DECL_DEPRECATED void setFontColor(const QColor &color)
Sets the label font color.
void setVAlign(Qt::AlignmentFlag alignment)
Sets for the vertical alignment of the label.
Qt::AlignmentFlag hAlign() const
Returns the horizontal alignment of the label.
@ 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:43
Base class for graphical items within a QgsLayout.
virtual void setFrameStrokeWidth(QgsLayoutMeasurement width)
Sets the frame stroke width.
virtual QIcon icon() const
Returns the item's icon.
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 void invalidateCache()
Forces a deferred update of any cached image the item uses.
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:49
The class is used as a container of context for various read/write operations on other objects.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
Represents a vector layer which manages a vector based data sets.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_FACTORY
Definition: qgis_sip.h:76