QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
24#include <QFont>
25#include <QUrl>
26
27class QgsVectorLayer;
28class QgsFeature;
29class QgsDistanceArea;
30
35class CORE_EXPORT QgsLayoutItemLabel: public QgsLayoutItem
36{
37 Q_OBJECT
38
39 public:
40
42 enum Mode
43 {
46 };
47
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
79 void adjustSizeToText( QgsLayoutItem::ReferencePoint referencePoint );
80
85 QSizeF sizeForText() const;
86
92 QString text() const { return mText; }
93
98 void setText( const QString &text );
99
105 QString currentText() const;
106
111 Mode mode() const { return mMode; }
112
118 void setMode( Mode mode );
119
125 Q_DECL_DEPRECATED QFont font() const SIP_DEPRECATED;
126
132 Q_DECL_DEPRECATED void setFont( const QFont &font ) SIP_DEPRECATED;
133
139 Qt::AlignmentFlag vAlign() const { return mVAlignment; }
140
146 Qt::AlignmentFlag hAlign() const { return mHAlignment; }
147
153 void setHAlign( Qt::AlignmentFlag alignment ) { mHAlignment = alignment; invalidateCache(); }
154
160 void setVAlign( Qt::AlignmentFlag alignment ) { mVAlignment = alignment; invalidateCache(); }
161
168 double marginX() const { return mMarginX; }
169
176 double marginY() const { return mMarginY; }
177
189 void setMargin( double margin );
190
197 void setMarginX( double margin );
198
205 void setMarginY( double margin );
206
212 Q_DECL_DEPRECATED void setFontColor( const QColor &color ) SIP_DEPRECATED { mFormat.setColor( color ); }
213
219 Q_DECL_DEPRECATED QColor fontColor() const SIP_DEPRECATED { return mFormat.color(); }
220
221 // In case of negative margins, the bounding rect may be larger than the
222 // label's frame
223 QRectF boundingRect() const override;
224 void setFrameEnabled( bool drawFrame ) override;
225 void setFrameStrokeWidth( QgsLayoutMeasurement strokeWidth ) override;
226
232 QgsTextFormat textFormat() const;
233
239 void setTextFormat( const QgsTextFormat &format );
240
241 public slots:
242
243 void refresh() override;
244
251 void convertToStaticText();
252
253 protected:
254 void draw( QgsLayoutItemRenderContext &context ) override;
255 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
256 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
257
258 private slots:
259
260 void refreshExpressionContext();
262 void updateBoundingRect();
263
264 private:
265 // Text
266 QString mText;
267
268 Mode mMode = ModeFont;
269 double mHtmlUnitsToLayoutUnits = 1.0;
270 double htmlUnitsToLayoutUnits(); //calculate scale factor
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::AlignLeft;
287
289 Qt::AlignmentFlag mVAlignment = Qt::AlignTop;
290
292 void replaceDateText( QString &text ) const;
293
295 QFont createDefaultFont() const;
296
298 QUrl createStylesheetUrl() const;
299
301 QString createStylesheet() const;
302
303 std::unique_ptr< QgsDistanceArea > mDistanceArea;
304
305 QRectF mCurrentRectangle;
306
307 friend class QgsLayoutItemHtml;
308};
309
310#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:58
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,...
Qt::AlignmentFlag vAlign() const
Returns for the vertical alignment of the label.
QgsLayoutItemLabel(QgsLayout *layout)
Constructor for QgsLayoutItemLabel, with the specified parent layout.
friend class QgsLayoutItemHtml
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.
virtual void setFrameStrokeWidth(QgsLayoutMeasurement width)
Sets the frame stroke width.
QgsLayoutItem(QgsLayout *layout, bool manageZValue=true)
Constructor for QgsLayoutItem, with the specified parent layout.
virtual QIcon icon() const
Returns the item's icon.
ReferencePoint
Fixed position reference point.
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.
Provides a method of storing measurements for use in QGIS layouts using a variety of different measur...
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:50
A container for the context for various read/write operations on objects.
Container for all settings relating to text rendering.
Represents a vector layer which manages a vector based dataset.
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_FACTORY
Definition qgis_sip.h:84