QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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"
23#include "qgsvectorlayer.h"
24#include "qgsdistancearea.h"
25#include "qgsfontutils.h"
26#include "qgstextformat.h"
27#include "qgstextrenderer.h"
29#include "qgslayoutitemmap.h"
30#include "qgssettings.h"
31#include "qgslayout.h"
34
35#include <QCoreApplication>
36#include <QDate>
37#include <QDomElement>
38#include <QPainter>
39#include <QTextDocument>
40
42 : QgsLayoutItem( layout )
43{
44 mDistanceArea.reset( new QgsDistanceArea() );
45 mHtmlUnitsToLayoutUnits = htmlUnitsToLayoutUnits();
46
47 //get default layout font from settings
48 const QgsSettings settings;
49 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
50 if ( !defaultFontString.isEmpty() )
51 {
52 QFont f = mFormat.font();
53 QgsFontUtils::setFontFamily( f, defaultFontString );
54 mFormat.setFont( f );
55 }
56
57 //default to a 10 point font size
58 mFormat.setSize( 10 );
60
61 connect( this, &QgsLayoutItem::sizePositionChanged, this, [this]
62 {
63 updateBoundingRect();
64 } );
65
66 //default to no background
67 setBackgroundEnabled( false );
68
69 //a label added while atlas preview is enabled needs to have the expression context set,
70 //otherwise fields in the label aren't correctly evaluated until atlas preview feature changes (#9457)
71 refreshExpressionContext();
72}
73
75{
76 return new QgsLayoutItemLabel( layout );
77}
78
80{
82}
83
85{
86 return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemLabel.svg" ) );
87}
88
90{
91 QPainter *painter = context.renderContext().painter();
92 const QgsScopedQPainterState painterState( painter );
93
94 const double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0;
95 const double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth;
96 const double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
97
98 QRectF painterRect;
99 if ( mMode == QgsLayoutItemLabel::ModeFont )
100 {
101 const double rectScale = context.renderContext().scaleFactor();
102 painterRect = QRectF( ( xPenAdjust + mMarginX ) * rectScale,
103 ( yPenAdjust + mMarginY ) * rectScale,
104 ( rect().width() - 2 * xPenAdjust - 2 * mMarginX ) * rectScale,
105 ( rect().height() - 2 * yPenAdjust - 2 * mMarginY ) * rectScale );
106 }
107 else
108 {
109 // The 3.77 adjustment value was found through trial and error, the author has however no clue as to where it comes from
110 const double adjustmentFactor = 3.77;
111 const double rectScale = context.renderContext().scaleFactor() * adjustmentFactor;
112 // The left/right margin is handled by the stylesheet while the top/bottom margin is ignored by QTextDocument
113 painterRect = QRectF( 0, 0,
114 ( rect().width() ) * rectScale,
115 ( rect().height() - yPenAdjust - mMarginY ) * rectScale );
116 painter->translate( 0, ( yPenAdjust + mMarginY ) * context.renderContext().scaleFactor() );
117 painter->scale( context.renderContext().scaleFactor() / adjustmentFactor, context.renderContext().scaleFactor() / adjustmentFactor );
118 }
119
120 switch ( mMode )
121 {
122 case ModeHtml:
123 {
124 QTextDocument document;
125 document.setDocumentMargin( 0 );
126 document.setPageSize( QSizeF( painterRect.width() / context.renderContext().scaleFactor(), painterRect.height() / context.renderContext().scaleFactor() ) );
127 document.setDefaultStyleSheet( createStylesheet() );
128
129 document.setDefaultFont( createDefaultFont() );
130
131 QTextOption textOption = document.defaultTextOption();
132 textOption.setAlignment( mHAlignment );
133 document.setDefaultTextOption( textOption );
134
135 document.setHtml( QStringLiteral( "<body>%1</body>" ).arg( currentText() ) );
136 document.drawContents( painter, painterRect );
137 break;
138 }
139
140 case ModeFont:
141 {
143 QgsTextRenderer::drawText( painterRect, 0,
145 currentText().split( '\n' ),
146 context.renderContext(),
147 mFormat,
148 true,
151 break;
152 }
153 }
154}
155
156void QgsLayoutItemLabel::contentChanged()
157{
158 switch ( mMode )
159 {
160 case ModeHtml:
161 {
163 break;
164 }
165 case ModeFont:
167 break;
168 }
169}
170
171void QgsLayoutItemLabel::setText( const QString &text )
172{
173 mText = text;
174 emit changed();
175
176 contentChanged();
177
178 if ( mLayout && id().isEmpty() && mMode != ModeHtml )
179 {
180 //notify the model that the display name has changed
181 mLayout->itemsModel()->updateItemDisplayName( this );
182 }
183}
184
186{
187 if ( mode == mMode )
188 {
189 return;
190 }
191
192 mMode = mode;
193 contentChanged();
194
195 if ( mLayout && id().isEmpty() )
196 {
197 //notify the model that the display name has changed
198 mLayout->itemsModel()->updateItemDisplayName( this );
199 }
200}
201
202void QgsLayoutItemLabel::refreshExpressionContext()
203{
204 if ( !mLayout )
205 return;
206
207 QgsVectorLayer *layer = mLayout->reportContext().layer();
208 //setup distance area conversion
209 if ( layer )
210 {
211 mDistanceArea->setSourceCrs( layer->crs(), mLayout->project()->transformContext() );
212 }
213 else
214 {
215 //set to composition's reference map's crs
216 QgsLayoutItemMap *referenceMap = mLayout->referenceMap();
217 if ( referenceMap )
218 mDistanceArea->setSourceCrs( referenceMap->crs(), mLayout->project()->transformContext() );
219 }
220 mDistanceArea->setEllipsoid( mLayout->project()->ellipsoid() );
221 contentChanged();
222
223 update();
224}
225
226void QgsLayoutItemLabel::updateBoundingRect()
227{
228 QRectF rectangle = rect();
229 const double frameExtension = frameEnabled() ? pen().widthF() / 2.0 : 0.0;
230 if ( frameExtension > 0 )
231 rectangle.adjust( -frameExtension, -frameExtension, frameExtension, frameExtension );
232
233 if ( mMarginX < 0 )
234 {
235 rectangle.adjust( mMarginX, 0, -mMarginX, 0 );
236 }
237 if ( mMarginY < 0 )
238 {
239 rectangle.adjust( 0, mMarginY, 0, -mMarginY );
240 }
241
242 if ( rectangle != mCurrentRectangle )
243 {
244 prepareGeometryChange();
245 mCurrentRectangle = rectangle;
246 }
248}
249
251{
252 QString displayText = mText;
253 replaceDateText( displayText );
254
256
257 return QgsExpression::replaceExpressionText( displayText, &context, mDistanceArea.get() );
258}
259
260void QgsLayoutItemLabel::replaceDateText( QString &text ) const
261{
262 const QString constant = QStringLiteral( "$CURRENT_DATE" );
263 const int currentDatePos = text.indexOf( constant );
264 if ( currentDatePos != -1 )
265 {
266 //check if there is a bracket just after $CURRENT_DATE
267 QString formatText;
268 const int openingBracketPos = text.indexOf( '(', currentDatePos );
269 const int closingBracketPos = text.indexOf( ')', openingBracketPos + 1 );
270 if ( openingBracketPos != -1 &&
271 closingBracketPos != -1 &&
272 ( closingBracketPos - openingBracketPos ) > 1 &&
273 openingBracketPos == currentDatePos + constant.size() )
274 {
275 formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
276 text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
277 }
278 else //no bracket
279 {
280 text.replace( QLatin1String( "$CURRENT_DATE" ), QDate::currentDate().toString() );
281 }
282 }
283}
284
285void QgsLayoutItemLabel::setFont( const QFont &f )
286{
287 mFormat.setFont( f );
288 if ( f.pointSizeF() > 0 )
289 mFormat.setSize( f.pointSizeF() );
291}
292
294{
295 return mFormat;
296}
297
299{
300 mFormat = format;
302}
303
304void QgsLayoutItemLabel::setMargin( const double m )
305{
306 mMarginX = m;
307 mMarginY = m;
308 updateBoundingRect();
309}
310
311void QgsLayoutItemLabel::setMarginX( const double margin )
312{
313 mMarginX = margin;
314 updateBoundingRect();
315}
316
317void QgsLayoutItemLabel::setMarginY( const double margin )
318{
319 mMarginY = margin;
320 updateBoundingRect();
321}
322
324{
325 const QSizeF newSize = sizeForText();
326
327 //keep alignment point constant
328 double xShift = 0;
329 double yShift = 0;
330
331 itemShiftAdjustSize( newSize.width(), newSize.height(), xShift, yShift );
332
333 //update rect for data defined size and position
334 attemptSetSceneRect( QRectF( pos().x() + xShift, pos().y() + yShift, newSize.width(), newSize.height() ) );
335}
336
338{
340
341 const QStringList lines = currentText().split( '\n' );
342 const double textWidth = QgsTextRenderer::textWidth( context, mFormat, lines ) / context.convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
343 const double fontHeight = QgsTextRenderer::textHeight( context, mFormat, lines ) / context.convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
344
345 const double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0;
346
347 const double width = textWidth + 2 * mMarginX + 2 * penWidth;
348 const double height = fontHeight + 2 * mMarginY + 2 * penWidth;
349
350 return mLayout->convertToLayoutUnits( QgsLayoutSize( width, height, Qgis::LayoutUnit::Millimeters ) );
351}
352
354{
355 return mFormat.font();
356}
357
358bool QgsLayoutItemLabel::writePropertiesToElement( QDomElement &layoutLabelElem, QDomDocument &doc, const QgsReadWriteContext &rwContext ) const
359{
360 layoutLabelElem.setAttribute( QStringLiteral( "htmlState" ), static_cast< int >( mMode ) );
361
362 layoutLabelElem.setAttribute( QStringLiteral( "labelText" ), mText );
363 layoutLabelElem.setAttribute( QStringLiteral( "marginX" ), QString::number( mMarginX ) );
364 layoutLabelElem.setAttribute( QStringLiteral( "marginY" ), QString::number( mMarginY ) );
365 layoutLabelElem.setAttribute( QStringLiteral( "halign" ), mHAlignment );
366 layoutLabelElem.setAttribute( QStringLiteral( "valign" ), mVAlignment );
367
368 QDomElement textElem = mFormat.writeXml( doc, rwContext );
369 layoutLabelElem.appendChild( textElem );
370
371 return true;
372}
373
374bool QgsLayoutItemLabel::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext &context )
375{
376 //restore label specific properties
377
378 //text
379 mText = itemElem.attribute( QStringLiteral( "labelText" ) );
380
381 //html state
382 mMode = static_cast< Mode >( itemElem.attribute( QStringLiteral( "htmlState" ) ).toInt() );
383
384 //margin
385 bool marginXOk = false;
386 bool marginYOk = false;
387 mMarginX = itemElem.attribute( QStringLiteral( "marginX" ) ).toDouble( &marginXOk );
388 mMarginY = itemElem.attribute( QStringLiteral( "marginY" ) ).toDouble( &marginYOk );
389 if ( !marginXOk || !marginYOk )
390 {
391 //upgrade old projects where margins where stored in a single attribute
392 const double margin = itemElem.attribute( QStringLiteral( "margin" ), QStringLiteral( "1.0" ) ).toDouble();
393 mMarginX = margin;
394 mMarginY = margin;
395 }
396
397 //Horizontal alignment
398 mHAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "halign" ) ).toInt() );
399
400 //Vertical alignment
401 mVAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "valign" ) ).toInt() );
402
403 //font
404 QDomNodeList textFormatNodeList = itemElem.elementsByTagName( QStringLiteral( "text-style" ) );
405 if ( !textFormatNodeList.isEmpty() )
406 {
407 QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
408 mFormat.readXml( textFormatElem, context );
409 }
410 else
411 {
412 QFont f;
413 if ( !QgsFontUtils::setFromXmlChildNode( f, itemElem, QStringLiteral( "LabelFont" ) ) )
414 {
415 f.fromString( itemElem.attribute( QStringLiteral( "font" ), QString() ) );
416 }
417 mFormat.setFont( f );
418 if ( f.pointSizeF() > 0 )
419 {
420 mFormat.setSize( f.pointSizeF() );
422 }
423 else if ( f.pixelSize() > 0 )
424 {
425 mFormat.setSize( f.pixelSize() );
427 }
428
429 //font color
430 const QDomNodeList fontColorList = itemElem.elementsByTagName( QStringLiteral( "FontColor" ) );
431 if ( !fontColorList.isEmpty() )
432 {
433 const QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
434 const int red = fontColorElem.attribute( QStringLiteral( "red" ), QStringLiteral( "0" ) ).toInt();
435 const int green = fontColorElem.attribute( QStringLiteral( "green" ), QStringLiteral( "0" ) ).toInt();
436 const int blue = fontColorElem.attribute( QStringLiteral( "blue" ), QStringLiteral( "0" ) ).toInt();
437 const int alpha = fontColorElem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "255" ) ).toInt();
438 mFormat.setColor( QColor( red, green, blue, alpha ) );
439 }
440 else if ( textFormatNodeList.isEmpty() )
441 {
442 mFormat.setColor( QColor( 0, 0, 0 ) );
443 }
444 }
445
446 updateBoundingRect();
447
448 return true;
449}
450
452{
453 if ( !id().isEmpty() )
454 {
455 return id();
456 }
457
458 switch ( mMode )
459 {
460 case ModeHtml:
461 return tr( "<HTML Label>" );
462
463 case ModeFont:
464 {
465
466 //if no id, default to portion of label text
467 const QString text = mText;
468 if ( text.isEmpty() )
469 {
470 return tr( "<Label>" );
471 }
472 if ( text.length() > 25 )
473 {
474 return QString( tr( "%1…" ) ).arg( text.left( 25 ).simplified() );
475 }
476 else
477 {
478 return text.simplified();
479 }
480 }
481 }
482 return QString(); // no warnings
483}
484
486{
487 return mCurrentRectangle;
488}
489
491{
493 updateBoundingRect();
494}
495
497{
499 updateBoundingRect();
500}
501
503{
506 refreshExpressionContext();
507}
508
510{
511 const QString evaluated = currentText();
512 if ( evaluated == mText )
513 return; // no changes
514
515 setText( evaluated );
516}
517
518void QgsLayoutItemLabel::itemShiftAdjustSize( double newWidth, double newHeight, double &xShift, double &yShift ) const
519{
520 //keep alignment point constant
521 const double currentWidth = rect().width();
522 const double currentHeight = rect().height();
523 xShift = 0;
524 yShift = 0;
525
526 const double r = rotation();
527 if ( r >= 0 && r < 90 )
528 {
529 if ( mHAlignment == Qt::AlignHCenter )
530 {
531 xShift = - ( newWidth - currentWidth ) / 2.0;
532 }
533 else if ( mHAlignment == Qt::AlignRight )
534 {
535 xShift = - ( newWidth - currentWidth );
536 }
537 if ( mVAlignment == Qt::AlignVCenter )
538 {
539 yShift = -( newHeight - currentHeight ) / 2.0;
540 }
541 else if ( mVAlignment == Qt::AlignBottom )
542 {
543 yShift = - ( newHeight - currentHeight );
544 }
545 }
546 if ( r >= 90 && r < 180 )
547 {
548 if ( mHAlignment == Qt::AlignHCenter )
549 {
550 yShift = -( newHeight - currentHeight ) / 2.0;
551 }
552 else if ( mHAlignment == Qt::AlignRight )
553 {
554 yShift = -( newHeight - currentHeight );
555 }
556 if ( mVAlignment == Qt::AlignTop )
557 {
558 xShift = -( newWidth - currentWidth );
559 }
560 else if ( mVAlignment == Qt::AlignVCenter )
561 {
562 xShift = -( newWidth - currentWidth / 2.0 );
563 }
564 }
565 else if ( r >= 180 && r < 270 )
566 {
567 if ( mHAlignment == Qt::AlignHCenter )
568 {
569 xShift = -( newWidth - currentWidth ) / 2.0;
570 }
571 else if ( mHAlignment == Qt::AlignLeft )
572 {
573 xShift = -( newWidth - currentWidth );
574 }
575 if ( mVAlignment == Qt::AlignVCenter )
576 {
577 yShift = ( newHeight - currentHeight ) / 2.0;
578 }
579 else if ( mVAlignment == Qt::AlignTop )
580 {
581 yShift = ( newHeight - currentHeight );
582 }
583 }
584 else if ( r >= 270 && r < 360 )
585 {
586 if ( mHAlignment == Qt::AlignHCenter )
587 {
588 yShift = -( newHeight - currentHeight ) / 2.0;
589 }
590 else if ( mHAlignment == Qt::AlignLeft )
591 {
592 yShift = -( newHeight - currentHeight );
593 }
594 if ( mVAlignment == Qt::AlignBottom )
595 {
596 xShift = -( newWidth - currentWidth );
597 }
598 else if ( mVAlignment == Qt::AlignVCenter )
599 {
600 xShift = -( newWidth - currentWidth / 2.0 );
601 }
602 }
603}
604
605QFont QgsLayoutItemLabel::createDefaultFont() const
606{
607 QFont f = mFormat.font();
608 switch ( mFormat.sizeUnit() )
609 {
611 f.setPointSizeF( mFormat.size() / 0.352778 );
612 break;
614 f.setPixelSize( mFormat.size() );
615 break;
617 f.setPointSizeF( mFormat.size() );
618 break;
620 f.setPointSizeF( mFormat.size() * 72 );
621 break;
626 break;
627 }
628 return f;
629}
630
631double QgsLayoutItemLabel::htmlUnitsToLayoutUnits()
632{
633 if ( !mLayout )
634 {
635 return 1.0;
636 }
637
638 //TODO : fix this more precisely so that the label's default text size is the same with or without "display as html"
639 return mLayout->convertToLayoutUnits( QgsLayoutMeasurement( mLayout->renderContext().dpi() / 72.0, Qgis::LayoutUnit::Millimeters ) ); //webkit seems to assume a standard dpi of 72
640}
641
642QString QgsLayoutItemLabel::createStylesheet() const
643{
644 QString stylesheet;
645
646 stylesheet += QStringLiteral( "body { margin: %1 %2;" ).arg( std::max( mMarginY * mHtmlUnitsToLayoutUnits, 0.0 ) ).arg( std::max( mMarginX * mHtmlUnitsToLayoutUnits, 0.0 ) );
647 stylesheet += mFormat.asCSS( 0.352778 * mHtmlUnitsToLayoutUnits );
648 stylesheet += QStringLiteral( "text-align: %1; }" ).arg( mHAlignment == Qt::AlignLeft ? QStringLiteral( "left" ) : mHAlignment == Qt::AlignRight ? QStringLiteral( "right" ) : mHAlignment == Qt::AlignHCenter ? QStringLiteral( "center" ) : QStringLiteral( "justify" ) );
649
650 return stylesheet;
651}
652
653QUrl QgsLayoutItemLabel::createStylesheetUrl() const
654{
655 QByteArray ba;
656 ba.append( createStylesheet().toUtf8() );
657 QUrl cssFileURL = QUrl( QString( "data:text/css;charset=utf-8;base64," + ba.toBase64() ) );
658
659 return cssFileURL;
660}
@ Millimeters
Millimeters.
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size)
@ Millimeters
Millimeters.
@ Points
Points (e.g., for font sizes)
@ Unknown
Mixed or unknown units.
@ MapUnits
Map units.
@ MetersInMapUnits
Meters value as Map units.
@ 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 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.
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:43
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
Definition: qgslayoutitem.h:70
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.
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.
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:40
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:49
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:81
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:64
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:41
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.
QString asCSS(double pointToPixelMultiplier=1.0) const
Returns a CSS string representing the specified text format as closely as possible.
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.