QGIS API Documentation  2.99.0-Master (19b062c)
qgscomposerlabel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerlabel.cpp
3  -------------------
4  begin : January 2005
5  copyright : (C) 2005 by Radim Blazek
6  email : blazek@itc.it
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 "qgscomposerlabel.h"
19 #include "qgscomposition.h"
20 #include "qgscomposerutils.h"
21 #include "qgsexpression.h"
23 #include "qgscomposermodel.h"
24 #include "qgsvectorlayer.h"
25 #include "qgsproject.h"
26 #include "qgsdistancearea.h"
27 #include "qgsfontutils.h"
28 #include "qgsexpressioncontext.h"
29 #include "qgsmapsettings.h"
30 #include "qgscomposermap.h"
31 #include "qgssettings.h"
32 
33 #include "qgswebview.h"
34 #include "qgswebframe.h"
35 #include "qgswebpage.h"
36 
37 #include <QCoreApplication>
38 #include <QDate>
39 #include <QDomElement>
40 #include <QPainter>
41 #include <QTimer>
42 #include <QEventLoop>
43 
45  : QgsComposerItem( composition )
46  , mHtmlState( 0 )
47  , mHtmlUnitsToMM( 1.0 )
48  , mHtmlLoaded( false )
49  , mMarginX( 1.0 )
50  , mMarginY( 1.0 )
51  , mFontColor( QColor( 0, 0, 0 ) )
52  , mHAlignment( Qt::AlignJustify )
53  , mVAlignment( Qt::AlignTop )
54 
55 {
56  mDistanceArea = new QgsDistanceArea();
57  mHtmlUnitsToMM = htmlUnitsToMM();
58 
59  //get default composer font from settings
60  QgsSettings settings;
61  QString defaultFontString = settings.value( QStringLiteral( "Composer/defaultFont" ) ).toString();
62  if ( !defaultFontString.isEmpty() )
63  {
64  mFont.setFamily( defaultFontString );
65  }
66 
67  //default to a 10 point font size
68  mFont.setPointSizeF( 10 );
69 
70  //default to no background
71  setBackgroundEnabled( false );
72 
73  //a label added while atlas preview is enabled needs to have the expression context set,
74  //otherwise fields in the label aren't correctly evaluated until atlas preview feature changes (#9457)
76 
77  if ( mComposition )
78  {
79  //connect to atlas feature changes
80  //to update the expression context
82  connect( mComposition, &QgsComposition::refreshItemsTriggered, this, &QgsComposerLabel::contentChanged );
83  }
84 
85  mWebPage = new QgsWebPage( this );
86  mWebPage->setIdentifier( tr( "Composer label item" ) );
87  mWebPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
88 
89  //This makes the background transparent. Found on http://blog.qt.digia.com/blog/2009/06/30/transparent-qwebview-or-qwebpage/
90  QPalette palette = mWebPage->palette();
91  palette.setBrush( QPalette::Base, Qt::transparent );
92  mWebPage->setPalette( palette );
93  //webPage->setAttribute(Qt::WA_OpaquePaintEvent, false); //this does not compile, why ?
94 
95  mWebPage->mainFrame()->setZoomFactor( 10.0 );
96  mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
97  mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
98 
99  connect( mWebPage, &QWebPage::loadFinished, this, &QgsComposerLabel::loadingHtmlFinished );
100 }
101 
103 {
104  delete mDistanceArea;
105  delete mWebPage;
106 }
107 
108 void QgsComposerLabel::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
109 {
110  Q_UNUSED( itemStyle );
111  Q_UNUSED( pWidget );
112  if ( !painter )
113  {
114  return;
115  }
116  if ( !shouldDrawItem() )
117  {
118  return;
119  }
120 
121  drawBackground( painter );
122  painter->save();
123 
124  //antialiasing on
125  painter->setRenderHint( QPainter::Antialiasing, true );
126 
127  double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
128  double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth;
129  double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
130  QRectF painterRect( xPenAdjust + mMarginX, yPenAdjust + mMarginY, rect().width() - 2 * xPenAdjust - 2 * mMarginX, rect().height() - 2 * yPenAdjust - 2 * mMarginY );
131 
132  if ( mHtmlState )
133  {
134  if ( mFirstRender )
135  {
136  contentChanged();
137  mFirstRender = false;
138  }
139  painter->scale( 1.0 / mHtmlUnitsToMM / 10.0, 1.0 / mHtmlUnitsToMM / 10.0 );
140  mWebPage->setViewportSize( QSize( painterRect.width() * mHtmlUnitsToMM * 10.0, painterRect.height() * mHtmlUnitsToMM * 10.0 ) );
141  mWebPage->settings()->setUserStyleSheetUrl( createStylesheetUrl() );
142  mWebPage->mainFrame()->render( painter );
143  }
144  else
145  {
146  const QString textToDraw = displayText();
147  painter->setFont( mFont );
148  //debug
149  //painter->setPen( QColor( Qt::red ) );
150  //painter->drawRect( painterRect );
151  QgsComposerUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap );
152  }
153 
154  painter->restore();
155 
156  drawFrame( painter );
157  if ( isSelected() )
158  {
159  drawSelectionBoxes( painter );
160  }
161 }
162 
163 void QgsComposerLabel::contentChanged()
164 {
165  if ( mHtmlState )
166  {
167  const QString textToDraw = displayText();
168 
169  //mHtmlLoaded tracks whether the QWebPage has completed loading
170  //its html contents, set it initially to false. The loadingHtmlFinished slot will
171  //set this to true after html is loaded.
172  mHtmlLoaded = false;
173 
174  const QUrl baseUrl = QUrl::fromLocalFile( mComposition->project()->fileInfo().absoluteFilePath() );
175  mWebPage->mainFrame()->setHtml( textToDraw, baseUrl );
176 
177  //For very basic html labels with no external assets, the html load will already be
178  //complete before we even get a chance to start the QEventLoop. Make sure we check
179  //this before starting the loop
180  if ( !mHtmlLoaded )
181  {
182  //Setup event loop and timeout for rendering html
183  QEventLoop loop;
184 
185  //Connect timeout and webpage loadFinished signals to loop
186  connect( mWebPage, &QWebPage::loadFinished, &loop, &QEventLoop::quit );
187 
188  // Start a 20 second timeout in case html loading will never complete
189  QTimer timeoutTimer;
190  timeoutTimer.setSingleShot( true );
191  connect( &timeoutTimer, &QTimer::timeout, &loop, &QEventLoop::quit );
192  timeoutTimer.start( 20000 );
193 
194  // Pause until html is loaded
195  loop.exec();
196  }
197  }
198 }
199 
200 /*Track when QWebPage has finished loading its html contents*/
201 void QgsComposerLabel::loadingHtmlFinished( bool result )
202 {
203  Q_UNUSED( result );
204  mHtmlLoaded = true;
205 }
206 
207 double QgsComposerLabel::htmlUnitsToMM()
208 {
209  if ( !mComposition )
210  {
211  return 1.0;
212  }
213 
214  //TODO : fix this more precisely so that the label's default text size is the same with or without "display as html"
215  return ( mComposition->printResolution() / 72.0 ); //webkit seems to assume a standard dpi of 72
216 }
217 
218 void QgsComposerLabel::setText( const QString &text )
219 {
220  mText = text;
221  emit itemChanged();
222 
223  contentChanged();
224 
225  if ( mComposition && id().isEmpty() && !mHtmlState )
226  {
227  //notify the model that the display name has changed
229  }
230 }
231 
233 {
234  if ( state == mHtmlState )
235  {
236  return;
237  }
238 
239  mHtmlState = state;
240  contentChanged();
241 
242  if ( mComposition && id().isEmpty() )
243  {
244  //notify the model that the display name has changed
246  }
247 }
248 
250 {
251  if ( !mComposition )
252  return;
253 
254  QgsVectorLayer *layer = nullptr;
256  {
258  }
259 
260  //setup distance area conversion
261  if ( layer )
262  {
263  mDistanceArea->setSourceCrs( layer->crs() );
264  }
265  else
266  {
267  //set to composition's reference map's crs
268  QgsComposerMap *referenceMap = mComposition->referenceMap();
269  if ( referenceMap )
270  mDistanceArea->setSourceCrs( referenceMap->crs() );
271  }
272  mDistanceArea->setEllipsoid( mComposition->project()->ellipsoid() );
273  contentChanged();
274 
275  update();
276 }
277 
279 {
280  QString displayText = mText;
281  replaceDateText( displayText );
282 
284 
285  return QgsExpression::replaceExpressionText( displayText, &context, mDistanceArea );
286 }
287 
288 void QgsComposerLabel::replaceDateText( QString &text ) const
289 {
290  QString constant = QStringLiteral( "$CURRENT_DATE" );
291  int currentDatePos = text.indexOf( constant );
292  if ( currentDatePos != -1 )
293  {
294  //check if there is a bracket just after $CURRENT_DATE
295  QString formatText;
296  int openingBracketPos = text.indexOf( '(', currentDatePos );
297  int closingBracketPos = text.indexOf( ')', openingBracketPos + 1 );
298  if ( openingBracketPos != -1 &&
299  closingBracketPos != -1 &&
300  ( closingBracketPos - openingBracketPos ) > 1 &&
301  openingBracketPos == currentDatePos + constant.size() )
302  {
303  formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
304  text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
305  }
306  else //no bracket
307  {
308  text.replace( QLatin1String( "$CURRENT_DATE" ), QDate::currentDate().toString() );
309  }
310  }
311 }
312 
313 void QgsComposerLabel::setFont( const QFont &f )
314 {
315  mFont = f;
316 }
317 
318 void QgsComposerLabel::setMargin( const double m )
319 {
320  mMarginX = m;
321  mMarginY = m;
322  prepareGeometryChange();
323 }
324 
325 void QgsComposerLabel::setMarginX( const double margin )
326 {
327  mMarginX = margin;
328  prepareGeometryChange();
329 }
330 
331 void QgsComposerLabel::setMarginY( const double margin )
332 {
333  mMarginY = margin;
334  prepareGeometryChange();
335 }
336 
338 {
339  double textWidth = QgsComposerUtils::textWidthMM( mFont, displayText() );
340  double fontHeight = QgsComposerUtils::fontHeightMM( mFont );
341 
342  double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
343 
344  double width = textWidth + 2 * mMarginX + 2 * penWidth + 1;
345  double height = fontHeight + 2 * mMarginY + 2 * penWidth;
346 
347  //keep alignment point constant
348  double xShift = 0;
349  double yShift = 0;
350  itemShiftAdjustSize( width, height, xShift, yShift );
351 
352  //update rect for data defined size and position
353  QRectF evaluatedRect = evalItemRect( QRectF( pos().x() + xShift, pos().y() + yShift, width, height ) );
354  setSceneRect( evaluatedRect );
355 }
356 
358 {
359  return mFont;
360 }
361 
362 bool QgsComposerLabel::writeXml( QDomElement &elem, QDomDocument &doc ) const
363 {
364  if ( elem.isNull() )
365  {
366  return false;
367  }
368 
369  QDomElement composerLabelElem = doc.createElement( QStringLiteral( "ComposerLabel" ) );
370 
371  composerLabelElem.setAttribute( QStringLiteral( "htmlState" ), mHtmlState );
372 
373  composerLabelElem.setAttribute( QStringLiteral( "labelText" ), mText );
374  composerLabelElem.setAttribute( QStringLiteral( "marginX" ), QString::number( mMarginX ) );
375  composerLabelElem.setAttribute( QStringLiteral( "marginY" ), QString::number( mMarginY ) );
376  composerLabelElem.setAttribute( QStringLiteral( "halign" ), mHAlignment );
377  composerLabelElem.setAttribute( QStringLiteral( "valign" ), mVAlignment );
378 
379  //font
380  QDomElement labelFontElem = QgsFontUtils::toXmlElement( mFont, doc, QStringLiteral( "LabelFont" ) );
381  composerLabelElem.appendChild( labelFontElem );
382 
383  //font color
384  QDomElement fontColorElem = doc.createElement( QStringLiteral( "FontColor" ) );
385  fontColorElem.setAttribute( QStringLiteral( "red" ), mFontColor.red() );
386  fontColorElem.setAttribute( QStringLiteral( "green" ), mFontColor.green() );
387  fontColorElem.setAttribute( QStringLiteral( "blue" ), mFontColor.blue() );
388  composerLabelElem.appendChild( fontColorElem );
389 
390  elem.appendChild( composerLabelElem );
391  return _writeXml( composerLabelElem, doc );
392 }
393 
394 bool QgsComposerLabel::readXml( const QDomElement &itemElem, const QDomDocument &doc )
395 {
396  if ( itemElem.isNull() )
397  {
398  return false;
399  }
400 
401  //restore label specific properties
402 
403  //text
404  mText = itemElem.attribute( QStringLiteral( "labelText" ) );
405 
406  //html state
407  mHtmlState = itemElem.attribute( QStringLiteral( "htmlState" ) ).toInt();
408 
409  //margin
410  bool marginXOk = false;
411  bool marginYOk = false;
412  mMarginX = itemElem.attribute( QStringLiteral( "marginX" ) ).toDouble( &marginXOk );
413  mMarginY = itemElem.attribute( QStringLiteral( "marginY" ) ).toDouble( &marginYOk );
414  if ( !marginXOk || !marginYOk )
415  {
416  //upgrade old projects where margins where stored in a single attribute
417  double margin = itemElem.attribute( QStringLiteral( "margin" ), QStringLiteral( "1.0" ) ).toDouble();
418  mMarginX = margin;
419  mMarginY = margin;
420  }
421 
422  //Horizontal alignment
423  mHAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "halign" ) ).toInt() );
424 
425  //Vertical alignment
426  mVAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "valign" ) ).toInt() );
427 
428  //font
429  QgsFontUtils::setFromXmlChildNode( mFont, itemElem, QStringLiteral( "LabelFont" ) );
430 
431  //font color
432  QDomNodeList fontColorList = itemElem.elementsByTagName( QStringLiteral( "FontColor" ) );
433  if ( !fontColorList.isEmpty() )
434  {
435  QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
436  int red = fontColorElem.attribute( QStringLiteral( "red" ), QStringLiteral( "0" ) ).toInt();
437  int green = fontColorElem.attribute( QStringLiteral( "green" ), QStringLiteral( "0" ) ).toInt();
438  int blue = fontColorElem.attribute( QStringLiteral( "blue" ), QStringLiteral( "0" ) ).toInt();
439  mFontColor = QColor( red, green, blue );
440  }
441  else
442  {
443  mFontColor = QColor( 0, 0, 0 );
444  }
445 
446  //restore general composer item properties
447  QDomNodeList composerItemList = itemElem.elementsByTagName( QStringLiteral( "ComposerItem" ) );
448  if ( !composerItemList.isEmpty() )
449  {
450  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
451 
452  //rotation
453  if ( !qgsDoubleNear( composerItemElem.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble(), 0.0 ) )
454  {
455  //check for old (pre 2.1) rotation attribute
456  setItemRotation( composerItemElem.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble() );
457  }
458 
459  _readXml( composerItemElem, doc );
460  }
461  emit itemChanged();
462  return true;
463 }
464 
466 {
467  if ( !id().isEmpty() )
468  {
469  return id();
470  }
471 
472  if ( mHtmlState )
473  {
474  return tr( "<HTML label>" );
475  }
476 
477  //if no id, default to portion of label text
478  QString text = mText;
479  if ( text.isEmpty() )
480  {
481  return tr( "<label>" );
482  }
483  if ( text.length() > 25 )
484  {
485  return QString( tr( "%1..." ) ).arg( text.left( 25 ).simplified() );
486  }
487  else
488  {
489  return text.simplified();
490  }
491 }
492 
494 {
495  QRectF rectangle = rect();
496  double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
497  rectangle.adjust( -penWidth, -penWidth, penWidth, penWidth );
498 
499  if ( mMarginX < 0 )
500  {
501  rectangle.adjust( mMarginX, 0, -mMarginX, 0 );
502  }
503  if ( mMarginY < 0 )
504  {
505  rectangle.adjust( 0, mMarginY, 0, -mMarginY );
506  }
507 
508  return rectangle;
509 }
510 
512 {
514  prepareGeometryChange();
515 }
516 
517 void QgsComposerLabel::setFrameStrokeWidth( const double strokeWidth )
518 {
520  prepareGeometryChange();
521 }
522 
523 void QgsComposerLabel::itemShiftAdjustSize( double newWidth, double newHeight, double &xShift, double &yShift ) const
524 {
525  //keep alignment point constant
526  double currentWidth = rect().width();
527  double currentHeight = rect().height();
528  xShift = 0;
529  yShift = 0;
530 
531  if ( mItemRotation >= 0 && mItemRotation < 90 )
532  {
533  if ( mHAlignment == Qt::AlignHCenter )
534  {
535  xShift = - ( newWidth - currentWidth ) / 2.0;
536  }
537  else if ( mHAlignment == Qt::AlignRight )
538  {
539  xShift = - ( newWidth - currentWidth );
540  }
541  if ( mVAlignment == Qt::AlignVCenter )
542  {
543  yShift = -( newHeight - currentHeight ) / 2.0;
544  }
545  else if ( mVAlignment == Qt::AlignBottom )
546  {
547  yShift = - ( newHeight - currentHeight );
548  }
549  }
550  if ( mItemRotation >= 90 && mItemRotation < 180 )
551  {
552  if ( mHAlignment == Qt::AlignHCenter )
553  {
554  yShift = -( newHeight - currentHeight ) / 2.0;
555  }
556  else if ( mHAlignment == Qt::AlignRight )
557  {
558  yShift = -( newHeight - currentHeight );
559  }
560  if ( mVAlignment == Qt::AlignTop )
561  {
562  xShift = -( newWidth - currentWidth );
563  }
564  else if ( mVAlignment == Qt::AlignVCenter )
565  {
566  xShift = -( newWidth - currentWidth / 2.0 );
567  }
568  }
569  else if ( mItemRotation >= 180 && mItemRotation < 270 )
570  {
571  if ( mHAlignment == Qt::AlignHCenter )
572  {
573  xShift = -( newWidth - currentWidth ) / 2.0;
574  }
575  else if ( mHAlignment == Qt::AlignLeft )
576  {
577  xShift = -( newWidth - currentWidth );
578  }
579  if ( mVAlignment == Qt::AlignVCenter )
580  {
581  yShift = ( newHeight - currentHeight ) / 2.0;
582  }
583  else if ( mVAlignment == Qt::AlignTop )
584  {
585  yShift = ( newHeight - currentHeight );
586  }
587  }
588  else if ( mItemRotation >= 270 && mItemRotation < 360 )
589  {
590  if ( mHAlignment == Qt::AlignHCenter )
591  {
592  yShift = -( newHeight - currentHeight ) / 2.0;
593  }
594  else if ( mHAlignment == Qt::AlignLeft )
595  {
596  yShift = -( newHeight - currentHeight );
597  }
598  if ( mVAlignment == Qt::AlignBottom )
599  {
600  xShift = -( newWidth - currentWidth );
601  }
602  else if ( mVAlignment == Qt::AlignVCenter )
603  {
604  xShift = -( newWidth - currentWidth / 2.0 );
605  }
606  }
607 }
608 
609 QUrl QgsComposerLabel::createStylesheetUrl() const
610 {
611  QString stylesheet;
612  stylesheet += QStringLiteral( "body { margin: %1 %2;" ).arg( std::max( mMarginY * mHtmlUnitsToMM, 0.0 ) ).arg( std::max( mMarginX * mHtmlUnitsToMM, 0.0 ) );
613  stylesheet += QgsFontUtils::asCSS( mFont, 0.352778 * mHtmlUnitsToMM );
614  stylesheet += QStringLiteral( "color: %1;" ).arg( mFontColor.name() );
615  stylesheet += QStringLiteral( "text-align: %1; }" ).arg( mHAlignment == Qt::AlignLeft ? QStringLiteral( "left" ) : mHAlignment == Qt::AlignRight ? QStringLiteral( "right" ) : mHAlignment == Qt::AlignHCenter ? QStringLiteral( "center" ) : QStringLiteral( "justify" ) );
616 
617  QByteArray ba;
618  ba.append( stylesheet.toUtf8() );
619  QUrl cssFileURL = QUrl( "data:text/css;charset=utf-8;base64," + ba.toBase64() );
620 
621  return cssFileURL;
622 }
void setHtmlState(int state)
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
virtual void setFrameStrokeWidth(const double strokeWidth)
Sets frame stroke width.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:55
void setIdentifier(const QString &identifier)
Sets an identifier for the QgsWebPage.
Definition: qgswebpage.h:237
QString displayText() const
Returns the text as it appears on screen (with replaced data field)
QgsComposerModel * itemsModel()
Returns the items model attached to the composition.
void itemChanged()
Emitted when the item changes.
bool readXml(const QDomElement &itemElem, const QDomDocument &doc) override
Sets state from Dom document.
int printResolution() const
A item that forms part of a map composition.
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false, const QgsExpressionContext *context=nullptr)
Evaluates an item&#39;s bounding rect to consider data defined position and size of item and reference po...
static void drawText(QPainter *painter, QPointF pos, const QString &text, const QFont &font, const QColor &color=QColor())
Draws text on a painter at a specific position, taking care of composer specific issues (calculation ...
virtual void drawFrame(QPainter *p)
Draw black frame around item.
virtual void setFrameEnabled(const bool drawFrame)
Set whether this item has a frame drawn around it or not.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
void setFont(const QFont &f)
QString ellipsoid
Definition: qgsproject.h:88
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:227
void updateItemDisplayName(QgsComposerItem *item)
Must be called when an item&#39;s display name is modified.
static QString asCSS(const QFont &font, double pointToPixelMultiplier=1.0)
Returns a CSS string representing the specified font as closely as possible.
void refreshItemsTriggered()
Is emitted when item in the composition must be refreshed.
bool _writeXml(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document. Usually called from writeXml methods of ...
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
QgsComposerMap * referenceMap() const
Returns the map item which will be used to generate corresponding world files when the composition is...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsCoordinateReferenceSystem crs() const
Returns the layer&#39;s spatial reference system.
QgsComposerLabel(QgsComposition *composition)
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.
bool _readXml(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document. Usually called from readXml methods of su...
void setBackgroundEnabled(const bool drawBackground)
Set whether this item has a Background drawn around it or not.
QRectF boundingRect() const override
In case of negative margins, the bounding rect may be larger than the label&#39;s frame.
void setMarginX(const double margin)
Sets the horizontal margin between the edge of the frame and the label contents.
Graphics scene for map printing.
Object representing map window.
void featureChanged(QgsFeature *feature)
Is emitted when the current atlas feature changes.
void setMarginY(const double margin)
Sets the vertical margin between the edge of the frame and the label contents.
virtual QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the item&#39;s current state.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Reimplementation of QCanvasItem::paint.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
static double textWidthMM(const QFont &font, const QString &text)
Calculate font width in millimeters for a string, including workarounds for QT font rendering issues...
QgsComposition * mComposition
static double fontHeightMM(const QFont &font)
Calculate font height in millimeters, including workarounds for QT font rendering issues The font hei...
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used for rendering the map.
QFileInfo fileInfo() const
Returns QFileInfo object for the project&#39;s associated file.
Definition: qgsproject.cpp:441
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
QgsProject * project() const
The project associated with the composition.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
virtual void drawBackground(QPainter *p)
Draw background.
void setMargin(const double m)
Sets the margin between the edge of the frame and the label contents.
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
void setText(const QString &text)
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
QgsAtlasComposition & atlasComposition()
bool writeXml(QDomElement &elem, QDomDocument &doc) const override
Stores state in Dom element.
virtual void setFrameStrokeWidth(const double strokeWidth) override
Reimplemented to call prepareGeometryChange after changing stroke width.
bool hasFrame() const
Whether this item has a frame or not.
void setSourceCrs(const QgsCoordinateReferenceSystem &srcCRS)
Sets source spatial reference system.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
Represents a vector layer which manages a vector based data sets.
QString id() const
Get item&#39;s id (which is not necessarly unique)
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log...
Definition: qgswebpage.h:216
double mItemRotation
Item rotation in degrees, clockwise.
virtual void setItemRotation(const double rotation, const bool adjustPosition=false)
Sets the item rotation, in degrees clockwise.
bool enabled() const
Returns whether the atlas generation is enabled.
void adjustSizeToText()
Resizes the widget such that the text fits to the item. Keeps top left point.
virtual void setFrameEnabled(const bool drawFrame) override
Reimplemented to call prepareGeometryChange after toggling frame.
virtual QString displayName() const override
Get item display name.