QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgslayoutitemmap.h"
21 #include "qgsprojectviewsettings.h"
22 #include "qgsrendercontext.h"
23 #include "qgssettings.h"
24 
25 #include <QStyleOptionGraphicsItem>
26 #include <QPainter>
27 #include <cmath>
28 
29 #ifndef M_DEG2RAD
30 #define M_DEG2RAD 0.0174532925
31 #endif
32 
33 void QgsLayoutUtils::rotate( double angle, double &x, double &y )
34 {
35  double rotToRad = angle * M_PI / 180.0;
36  double xRot, yRot;
37  xRot = x * std::cos( rotToRad ) - y * std::sin( rotToRad );
38  yRot = x * std::sin( rotToRad ) + y * std::cos( rotToRad );
39  x = xRot;
40  y = yRot;
41 }
42 
43 double QgsLayoutUtils::normalizedAngle( const double angle, const bool allowNegative )
44 {
45  double clippedAngle = angle;
46  if ( clippedAngle >= 360.0 || clippedAngle <= -360.0 )
47  {
48  clippedAngle = std::fmod( clippedAngle, 360.0 );
49  }
50  if ( !allowNegative && clippedAngle < 0.0 )
51  {
52  clippedAngle += 360.0;
53  }
54  return clippedAngle;
55 }
56 
58 {
59  //normalize angle to 0-360 degrees
60  double clippedAngle = normalizedAngle( angle );
61 
62  //snap angle to 45 degree
63  if ( clippedAngle >= 22.5 && clippedAngle < 67.5 )
64  {
65  return 45.0;
66  }
67  else if ( clippedAngle >= 67.5 && clippedAngle < 112.5 )
68  {
69  return 90.0;
70  }
71  else if ( clippedAngle >= 112.5 && clippedAngle < 157.5 )
72  {
73  return 135.0;
74  }
75  else if ( clippedAngle >= 157.5 && clippedAngle < 202.5 )
76  {
77  return 180.0;
78  }
79  else if ( clippedAngle >= 202.5 && clippedAngle < 247.5 )
80  {
81  return 225.0;
82  }
83  else if ( clippedAngle >= 247.5 && clippedAngle < 292.5 )
84  {
85  return 270.0;
86  }
87  else if ( clippedAngle >= 292.5 && clippedAngle < 337.5 )
88  {
89  return 315.0;
90  }
91  else
92  {
93  return 0.0;
94  }
95 }
96 
98 {
99  if ( !map )
100  {
101  QgsRenderContext context;
102  context.setPainter( painter );
103  if ( dpi < 0 && painter && painter->device() )
104  {
105  context.setScaleFactor( painter->device()->logicalDpiX() / 25.4 );
106  }
107  else if ( dpi > 0 )
108  {
109  context.setScaleFactor( dpi / 25.4 );
110  }
111  else
112  {
113  context.setScaleFactor( 3.465 ); //assume 88 dpi as standard value
114  }
115  return context;
116  }
117  else
118  {
119  // default to 88 dpi if no painter specified
120  if ( dpi < 0 )
121  {
122  dpi = ( painter && painter->device() ) ? painter->device()->logicalDpiX() : 88;
123  }
124  double dotsPerMM = dpi / 25.4;
125 
126  // get map settings from reference map
127  QgsRectangle extent = map->extent();
128  QSizeF mapSizeLayoutUnits = map->rect().size();
129  QSizeF mapSizeMM = map->layout()->convertFromLayoutUnits( mapSizeLayoutUnits, QgsUnitTypes::LayoutMillimeters ).toQSizeF();
130  QgsMapSettings ms = map->mapSettings( extent, mapSizeMM * dotsPerMM, dpi, false );
132  if ( painter )
133  context.setPainter( painter );
134 
135  context.setFlags( map->layout()->renderContext().renderContextFlags() );
137  return context;
138  }
139 }
140 
142 {
143  QgsLayoutItemMap *referenceMap = layout ? layout->referenceMap() : nullptr;
144  QgsRenderContext context = createRenderContextForMap( referenceMap, painter, dpi );
145  if ( layout )
146  {
147  context.setFlags( layout->renderContext().renderContextFlags() );
148  context.setTextRenderFormat( layout->renderContext().textRenderFormat() );
149  }
150 
151  return context;
152 }
153 
154 void QgsLayoutUtils::relativeResizeRect( QRectF &rectToResize, const QRectF &boundsBefore, const QRectF &boundsAfter )
155 {
156  //linearly scale rectToResize relative to the scaling from boundsBefore to boundsAfter
157  double left = relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
158  double right = relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
159  double top = relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
160  double bottom = relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
161 
162  rectToResize.setRect( left, top, right - left, bottom - top );
163 }
164 
165 double QgsLayoutUtils::relativePosition( const double position, const double beforeMin, const double beforeMax, const double afterMin, const double afterMax )
166 {
167  //calculate parameters for linear scale between before and after ranges
168  double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
169  double c = afterMin - ( beforeMin * m );
170 
171  //return linearly scaled position
172  return m * position + c;
173 }
174 QFont QgsLayoutUtils::scaledFontPixelSize( const QFont &font )
175 {
176  //upscale using FONT_WORKAROUND_SCALE
177  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
178  QFont scaledFont = font;
179  double pixelSize = pointsToMM( scaledFont.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
180  scaledFont.setPixelSize( pixelSize );
181  return scaledFont;
182 }
183 
184 double QgsLayoutUtils::fontAscentMM( const QFont &font )
185 {
186  //upscale using FONT_WORKAROUND_SCALE
187  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
188  QFont metricsFont = scaledFontPixelSize( font );
189  QFontMetricsF fontMetrics( metricsFont );
190  return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
191 }
192 
193 double QgsLayoutUtils::fontDescentMM( const QFont &font )
194 {
195  //upscale using FONT_WORKAROUND_SCALE
196  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
197  QFont metricsFont = scaledFontPixelSize( font );
198  QFontMetricsF fontMetrics( metricsFont );
199  return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
200 
201 }
202 
203 double QgsLayoutUtils::fontHeightMM( const QFont &font )
204 {
205  //upscale using FONT_WORKAROUND_SCALE
206  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
207  QFont metricsFont = scaledFontPixelSize( font );
208  QFontMetricsF fontMetrics( metricsFont );
209  return ( fontMetrics.height() / FONT_WORKAROUND_SCALE );
210 
211 }
212 
213 double QgsLayoutUtils::fontHeightCharacterMM( const QFont &font, QChar character )
214 {
215  //upscale using FONT_WORKAROUND_SCALE
216  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
217  QFont metricsFont = scaledFontPixelSize( font );
218  QFontMetricsF fontMetrics( metricsFont );
219  return ( fontMetrics.boundingRect( character ).height() / FONT_WORKAROUND_SCALE );
220 }
221 
222 double QgsLayoutUtils::textWidthMM( const QFont &font, const QString &text )
223 {
224  //upscale using FONT_WORKAROUND_SCALE
225  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
226 
227  const QStringList multiLineSplit = text.split( '\n' );
228  QFont metricsFont = scaledFontPixelSize( font );
229  QFontMetricsF fontMetrics( metricsFont );
230 
231  double maxWidth = 0;
232  for ( const QString &line : multiLineSplit )
233  {
234  maxWidth = std::max( maxWidth, ( fontMetrics.horizontalAdvance( line ) / FONT_WORKAROUND_SCALE ) );
235  }
236  return maxWidth;
237 }
238 
239 double QgsLayoutUtils::textHeightMM( const QFont &font, const QString &text, double multiLineHeight )
240 {
241  QStringList multiLineSplit = text.split( '\n' );
242  int lines = multiLineSplit.size();
243 
244  //upscale using FONT_WORKAROUND_SCALE
245  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
246  QFont metricsFont = scaledFontPixelSize( font );
247  QFontMetricsF fontMetrics( metricsFont );
248 
249  double fontHeight = fontMetrics.ascent() + fontMetrics.descent(); // ignore +1 for baseline
250  double textHeight = fontMetrics.ascent() + static_cast< double >( ( lines - 1 ) * fontHeight * multiLineHeight );
251 
252  return textHeight / FONT_WORKAROUND_SCALE;
253 }
254 
255 void QgsLayoutUtils::drawText( QPainter *painter, QPointF position, const QString &text, const QFont &font, const QColor &color )
256 {
257  if ( !painter )
258  {
259  return;
260  }
261 
262  //upscale using FONT_WORKAROUND_SCALE
263  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
264  QFont textFont = scaledFontPixelSize( font );
265 
266  QgsScopedQPainterState painterState( painter );
267  painter->setFont( textFont );
268  if ( color.isValid() )
269  {
270  painter->setPen( color );
271  }
272  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
273  painter->scale( scaleFactor, scaleFactor );
274  painter->drawText( position * FONT_WORKAROUND_SCALE, text );
275 }
276 
277 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 )
278 {
279  if ( !painter )
280  {
281  return;
282  }
283 
284  //upscale using FONT_WORKAROUND_SCALE
285  //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
286  QFont textFont = scaledFontPixelSize( font );
287 
288  QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
289  rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
290 
291  QgsScopedQPainterState painterState( painter );
292  painter->setFont( textFont );
293  if ( color.isValid() )
294  {
295  painter->setPen( color );
296  }
297  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
298  painter->scale( scaleFactor, scaleFactor );
299  painter->drawText( scaledRect, halignment | valignment | flags, text );
300 }
301 
302 QRectF QgsLayoutUtils::largestRotatedRectWithinBounds( const QRectF &originalRect, const QRectF &boundsRect, const double rotation )
303 {
304  double originalWidth = originalRect.width();
305  double originalHeight = originalRect.height();
306  double boundsWidth = boundsRect.width();
307  double boundsHeight = boundsRect.height();
308  double ratioBoundsRect = boundsWidth / boundsHeight;
309 
310  double clippedRotation = normalizedAngle( rotation );
311 
312  //shortcut for some rotation values
313  if ( qgsDoubleNear( clippedRotation, 0.0 ) || qgsDoubleNear( clippedRotation, 90.0 ) || qgsDoubleNear( clippedRotation, 180.0 ) || qgsDoubleNear( clippedRotation, 270.0 ) )
314  {
315  double rectScale;
316  if ( qgsDoubleNear( clippedRotation, 0.0 ) || qgsDoubleNear( clippedRotation, 180.0 ) )
317  {
318  rectScale = ( ( originalWidth / originalHeight ) > ratioBoundsRect ) ? boundsWidth / originalWidth : boundsHeight / originalHeight;
319  }
320  else
321  {
322  rectScale = ( ( originalHeight / originalWidth ) > ratioBoundsRect ) ? boundsWidth / originalHeight : boundsHeight / originalWidth;
323  }
324  double rectScaledWidth = rectScale * originalWidth;
325  double rectScaledHeight = rectScale * originalHeight;
326 
327  if ( qgsDoubleNear( clippedRotation, 0.0 ) || qgsDoubleNear( clippedRotation, 180.0 ) )
328  {
329  return QRectF( ( boundsWidth - rectScaledWidth ) / 2.0, ( boundsHeight - rectScaledHeight ) / 2.0, rectScaledWidth, rectScaledHeight );
330  }
331  else
332  {
333  return QRectF( ( boundsWidth - rectScaledHeight ) / 2.0, ( boundsHeight - rectScaledWidth ) / 2.0, rectScaledWidth, rectScaledHeight );
334  }
335  }
336 
337  //convert angle to radians and flip
338  double angleRad = -clippedRotation * M_DEG2RAD;
339  double cosAngle = std::cos( angleRad );
340  double sinAngle = std::sin( angleRad );
341 
342  //calculate size of bounds of rotated rectangle
343  double widthBoundsRotatedRect = originalWidth * std::fabs( cosAngle ) + originalHeight * std::fabs( sinAngle );
344  double heightBoundsRotatedRect = originalHeight * std::fabs( cosAngle ) + originalWidth * std::fabs( sinAngle );
345 
346  //compare ratio of rotated rect with bounds rect and calculate scaling of rotated
347  //rect to fit within bounds
348  double ratioBoundsRotatedRect = widthBoundsRotatedRect / heightBoundsRotatedRect;
349  double rectScale = ratioBoundsRotatedRect > ratioBoundsRect ? boundsWidth / widthBoundsRotatedRect : boundsHeight / heightBoundsRotatedRect;
350  double rectScaledWidth = rectScale * originalWidth;
351  double rectScaledHeight = rectScale * originalHeight;
352 
353  //now calculate offset so that rotated rectangle is centered within bounds
354  //first calculate min x and y coordinates
355  double currentCornerX = 0;
356  double minX = 0;
357  currentCornerX += rectScaledWidth * cosAngle;
358  minX = minX < currentCornerX ? minX : currentCornerX;
359  currentCornerX += rectScaledHeight * sinAngle;
360  minX = minX < currentCornerX ? minX : currentCornerX;
361  currentCornerX -= rectScaledWidth * cosAngle;
362  minX = minX < currentCornerX ? minX : currentCornerX;
363 
364  double currentCornerY = 0;
365  double minY = 0;
366  currentCornerY -= rectScaledWidth * sinAngle;
367  minY = minY < currentCornerY ? minY : currentCornerY;
368  currentCornerY += rectScaledHeight * cosAngle;
369  minY = minY < currentCornerY ? minY : currentCornerY;
370  currentCornerY += rectScaledWidth * sinAngle;
371  minY = minY < currentCornerY ? minY : currentCornerY;
372 
373  //now calculate offset position of rotated rectangle
374  double offsetX = ratioBoundsRotatedRect > ratioBoundsRect ? 0 : ( boundsWidth - rectScale * widthBoundsRotatedRect ) / 2.0;
375  offsetX += std::fabs( minX );
376  double offsetY = ratioBoundsRotatedRect > ratioBoundsRect ? ( boundsHeight - rectScale * heightBoundsRotatedRect ) / 2.0 : 0;
377  offsetY += std::fabs( minY );
378 
379  return QRectF( offsetX, offsetY, rectScaledWidth, rectScaledHeight );
380 }
381 
383 {
384  QString s = string.trimmed();
385  if ( s.compare( QLatin1String( "Portrait" ), Qt::CaseInsensitive ) == 0 )
386  {
387  ok = true;
389  }
390  else if ( s.compare( QLatin1String( "Landscape" ), Qt::CaseInsensitive ) == 0 )
391  {
392  ok = true;
394  }
395  ok = false;
396  return QgsLayoutItemPage::Landscape; // default to landscape
397 }
398 
399 double QgsLayoutUtils::scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style )
400 {
401 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
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 #else
412  Q_UNUSED( style )
413  return 1;
414 #endif
415 }
416 
417 double QgsLayoutUtils::scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style, QPainter *painter )
418 {
419  Q_UNUSED( style );
420  return QStyleOptionGraphicsItem::levelOfDetailFromTransform( painter->worldTransform() );
421 }
422 
423 QgsMapLayer *QgsLayoutUtils::mapLayerFromString( const QString &string, QgsProject *project )
424 {
425  // Maybe it's a layer id?
426  if ( QgsMapLayer *ml = project->mapLayer( string ) )
427  return ml;
428 
429  // Still nothing? Check for layer name
430  if ( QgsMapLayer *ml = project->mapLayersByName( string ).value( 0 ) )
431  return ml;
432 
433  // Still nothing? Check for layer name, case-insensitive
434  const auto layers = project->mapLayers();
435  for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
436  {
437  if ( it.value()->name().compare( string, Qt::CaseInsensitive ) == 0 )
438  return it.value();
439  }
440 
441  return nullptr;
442 }
443 
444 // nextNiceNumber(4573.23, d) = 5000 (d=1) -> 4600 (d=10) -> 4580 (d=100) -> 4574 (d=1000) -> etc
445 inline double nextNiceNumber( double a, double d = 1 )
446 {
447  double s = std::pow( 10.0, std::floor( std::log10( a ) ) ) / d;
448  return std::ceil( a / s ) * s;
449 }
450 
451 // prevNiceNumber(4573.23, d) = 4000 (d=1) -> 4500 (d=10) -> 4570 (d=100) -> 4573 (d=1000) -> etc
452 inline double prevNiceNumber( double a, double d = 1 )
453 {
454  double s = std::pow( 10.0, std::floor( std::log10( a ) ) ) / d;
455  return std::floor( a / s ) * s;
456 }
457 
458 double QgsLayoutUtils::calculatePrettySize( const double minimumSize, const double maximumSize )
459 {
460  if ( maximumSize < minimumSize )
461  {
462  return 0;
463  }
464  else
465  {
466  // Start with coarsest "nice" number closest to minimumSize resp
467  // maximumSize, then proceed to finer numbers as long as neither
468  // lowerNiceUnitsPerSeg nor upperNiceUnitsPerSeg are in
469  // [minimumSize, maximumSize]
470  double lowerNiceUnitsPerSeg = nextNiceNumber( minimumSize );
471  double upperNiceUnitsPerSeg = prevNiceNumber( maximumSize );
472 
473  double d = 1;
474  while ( lowerNiceUnitsPerSeg > maximumSize && upperNiceUnitsPerSeg < minimumSize )
475  {
476  d *= 10;
477  lowerNiceUnitsPerSeg = nextNiceNumber( minimumSize, d );
478  upperNiceUnitsPerSeg = prevNiceNumber( maximumSize, d );
479  }
480 
481  // Pick size from {lowerNiceUnitsPerSeg, upperNiceUnitsPerSeg}, use the larger if possible
482  return upperNiceUnitsPerSeg < minimumSize ? lowerNiceUnitsPerSeg : upperNiceUnitsPerSeg;
483  }
484 }
485 
487 {
488  if ( !( item->itemFlags() & QgsLayoutItem::FlagProvidesClipPath ) )
489  return false; // not a clipping provider, so shortcut out
490 
491  // current only maps can be clipped
492  QList< QgsLayoutItemMap * > maps;
493  item->layout()->layoutItems( maps );
494  for ( QgsLayoutItemMap *map : std::as_const( maps ) )
495  {
496  if ( map->itemClippingSettings()->isActive() && map->itemClippingSettings()->sourceItem() == item )
497  return true;
498  }
499  return false;
500 }
501 
502 double QgsLayoutUtils::pointsToMM( const double pointSize )
503 {
504  //conversion to mm based on 1 point = 1/72 inch
505  return ( pointSize * 0.3527 );
506 }
507 
508 double QgsLayoutUtils::mmToPoints( const double mmSize )
509 {
510  //conversion to points based on 1 point = 1/72 inch
511  return ( mmSize / 0.3527 );
512 }
513 
514 QVector< double > QgsLayoutUtils::predefinedScales( const QgsLayout *layout )
515 {
516  QgsProject *lProject = layout ? layout->project() : nullptr;
517  QVector< double > mapScales;
518  if ( lProject )
519  mapScales = lProject->viewSettings()->mapScales();
520 
521  bool hasProjectScales( lProject ? lProject->viewSettings()->useProjectScales() : false );
522  if ( !hasProjectScales || mapScales.isEmpty() )
523  {
524  // default to global map tool scales
525  QgsSettings settings;
526  QString scalesStr( settings.value( QStringLiteral( "Map/scales" ), Qgis::defaultProjectScales() ).toString() );
527  const QStringList scales = scalesStr.split( ',' );
528  for ( const QString &scale : scales )
529  {
530  QStringList parts( scale.split( ':' ) );
531  if ( parts.size() == 2 )
532  {
533  mapScales.push_back( parts[1].toDouble() );
534  }
535  }
536  }
537 
538  return mapScales;
539 }
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:165
QgsLayoutObject::layout
const QgsLayout * layout() const
Returns the layout the object is attached to.
Definition: qgslayoutobject.cpp:216
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
QgsRenderContext::setFlags
void setFlags(Qgis::RenderContextFlags flags)
Set combination of flags that will be used for rendering.
Definition: qgsrendercontext.cpp:211
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:239
QgsProject::viewSettings
const QgsProjectViewSettings * viewSettings() const
Returns the project's view settings, which contains settings and properties relating to how a QgsProj...
Definition: qgsproject.cpp:3491
QgsLayoutUtils::predefinedScales
static QVector< double > predefinedScales(const QgsLayout *layout)
Returns a list of predefined scales associated with a layout.
Definition: qgslayoututils.cpp:514
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:161
QgsRenderContext::fromMapSettings
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
Definition: qgsrendercontext.cpp:234
QgsLayout::layoutItems
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
Definition: qgslayout.h:122
QgsProject::mapLayersByName
QList< QgsMapLayer * > mapLayersByName(const QString &layerName) const
Retrieve a list of matching registered layers by layer name.
Definition: qgsproject.cpp:3685
QgsLayout::renderContext
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
Definition: qgslayout.cpp:359
QgsProject::mapLayers
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
Definition: qgsproject.cpp:3955
QgsLayoutUtils::snappedAngle
static double snappedAngle(double angle)
Snaps an angle (in degrees) to its closest 45 degree angle.
Definition: qgslayoututils.cpp:57
Qgis::defaultProjectScales
static QString defaultProjectScales()
A string with default project scales.
Definition: qgis.cpp:271
QgsLayoutRenderContext::renderContextFlags
Qgis::RenderContextFlags renderContextFlags() const
Returns the combination of render context flags matched to the layout context's settings.
Definition: qgslayoutrendercontext.cpp:62
QgsLayoutUtils::fontDescentMM
static double fontDescentMM(const QFont &font)
Calculate a font descent in millimeters, including workarounds for QT font rendering issues.
Definition: qgslayoututils.cpp:193
QgsRenderContext::setPainter
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
Definition: qgsrendercontext.h:512
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:1486
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
QgsProject::mapLayer
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Definition: qgsproject.cpp:3680
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
QgsLayoutItem::FlagProvidesClipPath
@ FlagProvidesClipPath
Item can act as a clipping path provider (see clipPath())
Definition: qgslayoutitem.h:306
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:486
prevNiceNumber
double prevNiceNumber(double a, double d=1)
Definition: qgslayoututils.cpp:452
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:141
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:174
QgsLayoutUtils::calculatePrettySize
static double calculatePrettySize(double minimumSize, double maximumSize)
Calculates a "pretty" size which falls between the range [minimumSize, maximumSize].
Definition: qgslayoututils.cpp:458
nextNiceNumber
double nextNiceNumber(double a, double d=1)
Definition: qgslayoututils.cpp:445
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:2265
qgsrendercontext.h
QgsLayoutItemMap::extent
QgsRectangle extent() const
Returns the current map extent.
Definition: qgslayoutitemmap.cpp:281
QgsLayoutItemPage::Landscape
@ Landscape
Landscape orientation.
Definition: qgslayoutitempage.h:69
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
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:213
QgsScopedQPainterState
Scoped object for saving and restoring a QPainter object's state.
Definition: qgsrendercontext.h:1336
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:97
qgslayout.h
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:317
QgsLayoutUtils::mapLayerFromString
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProject *project)
Resolves a string into a map layer from a given project.
Definition: qgslayoututils.cpp:423
M_DEG2RAD
#define M_DEG2RAD
Definition: qgslayoututils.cpp:30
qgsprojectviewsettings.h
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:33
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:255
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
QgsProjectViewSettings::mapScales
QVector< double > mapScales() const
Returns the list of custom project map scales.
Definition: qgsprojectviewsettings.cpp:115
QgsLayoutItemPage::Orientation
Orientation
Page orientation.
Definition: qgslayoutitempage.h:66
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:68
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
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:302
qgssettings.h
QgsLayoutRenderContext::textRenderFormat
Qgis::TextRenderFormat textRenderFormat() const
Returns the text render format, which dictates how text is rendered (e.g.
Definition: qgslayoutrendercontext.h:218
QgsLayoutUtils::fontHeightMM
static double fontHeightMM(const QFont &font)
Calculate a font height in millimeters, including workarounds for QT font rendering issues.
Definition: qgslayoututils.cpp:203
QgsLayoutUtils::fontAscentMM
static double fontAscentMM(const QFont &font)
Calculates a font ascent in millimeters, including workarounds for QT font rendering issues.
Definition: qgslayoututils.cpp:184
QgsUnitTypes::LayoutMillimeters
@ LayoutMillimeters
Millimeters.
Definition: qgsunittypes.h:183
QgsRenderContext::setTextRenderFormat
void setTextRenderFormat(Qgis::TextRenderFormat format)
Sets the text render format, which dictates how text is rendered (e.g.
Definition: qgsrendercontext.h:740
QgsLayout::project
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:132
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
QgsLayoutUtils::scaleFactorFromItemStyle
static Q_DECL_DEPRECATED double scaleFactorFromItemStyle(const QStyleOptionGraphicsItem *style)
Extracts the scale factor from an item style.
Definition: qgslayoututils.cpp:399
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:472
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:43
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:154
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:222
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:382
QgsProjectViewSettings::useProjectScales
bool useProjectScales() const
Returns true if project mapScales() are enabled.
Definition: qgsprojectviewsettings.cpp:129