QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgslayoutitemlabel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitemlabel.cpp
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
18#include "qgslayoutitemlabel.h"
20#include "qgslayoututils.h"
21#include "qgslayoutmodel.h"
22#include "qgsexpression.h"
24#include "qgsvectorlayer.h"
25#include "qgsdistancearea.h"
26#include "qgsfontutils.h"
27#include "qgstextformat.h"
28#include "qgstextrenderer.h"
30#include "qgslayoutitemmap.h"
31#include "qgssettings.h"
32#include "qgslayout.h"
35
36#include <QCoreApplication>
37#include <QDate>
38#include <QDomElement>
39#include <QPainter>
40#include <QTextDocument>
41
43 : QgsLayoutItem( layout )
44{
45 mDistanceArea.reset( new QgsDistanceArea() );
46 mHtmlUnitsToLayoutUnits = htmlUnitsToLayoutUnits();
47
48 //get default layout font from settings
49 const QgsSettings settings;
50 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
51 if ( !defaultFontString.isEmpty() )
52 {
53 QFont f = mFormat.font();
54 f.setFamily( defaultFontString );
55 mFormat.setFont( f );
56 }
57
58 //default to a 10 point font size
59 mFormat.setSize( 10 );
60 mFormat.setSizeUnit( Qgis::RenderUnit::Points );
61
62 //default to no background
63 setBackgroundEnabled( false );
64
65 //a label added while atlas preview is enabled needs to have the expression context set,
66 //otherwise fields in the label aren't correctly evaluated until atlas preview feature changes (#9457)
67 refreshExpressionContext();
68}
69
71{
72 return new QgsLayoutItemLabel( layout );
73}
74
76{
78}
79
81{
82 return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemLabel.svg" ) );
83}
84
86{
87 QPainter *painter = context.renderContext().painter();
88 const QgsScopedQPainterState painterState( painter );
89
90 const double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0;
91 const double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth;
92 const double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
93
94 QRectF painterRect;
95 if ( mMode == QgsLayoutItemLabel::ModeFont )
96 {
97 const double rectScale = context.renderContext().scaleFactor();
98 painterRect = QRectF( ( xPenAdjust + mMarginX ) * rectScale,
99 ( yPenAdjust + mMarginY ) * rectScale,
100 ( rect().width() - 2 * xPenAdjust - 2 * mMarginX ) * rectScale,
101 ( rect().height() - 2 * yPenAdjust - 2 * mMarginY ) * rectScale );
102 }
103 else
104 {
105 // The 3.77 adjustment value was found through trial and error, the author has however no clue as to where it comes from
106 const double adjustmentFactor = 3.77;
107 const double rectScale = context.renderContext().scaleFactor() * adjustmentFactor;
108 // The left/right margin is handled by the stylesheet while the top/bottom margin is ignored by QTextDocument
109 painterRect = QRectF( 0, 0,
110 ( rect().width() ) * rectScale,
111 ( rect().height() - yPenAdjust - mMarginY ) * rectScale );
112 painter->translate( 0, ( yPenAdjust + mMarginY ) * context.renderContext().scaleFactor() );
113 painter->scale( context.renderContext().scaleFactor() / adjustmentFactor, context.renderContext().scaleFactor() / adjustmentFactor );
114 }
115
116 switch ( mMode )
117 {
118 case ModeHtml:
119 {
120 QTextDocument document;
121 document.setDocumentMargin( 0 );
122 document.setPageSize( QSizeF( painterRect.width() / context.renderContext().scaleFactor(), painterRect.height() / context.renderContext().scaleFactor() ) );
123 document.setDefaultStyleSheet( createStylesheet() );
124
125 document.setDefaultFont( createDefaultFont() );
126
127 QTextOption textOption = document.defaultTextOption();
128 textOption.setAlignment( mHAlignment );
129 document.setDefaultTextOption( textOption );
130
131 document.setHtml( QStringLiteral( "<body>%1</body>" ).arg( currentText() ) );
132 document.drawContents( painter, painterRect );
133 break;
134 }
135
136 case ModeFont:
137 {
139 QgsTextRenderer::drawText( painterRect, 0,
141 currentText().split( '\n' ),
142 context.renderContext(),
143 mFormat,
144 true,
147 break;
148 }
149 }
150}
151
152void QgsLayoutItemLabel::contentChanged()
153{
154 switch ( mMode )
155 {
156 case ModeHtml:
157 {
159 break;
160 }
161 case ModeFont:
163 break;
164 }
165}
166
167void QgsLayoutItemLabel::setText( const QString &text )
168{
169 mText = text;
170 emit changed();
171
172 contentChanged();
173
174 if ( mLayout && id().isEmpty() && mMode != ModeHtml )
175 {
176 //notify the model that the display name has changed
177 mLayout->itemsModel()->updateItemDisplayName( this );
178 }
179}
180
182{
183 if ( mode == mMode )
184 {
185 return;
186 }
187
188 mMode = mode;
189 contentChanged();
190
191 if ( mLayout && id().isEmpty() )
192 {
193 //notify the model that the display name has changed
194 mLayout->itemsModel()->updateItemDisplayName( this );
195 }
196}
197
198void QgsLayoutItemLabel::refreshExpressionContext()
199{
200 if ( !mLayout )
201 return;
202
203 QgsVectorLayer *layer = mLayout->reportContext().layer();
204 //setup distance area conversion
205 if ( layer )
206 {
207 mDistanceArea->setSourceCrs( layer->crs(), mLayout->project()->transformContext() );
208 }
209 else
210 {
211 //set to composition's reference map's crs
212 QgsLayoutItemMap *referenceMap = mLayout->referenceMap();
213 if ( referenceMap )
214 mDistanceArea->setSourceCrs( referenceMap->crs(), mLayout->project()->transformContext() );
215 }
216 mDistanceArea->setEllipsoid( mLayout->project()->ellipsoid() );
217 contentChanged();
218
219 update();
220}
221
223{
224 QString displayText = mText;
225 replaceDateText( displayText );
226
228
229 return QgsExpression::replaceExpressionText( displayText, &context, mDistanceArea.get() );
230}
231
232void QgsLayoutItemLabel::replaceDateText( QString &text ) const
233{
234 const QString constant = QStringLiteral( "$CURRENT_DATE" );
235 const int currentDatePos = text.indexOf( constant );
236 if ( currentDatePos != -1 )
237 {
238 //check if there is a bracket just after $CURRENT_DATE
239 QString formatText;
240 const int openingBracketPos = text.indexOf( '(', currentDatePos );
241 const int closingBracketPos = text.indexOf( ')', openingBracketPos + 1 );
242 if ( openingBracketPos != -1 &&
243 closingBracketPos != -1 &&
244 ( closingBracketPos - openingBracketPos ) > 1 &&
245 openingBracketPos == currentDatePos + constant.size() )
246 {
247 formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
248 text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
249 }
250 else //no bracket
251 {
252 text.replace( QLatin1String( "$CURRENT_DATE" ), QDate::currentDate().toString() );
253 }
254 }
255}
256
257void QgsLayoutItemLabel::setFont( const QFont &f )
258{
259 mFormat.setFont( f );
260 if ( f.pointSizeF() > 0 )
261 mFormat.setSize( f.pointSizeF() );
262}
263
265{
266 return mFormat;
267}
268
270{
271 mFormat = format;
272}
273
274void QgsLayoutItemLabel::setMargin( const double m )
275{
276 mMarginX = m;
277 mMarginY = m;
278 prepareGeometryChange();
279}
280
281void QgsLayoutItemLabel::setMarginX( const double margin )
282{
283 mMarginX = margin;
284 prepareGeometryChange();
285}
286
287void QgsLayoutItemLabel::setMarginY( const double margin )
288{
289 mMarginY = margin;
290 prepareGeometryChange();
291}
292
294{
295 const QSizeF newSize = sizeForText();
296
297 //keep alignment point constant
298 double xShift = 0;
299 double yShift = 0;
300
301 itemShiftAdjustSize( newSize.width(), newSize.height(), xShift, yShift );
302
303 //update rect for data defined size and position
304 attemptSetSceneRect( QRectF( pos().x() + xShift, pos().y() + yShift, newSize.width(), newSize.height() ) );
305}
306
308{
310
311 const QStringList lines = currentText().split( '\n' );
312 const double textWidth = QgsTextRenderer::textWidth( context, mFormat, lines ) / context.convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
313 const double fontHeight = QgsTextRenderer::textHeight( context, mFormat, lines ) / context.convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
314
315 const double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0;
316
317 const double width = textWidth + 2 * mMarginX + 2 * penWidth;
318 const double height = fontHeight + 2 * mMarginY + 2 * penWidth;
319
320 return mLayout->convertToLayoutUnits( QgsLayoutSize( width, height, Qgis::LayoutUnit::Millimeters ) );
321}
322
324{
325 return mFormat.font();
326}
327
328bool QgsLayoutItemLabel::writePropertiesToElement( QDomElement &layoutLabelElem, QDomDocument &doc, const QgsReadWriteContext &rwContext ) const
329{
330 layoutLabelElem.setAttribute( QStringLiteral( "htmlState" ), static_cast< int >( mMode ) );
331
332 layoutLabelElem.setAttribute( QStringLiteral( "labelText" ), mText );
333 layoutLabelElem.setAttribute( QStringLiteral( "marginX" ), QString::number( mMarginX ) );
334 layoutLabelElem.setAttribute( QStringLiteral( "marginY" ), QString::number( mMarginY ) );
335 layoutLabelElem.setAttribute( QStringLiteral( "halign" ), mHAlignment );
336 layoutLabelElem.setAttribute( QStringLiteral( "valign" ), mVAlignment );
337
338 QDomElement textElem = mFormat.writeXml( doc, rwContext );
339 layoutLabelElem.appendChild( textElem );
340
341 return true;
342}
343
344bool QgsLayoutItemLabel::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext &context )
345{
346 //restore label specific properties
347
348 //text
349 mText = itemElem.attribute( QStringLiteral( "labelText" ) );
350
351 //html state
352 mMode = static_cast< Mode >( itemElem.attribute( QStringLiteral( "htmlState" ) ).toInt() );
353
354 //margin
355 bool marginXOk = false;
356 bool marginYOk = false;
357 mMarginX = itemElem.attribute( QStringLiteral( "marginX" ) ).toDouble( &marginXOk );
358 mMarginY = itemElem.attribute( QStringLiteral( "marginY" ) ).toDouble( &marginYOk );
359 if ( !marginXOk || !marginYOk )
360 {
361 //upgrade old projects where margins where stored in a single attribute
362 const double margin = itemElem.attribute( QStringLiteral( "margin" ), QStringLiteral( "1.0" ) ).toDouble();
363 mMarginX = margin;
364 mMarginY = margin;
365 }
366
367 //Horizontal alignment
368 mHAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "halign" ) ).toInt() );
369
370 //Vertical alignment
371 mVAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "valign" ) ).toInt() );
372
373 //font
374 QDomNodeList textFormatNodeList = itemElem.elementsByTagName( QStringLiteral( "text-style" ) );
375 if ( !textFormatNodeList.isEmpty() )
376 {
377 QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
378 mFormat.readXml( textFormatElem, context );
379 }
380 else
381 {
382 QFont f;
383 if ( !QgsFontUtils::setFromXmlChildNode( f, itemElem, QStringLiteral( "LabelFont" ) ) )
384 {
385 f.fromString( itemElem.attribute( QStringLiteral( "font" ), QString() ) );
386 }
387 mFormat.setFont( f );
388 if ( f.pointSizeF() > 0 )
389 {
390 mFormat.setSize( f.pointSizeF() );
391 mFormat.setSizeUnit( Qgis::RenderUnit::Points );
392 }
393 else if ( f.pixelSize() > 0 )
394 {
395 mFormat.setSize( f.pixelSize() );
396 mFormat.setSizeUnit( Qgis::RenderUnit::Pixels );
397 }
398
399 //font color
400 const QDomNodeList fontColorList = itemElem.elementsByTagName( QStringLiteral( "FontColor" ) );
401 if ( !fontColorList.isEmpty() )
402 {
403 const QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
404 const int red = fontColorElem.attribute( QStringLiteral( "red" ), QStringLiteral( "0" ) ).toInt();
405 const int green = fontColorElem.attribute( QStringLiteral( "green" ), QStringLiteral( "0" ) ).toInt();
406 const int blue = fontColorElem.attribute( QStringLiteral( "blue" ), QStringLiteral( "0" ) ).toInt();
407 const int alpha = fontColorElem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "255" ) ).toInt();
408 mFormat.setColor( QColor( red, green, blue, alpha ) );
409 }
410 else if ( textFormatNodeList.isEmpty() )
411 {
412 mFormat.setColor( QColor( 0, 0, 0 ) );
413 }
414 }
415
416 return true;
417}
418
420{
421 if ( !id().isEmpty() )
422 {
423 return id();
424 }
425
426 switch ( mMode )
427 {
428 case ModeHtml:
429 return tr( "<HTML Label>" );
430
431 case ModeFont:
432 {
433
434 //if no id, default to portion of label text
435 const QString text = mText;
436 if ( text.isEmpty() )
437 {
438 return tr( "<Label>" );
439 }
440 if ( text.length() > 25 )
441 {
442 return QString( tr( "%1…" ) ).arg( text.left( 25 ).simplified() );
443 }
444 else
445 {
446 return text.simplified();
447 }
448 }
449 }
450 return QString(); // no warnings
451}
452
454{
455 QRectF rectangle = rect();
456 const double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0;
457 rectangle.adjust( -penWidth, -penWidth, penWidth, penWidth );
458
459 if ( mMarginX < 0 )
460 {
461 rectangle.adjust( mMarginX, 0, -mMarginX, 0 );
462 }
463 if ( mMarginY < 0 )
464 {
465 rectangle.adjust( 0, mMarginY, 0, -mMarginY );
466 }
467
468 return rectangle;
469}
470
471void QgsLayoutItemLabel::setFrameEnabled( const bool drawFrame )
472{
474 prepareGeometryChange();
475}
476
478{
480 prepareGeometryChange();
481}
482
484{
487 refreshExpressionContext();
488}
489
491{
492 const QString evaluated = currentText();
493 if ( evaluated == mText )
494 return; // no changes
495
496 setText( evaluated );
497}
498
499void QgsLayoutItemLabel::itemShiftAdjustSize( double newWidth, double newHeight, double &xShift, double &yShift ) const
500{
501 //keep alignment point constant
502 const double currentWidth = rect().width();
503 const double currentHeight = rect().height();
504 xShift = 0;
505 yShift = 0;
506
507 const double r = rotation();
508 if ( r >= 0 && r < 90 )
509 {
510 if ( mHAlignment == Qt::AlignHCenter )
511 {
512 xShift = - ( newWidth - currentWidth ) / 2.0;
513 }
514 else if ( mHAlignment == Qt::AlignRight )
515 {
516 xShift = - ( newWidth - currentWidth );
517 }
518 if ( mVAlignment == Qt::AlignVCenter )
519 {
520 yShift = -( newHeight - currentHeight ) / 2.0;
521 }
522 else if ( mVAlignment == Qt::AlignBottom )
523 {
524 yShift = - ( newHeight - currentHeight );
525 }
526 }
527 if ( r >= 90 && r < 180 )
528 {
529 if ( mHAlignment == Qt::AlignHCenter )
530 {
531 yShift = -( newHeight - currentHeight ) / 2.0;
532 }
533 else if ( mHAlignment == Qt::AlignRight )
534 {
535 yShift = -( newHeight - currentHeight );
536 }
537 if ( mVAlignment == Qt::AlignTop )
538 {
539 xShift = -( newWidth - currentWidth );
540 }
541 else if ( mVAlignment == Qt::AlignVCenter )
542 {
543 xShift = -( newWidth - currentWidth / 2.0 );
544 }
545 }
546 else if ( r >= 180 && r < 270 )
547 {
548 if ( mHAlignment == Qt::AlignHCenter )
549 {
550 xShift = -( newWidth - currentWidth ) / 2.0;
551 }
552 else if ( mHAlignment == Qt::AlignLeft )
553 {
554 xShift = -( newWidth - currentWidth );
555 }
556 if ( mVAlignment == Qt::AlignVCenter )
557 {
558 yShift = ( newHeight - currentHeight ) / 2.0;
559 }
560 else if ( mVAlignment == Qt::AlignTop )
561 {
562 yShift = ( newHeight - currentHeight );
563 }
564 }
565 else if ( r >= 270 && r < 360 )
566 {
567 if ( mHAlignment == Qt::AlignHCenter )
568 {
569 yShift = -( newHeight - currentHeight ) / 2.0;
570 }
571 else if ( mHAlignment == Qt::AlignLeft )
572 {
573 yShift = -( newHeight - currentHeight );
574 }
575 if ( mVAlignment == Qt::AlignBottom )
576 {
577 xShift = -( newWidth - currentWidth );
578 }
579 else if ( mVAlignment == Qt::AlignVCenter )
580 {
581 xShift = -( newWidth - currentWidth / 2.0 );
582 }
583 }
584}
585
586QFont QgsLayoutItemLabel::createDefaultFont() const
587{
588 QFont f = mFormat.font();
589 switch ( mFormat.sizeUnit() )
590 {
591 case Qgis::RenderUnit::Millimeters:
592 f.setPointSizeF( mFormat.size() / 0.352778 );
593 break;
594 case Qgis::RenderUnit::Pixels:
595 f.setPixelSize( mFormat.size() );
596 break;
597 case Qgis::RenderUnit::Points:
598 f.setPointSizeF( mFormat.size() );
599 break;
600 case Qgis::RenderUnit::Inches:
601 f.setPointSizeF( mFormat.size() * 72 );
602 break;
603 case Qgis::RenderUnit::Unknown:
604 case Qgis::RenderUnit::Percentage:
605 case Qgis::RenderUnit::MetersInMapUnits:
606 case Qgis::RenderUnit::MapUnits:
607 break;
608 }
609 return f;
610}
611
612double QgsLayoutItemLabel::htmlUnitsToLayoutUnits()
613{
614 if ( !mLayout )
615 {
616 return 1.0;
617 }
618
619 //TODO : fix this more precisely so that the label's default text size is the same with or without "display as html"
620 return mLayout->convertToLayoutUnits( QgsLayoutMeasurement( mLayout->renderContext().dpi() / 72.0, Qgis::LayoutUnit::Millimeters ) ); //webkit seems to assume a standard dpi of 72
621}
622
623QString QgsLayoutItemLabel::createStylesheet() const
624{
625 QString stylesheet;
626 stylesheet += QStringLiteral( "body { margin: %1 %2;" ).arg( std::max( mMarginY * mHtmlUnitsToLayoutUnits, 0.0 ) ).arg( std::max( mMarginX * mHtmlUnitsToLayoutUnits, 0.0 ) );
627
628 QFont f = createDefaultFont();
629 stylesheet += QgsFontUtils::asCSS( f, 0.352778 * mHtmlUnitsToLayoutUnits );
630
631 stylesheet += QStringLiteral( "color: rgba(%1,%2,%3,%4);" ).arg( mFormat.color().red() ).arg( mFormat.color().green() ).arg( mFormat.color().blue() ).arg( QString::number( mFormat.color().alphaF(), 'f', 4 ) );
632 stylesheet += QStringLiteral( "text-align: %1; }" ).arg( mHAlignment == Qt::AlignLeft ? QStringLiteral( "left" ) : mHAlignment == Qt::AlignRight ? QStringLiteral( "right" ) : mHAlignment == Qt::AlignHCenter ? QStringLiteral( "center" ) : QStringLiteral( "justify" ) );
633
634 return stylesheet;
635}
636
637QUrl QgsLayoutItemLabel::createStylesheetUrl() const
638{
639 QByteArray ba;
640 ba.append( createStylesheet().toUtf8() );
641 QUrl cssFileURL = QUrl( QString( "data:text/css;charset=utf-8;base64," + ba.toBase64() ) );
642
643 return cssFileURL;
644}
@ ApplyScalingWorkaroundForTextRendering
Whether a scaling workaround designed to stablise the rendering of small font sizes (or for painters ...
@ WrapLines
Automatically wrap long lines of text.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static QString replaceExpressionText(const QString &action, const QgsExpressionContext *context, const QgsDistanceArea *distanceArea=nullptr)
This function replaces each expression between [% and %] in the string with the result of its evaluat...
static QString asCSS(const QFont &font, double pointToPixelMultiplier=1.0)
Returns a CSS string representing the specified font as closely as possible.
static bool setFromXmlChildNode(QFont &font, const QDomElement &element, const QString &childNode)
Sets the properties of a font to match the properties stored in an XML child node.
A layout item subclass for text labels.
Mode mode() const
Returns the label's current mode.
void setMarginX(double margin)
Sets the horizontal margin between the edge of the frame and the label contents, in layout units.
void setFrameEnabled(bool drawFrame) override
Sets whether this item has a frame drawn around it or not.
QRectF boundingRect() const override
QSizeF sizeForText() const
Returns the required item size (in layout units) for the label's text to fill the item.
void setMargin(double margin)
Sets the margin between the edge of the frame and the label contents.
int type() const override
static QgsLayoutItemLabel * create(QgsLayout *layout)
Returns a new label item for the specified layout.
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
Q_DECL_DEPRECATED QFont font() const
Returns the label's current font.
QgsLayoutItemLabel(QgsLayout *layout)
Constructor for QgsLayoutItemLabel, with the specified parent layout.
void setText(const QString &text)
Sets the label's preset text.
void setFrameStrokeWidth(QgsLayoutMeasurement strokeWidth) override
Sets the frame stroke width.
void setMarginY(double margin)
Sets the vertical margin between the edge of the frame and the label contents, in layout units.
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
Q_DECL_DEPRECATED void setFont(const QFont &font)
Sets the label's current font.
bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
void setMode(Mode mode)
Sets the label's current mode, allowing the label to switch between font based and HTML based renderi...
QString displayName() const override
Gets item display name.
QString text() const
Returns the label's preset text.
void convertToStaticText()
Converts the label's text() to a static string, by evaluating any expressions included in the text an...
QgsTextFormat textFormat() const
Returns the text format used for drawing text in the label.
void setTextFormat(const QgsTextFormat &format)
Sets the text format used for drawing text in the label.
QString currentText() const
Returns the text as it appears on the label (with evaluated expressions and other dynamic content).
QIcon icon() const override
Returns the item's icon.
void adjustSizeToText()
Resizes the item so that the label's text fits to the item.
@ ModeHtml
Label displays rendered HTML content.
@ ModeFont
Label displays text rendered using a single font.
Layout graphical items for displaying a map.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used for rendering the map.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:44
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
Definition: qgslayoutitem.h:71
Base class for graphical items within a QgsLayout.
virtual void drawFrame(QgsRenderContext &context)
Draws the frame around the item.
virtual void setFrameStrokeWidth(QgsLayoutMeasurement width)
Sets the frame stroke width.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
virtual void setFrameEnabled(bool drawFrame)
Sets whether this item has a frame drawn around it or not.
virtual void invalidateCache()
Forces a deferred update of any cached image the item uses.
QString id() const
Returns the item's ID name.
bool frameEnabled() const
Returns true if the item includes a frame.
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
void attemptSetSceneRect(const QRectF &rect, bool includesFrame=false)
Attempts to update the item's position and size to match the passed rect in layout coordinates.
void setBackgroundEnabled(bool drawBackground)
Sets whether this item has a background drawn under it or not.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
const QgsLayout * layout() const
Returns the layout the object is attached to.
void changed()
Emitted when the object's properties change.
QPointer< QgsLayout > mLayout
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:41
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
Scoped object for saving and restoring a QPainter object's state.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:63
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:42
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setSize(double size)
Sets the size for rendered text.
void setFont(const QFont &font)
Sets the font used for rendering text.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the size of rendered text.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
Qgis::RenderUnit sizeUnit() const
Returns the units for the size of rendered text.
double size() const
Returns the size for rendered text.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
QColor color() const
Returns the color that text will be rendered in.
QFont font() const
Returns the font used for rendering text.
static Qgis::TextVerticalAlignment convertQtVAlignment(Qt::Alignment alignment)
Converts a Qt vertical alignment flag to a Qgis::TextVerticalAlignment value.
static double textWidth(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, QFontMetricsF *fontMetrics=nullptr)
Returns the width of a text based on a given format.
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Point, QFontMetricsF *fontMetrics=nullptr, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), double maxLineWidth=0)
Returns the height of a text based on a given format.
static Qgis::TextHorizontalAlignment convertQtHAlignment(Qt::Alignment alignment)
Converts a Qt horizontal alignment flag to a Qgis::TextHorizontalAlignment value.
Represents a vector layer which manages a vector based data sets.