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