QGIS API Documentation  2.99.0-Master (08ee180)
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 : [email protected]
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 
31 #include "qgswebview.h"
32 #include "qgswebframe.h"
33 #include "qgswebpage.h"
34 
35 #include <QCoreApplication>
36 #include <QDate>
37 #include <QDomElement>
38 #include <QPainter>
39 #include <QSettings>
40 #include <QTimer>
41 #include <QEventLoop>
42 
44  : QgsComposerItem( composition )
45  , mHtmlState( 0 )
46  , mHtmlUnitsToMM( 1.0 )
47  , mHtmlLoaded( false )
48  , mMarginX( 1.0 )
49  , mMarginY( 1.0 )
50  , mFontColor( QColor( 0, 0, 0 ) )
51  , mHAlignment( Qt::AlignLeft )
52  , mVAlignment( Qt::AlignTop )
53  , mExpressionLayer( nullptr )
54  , mDistanceArea( nullptr )
55 {
56  mDistanceArea = new QgsDistanceArea();
57  mHtmlUnitsToMM = htmlUnitsToMM();
58 
59  //get default composer font from settings
60  QSettings 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
81  connect( &mComposition->atlasComposition(), SIGNAL( featureChanged( QgsFeature* ) ), this, SLOT( refreshExpressionContext() ) );
82  }
83 
84  mWebPage = new QgsWebPage( this );
85  mWebPage->setIdentifier( tr( "Composer label item" ) );
86  mWebPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
87 
88  //This makes the background transparent. Found on http://blog.qt.digia.com/blog/2009/06/30/transparent-qwebview-or-qwebpage/
89  QPalette palette = mWebPage->palette();
90  palette.setBrush( QPalette::Base, Qt::transparent );
91  mWebPage->setPalette( palette );
92  //webPage->setAttribute(Qt::WA_OpaquePaintEvent, false); //this does not compile, why ?
93 
94  mWebPage->mainFrame()->setZoomFactor( 10.0 );
95  mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
96  mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
97 
98  connect( mWebPage, SIGNAL( loadFinished( bool ) ), SLOT( loadingHtmlFinished( bool ) ) );
99 }
100 
102 {
103  delete mDistanceArea;
104  delete mWebPage;
105 }
106 
107 void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
108 {
109  Q_UNUSED( itemStyle );
110  Q_UNUSED( pWidget );
111  if ( !painter )
112  {
113  return;
114  }
115  if ( !shouldDrawItem() )
116  {
117  return;
118  }
119 
120  drawBackground( painter );
121  painter->save();
122 
123  //antialiasing on
124  painter->setRenderHint( QPainter::Antialiasing, true );
125 
126  double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
127  double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth;
128  double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
129  QRectF painterRect( xPenAdjust + mMarginX, yPenAdjust + mMarginY, rect().width() - 2 * xPenAdjust - 2 * mMarginX, rect().height() - 2 * yPenAdjust - 2 * mMarginY );
130 
131  if ( mHtmlState )
132  {
133  painter->scale( 1.0 / mHtmlUnitsToMM / 10.0, 1.0 / mHtmlUnitsToMM / 10.0 );
134  mWebPage->setViewportSize( QSize( painterRect.width() * mHtmlUnitsToMM * 10.0, painterRect.height() * mHtmlUnitsToMM * 10.0 ) );
135  mWebPage->settings()->setUserStyleSheetUrl( createStylesheetUrl() );
136  mWebPage->mainFrame()->render( painter );
137  }
138  else
139  {
140  const QString textToDraw = displayText();
141  painter->setFont( mFont );
142  //debug
143  //painter->setPen( QColor( Qt::red ) );
144  //painter->drawRect( painterRect );
145  QgsComposerUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap );
146  }
147 
148  painter->restore();
149 
150  drawFrame( painter );
151  if ( isSelected() )
152  {
153  drawSelectionBoxes( painter );
154  }
155 }
156 
157 void QgsComposerLabel::contentChanged()
158 {
159  if ( mHtmlState )
160  {
161  const QString textToDraw = displayText();
162 
163  //mHtmlLoaded tracks whether the QWebPage has completed loading
164  //its html contents, set it initially to false. The loadingHtmlFinished slot will
165  //set this to true after html is loaded.
166  mHtmlLoaded = false;
167 
168  const QUrl baseUrl = QUrl::fromLocalFile( QgsProject::instance()->fileInfo().absoluteFilePath() );
169  mWebPage->mainFrame()->setHtml( textToDraw, baseUrl );
170 
171  //For very basic html labels with no external assets, the html load will already be
172  //complete before we even get a chance to start the QEventLoop. Make sure we check
173  //this before starting the loop
174  if ( !mHtmlLoaded )
175  {
176  //Setup event loop and timeout for rendering html
177  QEventLoop loop;
178 
179  //Connect timeout and webpage loadFinished signals to loop
180  connect( mWebPage, SIGNAL( loadFinished( bool ) ), &loop, SLOT( quit() ) );
181 
182  // Start a 20 second timeout in case html loading will never complete
183  QTimer timeoutTimer;
184  timeoutTimer.setSingleShot( true );
185  connect( &timeoutTimer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
186  timeoutTimer.start( 20000 );
187 
188  // Pause until html is loaded
189  loop.exec();
190  }
191  }
192 }
193 
194 /*Track when QWebPage has finished loading its html contents*/
195 void QgsComposerLabel::loadingHtmlFinished( bool result )
196 {
197  Q_UNUSED( result );
198  mHtmlLoaded = true;
199 }
200 
201 double QgsComposerLabel::htmlUnitsToMM()
202 {
203  if ( !mComposition )
204  {
205  return 1.0;
206  }
207 
208  //TODO : fix this more precisely so that the label's default text size is the same with or without "display as html"
209  return ( mComposition->printResolution() / 72.0 ); //webkit seems to assume a standard dpi of 72
210 }
211 
212 void QgsComposerLabel::setText( const QString& text )
213 {
214  mText = text;
215  emit itemChanged();
216 
217  contentChanged();
218 
219  if ( mComposition && id().isEmpty() && !mHtmlState )
220  {
221  //notify the model that the display name has changed
223  }
224 }
225 
227 {
228  if ( state == mHtmlState )
229  {
230  return;
231  }
232 
233  mHtmlState = state;
234  contentChanged();
235 
236  if ( mComposition && id().isEmpty() )
237  {
238  //notify the model that the display name has changed
240  }
241 }
242 
244 {
245  mExpressionLayer = nullptr;
246  mExpressionFeature.reset();
247 
248  if ( !mComposition )
249  return;
250 
251  QgsVectorLayer* layer = nullptr;
253  {
255  }
256 
257  //setup distance area conversion
258  if ( layer )
259  {
260  mDistanceArea->setSourceCrs( layer->crs().srsid() );
261  }
262  else
263  {
264  //set to composition's mapsettings' crs
265  mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
266  }
268  mDistanceArea->setEllipsoid( QgsProject::instance()->ellipsoid() );
269  contentChanged();
270 
271  update();
272 }
273 
275 {
276  QString displayText = mText;
277  replaceDateText( displayText );
278 
280  //overwrite layer/feature if they have been set via setExpressionContext
281  //TODO remove when setExpressionContext is removed
282  if ( mExpressionFeature.data() )
283  context.setFeature( *mExpressionFeature.data() );
284  if ( mExpressionLayer )
285  context.setFields( mExpressionLayer->fields() );
286 
287  return QgsExpression::replaceExpressionText( displayText, &context, mDistanceArea );
288 }
289 
290 void QgsComposerLabel::replaceDateText( QString& text ) const
291 {
292  QString constant = QStringLiteral( "$CURRENT_DATE" );
293  int currentDatePos = text.indexOf( constant );
294  if ( currentDatePos != -1 )
295  {
296  //check if there is a bracket just after $CURRENT_DATE
297  QString formatText;
298  int openingBracketPos = text.indexOf( '(', currentDatePos );
299  int closingBracketPos = text.indexOf( ')', openingBracketPos + 1 );
300  if ( openingBracketPos != -1 &&
301  closingBracketPos != -1 &&
302  ( closingBracketPos - openingBracketPos ) > 1 &&
303  openingBracketPos == currentDatePos + constant.size() )
304  {
305  formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
306  text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
307  }
308  else //no bracket
309  {
310  text.replace( QLatin1String( "$CURRENT_DATE" ), QDate::currentDate().toString() );
311  }
312  }
313 }
314 
315 void QgsComposerLabel::setFont( const QFont& f )
316 {
317  mFont = f;
318 }
319 
320 void QgsComposerLabel::setMargin( const double m )
321 {
322  mMarginX = m;
323  mMarginY = m;
324  prepareGeometryChange();
325 }
326 
327 void QgsComposerLabel::setMarginX( const double margin )
328 {
329  mMarginX = margin;
330  prepareGeometryChange();
331 }
332 
333 void QgsComposerLabel::setMarginY( const double margin )
334 {
335  mMarginY = margin;
336  prepareGeometryChange();
337 }
338 
340 {
341  double textWidth = QgsComposerUtils::textWidthMM( mFont, displayText() );
342  double fontHeight = QgsComposerUtils::fontHeightMM( mFont );
343 
344  double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
345 
346  double width = textWidth + 2 * mMarginX + 2 * penWidth + 1;
347  double height = fontHeight + 2 * mMarginY + 2 * penWidth;
348 
349  //keep alignment point constant
350  double xShift = 0;
351  double yShift = 0;
352  itemShiftAdjustSize( width, height, xShift, yShift );
353 
354  //update rect for data defined size and position
355  QRectF evaluatedRect = evalItemRect( QRectF( pos().x() + xShift, pos().y() + yShift, width, height ) );
356  setSceneRect( evaluatedRect );
357 }
358 
360 {
361  return mFont;
362 }
363 
364 bool QgsComposerLabel::writeXml( QDomElement& elem, QDomDocument & doc ) const
365 {
366  if ( elem.isNull() )
367  {
368  return false;
369  }
370 
371  QDomElement composerLabelElem = doc.createElement( QStringLiteral( "ComposerLabel" ) );
372 
373  composerLabelElem.setAttribute( QStringLiteral( "htmlState" ), mHtmlState );
374 
375  composerLabelElem.setAttribute( QStringLiteral( "labelText" ), mText );
376  composerLabelElem.setAttribute( QStringLiteral( "marginX" ), QString::number( mMarginX ) );
377  composerLabelElem.setAttribute( QStringLiteral( "marginY" ), QString::number( mMarginY ) );
378  composerLabelElem.setAttribute( QStringLiteral( "halign" ), mHAlignment );
379  composerLabelElem.setAttribute( QStringLiteral( "valign" ), mVAlignment );
380 
381  //font
382  QDomElement labelFontElem = QgsFontUtils::toXmlElement( mFont, doc, QStringLiteral( "LabelFont" ) );
383  composerLabelElem.appendChild( labelFontElem );
384 
385  //font color
386  QDomElement fontColorElem = doc.createElement( QStringLiteral( "FontColor" ) );
387  fontColorElem.setAttribute( QStringLiteral( "red" ), mFontColor.red() );
388  fontColorElem.setAttribute( QStringLiteral( "green" ), mFontColor.green() );
389  fontColorElem.setAttribute( QStringLiteral( "blue" ), mFontColor.blue() );
390  composerLabelElem.appendChild( fontColorElem );
391 
392  elem.appendChild( composerLabelElem );
393  return _writeXml( composerLabelElem, doc );
394 }
395 
396 bool QgsComposerLabel::readXml( const QDomElement& itemElem, const QDomDocument& doc )
397 {
398  if ( itemElem.isNull() )
399  {
400  return false;
401  }
402 
403  //restore label specific properties
404 
405  //text
406  mText = itemElem.attribute( QStringLiteral( "labelText" ) );
407 
408  //html state
409  mHtmlState = itemElem.attribute( QStringLiteral( "htmlState" ) ).toInt();
410 
411  //margin
412  bool marginXOk = false;
413  bool marginYOk = false;
414  mMarginX = itemElem.attribute( QStringLiteral( "marginX" ) ).toDouble( &marginXOk );
415  mMarginY = itemElem.attribute( QStringLiteral( "marginY" ) ).toDouble( &marginYOk );
416  if ( !marginXOk || !marginYOk )
417  {
418  //upgrade old projects where margins where stored in a single attribute
419  double margin = itemElem.attribute( QStringLiteral( "margin" ), QStringLiteral( "1.0" ) ).toDouble();
420  mMarginX = margin;
421  mMarginY = margin;
422  }
423 
424  //Horizontal alignment
425  mHAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "halign" ) ).toInt() );
426 
427  //Vertical alignment
428  mVAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "valign" ) ).toInt() );
429 
430  //font
431  QgsFontUtils::setFromXmlChildNode( mFont, itemElem, QStringLiteral( "LabelFont" ) );
432 
433  //font color
434  QDomNodeList fontColorList = itemElem.elementsByTagName( QStringLiteral( "FontColor" ) );
435  if ( !fontColorList.isEmpty() )
436  {
437  QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
438  int red = fontColorElem.attribute( QStringLiteral( "red" ), QStringLiteral( "0" ) ).toInt();
439  int green = fontColorElem.attribute( QStringLiteral( "green" ), QStringLiteral( "0" ) ).toInt();
440  int blue = fontColorElem.attribute( QStringLiteral( "blue" ), QStringLiteral( "0" ) ).toInt();
441  mFontColor = QColor( red, green, blue );
442  }
443  else
444  {
445  mFontColor = QColor( 0, 0, 0 );
446  }
447 
448  //restore general composer item properties
449  QDomNodeList composerItemList = itemElem.elementsByTagName( QStringLiteral( "ComposerItem" ) );
450  if ( !composerItemList.isEmpty() )
451  {
452  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
453 
454  //rotation
455  if ( !qgsDoubleNear( composerItemElem.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble(), 0.0 ) )
456  {
457  //check for old (pre 2.1) rotation attribute
458  setItemRotation( composerItemElem.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble() );
459  }
460 
461  _readXml( composerItemElem, doc );
462  }
463  emit itemChanged();
464  contentChanged();
465  return true;
466 }
467 
469 {
470  if ( !id().isEmpty() )
471  {
472  return id();
473  }
474 
475  if ( mHtmlState )
476  {
477  return tr( "<HTML label>" );
478  }
479 
480  //if no id, default to portion of label text
481  QString text = mText;
482  if ( text.isEmpty() )
483  {
484  return tr( "<label>" );
485  }
486  if ( text.length() > 25 )
487  {
488  return QString( tr( "%1..." ) ).arg( text.left( 25 ).simplified() );
489  }
490  else
491  {
492  return text.simplified();
493  }
494 }
495 
497 {
498  QRectF rectangle = rect();
499  double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
500  rectangle.adjust( -penWidth, -penWidth, penWidth, penWidth );
501 
502  if ( mMarginX < 0 )
503  {
504  rectangle.adjust( mMarginX, 0, -mMarginX, 0 );
505  }
506  if ( mMarginY < 0 )
507  {
508  rectangle.adjust( 0, mMarginY, 0, -mMarginY );
509  }
510 
511  return rectangle;
512 }
513 
515 {
517  prepareGeometryChange();
518 }
519 
520 void QgsComposerLabel::setFrameOutlineWidth( const double outlineWidth )
521 {
523  prepareGeometryChange();
524 }
525 
526 void QgsComposerLabel::itemShiftAdjustSize( double newWidth, double newHeight, double& xShift, double& yShift ) const
527 {
528  //keep alignment point constant
529  double currentWidth = rect().width();
530  double currentHeight = rect().height();
531  xShift = 0;
532  yShift = 0;
533 
534  if ( mItemRotation >= 0 && mItemRotation < 90 )
535  {
536  if ( mHAlignment == Qt::AlignHCenter )
537  {
538  xShift = - ( newWidth - currentWidth ) / 2.0;
539  }
540  else if ( mHAlignment == Qt::AlignRight )
541  {
542  xShift = - ( newWidth - currentWidth );
543  }
544  if ( mVAlignment == Qt::AlignVCenter )
545  {
546  yShift = -( newHeight - currentHeight ) / 2.0;
547  }
548  else if ( mVAlignment == Qt::AlignBottom )
549  {
550  yShift = - ( newHeight - currentHeight );
551  }
552  }
553  if ( mItemRotation >= 90 && mItemRotation < 180 )
554  {
555  if ( mHAlignment == Qt::AlignHCenter )
556  {
557  yShift = -( newHeight - currentHeight ) / 2.0;
558  }
559  else if ( mHAlignment == Qt::AlignRight )
560  {
561  yShift = -( newHeight - currentHeight );
562  }
563  if ( mVAlignment == Qt::AlignTop )
564  {
565  xShift = -( newWidth - currentWidth );
566  }
567  else if ( mVAlignment == Qt::AlignVCenter )
568  {
569  xShift = -( newWidth - currentWidth / 2.0 );
570  }
571  }
572  else if ( mItemRotation >= 180 && mItemRotation < 270 )
573  {
574  if ( mHAlignment == Qt::AlignHCenter )
575  {
576  xShift = -( newWidth - currentWidth ) / 2.0;
577  }
578  else if ( mHAlignment == Qt::AlignLeft )
579  {
580  xShift = -( newWidth - currentWidth );
581  }
582  if ( mVAlignment == Qt::AlignVCenter )
583  {
584  yShift = ( newHeight - currentHeight ) / 2.0;
585  }
586  else if ( mVAlignment == Qt::AlignTop )
587  {
588  yShift = ( newHeight - currentHeight );
589  }
590  }
591  else if ( mItemRotation >= 270 && mItemRotation < 360 )
592  {
593  if ( mHAlignment == Qt::AlignHCenter )
594  {
595  yShift = -( newHeight - currentHeight ) / 2.0;
596  }
597  else if ( mHAlignment == Qt::AlignLeft )
598  {
599  yShift = -( newHeight - currentHeight );
600  }
601  if ( mVAlignment == Qt::AlignBottom )
602  {
603  xShift = -( newWidth - currentWidth );
604  }
605  else if ( mVAlignment == Qt::AlignVCenter )
606  {
607  xShift = -( newWidth - currentWidth / 2.0 );
608  }
609  }
610 }
611 
612 QUrl QgsComposerLabel::createStylesheetUrl() const
613 {
614  QString stylesheet;
615  stylesheet += QStringLiteral( "body { margin: %1 %2;" ).arg( qMax( mMarginY * mHtmlUnitsToMM, 0.0 ) ).arg( qMax( mMarginX * mHtmlUnitsToMM, 0.0 ) );
616  stylesheet += QgsFontUtils::asCSS( mFont, 0.352778 * mHtmlUnitsToMM );
617  stylesheet += QStringLiteral( "color: %1;" ).arg( mFontColor.name() );
618  stylesheet += QStringLiteral( "text-align: %1; }" ).arg( mHAlignment == Qt::AlignLeft ? "left" : mHAlignment == Qt::AlignRight ? "right" : "center" );
619 
620  QByteArray ba;
621  ba.append( stylesheet.toUtf8() );
622  QUrl cssFileURL = QUrl( "data:text/css;charset=utf-8;base64," + ba.toBase64() );
623 
624  return cssFileURL;
625 }
void setHtmlState(int state)
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
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 setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
void itemChanged()
Emitted when the item changes.
bool readXml(const QDomElement &itemElem, const QDomDocument &doc) override
Sets state from Dom document.
void setSourceCrs(long srsid)
sets source spatial reference system (by QGIS CRS)
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...
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
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 ellipsoid by its acronym.
void setFont(const QFont &f)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:135
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:196
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.
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
QgsFields fields() const
Returns the list of fields of this layer.
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.
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.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
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.
virtual void setFrameOutlineWidth(const double outlineWidth)
Sets frame outline width.
General purpose distance and area calculator.
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...
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
virtual void setItemRotation(const double r, const bool adjustPosition=false)
Sets the item rotation.
virtual void drawBackground(QPainter *p)
Draw background.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:348
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.
bool hasFrame() const
Whether this item has a frame or not.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
long srsid() const
Returns the internal CRS ID, if available.
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:207
double mItemRotation
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.
void setEllipsoidalMode(bool flag)
Sets whether coordinates must be projected to ellipsoid before measuring.
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...
virtual void setFrameEnabled(const bool drawFrame) override
Reimplemented to call prepareGeometryChange after toggling frame.
virtual QString displayName() const override
Get item display name.
virtual void setFrameOutlineWidth(const double outlineWidth) override
Reimplemented to call prepareGeometryChange after changing outline width.