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