QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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"
19
20#include <memory>
21
22#include "qgsdistancearea.h"
23#include "qgsexpression.h"
25#include "qgsfontutils.h"
26#include "qgslayout.h"
27#include "qgslayoutitemmap.h"
29#include "qgslayoutmodel.h"
32#include "qgslayoututils.h"
33#include "qgssettings.h"
34#include "qgstextformat.h"
35#include "qgstextrenderer.h"
36#include "qgsvectorlayer.h"
37
38#include <QCoreApplication>
39#include <QDate>
40#include <QDomElement>
41#include <QPainter>
42#include <QTextDocument>
43
44#include "moc_qgslayoutitemlabel.cpp"
45
48{
49 mDistanceArea = std::make_unique<QgsDistanceArea>( );
50 mHtmlUnitsToLayoutUnits = htmlUnitsToLayoutUnits();
51
52 //get default layout font from settings
53 const QgsSettings settings;
54 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
55 if ( !defaultFontString.isEmpty() )
56 {
57 QFont f = mFormat.font();
58 QgsFontUtils::setFontFamily( f, defaultFontString );
59 mFormat.setFont( f );
60 }
61
62 //default to a 10 point font size
63 mFormat.setSize( 10 );
64 mFormat.setSizeUnit( Qgis::RenderUnit::Points );
65
66 connect( this, &QgsLayoutItem::sizePositionChanged, this, [this]
67 {
68 updateBoundingRect();
69 } );
70
71 //default to no background
72 setBackgroundEnabled( false );
73
74 //a label added while atlas preview is enabled needs to have the expression context set,
75 //otherwise fields in the label aren't correctly evaluated until atlas preview feature changes (#9457)
76 refreshExpressionContext();
77}
78
83
88
90{
91 return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemLabel.svg" ) );
92}
93
95{
96 QPainter *painter = context.renderContext().painter();
97 const QgsScopedQPainterState painterState( painter );
98
99 const double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0;
100 const double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth;
101 const double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
102
103 QRectF painterRect;
104 if ( mMode == QgsLayoutItemLabel::ModeFont )
105 {
106 const double rectScale = context.renderContext().scaleFactor();
107 painterRect = QRectF( ( xPenAdjust + mMarginX ) * rectScale,
108 ( yPenAdjust + mMarginY ) * rectScale,
109 ( rect().width() - 2 * xPenAdjust - 2 * mMarginX ) * rectScale,
110 ( rect().height() - 2 * yPenAdjust - 2 * mMarginY ) * rectScale );
111 }
112 else
113 {
114 // The adjustment value was found through trial and error, the author has however no clue as to where it comes from
115#if QT_VERSION >= QT_VERSION_CHECK( 6, 7, 0 )
116 constexpr double adjustmentFactor = 4.18;
117#else
118 constexpr double adjustmentFactor = 3.77;
119#endif
120 const double rectScale = context.renderContext().scaleFactor() * adjustmentFactor;
121 // The left/right margin is handled by the stylesheet while the top/bottom margin is ignored by QTextDocument
122 painterRect = QRectF( 0, 0,
123 ( rect().width() ) * rectScale,
124 ( rect().height() - yPenAdjust - mMarginY ) * rectScale );
125 painter->translate( 0, ( yPenAdjust + mMarginY ) * context.renderContext().scaleFactor() );
126 painter->scale( context.renderContext().scaleFactor() / adjustmentFactor, context.renderContext().scaleFactor() / adjustmentFactor );
127 }
128
129 switch ( mMode )
130 {
131 case ModeHtml:
132 {
133 QTextDocument document;
134 document.setDocumentMargin( 0 );
135 document.setPageSize( QSizeF( painterRect.width() / context.renderContext().scaleFactor(), painterRect.height() / context.renderContext().scaleFactor() ) );
136 document.setDefaultStyleSheet( createStylesheet() );
137
138 document.setDefaultFont( createDefaultFont() );
139
140 QTextOption textOption = document.defaultTextOption();
141 textOption.setAlignment( mHAlignment );
142 document.setDefaultTextOption( textOption );
143
144 document.setHtml( QStringLiteral( "<body>%1</body>" ).arg( currentText() ) );
145 document.drawContents( painter, painterRect );
146 break;
147 }
148
149 case ModeFont:
150 {
152 QgsTextRenderer::drawText( painterRect, 0,
154 currentText().split( '\n' ),
155 context.renderContext(),
156 mFormat,
157 true,
160 break;
161 }
162 }
163}
164
165void QgsLayoutItemLabel::contentChanged()
166{
167 switch ( mMode )
168 {
169 case ModeHtml:
170 {
172 break;
173 }
174 case ModeFont:
176 break;
177 }
178}
179
180void QgsLayoutItemLabel::setText( const QString &text )
181{
182 mText = text;
183 emit changed();
184
185 contentChanged();
186
187 if ( mLayout && id().isEmpty() && mMode != ModeHtml )
188 {
189 //notify the model that the display name has changed
190 mLayout->itemsModel()->updateItemDisplayName( this );
191 }
192}
193
195{
196 if ( mode == mMode )
197 {
198 return;
199 }
200
201 mMode = mode;
202 contentChanged();
203
204 if ( mLayout && id().isEmpty() )
205 {
206 //notify the model that the display name has changed
207 mLayout->itemsModel()->updateItemDisplayName( this );
208 }
209}
210
211void QgsLayoutItemLabel::refreshExpressionContext()
212{
213 if ( !mLayout )
214 return;
215
216 QgsVectorLayer *layer = mLayout->reportContext().layer();
217 //setup distance area conversion
218 if ( layer )
219 {
220 mDistanceArea->setSourceCrs( layer->crs(), mLayout->project()->transformContext() );
221 }
222 else
223 {
224 //set to composition's reference map's crs
225 QgsLayoutItemMap *referenceMap = mLayout->referenceMap();
226 if ( referenceMap )
227 mDistanceArea->setSourceCrs( referenceMap->crs(), mLayout->project()->transformContext() );
228 }
229 mDistanceArea->setEllipsoid( mLayout->project()->ellipsoid() );
230 contentChanged();
231
232 update();
233}
234
235void QgsLayoutItemLabel::updateBoundingRect()
236{
237 QRectF rectangle = rect();
238 const double frameExtension = frameEnabled() ? pen().widthF() / 2.0 : 0.0;
239 if ( frameExtension > 0 )
240 rectangle.adjust( -frameExtension, -frameExtension, frameExtension, frameExtension );
241
242 if ( mMarginX < 0 )
243 {
244 rectangle.adjust( mMarginX, 0, -mMarginX, 0 );
245 }
246 if ( mMarginY < 0 )
247 {
248 rectangle.adjust( 0, mMarginY, 0, -mMarginY );
249 }
250
251 if ( rectangle != mCurrentRectangle )
252 {
253 prepareGeometryChange();
254 mCurrentRectangle = rectangle;
255 }
257}
258
260{
261 QString displayText = mText;
262 replaceDateText( displayText );
263
265
266 return QgsExpression::replaceExpressionText( displayText, &context, mDistanceArea.get() );
267}
268
269void QgsLayoutItemLabel::replaceDateText( QString &text ) const
270{
271 const QString constant = QStringLiteral( "$CURRENT_DATE" );
272 const int currentDatePos = text.indexOf( constant );
273 if ( currentDatePos != -1 )
274 {
275 //check if there is a bracket just after $CURRENT_DATE
276 QString formatText;
277 const int openingBracketPos = text.indexOf( '(', currentDatePos );
278 const int closingBracketPos = text.indexOf( ')', openingBracketPos + 1 );
279 if ( openingBracketPos != -1 &&
280 closingBracketPos != -1 &&
281 ( closingBracketPos - openingBracketPos ) > 1 &&
282 openingBracketPos == currentDatePos + constant.size() )
283 {
284 formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
285 text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
286 }
287 else //no bracket
288 {
289 text.replace( QLatin1String( "$CURRENT_DATE" ), QDate::currentDate().toString() );
290 }
291 }
292}
293
294void QgsLayoutItemLabel::setFont( const QFont &f )
295{
296 mFormat.setFont( f );
297 if ( f.pointSizeF() > 0 )
298 mFormat.setSize( f.pointSizeF() );
300}
301
303{
304 return mFormat;
305}
306
308{
309 mFormat = format;
311}
312
313void QgsLayoutItemLabel::setMargin( const double m )
314{
315 mMarginX = m;
316 mMarginY = m;
317 updateBoundingRect();
318}
319
320void QgsLayoutItemLabel::setMarginX( const double margin )
321{
322 mMarginX = margin;
323 updateBoundingRect();
324}
325
326void QgsLayoutItemLabel::setMarginY( const double margin )
327{
328 mMarginY = margin;
329 updateBoundingRect();
330}
331
333{
334 const QSizeF newSize = sizeForText();
335
336 //keep alignment point constant
337 double xShift = 0;
338 double yShift = 0;
339
340 itemShiftAdjustSize( newSize.width(), newSize.height(), xShift, yShift );
341
342 //update rect for data defined size and position
343 attemptSetSceneRect( QRectF( pos().x() + xShift, pos().y() + yShift, newSize.width(), newSize.height() ) );
344}
345
347{
348 const QSizeF newSize = sizeForText();
349 const double newWidth = newSize.width();
350 const double newHeight = newSize.height();
351 const double currentWidth = rect().width();
352 const double currentHeight = rect().height();
353
354 //keep reference point constant
355 double xShift = 0;
356 double yShift = 0;
357 switch ( referencePoint )
358 {
360 xShift = 0;
361 yShift = 0;
362 break;
364 xShift = - ( newWidth - currentWidth ) / 2.0;
365 yShift = 0;
366 break;
367
369 xShift = - ( newWidth - currentWidth );
370 yShift = 0;
371 break;
372
374 xShift = 0;
375 yShift = -( newHeight - currentHeight ) / 2.0;
376 break;
377
379 xShift = - ( newWidth - currentWidth ) / 2.0;
380 yShift = -( newHeight - currentHeight ) / 2.0;
381 break;
382
384 xShift = - ( newWidth - currentWidth );
385 yShift = -( newHeight - currentHeight ) / 2.0;
386 break;
387
389 xShift = 0;
390 yShift = - ( newHeight - currentHeight );
391 break;
392
394 xShift = - ( newWidth - currentWidth ) / 2.0;
395 yShift = - ( newHeight - currentHeight );
396 break;
397
399 xShift = - ( newWidth - currentWidth );
400 yShift = - ( newHeight - currentHeight );
401 break;
402 }
403
404 //update rect for data defined size and position
405 attemptSetSceneRect( QRectF( pos().x() + xShift, pos().y() + yShift, newSize.width(), newSize.height() ) );
406}
407
409{
412
413 const QStringList lines = currentText().split( '\n' );
414 const double textWidth = std::ceil( QgsTextRenderer::textWidth( context, mFormat, lines ) + 1 ) / context.convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
415 const double fontHeight = std::ceil( QgsTextRenderer::textHeight( context, mFormat, lines ) + 1 ) / context.convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
416
417 const double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0;
418
419 const double width = textWidth + 2 * mMarginX + 2 * penWidth;
420 const double height = fontHeight + 2 * mMarginY + 2 * penWidth;
421
422 return mLayout->convertToLayoutUnits( QgsLayoutSize( width, height, Qgis::LayoutUnit::Millimeters ) );
423}
424
426{
427 return mFormat.font();
428}
429
430bool QgsLayoutItemLabel::writePropertiesToElement( QDomElement &layoutLabelElem, QDomDocument &doc, const QgsReadWriteContext &rwContext ) const
431{
432 layoutLabelElem.setAttribute( QStringLiteral( "htmlState" ), static_cast< int >( mMode ) );
433
434 layoutLabelElem.setAttribute( QStringLiteral( "labelText" ), mText );
435 layoutLabelElem.setAttribute( QStringLiteral( "marginX" ), QString::number( mMarginX ) );
436 layoutLabelElem.setAttribute( QStringLiteral( "marginY" ), QString::number( mMarginY ) );
437 layoutLabelElem.setAttribute( QStringLiteral( "halign" ), mHAlignment );
438 layoutLabelElem.setAttribute( QStringLiteral( "valign" ), mVAlignment );
439
440 QDomElement textElem = mFormat.writeXml( doc, rwContext );
441 layoutLabelElem.appendChild( textElem );
442
443 return true;
444}
445
446bool QgsLayoutItemLabel::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext &context )
447{
448 //restore label specific properties
449
450 //text
451 mText = itemElem.attribute( QStringLiteral( "labelText" ) );
452
453 //html state
454 mMode = static_cast< Mode >( itemElem.attribute( QStringLiteral( "htmlState" ) ).toInt() );
455
456 //margin
457 bool marginXOk = false;
458 bool marginYOk = false;
459 mMarginX = itemElem.attribute( QStringLiteral( "marginX" ) ).toDouble( &marginXOk );
460 mMarginY = itemElem.attribute( QStringLiteral( "marginY" ) ).toDouble( &marginYOk );
461 if ( !marginXOk || !marginYOk )
462 {
463 //upgrade old projects where margins where stored in a single attribute
464 const double margin = itemElem.attribute( QStringLiteral( "margin" ), QStringLiteral( "1.0" ) ).toDouble();
465 mMarginX = margin;
466 mMarginY = margin;
467 }
468
469 //Horizontal alignment
470 mHAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "halign" ) ).toInt() );
471
472 //Vertical alignment
473 mVAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "valign" ) ).toInt() );
474
475 //font
476 QDomNodeList textFormatNodeList = itemElem.elementsByTagName( QStringLiteral( "text-style" ) );
477 if ( !textFormatNodeList.isEmpty() )
478 {
479 QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
480 mFormat.readXml( textFormatElem, context );
481 }
482 else
483 {
484 QFont f;
485 if ( !QgsFontUtils::setFromXmlChildNode( f, itemElem, QStringLiteral( "LabelFont" ) ) )
486 {
487 f.fromString( itemElem.attribute( QStringLiteral( "font" ), QString() ) );
488 }
489 mFormat.setFont( f );
490 if ( f.pointSizeF() > 0 )
491 {
492 mFormat.setSize( f.pointSizeF() );
493 mFormat.setSizeUnit( Qgis::RenderUnit::Points );
494 }
495 else if ( f.pixelSize() > 0 )
496 {
497 mFormat.setSize( f.pixelSize() );
498 mFormat.setSizeUnit( Qgis::RenderUnit::Pixels );
499 }
500
501 //font color
502 const QDomNodeList fontColorList = itemElem.elementsByTagName( QStringLiteral( "FontColor" ) );
503 if ( !fontColorList.isEmpty() )
504 {
505 const QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
506 const int red = fontColorElem.attribute( QStringLiteral( "red" ), QStringLiteral( "0" ) ).toInt();
507 const int green = fontColorElem.attribute( QStringLiteral( "green" ), QStringLiteral( "0" ) ).toInt();
508 const int blue = fontColorElem.attribute( QStringLiteral( "blue" ), QStringLiteral( "0" ) ).toInt();
509 const int alpha = fontColorElem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "255" ) ).toInt();
510 mFormat.setColor( QColor( red, green, blue, alpha ) );
511 }
512 else if ( textFormatNodeList.isEmpty() )
513 {
514 mFormat.setColor( QColor( 0, 0, 0 ) );
515 }
516 }
517
518 updateBoundingRect();
519
520 return true;
521}
522
524{
525 if ( !id().isEmpty() )
526 {
527 return id();
528 }
529
530 switch ( mMode )
531 {
532 case ModeHtml:
533 return tr( "<HTML Label>" );
534
535 case ModeFont:
536 {
537
538 //if no id, default to portion of label text
539 const QString text = mText;
540 if ( text.isEmpty() )
541 {
542 return tr( "<Label>" );
543 }
544 if ( text.length() > 25 )
545 {
546 return QString( tr( "%1…" ) ).arg( text.left( 25 ).simplified() );
547 }
548 else
549 {
550 return text.simplified();
551 }
552 }
553 }
554 return QString(); // no warnings
555}
556
558{
559 return mCurrentRectangle;
560}
561
563{
565 updateBoundingRect();
566}
567
569{
571 updateBoundingRect();
572}
573
575{
578 refreshExpressionContext();
579}
580
582{
583 const QString evaluated = currentText();
584 if ( evaluated == mText )
585 return; // no changes
586
587 setText( evaluated );
588}
589
590void QgsLayoutItemLabel::itemShiftAdjustSize( double newWidth, double newHeight, double &xShift, double &yShift ) const
591{
592 //keep alignment point constant
593 const double currentWidth = rect().width();
594 const double currentHeight = rect().height();
595 xShift = 0;
596 yShift = 0;
597
598 const double r = rotation();
599 if ( r >= 0 && r < 90 )
600 {
601 if ( mHAlignment == Qt::AlignHCenter )
602 {
603 xShift = - ( newWidth - currentWidth ) / 2.0;
604 }
605 else if ( mHAlignment == Qt::AlignRight )
606 {
607 xShift = - ( newWidth - currentWidth );
608 }
609 if ( mVAlignment == Qt::AlignVCenter )
610 {
611 yShift = -( newHeight - currentHeight ) / 2.0;
612 }
613 else if ( mVAlignment == Qt::AlignBottom )
614 {
615 yShift = - ( newHeight - currentHeight );
616 }
617 }
618 if ( r >= 90 && r < 180 )
619 {
620 if ( mHAlignment == Qt::AlignHCenter )
621 {
622 yShift = -( newHeight - currentHeight ) / 2.0;
623 }
624 else if ( mHAlignment == Qt::AlignRight )
625 {
626 yShift = -( newHeight - currentHeight );
627 }
628 if ( mVAlignment == Qt::AlignTop )
629 {
630 xShift = -( newWidth - currentWidth );
631 }
632 else if ( mVAlignment == Qt::AlignVCenter )
633 {
634 xShift = -( newWidth - currentWidth / 2.0 );
635 }
636 }
637 else if ( r >= 180 && r < 270 )
638 {
639 if ( mHAlignment == Qt::AlignHCenter )
640 {
641 xShift = -( newWidth - currentWidth ) / 2.0;
642 }
643 else if ( mHAlignment == Qt::AlignLeft )
644 {
645 xShift = -( newWidth - currentWidth );
646 }
647 if ( mVAlignment == Qt::AlignVCenter )
648 {
649 yShift = ( newHeight - currentHeight ) / 2.0;
650 }
651 else if ( mVAlignment == Qt::AlignTop )
652 {
653 yShift = ( newHeight - currentHeight );
654 }
655 }
656 else if ( r >= 270 && r < 360 )
657 {
658 if ( mHAlignment == Qt::AlignHCenter )
659 {
660 yShift = -( newHeight - currentHeight ) / 2.0;
661 }
662 else if ( mHAlignment == Qt::AlignLeft )
663 {
664 yShift = -( newHeight - currentHeight );
665 }
666 if ( mVAlignment == Qt::AlignBottom )
667 {
668 xShift = -( newWidth - currentWidth );
669 }
670 else if ( mVAlignment == Qt::AlignVCenter )
671 {
672 xShift = -( newWidth - currentWidth / 2.0 );
673 }
674 }
675}
676
677QFont QgsLayoutItemLabel::createDefaultFont() const
678{
679 QFont f = mFormat.font();
680 switch ( mFormat.sizeUnit() )
681 {
683 f.setPointSizeF( mFormat.size() / 0.352778 );
684 break;
686 f.setPixelSize( mFormat.size() );
687 break;
689 f.setPointSizeF( mFormat.size() );
690 break;
692 f.setPointSizeF( mFormat.size() * 72 );
693 break;
698 break;
699 }
700 return f;
701}
702
703double QgsLayoutItemLabel::htmlUnitsToLayoutUnits()
704{
705 if ( !mLayout )
706 {
707 return 1.0;
708 }
709
710 //TODO : fix this more precisely so that the label's default text size is the same with or without "display as html"
711 return mLayout->convertToLayoutUnits( QgsLayoutMeasurement( mLayout->renderContext().dpi() / 72.0, Qgis::LayoutUnit::Millimeters ) ); //webkit seems to assume a standard dpi of 72
712}
713
714QString QgsLayoutItemLabel::createStylesheet() const
715{
716 QString stylesheet;
717
718 stylesheet += QStringLiteral( "body { margin: %1 %2;" ).arg( std::max( mMarginY * mHtmlUnitsToLayoutUnits, 0.0 ) ).arg( std::max( mMarginX * mHtmlUnitsToLayoutUnits, 0.0 ) );
719 stylesheet += mFormat.asCSS( 0.352778 * mHtmlUnitsToLayoutUnits );
720 stylesheet += QStringLiteral( "text-align: %1; }" ).arg( mHAlignment == Qt::AlignLeft ? QStringLiteral( "left" ) : mHAlignment == Qt::AlignRight ? QStringLiteral( "right" ) : mHAlignment == Qt::AlignHCenter ? QStringLiteral( "center" ) : QStringLiteral( "justify" ) );
721
722 return stylesheet;
723}
724
725QUrl QgsLayoutItemLabel::createStylesheetUrl() const
726{
727 QByteArray ba;
728 ba.append( createStylesheet().toUtf8() );
729 QUrl cssFileURL = QUrl( QString( "data:text/css;charset=utf-8;base64," + ba.toBase64() ) );
730
731 return cssFileURL;
732}
@ Millimeters
Millimeters.
Definition qgis.h:5204
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size).
Definition qgis.h:5187
@ Millimeters
Millimeters.
Definition qgis.h:5184
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5188
@ Unknown
Mixed or unknown units.
Definition qgis.h:5190
@ MapUnits
Map units.
Definition qgis.h:5185
@ Pixels
Pixels.
Definition qgis.h:5186
@ Inches
Inches.
Definition qgis.h:5189
@ MetersInMapUnits
Meters value as Map units.
Definition qgis.h:5191
@ ApplyScalingWorkaroundForTextRendering
Whether a scaling workaround designed to stablise the rendering of small font sizes (or for painters ...
Definition qgis.h:2762
@ WrapLines
Automatically wrap long lines of text.
Definition qgis.h:3408
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
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 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.
static void setFontFamily(QFont &font, const QString &family)
Sets the family for a font object.
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.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used for rendering the map.
Contains settings and helpers relating to a render of a QgsLayoutItem.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
virtual void drawFrame(QgsRenderContext &context)
Draws the frame around the item.
friend class QgsLayout
virtual void setFrameStrokeWidth(QgsLayoutMeasurement width)
Sets the frame stroke width.
QgsLayoutItem(QgsLayout *layout, bool manageZValue=true)
Constructor for QgsLayoutItem, with the specified parent layout.
ReferencePoint referencePoint() const
Returns the reference point for positioning of the layout item.
ReferencePoint
Fixed position reference point.
@ LowerMiddle
Lower center of item.
@ MiddleLeft
Middle left of item.
@ Middle
Center of item.
@ UpperRight
Upper right corner of item.
@ LowerLeft
Lower left corner of item.
@ UpperLeft
Upper left corner of item.
@ UpperMiddle
Upper center of item.
@ MiddleRight
Middle right of item.
@ LowerRight
Lower right corner of item.
friend class QgsLayoutItemMap
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.
void sizePositionChanged()
Emitted when the item's size or position changes.
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.
Provides a method of storing measurements for use in QGIS layouts using a variety of different measur...
const QgsLayout * layout() const
Returns the layout the object is attached to.
void changed()
Emitted when the object's properties change.
QPointer< QgsLayout > mLayout
Provides a method of storing sizes, consisting of a width and height, for use in QGIS layouts.
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:87
A container for the context for various read/write operations on 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.
Stores settings for use within QGIS.
Definition qgssettings.h:65
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.
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 dataset.