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