QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 "qgslayoutitemregistry.h"
20 #include "qgslayout.h"
21 #include "qgslayoututils.h"
22 #include "qgslayoutmodel.h"
23 #include "qgsexpression.h"
25 #include "qgsvectorlayer.h"
26 #include "qgsproject.h"
27 #include "qgsdistancearea.h"
28 #include "qgsfontutils.h"
29 #include "qgsexpressioncontext.h"
30 #include "qgsmapsettings.h"
31 #include "qgslayoutitemmap.h"
32 #include "qgssettings.h"
33 
34 #include "qgswebview.h"
35 #include "qgswebframe.h"
36 #include "qgswebpage.h"
37 
38 #include <QCoreApplication>
39 #include <QDate>
40 #include <QDomElement>
41 #include <QPainter>
42 #include <QTimer>
43 #include <QEventLoop>
44 
46  : QgsLayoutItem( layout )
47 {
48  mDistanceArea.reset( new QgsDistanceArea() );
49  mHtmlUnitsToLayoutUnits = htmlUnitsToLayoutUnits();
50 
51  //get default layout font from settings
52  QgsSettings settings;
53  QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
54  if ( !defaultFontString.isEmpty() )
55  {
56  mFont.setFamily( defaultFontString );
57  }
58 
59  //default to a 10 point font size
60  mFont.setPointSizeF( 10 );
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  mWebPage.reset( new QgsWebPage( this ) );
70  mWebPage->setIdentifier( tr( "Layout label item" ) );
71  mWebPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
72 
73  //This makes the background transparent. Found on http://blog.qt.digia.com/blog/2009/06/30/transparent-qwebview-or-qwebpage/
74  QPalette palette = mWebPage->palette();
75  palette.setBrush( QPalette::Base, Qt::transparent );
76  mWebPage->setPalette( palette );
77 
78  mWebPage->mainFrame()->setZoomFactor( 10.0 );
79  mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
80  mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
81 
82  connect( mWebPage.get(), &QWebPage::loadFinished, this, &QgsLayoutItemLabel::loadingHtmlFinished );
83 }
84 
86 {
87  return new QgsLayoutItemLabel( layout );
88 }
89 
91 {
93 }
94 
96 {
97  return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemLabel.svg" ) );
98 }
99 
101 {
102  QPainter *painter = context.renderContext().painter();
103  painter->save();
104 
105  // painter is scaled to dots, so scale back to layout units
106  painter->scale( context.renderContext().scaleFactor(), context.renderContext().scaleFactor() );
107 
108  double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0;
109  double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth;
110  double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
111  QRectF painterRect( xPenAdjust + mMarginX, yPenAdjust + mMarginY, rect().width() - 2 * xPenAdjust - 2 * mMarginX, rect().height() - 2 * yPenAdjust - 2 * mMarginY );
112 
113  switch ( mMode )
114  {
115  case ModeHtml:
116  {
117  if ( mFirstRender )
118  {
119  contentChanged();
120  mFirstRender = false;
121  }
122  painter->scale( 1.0 / mHtmlUnitsToLayoutUnits / 10.0, 1.0 / mHtmlUnitsToLayoutUnits / 10.0 );
123  mWebPage->setViewportSize( QSize( painterRect.width() * mHtmlUnitsToLayoutUnits * 10.0, painterRect.height() * mHtmlUnitsToLayoutUnits * 10.0 ) );
124  mWebPage->settings()->setUserStyleSheetUrl( createStylesheetUrl() );
125  mWebPage->mainFrame()->render( painter );
126  break;
127  }
128 
129  case ModeFont:
130  {
131  const QString textToDraw = currentText();
132  painter->setFont( mFont );
133  QgsLayoutUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap );
134  break;
135  }
136  }
137 
138  painter->restore();
139 }
140 
141 void QgsLayoutItemLabel::contentChanged()
142 {
143  switch ( mMode )
144  {
145  case ModeHtml:
146  {
147  const QString textToDraw = currentText();
148 
149  //mHtmlLoaded tracks whether the QWebPage has completed loading
150  //its html contents, set it initially to false. The loadingHtmlFinished slot will
151  //set this to true after html is loaded.
152  mHtmlLoaded = false;
153 
154  const QUrl baseUrl = QUrl::fromLocalFile( mLayout->project()->absoluteFilePath() );
155  mWebPage->mainFrame()->setHtml( textToDraw, baseUrl );
156 
157  //For very basic html labels with no external assets, the html load will already be
158  //complete before we even get a chance to start the QEventLoop. Make sure we check
159  //this before starting the loop
160  if ( !mHtmlLoaded )
161  {
162  //Setup event loop and timeout for rendering html
163  QEventLoop loop;
164 
165  //Connect timeout and webpage loadFinished signals to loop
166  connect( mWebPage.get(), &QWebPage::loadFinished, &loop, &QEventLoop::quit );
167 
168  // Start a 20 second timeout in case html loading will never complete
169  QTimer timeoutTimer;
170  timeoutTimer.setSingleShot( true );
171  connect( &timeoutTimer, &QTimer::timeout, &loop, &QEventLoop::quit );
172  timeoutTimer.start( 20000 );
173 
174  // Pause until html is loaded
175  loop.exec( QEventLoop::ExcludeUserInputEvents );
176  }
177  break;
178  }
179  case ModeFont:
180  break;
181  }
182 }
183 
184 void QgsLayoutItemLabel::loadingHtmlFinished( bool result )
185 {
186  Q_UNUSED( result )
187  mHtmlLoaded = true;
188 }
189 
190 double QgsLayoutItemLabel::htmlUnitsToLayoutUnits()
191 {
192  if ( !mLayout )
193  {
194  return 1.0;
195  }
196 
197  //TODO : fix this more precisely so that the label's default text size is the same with or without "display as html"
198  return mLayout->convertToLayoutUnits( QgsLayoutMeasurement( mLayout->renderContext().dpi() / 72.0, QgsUnitTypes::LayoutMillimeters ) ); //webkit seems to assume a standard dpi of 72
199 }
200 
201 void QgsLayoutItemLabel::setText( const QString &text )
202 {
203  mText = text;
204  emit changed();
205 
206  contentChanged();
207 
208  if ( mLayout && id().isEmpty() && mMode != ModeHtml )
209  {
210  //notify the model that the display name has changed
211  mLayout->itemsModel()->updateItemDisplayName( this );
212  }
213 }
214 
216 {
217  if ( mode == mMode )
218  {
219  return;
220  }
221 
222  mMode = mode;
223  contentChanged();
224 
225  if ( mLayout && id().isEmpty() )
226  {
227  //notify the model that the display name has changed
228  mLayout->itemsModel()->updateItemDisplayName( this );
229  }
230 }
231 
232 void QgsLayoutItemLabel::refreshExpressionContext()
233 {
234  if ( !mLayout )
235  return;
236 
237  QgsVectorLayer *layer = mLayout->reportContext().layer();
238  //setup distance area conversion
239  if ( layer )
240  {
241  mDistanceArea->setSourceCrs( layer->crs(), mLayout->project()->transformContext() );
242  }
243  else
244  {
245  //set to composition's reference map's crs
246  QgsLayoutItemMap *referenceMap = mLayout->referenceMap();
247  if ( referenceMap )
248  mDistanceArea->setSourceCrs( referenceMap->crs(), mLayout->project()->transformContext() );
249  }
250  mDistanceArea->setEllipsoid( mLayout->project()->ellipsoid() );
251  contentChanged();
252 
253  update();
254 }
255 
257 {
258  QString displayText = mText;
259  replaceDateText( displayText );
260 
262 
263  return QgsExpression::replaceExpressionText( displayText, &context, mDistanceArea.get() );
264 }
265 
266 void QgsLayoutItemLabel::replaceDateText( QString &text ) const
267 {
268  QString constant = QStringLiteral( "$CURRENT_DATE" );
269  int currentDatePos = text.indexOf( constant );
270  if ( currentDatePos != -1 )
271  {
272  //check if there is a bracket just after $CURRENT_DATE
273  QString formatText;
274  int openingBracketPos = text.indexOf( '(', currentDatePos );
275  int closingBracketPos = text.indexOf( ')', openingBracketPos + 1 );
276  if ( openingBracketPos != -1 &&
277  closingBracketPos != -1 &&
278  ( closingBracketPos - openingBracketPos ) > 1 &&
279  openingBracketPos == currentDatePos + constant.size() )
280  {
281  formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
282  text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
283  }
284  else //no bracket
285  {
286  text.replace( QLatin1String( "$CURRENT_DATE" ), QDate::currentDate().toString() );
287  }
288  }
289 }
290 
291 void QgsLayoutItemLabel::setFont( const QFont &f )
292 {
293  mFont = f;
294 }
295 
296 void QgsLayoutItemLabel::setMargin( const double m )
297 {
298  mMarginX = m;
299  mMarginY = m;
300  prepareGeometryChange();
301 }
302 
303 void QgsLayoutItemLabel::setMarginX( const double margin )
304 {
305  mMarginX = margin;
306  prepareGeometryChange();
307 }
308 
309 void QgsLayoutItemLabel::setMarginY( const double margin )
310 {
311  mMarginY = margin;
312  prepareGeometryChange();
313 }
314 
316 {
317  QSizeF newSize = sizeForText();
318 
319  //keep alignment point constant
320  double xShift = 0;
321  double yShift = 0;
322 
323  itemShiftAdjustSize( newSize.width(), newSize.height(), xShift, yShift );
324 
325  //update rect for data defined size and position
326  attemptSetSceneRect( QRectF( pos().x() + xShift, pos().y() + yShift, newSize.width(), newSize.height() ) );
327 }
328 
330 {
331  double textWidth = QgsLayoutUtils::textWidthMM( mFont, currentText() );
332  double fontHeight = QgsLayoutUtils::fontHeightMM( mFont );
333 
334  double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0;
335 
336  double width = textWidth + 2 * mMarginX + 2 * penWidth + 1;
337  double height = fontHeight + 2 * mMarginY + 2 * penWidth;
338 
339  return mLayout->convertToLayoutUnits( QgsLayoutSize( width, height, QgsUnitTypes::LayoutMillimeters ) );
340 }
341 
343 {
344  return mFont;
345 }
346 
347 bool QgsLayoutItemLabel::writePropertiesToElement( QDomElement &layoutLabelElem, QDomDocument &doc, const QgsReadWriteContext & ) const
348 {
349  layoutLabelElem.setAttribute( QStringLiteral( "htmlState" ), static_cast< int >( mMode ) );
350 
351  layoutLabelElem.setAttribute( QStringLiteral( "labelText" ), mText );
352  layoutLabelElem.setAttribute( QStringLiteral( "marginX" ), QString::number( mMarginX ) );
353  layoutLabelElem.setAttribute( QStringLiteral( "marginY" ), QString::number( mMarginY ) );
354  layoutLabelElem.setAttribute( QStringLiteral( "halign" ), mHAlignment );
355  layoutLabelElem.setAttribute( QStringLiteral( "valign" ), mVAlignment );
356 
357  //font
358  QDomElement labelFontElem = QgsFontUtils::toXmlElement( mFont, doc, QStringLiteral( "LabelFont" ) );
359  layoutLabelElem.appendChild( labelFontElem );
360 
361  //font color
362  QDomElement fontColorElem = doc.createElement( QStringLiteral( "FontColor" ) );
363  fontColorElem.setAttribute( QStringLiteral( "red" ), mFontColor.red() );
364  fontColorElem.setAttribute( QStringLiteral( "green" ), mFontColor.green() );
365  fontColorElem.setAttribute( QStringLiteral( "blue" ), mFontColor.blue() );
366  fontColorElem.setAttribute( QStringLiteral( "alpha" ), mFontColor.alpha() );
367  layoutLabelElem.appendChild( fontColorElem );
368 
369  return true;
370 }
371 
372 bool QgsLayoutItemLabel::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext & )
373 {
374  //restore label specific properties
375 
376  //text
377  mText = itemElem.attribute( QStringLiteral( "labelText" ) );
378 
379  //html state
380  mMode = static_cast< Mode >( itemElem.attribute( QStringLiteral( "htmlState" ) ).toInt() );
381 
382  //margin
383  bool marginXOk = false;
384  bool marginYOk = false;
385  mMarginX = itemElem.attribute( QStringLiteral( "marginX" ) ).toDouble( &marginXOk );
386  mMarginY = itemElem.attribute( QStringLiteral( "marginY" ) ).toDouble( &marginYOk );
387  if ( !marginXOk || !marginYOk )
388  {
389  //upgrade old projects where margins where stored in a single attribute
390  double margin = itemElem.attribute( QStringLiteral( "margin" ), QStringLiteral( "1.0" ) ).toDouble();
391  mMarginX = margin;
392  mMarginY = margin;
393  }
394 
395  //Horizontal alignment
396  mHAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "halign" ) ).toInt() );
397 
398  //Vertical alignment
399  mVAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "valign" ) ).toInt() );
400 
401  //font
402  QgsFontUtils::setFromXmlChildNode( mFont, itemElem, QStringLiteral( "LabelFont" ) );
403 
404  //font color
405  QDomNodeList fontColorList = itemElem.elementsByTagName( QStringLiteral( "FontColor" ) );
406  if ( !fontColorList.isEmpty() )
407  {
408  QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
409  int red = fontColorElem.attribute( QStringLiteral( "red" ), QStringLiteral( "0" ) ).toInt();
410  int green = fontColorElem.attribute( QStringLiteral( "green" ), QStringLiteral( "0" ) ).toInt();
411  int blue = fontColorElem.attribute( QStringLiteral( "blue" ), QStringLiteral( "0" ) ).toInt();
412  int alpha = fontColorElem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "255" ) ).toInt();
413  mFontColor = QColor( red, green, blue, alpha );
414  }
415  else
416  {
417  mFontColor = QColor( 0, 0, 0 );
418  }
419 
420  return true;
421 }
422 
424 {
425  if ( !id().isEmpty() )
426  {
427  return id();
428  }
429 
430  switch ( mMode )
431  {
432  case ModeHtml:
433  return tr( "<HTML Label>" );
434 
435  case ModeFont:
436  {
437 
438  //if no id, default to portion of label text
439  QString text = mText;
440  if ( text.isEmpty() )
441  {
442  return tr( "<Label>" );
443  }
444  if ( text.length() > 25 )
445  {
446  return QString( tr( "%1…" ) ).arg( text.left( 25 ).simplified() );
447  }
448  else
449  {
450  return text.simplified();
451  }
452  }
453  }
454  return QString(); // no warnings
455 }
456 
458 {
459  QRectF rectangle = rect();
460  double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0;
461  rectangle.adjust( -penWidth, -penWidth, penWidth, penWidth );
462 
463  if ( mMarginX < 0 )
464  {
465  rectangle.adjust( mMarginX, 0, -mMarginX, 0 );
466  }
467  if ( mMarginY < 0 )
468  {
469  rectangle.adjust( 0, mMarginY, 0, -mMarginY );
470  }
471 
472  return rectangle;
473 }
474 
476 {
477  QgsLayoutItem::setFrameEnabled( drawFrame );
478  prepareGeometryChange();
479 }
480 
482 {
483  QgsLayoutItem::setFrameStrokeWidth( strokeWidth );
484  prepareGeometryChange();
485 }
486 
488 {
490  refreshExpressionContext();
491 }
492 
493 void QgsLayoutItemLabel::itemShiftAdjustSize( double newWidth, double newHeight, double &xShift, double &yShift ) const
494 {
495  //keep alignment point constant
496  double currentWidth = rect().width();
497  double currentHeight = rect().height();
498  xShift = 0;
499  yShift = 0;
500 
501  double r = rotation();
502  if ( r >= 0 && r < 90 )
503  {
504  if ( mHAlignment == Qt::AlignHCenter )
505  {
506  xShift = - ( newWidth - currentWidth ) / 2.0;
507  }
508  else if ( mHAlignment == Qt::AlignRight )
509  {
510  xShift = - ( newWidth - currentWidth );
511  }
512  if ( mVAlignment == Qt::AlignVCenter )
513  {
514  yShift = -( newHeight - currentHeight ) / 2.0;
515  }
516  else if ( mVAlignment == Qt::AlignBottom )
517  {
518  yShift = - ( newHeight - currentHeight );
519  }
520  }
521  if ( r >= 90 && r < 180 )
522  {
523  if ( mHAlignment == Qt::AlignHCenter )
524  {
525  yShift = -( newHeight - currentHeight ) / 2.0;
526  }
527  else if ( mHAlignment == Qt::AlignRight )
528  {
529  yShift = -( newHeight - currentHeight );
530  }
531  if ( mVAlignment == Qt::AlignTop )
532  {
533  xShift = -( newWidth - currentWidth );
534  }
535  else if ( mVAlignment == Qt::AlignVCenter )
536  {
537  xShift = -( newWidth - currentWidth / 2.0 );
538  }
539  }
540  else if ( r >= 180 && r < 270 )
541  {
542  if ( mHAlignment == Qt::AlignHCenter )
543  {
544  xShift = -( newWidth - currentWidth ) / 2.0;
545  }
546  else if ( mHAlignment == Qt::AlignLeft )
547  {
548  xShift = -( newWidth - currentWidth );
549  }
550  if ( mVAlignment == Qt::AlignVCenter )
551  {
552  yShift = ( newHeight - currentHeight ) / 2.0;
553  }
554  else if ( mVAlignment == Qt::AlignTop )
555  {
556  yShift = ( newHeight - currentHeight );
557  }
558  }
559  else if ( r >= 270 && r < 360 )
560  {
561  if ( mHAlignment == Qt::AlignHCenter )
562  {
563  yShift = -( newHeight - currentHeight ) / 2.0;
564  }
565  else if ( mHAlignment == Qt::AlignLeft )
566  {
567  yShift = -( newHeight - currentHeight );
568  }
569  if ( mVAlignment == Qt::AlignBottom )
570  {
571  xShift = -( newWidth - currentWidth );
572  }
573  else if ( mVAlignment == Qt::AlignVCenter )
574  {
575  xShift = -( newWidth - currentWidth / 2.0 );
576  }
577  }
578 }
579 
580 QUrl QgsLayoutItemLabel::createStylesheetUrl() const
581 {
582  QString stylesheet;
583  stylesheet += QStringLiteral( "body { margin: %1 %2;" ).arg( std::max( mMarginY * mHtmlUnitsToLayoutUnits, 0.0 ) ).arg( std::max( mMarginX * mHtmlUnitsToLayoutUnits, 0.0 ) );
584  stylesheet += QgsFontUtils::asCSS( mFont, 0.352778 * mHtmlUnitsToLayoutUnits );
585  stylesheet += QStringLiteral( "color: rgba(%1,%2,%3,%4);" ).arg( mFontColor.red() ).arg( mFontColor.green() ).arg( mFontColor.blue() ).arg( QString::number( mFontColor.alphaF(), 'f', 4 ) );
586  stylesheet += QStringLiteral( "text-align: %1; }" ).arg( mHAlignment == Qt::AlignLeft ? QStringLiteral( "left" ) : mHAlignment == Qt::AlignRight ? QStringLiteral( "right" ) : mHAlignment == Qt::AlignHCenter ? QStringLiteral( "center" ) : QStringLiteral( "justify" ) );
587 
588  QByteArray ba;
589  ba.append( stylesheet.toUtf8() );
590  QUrl cssFileURL = QUrl( "data:text/css;charset=utf-8;base64," + ba.toBase64() );
591 
592  return cssFileURL;
593 }
void setMargin(double margin)
Sets the margin between the edge of the frame and the label contents.
The class is used as a container of context for various read/write operations on other objects...
void draw(QgsLayoutItemRenderContext &context) override
Draws the item&#39;s contents using the specified item render context.
bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QRectF boundingRect() const override
Base class for graphical items within a QgsLayout.
Mode mode() const
Returns the label&#39;s current mode.
static double textWidthMM(const QFont &font, const QString &text)
Calculate a font width in millimeters for a text string, including workarounds for QT font rendering ...
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
A layout item subclass for text labels.
Label displays rendered HTML content.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
int type() const override
bool frameEnabled() const
Returns true if the item includes a frame.
static QgsLayoutItemLabel * create(QgsLayout *layout)
Returns a new label item for the specified layout.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
virtual void setFrameStrokeWidth(QgsLayoutMeasurement width)
Sets the frame stroke width.
void setMarginX(double margin)
Sets the horizontal margin between the edge of the frame and the label contents, in layout units...
void setFrameStrokeWidth(QgsLayoutMeasurement strokeWidth) override
Sets the frame stroke width.
QString text() const
Returns the label&#39;s preset text.
static QString asCSS(const QFont &font, double pointToPixelMultiplier=1.0)
Returns a CSS string representing the specified font as closely as possible.
virtual void drawFrame(QgsRenderContext &context)
Draws the frame around the item.
Layout graphical items for displaying a map.
const QgsLayout * layout() const
Returns the layout the object is attached to.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
QString currentText() const
Returns the text as it appears on the label (with evaluated expressions and other dynamic content)...
QgsLayoutItemLabel(QgsLayout *layout)
Constructor for QgsLayoutItemLabel, with the specified parent layout.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void attemptSetSceneRect(const QRectF &rect, bool includesFrame=false)
Attempts to update the item&#39;s position and size to match the passed rect in layout coordinates...
QgsRenderContext & renderContext()
Returns a reference to the context&#39;s render context.
Definition: qgslayoutitem.h:72
QPointer< QgsLayout > mLayout
void setMode(Mode mode)
Sets the label&#39;s current mode, allowing the label to switch between font based and HTML based renderi...
void setMarginY(double margin)
Sets the vertical margin between the edge of the frame and the label contents, in layout units...
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.
QString id() const
Returns the item&#39;s ID name.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
static void drawText(QPainter *painter, QPointF position, const QString &text, const QFont &font, const QColor &color=QColor())
Draws text on a painter at a specific position, taking care of layout specific issues (calculation to...
void setBackgroundEnabled(bool drawBackground)
Sets whether this item has a background drawn under it or not.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:44
Label displays text rendered using a single font.
QSizeF sizeForText() const
Returns the required item size (in layout units) for the label&#39;s text to fill the item...
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
static double fontHeightMM(const QFont &font)
Calculate a font height in millimeters, including workarounds for QT font rendering issues...
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used for rendering the map.
void setFont(const QFont &font)
Sets the label&#39;s current font.
void setFrameEnabled(bool drawFrame) override
Sets whether this item has a frame drawn around it or not.
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
virtual void setFrameEnabled(bool drawFrame)
Sets whether this item has a frame drawn around it or not.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
void adjustSizeToText()
Resizes the item so that the label&#39;s text fits to the item.
QFont font() const
Returns the label&#39;s current font.
QString displayName() const override
Gets item display name.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:40
void changed()
Emitted when the object&#39;s properties change.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
Represents a vector layer which manages a vector based data sets.
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log...
Definition: qgswebpage.h:216
void setText(const QString &text)
Sets the label&#39;s preset text.
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:85
QIcon icon() const override
Returns the item&#39;s icon.
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...