QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgslayoututils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoututils.cpp
3  ------------------
4  begin : July 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 "qgslayoututils.h"
19 #include "qgslayout.h"
20 #include "qgsrendercontext.h"
21 #include "qgslayoutitemmap.h"
22 #include <QStyleOptionGraphicsItem>
23 #include <QPainter>
24 #include <cmath>
25 
26 #ifndef M_DEG2RAD
27 #define M_DEG2RAD 0.0174532925
28 #endif
29 
30 void QgsLayoutUtils::rotate( double angle, double &x, double &y )
31 {
32  double rotToRad = angle * M_PI / 180.0;
33  double xRot, yRot;
34  xRot = x * std::cos( rotToRad ) - y * std::sin( rotToRad );
35  yRot = x * std::sin( rotToRad ) + y * std::cos( rotToRad );
36  x = xRot;
37  y = yRot;
38 }
39 
40 double QgsLayoutUtils::normalizedAngle( const double angle, const bool allowNegative )
41 {
42  double clippedAngle = angle;
43  if ( clippedAngle >= 360.0 || clippedAngle <= -360.0 )
44  {
45  clippedAngle = std::fmod( clippedAngle, 360.0 );
46  }
47  if ( !allowNegative && clippedAngle < 0.0 )
48  {
49  clippedAngle += 360.0;
50  }
51  return clippedAngle;
52 }
53 
55 {
56  //normalize angle to 0-360 degrees
57  double clippedAngle = normalizedAngle( angle );
58 
59  //snap angle to 45 degree
60  if ( clippedAngle >= 22.5 && clippedAngle < 67.5 )
61  {
62  return 45.0;
63  }
64  else if ( clippedAngle >= 67.5 && clippedAngle < 112.5 )
65  {
66  return 90.0;
67  }
68  else if ( clippedAngle >= 112.5 && clippedAngle < 157.5 )
69  {
70  return 135.0;
71  }
72  else if ( clippedAngle >= 157.5 && clippedAngle < 202.5 )
73  {
74  return 180.0;
75  }
76  else if ( clippedAngle >= 202.5 && clippedAngle < 247.5 )
77  {
78  return 225.0;
79  }
80  else if ( clippedAngle >= 247.5 && clippedAngle < 292.5 )
81  {
82  return 270.0;
83  }
84  else if ( clippedAngle >= 292.5 && clippedAngle < 337.5 )
85  {
86  return 315.0;
87  }
88  else
89  {
90  return 0.0;
91  }
92 }
93 
95 {
96  if ( !map )
97  {
98  QgsRenderContext context;
99  context.setPainter( painter );
100  if ( dpi < 0 && painter && painter->device() )
101  {
102  context.setScaleFactor( painter->device()->logicalDpiX() / 25.4 );
103  }
104  else if ( dpi > 0 )
105  {
106  context.setScaleFactor( dpi / 25.4 );
107  }
108  else
109  {
110  context.setScaleFactor( 3.465 ); //assume 88 dpi as standard value
111  }
112  return context;
113  }
114  else
115  {
116  // default to 88 dpi if no painter specified
117  if ( dpi < 0 )
118  {
119  dpi = ( painter && painter->device() ) ? painter->device()->logicalDpiX() : 88;
120  }
121  double dotsPerMM = dpi / 25.4;
122 
123  // get map settings from reference map
124  QgsRectangle extent = map->extent();
125  QSizeF mapSizeLayoutUnits = map->rect().size();
126  QSizeF mapSizeMM = map->layout()->convertFromLayoutUnits( mapSizeLayoutUnits, QgsUnitTypes::LayoutMillimeters ).toQSizeF();
127  QgsMapSettings ms = map->mapSettings( extent, mapSizeMM * dotsPerMM, dpi, false );
129  if ( painter )
130  context.setPainter( painter );
131 
132  context.setFlags( map->layout()->renderContext().renderContextFlags() );
134  return context;
135  }
136 }
137 
139 {
140  QgsLayoutItemMap *referenceMap = layout ? layout->referenceMap() : nullptr;
141  QgsRenderContext context = createRenderContextForMap( referenceMap, painter, dpi );
142  if ( layout )
143  {
144  context.setFlags( layout->renderContext().renderContextFlags() );
145  context.setTextRenderFormat( layout->renderContext().textRenderFormat() );
146  }
147 
148  return context;
149 }
150 
151 void QgsLayoutUtils::relativeResizeRect( QRectF &rectToResize, const QRectF &boundsBefore, const QRectF &boundsAfter )
152 {
153  //linearly scale rectToResize relative to the scaling from boundsBefore to boundsAfter
154  double left = relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
155  double right = relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
156  double top = relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
157  double bottom = relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
158 
159  rectToResize.setRect( left, top, right - left, bottom - top );
160 }
161 
162 double QgsLayoutUtils::relativePosition( const double position, const double beforeMin, const double beforeMax, const double afterMin, const double afterMax )
163 {
164  //calculate parameters for linear scale between before and after ranges
165  double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
166  double c = afterMin - ( beforeMin * m );
167 
168  //return linearly scaled position
169  return m * position + c;
170 }
171 QFont QgsLayoutUtils::scaledFontPixelSize( const QFont &font )
172 {
173  //upscale using FONT_WORKAROUND_SCALE
174  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
175  QFont scaledFont = font;
176  double pixelSize = pointsToMM( scaledFont.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
177  scaledFont.setPixelSize( pixelSize );
178  return scaledFont;
179 }
180 
181 double QgsLayoutUtils::fontAscentMM( const QFont &font )
182 {
183  //upscale using FONT_WORKAROUND_SCALE
184  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
185  QFont metricsFont = scaledFontPixelSize( font );
186  QFontMetricsF fontMetrics( metricsFont );
187  return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
188 }
189 
190 double QgsLayoutUtils::fontDescentMM( const QFont &font )
191 {
192  //upscale using FONT_WORKAROUND_SCALE
193  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
194  QFont metricsFont = scaledFontPixelSize( font );
195  QFontMetricsF fontMetrics( metricsFont );
196  return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
197 
198 }
199 
200 double QgsLayoutUtils::fontHeightMM( const QFont &font )
201 {
202  //upscale using FONT_WORKAROUND_SCALE
203  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
204  QFont metricsFont = scaledFontPixelSize( font );
205  QFontMetricsF fontMetrics( metricsFont );
206  return ( fontMetrics.height() / FONT_WORKAROUND_SCALE );
207 
208 }
209 
210 double QgsLayoutUtils::fontHeightCharacterMM( const QFont &font, QChar character )
211 {
212  //upscale using FONT_WORKAROUND_SCALE
213  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
214  QFont metricsFont = scaledFontPixelSize( font );
215  QFontMetricsF fontMetrics( metricsFont );
216  return ( fontMetrics.boundingRect( character ).height() / FONT_WORKAROUND_SCALE );
217 }
218 
219 double QgsLayoutUtils::textWidthMM( const QFont &font, const QString &text )
220 {
221  //upscale using FONT_WORKAROUND_SCALE
222  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
223 
224  const QStringList multiLineSplit = text.split( '\n' );
225  QFont metricsFont = scaledFontPixelSize( font );
226  QFontMetricsF fontMetrics( metricsFont );
227 
228  double maxWidth = 0;
229  for ( const QString &line : multiLineSplit )
230  {
231 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
232  maxWidth = std::max( maxWidth, ( fontMetrics.width( line ) / FONT_WORKAROUND_SCALE ) );
233 #else
234  maxWidth = std::max( maxWidth, ( fontMetrics.horizontalAdvance( line ) / FONT_WORKAROUND_SCALE ) );
235 #endif
236  }
237  return maxWidth;
238 }
239 
240 double QgsLayoutUtils::textHeightMM( const QFont &font, const QString &text, double multiLineHeight )
241 {
242  QStringList multiLineSplit = text.split( '\n' );
243  int lines = multiLineSplit.size();
244 
245  //upscale using FONT_WORKAROUND_SCALE
246  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
247  QFont metricsFont = scaledFontPixelSize( font );
248  QFontMetricsF fontMetrics( metricsFont );
249 
250  double fontHeight = fontMetrics.ascent() + fontMetrics.descent(); // ignore +1 for baseline
251  double textHeight = fontMetrics.ascent() + static_cast< double >( ( lines - 1 ) * fontHeight * multiLineHeight );
252 
253  return textHeight / FONT_WORKAROUND_SCALE;
254 }
255 
256 void QgsLayoutUtils::drawText( QPainter *painter, QPointF position, const QString &text, const QFont &font, const QColor &color )
257 {
258  if ( !painter )
259  {
260  return;
261  }
262 
263  //upscale using FONT_WORKAROUND_SCALE
264  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
265  QFont textFont = scaledFontPixelSize( font );
266 
267  QgsScopedQPainterState painterState( painter );
268  painter->setFont( textFont );
269  if ( color.isValid() )
270  {
271  painter->setPen( color );
272  }
273  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
274  painter->scale( scaleFactor, scaleFactor );
275  painter->drawText( position * FONT_WORKAROUND_SCALE, text );
276 }
277 
278 void QgsLayoutUtils::drawText( QPainter *painter, const QRectF &rect, const QString &text, const QFont &font, const QColor &color, const Qt::AlignmentFlag halignment, const Qt::AlignmentFlag valignment, const int flags )
279 {
280  if ( !painter )
281  {
282  return;
283  }
284 
285  //upscale using FONT_WORKAROUND_SCALE
286  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
287  QFont textFont = scaledFontPixelSize( font );
288 
289  QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
290  rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
291 
292  QgsScopedQPainterState painterState( painter );
293  painter->setFont( textFont );
294  if ( color.isValid() )
295  {
296  painter->setPen( color );
297  }
298  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
299  painter->scale( scaleFactor, scaleFactor );
300  painter->drawText( scaledRect, halignment | valignment | flags, text );
301 }
302 
303 QRectF QgsLayoutUtils::largestRotatedRectWithinBounds( const QRectF &originalRect, const QRectF &boundsRect, const double rotation )
304 {
305  double originalWidth = originalRect.width();
306  double originalHeight = originalRect.height();
307  double boundsWidth = boundsRect.width();
308  double boundsHeight = boundsRect.height();
309  double ratioBoundsRect = boundsWidth / boundsHeight;
310 
311  double clippedRotation = normalizedAngle( rotation );
312 
313  //shortcut for some rotation values
314  if ( qgsDoubleNear( clippedRotation, 0.0 ) || qgsDoubleNear( clippedRotation, 90.0 ) || qgsDoubleNear( clippedRotation, 180.0 ) || qgsDoubleNear( clippedRotation, 270.0 ) )
315  {
316  double rectScale;
317  if ( qgsDoubleNear( clippedRotation, 0.0 ) || qgsDoubleNear( clippedRotation, 180.0 ) )
318  {
319  rectScale = ( ( originalWidth / originalHeight ) > ratioBoundsRect ) ? boundsWidth / originalWidth : boundsHeight / originalHeight;
320  }
321  else
322  {
323  rectScale = ( ( originalHeight / originalWidth ) > ratioBoundsRect ) ? boundsWidth / originalHeight : boundsHeight / originalWidth;
324  }
325  double rectScaledWidth = rectScale * originalWidth;
326  double rectScaledHeight = rectScale * originalHeight;
327 
328  if ( qgsDoubleNear( clippedRotation, 0.0 ) || qgsDoubleNear( clippedRotation, 180.0 ) )
329  {
330  return QRectF( ( boundsWidth - rectScaledWidth ) / 2.0, ( boundsHeight - rectScaledHeight ) / 2.0, rectScaledWidth, rectScaledHeight );
331  }
332  else
333  {
334  return QRectF( ( boundsWidth - rectScaledHeight ) / 2.0, ( boundsHeight - rectScaledWidth ) / 2.0, rectScaledWidth, rectScaledHeight );
335  }
336  }
337 
338  //convert angle to radians and flip
339  double angleRad = -clippedRotation * M_DEG2RAD;
340  double cosAngle = std::cos( angleRad );
341  double sinAngle = std::sin( angleRad );
342 
343  //calculate size of bounds of rotated rectangle
344  double widthBoundsRotatedRect = originalWidth * std::fabs( cosAngle ) + originalHeight * std::fabs( sinAngle );
345  double heightBoundsRotatedRect = originalHeight * std::fabs( cosAngle ) + originalWidth * std::fabs( sinAngle );
346 
347  //compare ratio of rotated rect with bounds rect and calculate scaling of rotated
348  //rect to fit within bounds
349  double ratioBoundsRotatedRect = widthBoundsRotatedRect / heightBoundsRotatedRect;
350  double rectScale = ratioBoundsRotatedRect > ratioBoundsRect ? boundsWidth / widthBoundsRotatedRect : boundsHeight / heightBoundsRotatedRect;
351  double rectScaledWidth = rectScale * originalWidth;
352  double rectScaledHeight = rectScale * originalHeight;
353 
354  //now calculate offset so that rotated rectangle is centered within bounds
355  //first calculate min x and y coordinates
356  double currentCornerX = 0;
357  double minX = 0;
358  currentCornerX += rectScaledWidth * cosAngle;
359  minX = minX < currentCornerX ? minX : currentCornerX;
360  currentCornerX += rectScaledHeight * sinAngle;
361  minX = minX < currentCornerX ? minX : currentCornerX;
362  currentCornerX -= rectScaledWidth * cosAngle;
363  minX = minX < currentCornerX ? minX : currentCornerX;
364 
365  double currentCornerY = 0;
366  double minY = 0;
367  currentCornerY -= rectScaledWidth * sinAngle;
368  minY = minY < currentCornerY ? minY : currentCornerY;
369  currentCornerY += rectScaledHeight * cosAngle;
370  minY = minY < currentCornerY ? minY : currentCornerY;
371  currentCornerY += rectScaledWidth * sinAngle;
372  minY = minY < currentCornerY ? minY : currentCornerY;
373 
374  //now calculate offset position of rotated rectangle
375  double offsetX = ratioBoundsRotatedRect > ratioBoundsRect ? 0 : ( boundsWidth - rectScale * widthBoundsRotatedRect ) / 2.0;
376  offsetX += std::fabs( minX );
377  double offsetY = ratioBoundsRotatedRect > ratioBoundsRect ? ( boundsHeight - rectScale * heightBoundsRotatedRect ) / 2.0 : 0;
378  offsetY += std::fabs( minY );
379 
380  return QRectF( offsetX, offsetY, rectScaledWidth, rectScaledHeight );
381 }
382 
384 {
385  QString s = string.trimmed();
386  if ( s.compare( QLatin1String( "Portrait" ), Qt::CaseInsensitive ) == 0 )
387  {
388  ok = true;
390  }
391  else if ( s.compare( QLatin1String( "Landscape" ), Qt::CaseInsensitive ) == 0 )
392  {
393  ok = true;
395  }
396  ok = false;
397  return QgsLayoutItemPage::Landscape; // default to landscape
398 }
399 
400 double QgsLayoutUtils::scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style )
401 {
402  // workaround Qt bug 66185
403 
404  // Refs #18027 - if a QGraphicsItem is rotated by 90 or 270 degrees, then the item
405  // style given to QGraphicsItem::paint incorrectly uses the shear parameter of the matrix (m12)
406  // to store the current view scale, instead of the horizontal scale parameter (m11) which
407  // is used in all other cases
408 
409  // TODO - ifdef this out if Qt fixes upstream
410  return !qgsDoubleNear( style->matrix.m11(), 0.0 ) ? style->matrix.m11() : style->matrix.m12();
411 }
412 
413 QgsMapLayer *QgsLayoutUtils::mapLayerFromString( const QString &string, QgsProject *project )
414 {
415  // Maybe it's a layer id?
416  if ( QgsMapLayer *ml = project->mapLayer( string ) )
417  return ml;
418 
419  // Still nothing? Check for layer name
420  if ( QgsMapLayer *ml = project->mapLayersByName( string ).value( 0 ) )
421  return ml;
422 
423  // Still nothing? Check for layer name, case-insensitive
424  const auto layers = project->mapLayers();
425  for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
426  {
427  if ( it.value()->name().compare( string, Qt::CaseInsensitive ) == 0 )
428  return it.value();
429  }
430 
431  return nullptr;
432 }
433 
434 // nextNiceNumber(4573.23, d) = 5000 (d=1) -> 4600 (d=10) -> 4580 (d=100) -> 4574 (d=1000) -> etc
435 inline double nextNiceNumber( double a, double d = 1 )
436 {
437  double s = std::pow( 10.0, std::floor( std::log10( a ) ) ) / d;
438  return std::ceil( a / s ) * s;
439 }
440 
441 // prevNiceNumber(4573.23, d) = 4000 (d=1) -> 4500 (d=10) -> 4570 (d=100) -> 4573 (d=1000) -> etc
442 inline double prevNiceNumber( double a, double d = 1 )
443 {
444  double s = std::pow( 10.0, std::floor( std::log10( a ) ) ) / d;
445  return std::floor( a / s ) * s;
446 }
447 
448 double QgsLayoutUtils::calculatePrettySize( const double minimumSize, const double maximumSize )
449 {
450  if ( maximumSize < minimumSize )
451  {
452  return 0;
453  }
454  else
455  {
456  // Start with coarsest "nice" number closest to minimumSize resp
457  // maximumSize, then proceed to finer numbers as long as neither
458  // lowerNiceUnitsPerSeg nor upperNiceUnitsPerSeg are in
459  // [minimumSize, maximumSize]
460  double lowerNiceUnitsPerSeg = nextNiceNumber( minimumSize );
461  double upperNiceUnitsPerSeg = prevNiceNumber( maximumSize );
462 
463  double d = 1;
464  while ( lowerNiceUnitsPerSeg > maximumSize && upperNiceUnitsPerSeg < minimumSize )
465  {
466  d *= 10;
467  lowerNiceUnitsPerSeg = nextNiceNumber( minimumSize, d );
468  upperNiceUnitsPerSeg = prevNiceNumber( maximumSize, d );
469  }
470 
471  // Pick size from {lowerNiceUnitsPerSeg, upperNiceUnitsPerSeg}, use the larger if possible
472  return upperNiceUnitsPerSeg < minimumSize ? lowerNiceUnitsPerSeg : upperNiceUnitsPerSeg;
473  }
474 }
475 
477 {
478  if ( !( item->itemFlags() & QgsLayoutItem::FlagProvidesClipPath ) )
479  return false; // not a clipping provider, so shortcut out
480 
481  // current only maps can be clipped
482  QList< QgsLayoutItemMap * > maps;
483  item->layout()->layoutItems( maps );
484  for ( QgsLayoutItemMap *map : qgis::as_const( maps ) )
485  {
486  if ( map->itemClippingSettings()->isActive() && map->itemClippingSettings()->sourceItem() == item )
487  return true;
488  }
489  return false;
490 }
491 
492 double QgsLayoutUtils::pointsToMM( const double pointSize )
493 {
494  //conversion to mm based on 1 point = 1/72 inch
495  return ( pointSize * 0.3527 );
496 }
497 
498 double QgsLayoutUtils::mmToPoints( const double mmSize )
499 {
500  //conversion to points based on 1 point = 1/72 inch
501  return ( mmSize / 0.3527 );
502 }
QgsLayoutUtils::relativePosition
static double relativePosition(double position, double beforeMin, double beforeMax, double afterMin, double afterMax)
Returns a scaled position given a before and after range.
Definition: qgslayoututils.cpp:162
QgsLayoutObject::layout
const QgsLayout * layout() const
Returns the layout the object is attached to.
Definition: qgslayoutobject.cpp:126
QgsLayout::referenceMap
QgsLayoutItemMap * referenceMap() const
Returns the map item which will be used to generate corresponding world files when the layout is expo...
Definition: qgslayout.cpp:430
QgsLayoutUtils::textHeightMM
static double textHeightMM(const QFont &font, const QString &text, double multiLineHeight=1.0)
Calculate a font height in millimeters for a text string, including workarounds for QT font rendering...
Definition: qgslayoututils.cpp:240
QgsRenderContext::fromMapSettings
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
Definition: qgsrendercontext.cpp:197
QgsLayout::layoutItems
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
Definition: qgslayout.h:121
QgsProject::mapLayersByName
QList< QgsMapLayer * > mapLayersByName(const QString &layerName) const
Retrieve a list of matching registered layers by layer name.
Definition: qgsproject.cpp:3213
QgsProject::mapLayers
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
Definition: qgsproject.cpp:3436
QgsLayoutUtils::snappedAngle
static double snappedAngle(double angle)
Snaps an angle (in degrees) to its closest 45 degree angle.
Definition: qgslayoututils.cpp:54
QgsLayoutUtils::fontDescentMM
static double fontDescentMM(const QFont &font)
Calculate a font descent in millimeters, including workarounds for QT font rendering issues.
Definition: qgslayoututils.cpp:190
QgsRenderContext::setPainter
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
Definition: qgsrendercontext.h:491
QgsLayoutRenderContext::textRenderFormat
QgsRenderContext::TextRenderFormat textRenderFormat() const
Returns the text render format, which dictates how text is rendered (e.g.
Definition: qgslayoutrendercontext.h:214
QgsLayoutItemMap::mapSettings
QgsMapSettings mapSettings(const QgsRectangle &extent, QSizeF size, double dpi, bool includeLayerSettings) const
Returns map settings that will be used for drawing of the map.
Definition: qgslayoutitemmap.cpp:1458
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsProject::mapLayer
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Definition: qgsproject.cpp:3208
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:95
QgsLayoutItem::FlagProvidesClipPath
@ FlagProvidesClipPath
Item can act as a clipping path provider (see clipPath())
Definition: qgslayoutitem.h:304
qgslayoututils.h
QgsLayoutUtils::itemIsAClippingSource
static bool itemIsAClippingSource(const QgsLayoutItem *item)
Returns true if an item is a clipping item for another layout item.
Definition: qgslayoututils.cpp:476
prevNiceNumber
double prevNiceNumber(double a, double d=1)
Definition: qgslayoututils.cpp:442
QgsLayoutUtils::createRenderContextForLayout
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
Definition: qgslayoututils.cpp:138
QgsRenderContext::setFlags
void setFlags(QgsRenderContext::Flags flags)
Set combination of flags that will be used for rendering.
Definition: qgsrendercontext.cpp:174
QgsLayoutUtils::scaledFontPixelSize
static QFont scaledFontPixelSize(const QFont &font)
Returns a font where size is set in points and the size has been upscaled with FONT_WORKAROUND_SCALE ...
Definition: qgslayoututils.cpp:171
QgsLayoutUtils::calculatePrettySize
static double calculatePrettySize(double minimumSize, double maximumSize)
Calculates a "pretty" size which falls between the range [minimumSize, maximumSize].
Definition: qgslayoututils.cpp:448
nextNiceNumber
double nextNiceNumber(double a, double d=1)
Definition: qgslayoututils.cpp:435
QgsLayout::renderContext
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
Definition: qgslayout.cpp:359
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
qgsrendercontext.h
QgsLayoutItemMap::extent
QgsRectangle extent() const
Returns the current map extent.
Definition: qgslayoutitemmap.cpp:265
QgsLayoutItemPage::Landscape
@ Landscape
Landscape orientation.
Definition: qgslayoutitempage.h:65
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:113
QgsLayoutUtils::fontHeightCharacterMM
static double fontHeightCharacterMM(const QFont &font, QChar character)
Calculate a font height in millimeters of a single character, including workarounds for QT font rende...
Definition: qgslayoututils.cpp:210
QgsScopedQPainterState
Scoped object for saving and restoring a QPainter object's state.
Definition: qgsrendercontext.h:1120
QgsLayoutUtils::createRenderContextForMap
static QgsRenderContext createRenderContextForMap(QgsLayoutItemMap *map, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout map and painter destination.
Definition: qgslayoututils.cpp:94
qgslayout.h
QgsRenderContext::setTextRenderFormat
void setTextRenderFormat(TextRenderFormat format)
Sets the text render format, which dictates how text is rendered (e.g.
Definition: qgsrendercontext.h:706
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:318
QgsLayoutUtils::mapLayerFromString
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProject *project)
Resolves a string into a map layer from a given project.
Definition: qgslayoututils.cpp:413
M_DEG2RAD
#define M_DEG2RAD
Definition: qgslayoututils.cpp:27
QgsLayoutItem::itemFlags
virtual Flags itemFlags() const
Returns the item's flags, which indicate how the item behaves.
Definition: qgslayoutitem.cpp:129
QgsLayoutUtils::rotate
static void rotate(double angle, double &x, double &y)
Rotates a point / vector around the origin.
Definition: qgslayoututils.cpp:30
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
c
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Definition: porting_processing.dox:1
QgsLayoutItemPage::Orientation
Orientation
Page orientation.
Definition: qgslayoutitempage.h:63
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutItemPage::Portrait
@ Portrait
Portrait orientation.
Definition: qgslayoutitempage.h:64
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsLayoutUtils::largestRotatedRectWithinBounds
static QRectF largestRotatedRectWithinBounds(const QRectF &originalRect, const QRectF &boundsRect, double rotation)
Calculates the largest scaled version of originalRect which fits within boundsRect,...
Definition: qgslayoututils.cpp:303
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
QgsLayoutUtils::fontAscentMM
static double fontAscentMM(const QFont &font)
Calculates a font ascent in millimeters, including workarounds for QT font rendering issues.
Definition: qgslayoututils.cpp:181
QgsLayoutRenderContext::renderContextFlags
QgsRenderContext::Flags renderContextFlags() const
Returns the combination of render context flags matched to the layout context's settings.
Definition: qgslayoutrendercontext.cpp:62
QgsUnitTypes::LayoutMillimeters
@ LayoutMillimeters
Millimeters.
Definition: qgsunittypes.h:182
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
QgsLayoutUtils::scaleFactorFromItemStyle
static double scaleFactorFromItemStyle(const QStyleOptionGraphicsItem *style)
Extracts the scale factor from an item style.
Definition: qgslayoututils.cpp:400
MathUtils::angle
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
QgsRenderContext::setScaleFactor
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
Definition: qgsrendercontext.h:476
QgsLayoutUtils::normalizedAngle
static double normalizedAngle(double angle, bool allowNegative=false)
Ensures that an angle (in degrees) is in the range 0 <= angle < 360.
Definition: qgslayoututils.cpp:40
QgsLayoutUtils::relativeResizeRect
static void relativeResizeRect(QRectF &rectToResize, const QRectF &boundsBefore, const QRectF &boundsAfter)
Resizes a QRectF relative to a resized bounding rectangle.
Definition: qgslayoututils.cpp:151
QgsLayout::convertFromLayoutUnits
QgsLayoutMeasurement convertFromLayoutUnits(double length, QgsUnitTypes::LayoutUnit unit) const
Converts a length measurement from the layout's native units to a specified target unit.
Definition: qgslayout.cpp:344
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
QgsLayoutUtils::decodePaperOrientation
static QgsLayoutItemPage::Orientation decodePaperOrientation(const QString &string, bool &ok)
Decodes a string representing a paper orientation and returns the decoded orientation.
Definition: qgslayoututils.cpp:383