QGIS API Documentation  2.99.0-Master (08c2e66)
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 #include "qgscomposermap.h"
31 
32 #include "qgswebview.h"
33 #include "qgswebframe.h"
34 #include "qgswebpage.h"
35 
36 #include <QCoreApplication>
37 #include <QDate>
38 #include <QDomElement>
39 #include <QPainter>
40 #include <QSettings>
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::AlignLeft )
53  , mVAlignment( Qt::AlignTop )
54  , mExpressionLayer( nullptr )
55  , mDistanceArea( nullptr )
56 {
57  mDistanceArea = new QgsDistanceArea();
58  mHtmlUnitsToMM = htmlUnitsToMM();
59 
60  //get default composer font from settings
61  QSettings settings;
62  QString defaultFontString = settings.value( QStringLiteral( "/Composer/defaultFont" ) ).toString();
63  if ( !defaultFontString.isEmpty() )
64  {
65  mFont.setFamily( defaultFontString );
66  }
67 
68  //default to a 10 point font size
69  mFont.setPointSizeF( 10 );
70 
71  //default to no background
72  setBackgroundEnabled( false );
73 
74  //a label added while atlas preview is enabled needs to have the expression context set,
75  //otherwise fields in the label aren't correctly evaluated until atlas preview feature changes (#9457)
77 
78  if ( mComposition )
79  {
80  //connect to atlas feature changes
81  //to update the expression context
82  connect( &mComposition->atlasComposition(), SIGNAL( featureChanged( QgsFeature* ) ), this, SLOT( refreshExpressionContext() ) );
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, SIGNAL( loadFinished( bool ) ), SLOT( loadingHtmlFinished( bool ) ) );
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  painter->scale( 1.0 / mHtmlUnitsToMM / 10.0, 1.0 / mHtmlUnitsToMM / 10.0 );
135  mWebPage->setViewportSize( QSize( painterRect.width() * mHtmlUnitsToMM * 10.0, painterRect.height() * mHtmlUnitsToMM * 10.0 ) );
136  mWebPage->settings()->setUserStyleSheetUrl( createStylesheetUrl() );
137  mWebPage->mainFrame()->render( painter );
138  }
139  else
140  {
141  const QString textToDraw = displayText();
142  painter->setFont( mFont );
143  //debug
144  //painter->setPen( QColor( Qt::red ) );
145  //painter->drawRect( painterRect );
146  QgsComposerUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap );
147  }
148 
149  painter->restore();
150 
151  drawFrame( painter );
152  if ( isSelected() )
153  {
154  drawSelectionBoxes( painter );
155  }
156 }
157 
158 void QgsComposerLabel::contentChanged()
159 {
160  if ( mHtmlState )
161  {
162  const QString textToDraw = displayText();
163 
164  //mHtmlLoaded tracks whether the QWebPage has completed loading
165  //its html contents, set it initially to false. The loadingHtmlFinished slot will
166  //set this to true after html is loaded.
167  mHtmlLoaded = false;
168 
169  const QUrl baseUrl = QUrl::fromLocalFile( mComposition->project()->fileInfo().absoluteFilePath() );
170  mWebPage->mainFrame()->setHtml( textToDraw, baseUrl );
171 
172  //For very basic html labels with no external assets, the html load will already be
173  //complete before we even get a chance to start the QEventLoop. Make sure we check
174  //this before starting the loop
175  if ( !mHtmlLoaded )
176  {
177  //Setup event loop and timeout for rendering html
178  QEventLoop loop;
179 
180  //Connect timeout and webpage loadFinished signals to loop
181  connect( mWebPage, SIGNAL( loadFinished( bool ) ), &loop, SLOT( quit() ) );
182 
183  // Start a 20 second timeout in case html loading will never complete
184  QTimer timeoutTimer;
185  timeoutTimer.setSingleShot( true );
186  connect( &timeoutTimer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
187  timeoutTimer.start( 20000 );
188 
189  // Pause until html is loaded
190  loop.exec();
191  }
192  }
193 }
194 
195 /*Track when QWebPage has finished loading its html contents*/
196 void QgsComposerLabel::loadingHtmlFinished( bool result )
197 {
198  Q_UNUSED( result );
199  mHtmlLoaded = true;
200 }
201 
202 double QgsComposerLabel::htmlUnitsToMM()
203 {
204  if ( !mComposition )
205  {
206  return 1.0;
207  }
208 
209  //TODO : fix this more precisely so that the label's default text size is the same with or without "display as html"
210  return ( mComposition->printResolution() / 72.0 ); //webkit seems to assume a standard dpi of 72
211 }
212 
213 void QgsComposerLabel::setText( const QString& text )
214 {
215  mText = text;
216  emit itemChanged();
217 
218  contentChanged();
219 
220  if ( mComposition && id().isEmpty() && !mHtmlState )
221  {
222  //notify the model that the display name has changed
224  }
225 }
226 
228 {
229  if ( state == mHtmlState )
230  {
231  return;
232  }
233 
234  mHtmlState = state;
235  contentChanged();
236 
237  if ( mComposition && id().isEmpty() )
238  {
239  //notify the model that the display name has changed
241  }
242 }
243 
245 {
246  mExpressionLayer = nullptr;
247  mExpressionFeature.reset();
248 
249  if ( !mComposition )
250  return;
251 
252  QgsVectorLayer* layer = nullptr;
254  {
256  }
257 
258  //setup distance area conversion
259  if ( layer )
260  {
261  mDistanceArea->setSourceCrs( layer->crs().srsid() );
262  }
263  else
264  {
265  //set to composition's reference map's crs
266  QgsComposerMap* referenceMap = mComposition->referenceMap();
267  if ( referenceMap )
268  mDistanceArea->setSourceCrs( referenceMap->crs().srsid() );
269  }
270  mDistanceArea->setEllipsoidalMode( true );
271  mDistanceArea->setEllipsoid( mComposition->project()->ellipsoid() );
272  contentChanged();
273 
274  update();
275 }
276 
278 {
279  QString displayText = mText;
280  replaceDateText( displayText );
281 
283  //overwrite layer/feature if they have been set via setExpressionContext
284  //TODO remove when setExpressionContext is removed
285  if ( mExpressionFeature.get() )
286  context.setFeature( *mExpressionFeature );
287  if ( mExpressionLayer )
288  context.setFields( mExpressionLayer->fields() );
289 
290  return QgsExpression::replaceExpressionText( displayText, &context, mDistanceArea );
291 }
292 
293 void QgsComposerLabel::replaceDateText( QString& text ) const
294 {
295  QString constant = QStringLiteral( "$CURRENT_DATE" );
296  int currentDatePos = text.indexOf( constant );
297  if ( currentDatePos != -1 )
298  {
299  //check if there is a bracket just after $CURRENT_DATE
300  QString formatText;
301  int openingBracketPos = text.indexOf( '(', currentDatePos );
302  int closingBracketPos = text.indexOf( ')', openingBracketPos + 1 );
303  if ( openingBracketPos != -1 &&
304  closingBracketPos != -1 &&
305  ( closingBracketPos - openingBracketPos ) > 1 &&
306  openingBracketPos == currentDatePos + constant.size() )
307  {
308  formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
309  text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
310  }
311  else //no bracket
312  {
313  text.replace( QLatin1String( "$CURRENT_DATE" ), QDate::currentDate().toString() );
314  }
315  }
316 }
317 
318 void QgsComposerLabel::setFont( const QFont& f )
319 {
320  mFont = f;
321 }
322 
323 void QgsComposerLabel::setMargin( const double m )
324 {
325  mMarginX = m;
326  mMarginY = m;
327  prepareGeometryChange();
328 }
329 
330 void QgsComposerLabel::setMarginX( const double margin )
331 {
332  mMarginX = margin;
333  prepareGeometryChange();
334 }
335 
336 void QgsComposerLabel::setMarginY( const double margin )
337 {
338  mMarginY = margin;
339  prepareGeometryChange();
340 }
341 
343 {
344  double textWidth = QgsComposerUtils::textWidthMM( mFont, displayText() );
345  double fontHeight = QgsComposerUtils::fontHeightMM( mFont );
346 
347  double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
348 
349  double width = textWidth + 2 * mMarginX + 2 * penWidth + 1;
350  double height = fontHeight + 2 * mMarginY + 2 * penWidth;
351 
352  //keep alignment point constant
353  double xShift = 0;
354  double yShift = 0;
355  itemShiftAdjustSize( width, height, xShift, yShift );
356 
357  //update rect for data defined size and position
358  QRectF evaluatedRect = evalItemRect( QRectF( pos().x() + xShift, pos().y() + yShift, width, height ) );
359  setSceneRect( evaluatedRect );
360 }
361 
363 {
364  return mFont;
365 }
366 
367 bool QgsComposerLabel::writeXml( QDomElement& elem, QDomDocument & doc ) const
368 {
369  if ( elem.isNull() )
370  {
371  return false;
372  }
373 
374  QDomElement composerLabelElem = doc.createElement( QStringLiteral( "ComposerLabel" ) );
375 
376  composerLabelElem.setAttribute( QStringLiteral( "htmlState" ), mHtmlState );
377 
378  composerLabelElem.setAttribute( QStringLiteral( "labelText" ), mText );
379  composerLabelElem.setAttribute( QStringLiteral( "marginX" ), QString::number( mMarginX ) );
380  composerLabelElem.setAttribute( QStringLiteral( "marginY" ), QString::number( mMarginY ) );
381  composerLabelElem.setAttribute( QStringLiteral( "halign" ), mHAlignment );
382  composerLabelElem.setAttribute( QStringLiteral( "valign" ), mVAlignment );
383 
384  //font
385  QDomElement labelFontElem = QgsFontUtils::toXmlElement( mFont, doc, QStringLiteral( "LabelFont" ) );
386  composerLabelElem.appendChild( labelFontElem );
387 
388  //font color
389  QDomElement fontColorElem = doc.createElement( QStringLiteral( "FontColor" ) );
390  fontColorElem.setAttribute( QStringLiteral( "red" ), mFontColor.red() );
391  fontColorElem.setAttribute( QStringLiteral( "green" ), mFontColor.green() );
392  fontColorElem.setAttribute( QStringLiteral( "blue" ), mFontColor.blue() );
393  composerLabelElem.appendChild( fontColorElem );
394 
395  elem.appendChild( composerLabelElem );
396  return _writeXml( composerLabelElem, doc );
397 }
398 
399 bool QgsComposerLabel::readXml( const QDomElement& itemElem, const QDomDocument& doc )
400 {
401  if ( itemElem.isNull() )
402  {
403  return false;
404  }
405 
406  //restore label specific properties
407 
408  //text
409  mText = itemElem.attribute( QStringLiteral( "labelText" ) );
410 
411  //html state
412  mHtmlState = itemElem.attribute( QStringLiteral( "htmlState" ) ).toInt();
413 
414  //margin
415  bool marginXOk = false;
416  bool marginYOk = false;
417  mMarginX = itemElem.attribute( QStringLiteral( "marginX" ) ).toDouble( &marginXOk );
418  mMarginY = itemElem.attribute( QStringLiteral( "marginY" ) ).toDouble( &marginYOk );
419  if ( !marginXOk || !marginYOk )
420  {
421  //upgrade old projects where margins where stored in a single attribute
422  double margin = itemElem.attribute( QStringLiteral( "margin" ), QStringLiteral( "1.0" ) ).toDouble();
423  mMarginX = margin;
424  mMarginY = margin;
425  }
426 
427  //Horizontal alignment
428  mHAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "halign" ) ).toInt() );
429 
430  //Vertical alignment
431  mVAlignment = static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "valign" ) ).toInt() );
432 
433  //font
434  QgsFontUtils::setFromXmlChildNode( mFont, itemElem, QStringLiteral( "LabelFont" ) );
435 
436  //font color
437  QDomNodeList fontColorList = itemElem.elementsByTagName( QStringLiteral( "FontColor" ) );
438  if ( !fontColorList.isEmpty() )
439  {
440  QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
441  int red = fontColorElem.attribute( QStringLiteral( "red" ), QStringLiteral( "0" ) ).toInt();
442  int green = fontColorElem.attribute( QStringLiteral( "green" ), QStringLiteral( "0" ) ).toInt();
443  int blue = fontColorElem.attribute( QStringLiteral( "blue" ), QStringLiteral( "0" ) ).toInt();
444  mFontColor = QColor( red, green, blue );
445  }
446  else
447  {
448  mFontColor = QColor( 0, 0, 0 );
449  }
450 
451  //restore general composer item properties
452  QDomNodeList composerItemList = itemElem.elementsByTagName( QStringLiteral( "ComposerItem" ) );
453  if ( !composerItemList.isEmpty() )
454  {
455  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
456 
457  //rotation
458  if ( !qgsDoubleNear( composerItemElem.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble(), 0.0 ) )
459  {
460  //check for old (pre 2.1) rotation attribute
461  setItemRotation( composerItemElem.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble() );
462  }
463 
464  _readXml( composerItemElem, doc );
465  }
466  emit itemChanged();
467  contentChanged();
468  return true;
469 }
470 
472 {
473  if ( !id().isEmpty() )
474  {
475  return id();
476  }
477 
478  if ( mHtmlState )
479  {
480  return tr( "<HTML label>" );
481  }
482 
483  //if no id, default to portion of label text
484  QString text = mText;
485  if ( text.isEmpty() )
486  {
487  return tr( "<label>" );
488  }
489  if ( text.length() > 25 )
490  {
491  return QString( tr( "%1..." ) ).arg( text.left( 25 ).simplified() );
492  }
493  else
494  {
495  return text.simplified();
496  }
497 }
498 
500 {
501  QRectF rectangle = rect();
502  double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
503  rectangle.adjust( -penWidth, -penWidth, penWidth, penWidth );
504 
505  if ( mMarginX < 0 )
506  {
507  rectangle.adjust( mMarginX, 0, -mMarginX, 0 );
508  }
509  if ( mMarginY < 0 )
510  {
511  rectangle.adjust( 0, mMarginY, 0, -mMarginY );
512  }
513 
514  return rectangle;
515 }
516 
518 {
520  prepareGeometryChange();
521 }
522 
523 void QgsComposerLabel::setFrameStrokeWidth( const double strokeWidth )
524 {
526  prepareGeometryChange();
527 }
528 
529 void QgsComposerLabel::itemShiftAdjustSize( double newWidth, double newHeight, double& xShift, double& yShift ) const
530 {
531  //keep alignment point constant
532  double currentWidth = rect().width();
533  double currentHeight = rect().height();
534  xShift = 0;
535  yShift = 0;
536 
537  if ( mItemRotation >= 0 && mItemRotation < 90 )
538  {
539  if ( mHAlignment == Qt::AlignHCenter )
540  {
541  xShift = - ( newWidth - currentWidth ) / 2.0;
542  }
543  else if ( mHAlignment == Qt::AlignRight )
544  {
545  xShift = - ( newWidth - currentWidth );
546  }
547  if ( mVAlignment == Qt::AlignVCenter )
548  {
549  yShift = -( newHeight - currentHeight ) / 2.0;
550  }
551  else if ( mVAlignment == Qt::AlignBottom )
552  {
553  yShift = - ( newHeight - currentHeight );
554  }
555  }
556  if ( mItemRotation >= 90 && mItemRotation < 180 )
557  {
558  if ( mHAlignment == Qt::AlignHCenter )
559  {
560  yShift = -( newHeight - currentHeight ) / 2.0;
561  }
562  else if ( mHAlignment == Qt::AlignRight )
563  {
564  yShift = -( newHeight - currentHeight );
565  }
566  if ( mVAlignment == Qt::AlignTop )
567  {
568  xShift = -( newWidth - currentWidth );
569  }
570  else if ( mVAlignment == Qt::AlignVCenter )
571  {
572  xShift = -( newWidth - currentWidth / 2.0 );
573  }
574  }
575  else if ( mItemRotation >= 180 && mItemRotation < 270 )
576  {
577  if ( mHAlignment == Qt::AlignHCenter )
578  {
579  xShift = -( newWidth - currentWidth ) / 2.0;
580  }
581  else if ( mHAlignment == Qt::AlignLeft )
582  {
583  xShift = -( newWidth - currentWidth );
584  }
585  if ( mVAlignment == Qt::AlignVCenter )
586  {
587  yShift = ( newHeight - currentHeight ) / 2.0;
588  }
589  else if ( mVAlignment == Qt::AlignTop )
590  {
591  yShift = ( newHeight - currentHeight );
592  }
593  }
594  else if ( mItemRotation >= 270 && mItemRotation < 360 )
595  {
596  if ( mHAlignment == Qt::AlignHCenter )
597  {
598  yShift = -( newHeight - currentHeight ) / 2.0;
599  }
600  else if ( mHAlignment == Qt::AlignLeft )
601  {
602  yShift = -( newHeight - currentHeight );
603  }
604  if ( mVAlignment == Qt::AlignBottom )
605  {
606  xShift = -( newWidth - currentWidth );
607  }
608  else if ( mVAlignment == Qt::AlignVCenter )
609  {
610  xShift = -( newWidth - currentWidth / 2.0 );
611  }
612  }
613 }
614 
615 QUrl QgsComposerLabel::createStylesheetUrl() const
616 {
617  QString stylesheet;
618  stylesheet += QStringLiteral( "body { margin: %1 %2;" ).arg( qMax( mMarginY * mHtmlUnitsToMM, 0.0 ) ).arg( qMax( mMarginX * mHtmlUnitsToMM, 0.0 ) );
619  stylesheet += QgsFontUtils::asCSS( mFont, 0.352778 * mHtmlUnitsToMM );
620  stylesheet += QStringLiteral( "color: %1;" ).arg( mFontColor.name() );
621  stylesheet += QStringLiteral( "text-align: %1; }" ).arg( mHAlignment == Qt::AlignLeft ? "left" : mHAlignment == Qt::AlignRight ? "right" : "center" );
622 
623  QByteArray ba;
624  ba.append( stylesheet.toUtf8() );
625  QUrl cssFileURL = QUrl( "data:text/css;charset=utf-8;base64," + ba.toBase64() );
626 
627  return cssFileURL;
628 }
bool _writeXml(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document. Usually called from writeXml methods of ...
void setHtmlState(int state)
virtual void setFrameStrokeWidth(const double strokeWidth)
Sets frame stroke width.
QgsComposerModel * itemsModel()
Returns the items model attached to the composition.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QgsFields fields() const
Returns the list of fields of this layer.
long srsid() const
Returns the internal CRS ID, if available.
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)
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 ...
bool enabled() const
Returns whether the atlas generation is enabled.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used for rendering the map.
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:136
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:198
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.
QFont font() const
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
int printResolution() const
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsComposerMap * referenceMap() const
Returns the map item which will be used to generate corresponding world files when the composition is...
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 shouldDrawItem() const
Returns whether the item should be drawn in the current context.
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.
Object representing map window.
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.
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.
bool hasFrame() const
Whether this item has a frame or not.
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)
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.
QgsCoordinateReferenceSystem crs() const
Returns the layer&#39;s spatial reference system.
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
QFileInfo fileInfo() const
Returns QFileInfo object for the project&#39;s associated file.
Definition: qgsproject.cpp:413
Represents a vector layer which manages a vector based data sets.
QString displayText() const
Returns the text as it appears on screen (with replaced data field)
QString ellipsoid() const
Returns a proj string representing the project&#39;s ellipsoid setting, e.g., "WGS84".
Definition: qgsproject.cpp:437
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log...
Definition: qgswebpage.h:208
double mItemRotation
Item rotation in degrees, clockwise.
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.
QgsProject * project() const
The project associated with the composition.
virtual QString displayName() const override
Get item display name.
QString id() const
Get item&#39;s id (which is not necessarly unique)