QGIS API Documentation 4.1.0-Master (467af3bbe65)
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
20#include <cmath>
21
22#include "qgslayout.h"
23#include "qgslayoutitemmap.h"
27#include "qgsrendercontext.h"
28#include "qgssettings.h"
30
31#include <QPainter>
32#include <QString>
33#include <QStyleOptionGraphicsItem>
34
35using namespace Qt::StringLiterals;
36
37#ifndef M_DEG2RAD
38#define M_DEG2RAD 0.0174532925
39#endif
40
41void QgsLayoutUtils::rotate( double angle, double &x, double &y )
42{
43 double rotToRad = angle * M_PI / 180.0;
44 double xRot, yRot;
45 xRot = x * std::cos( rotToRad ) - y * std::sin( rotToRad );
46 yRot = x * std::sin( rotToRad ) + y * std::cos( rotToRad );
47 x = xRot;
48 y = yRot;
49}
50
51double QgsLayoutUtils::normalizedAngle( const double angle, const bool allowNegative )
52{
53 double clippedAngle = angle;
54 if ( clippedAngle >= 360.0 || clippedAngle <= -360.0 )
55 {
56 clippedAngle = std::fmod( clippedAngle, 360.0 );
57 }
58 if ( !allowNegative && clippedAngle < 0.0 )
59 {
60 clippedAngle += 360.0;
61 }
62 return clippedAngle;
63}
64
65double QgsLayoutUtils::snappedAngle( double angle )
66{
67 //normalize angle to 0-360 degrees
68 double clippedAngle = normalizedAngle( angle );
69
70 //snap angle to 45 degree
71 if ( clippedAngle >= 22.5 && clippedAngle < 67.5 )
72 {
73 return 45.0;
74 }
75 else if ( clippedAngle >= 67.5 && clippedAngle < 112.5 )
76 {
77 return 90.0;
78 }
79 else if ( clippedAngle >= 112.5 && clippedAngle < 157.5 )
80 {
81 return 135.0;
82 }
83 else if ( clippedAngle >= 157.5 && clippedAngle < 202.5 )
84 {
85 return 180.0;
86 }
87 else if ( clippedAngle >= 202.5 && clippedAngle < 247.5 )
88 {
89 return 225.0;
90 }
91 else if ( clippedAngle >= 247.5 && clippedAngle < 292.5 )
92 {
93 return 270.0;
94 }
95 else if ( clippedAngle >= 292.5 && clippedAngle < 337.5 )
96 {
97 return 315.0;
98 }
99 else
100 {
101 return 0.0;
102 }
103}
104
106{
107 if ( !map )
108 {
109 QgsRenderContext context;
110 context.setPainter( painter );
111 if ( dpi < 0 && painter && painter->device() )
112 {
113 context.setScaleFactor( painter->device()->logicalDpiX() / 25.4 );
114 }
115 else if ( dpi > 0 )
116 {
117 context.setScaleFactor( dpi / 25.4 );
118 }
119 else
120 {
121 context.setScaleFactor( 3.465 ); //assume 88 dpi as standard value
122 }
123 return context;
124 }
125 else
126 {
127 // default to 88 dpi if no painter specified
128 if ( dpi < 0 )
129 {
130 dpi = ( painter && painter->device() ) ? painter->device()->logicalDpiX() : 88;
131 }
132 double dotsPerMM = dpi / 25.4;
133
134 // get map settings from reference map
135 QgsRectangle extent = map->extent();
136 QSizeF mapSizeLayoutUnits = map->rect().size();
137 QSizeF mapSizeMM = map->layout()->convertFromLayoutUnits( mapSizeLayoutUnits, Qgis::LayoutUnit::Millimeters ).toQSizeF();
138 QgsMapSettings ms = map->mapSettings( extent, mapSizeMM * dotsPerMM, dpi, false );
140 if ( painter )
141 context.setPainter( painter );
142
143 context.setFlags( map->layout()->renderContext().renderContextFlags() );
145 return context;
146 }
147}
148
150{
151 QgsLayoutItemMap *referenceMap = layout ? layout->referenceMap() : nullptr;
152 QgsRenderContext context = createRenderContextForMap( referenceMap, painter, dpi );
153 if ( layout )
154 {
155 // TODO -- handle RasterizedRenderingPolicy here!!!!
156 context.setFlags( layout->renderContext().renderContextFlags() );
158 }
159
160 return context;
161}
162
163void QgsLayoutUtils::relativeResizeRect( QRectF &rectToResize, const QRectF &boundsBefore, const QRectF &boundsAfter )
164{
165 //linearly scale rectToResize relative to the scaling from boundsBefore to boundsAfter
166 const double left = !qgsDoubleNear( boundsBefore.left(), boundsBefore.right() )
167 ? relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() )
168 : boundsAfter.left();
169 const double right = !qgsDoubleNear( boundsBefore.left(), boundsBefore.right() )
170 ? relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() )
171 : boundsAfter.right();
172 const double top = !qgsDoubleNear( boundsBefore.top(), boundsBefore.bottom() )
173 ? relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() )
174 : boundsAfter.top();
175 const double bottom = !qgsDoubleNear( boundsBefore.top(), boundsBefore.bottom() )
176 ? relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() )
177 : boundsAfter.bottom();
178
179 rectToResize.setRect( left, top, right - left, bottom - top );
180}
181
182double QgsLayoutUtils::relativePosition( const double position, const double beforeMin, const double beforeMax, const double afterMin, const double afterMax )
183{
184 //calculate parameters for linear scale between before and after ranges
185 double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
186 double c = afterMin - ( beforeMin * m );
187
188 //return linearly scaled position
189 return m * position + c;
190}
191QFont QgsLayoutUtils::scaledFontPixelSize( const QFont &font )
192{
193 //upscale using FONT_WORKAROUND_SCALE
194 //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
195 QFont scaledFont = font;
196 double pixelSize = pointsToMM( scaledFont.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
197 scaledFont.setPixelSize( pixelSize );
198 return scaledFont;
199}
200
201double QgsLayoutUtils::fontAscentMM( const QFont &font )
202{
203 //upscale using FONT_WORKAROUND_SCALE
204 //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
205 QFont metricsFont = scaledFontPixelSize( font );
206 QFontMetricsF fontMetrics( metricsFont );
207 return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
208}
209
210double QgsLayoutUtils::fontDescentMM( const QFont &font )
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.descent() / FONT_WORKAROUND_SCALE );
217}
218
219double QgsLayoutUtils::fontHeightMM( const QFont &font )
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 QFont metricsFont = scaledFontPixelSize( font );
224 QFontMetricsF fontMetrics( metricsFont );
225 return ( fontMetrics.height() / FONT_WORKAROUND_SCALE );
226}
227
228double QgsLayoutUtils::fontHeightCharacterMM( const QFont &font, QChar character )
229{
230 //upscale using FONT_WORKAROUND_SCALE
231 //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
232 QFont metricsFont = scaledFontPixelSize( font );
233 QFontMetricsF fontMetrics( metricsFont );
234 return ( fontMetrics.boundingRect( character ).height() / FONT_WORKAROUND_SCALE );
235}
236
237double QgsLayoutUtils::textWidthMM( const QFont &font, const QString &text )
238{
239 //upscale using FONT_WORKAROUND_SCALE
240 //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
241
242 const QStringList multiLineSplit = text.split( '\n' );
243 QFont metricsFont = scaledFontPixelSize( font );
244 QFontMetricsF fontMetrics( metricsFont );
245
246 double maxWidth = 0;
247 for ( const QString &line : multiLineSplit )
248 {
249 maxWidth = std::max( maxWidth, ( fontMetrics.horizontalAdvance( line ) / FONT_WORKAROUND_SCALE ) );
250 }
251 return maxWidth;
252}
253
254double QgsLayoutUtils::textHeightMM( const QFont &font, const QString &text, double multiLineHeight )
255{
256 QStringList multiLineSplit = text.split( '\n' );
257 int lines = multiLineSplit.size();
258
259 //upscale using FONT_WORKAROUND_SCALE
260 //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
261 QFont metricsFont = scaledFontPixelSize( font );
262 QFontMetricsF fontMetrics( metricsFont );
263
264 double fontHeight = fontMetrics.ascent() + fontMetrics.descent(); // ignore +1 for baseline
265 double textHeight = fontMetrics.ascent() + static_cast< double >( ( lines - 1 ) * fontHeight * multiLineHeight );
266
267 return textHeight / FONT_WORKAROUND_SCALE;
268}
269
270void QgsLayoutUtils::drawText( QPainter *painter, QPointF position, const QString &text, const QFont &font, const QColor &color )
271{
272 if ( !painter )
273 {
274 return;
275 }
276
277 //upscale using FONT_WORKAROUND_SCALE
278 //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
279 QFont textFont = scaledFontPixelSize( font );
280
281 QgsScopedQPainterState painterState( painter );
282 painter->setFont( textFont );
283 if ( color.isValid() )
284 {
285 painter->setPen( color );
286 }
287 double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
288 painter->scale( scaleFactor, scaleFactor );
289 painter->drawText( position * FONT_WORKAROUND_SCALE, text );
290}
291
293 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
294)
295{
296 if ( !painter )
297 {
298 return;
299 }
300
301 //upscale using FONT_WORKAROUND_SCALE
302 //ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
303 QFont textFont = scaledFontPixelSize( font );
304
305 QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE, rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
306
307 QgsScopedQPainterState painterState( painter );
308 painter->setFont( textFont );
309 if ( color.isValid() )
310 {
311 painter->setPen( color );
312 }
313 double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
314 painter->scale( scaleFactor, scaleFactor );
315 painter->drawText( scaledRect, halignment | valignment | flags, text );
316}
317
318QRectF QgsLayoutUtils::largestRotatedRectWithinBounds( const QRectF &originalRect, const QRectF &boundsRect, const double rotation )
319{
320 double originalWidth = originalRect.width();
321 double originalHeight = originalRect.height();
322 double boundsWidth = boundsRect.width();
323 double boundsHeight = boundsRect.height();
324 double ratioBoundsRect = boundsWidth / boundsHeight;
325
326 double clippedRotation = normalizedAngle( rotation );
327
328 //shortcut for some rotation values
329 if ( qgsDoubleNear( clippedRotation, 0.0 ) || qgsDoubleNear( clippedRotation, 90.0 ) || qgsDoubleNear( clippedRotation, 180.0 ) || qgsDoubleNear( clippedRotation, 270.0 ) )
330 {
331 double rectScale;
332 if ( qgsDoubleNear( clippedRotation, 0.0 ) || qgsDoubleNear( clippedRotation, 180.0 ) )
333 {
334 rectScale = ( ( originalWidth / originalHeight ) > ratioBoundsRect ) ? boundsWidth / originalWidth : boundsHeight / originalHeight;
335 }
336 else
337 {
338 rectScale = ( ( originalHeight / originalWidth ) > ratioBoundsRect ) ? boundsWidth / originalHeight : boundsHeight / originalWidth;
339 }
340 double rectScaledWidth = rectScale * originalWidth;
341 double rectScaledHeight = rectScale * originalHeight;
342
343 if ( qgsDoubleNear( clippedRotation, 0.0 ) || qgsDoubleNear( clippedRotation, 180.0 ) )
344 {
345 return QRectF( ( boundsWidth - rectScaledWidth ) / 2.0, ( boundsHeight - rectScaledHeight ) / 2.0, rectScaledWidth, rectScaledHeight );
346 }
347 else
348 {
349 return QRectF( ( boundsWidth - rectScaledHeight ) / 2.0, ( boundsHeight - rectScaledWidth ) / 2.0, rectScaledWidth, rectScaledHeight );
350 }
351 }
352
353 //convert angle to radians and flip
354 double angleRad = -clippedRotation * M_DEG2RAD;
355 double cosAngle = std::cos( angleRad );
356 double sinAngle = std::sin( angleRad );
357
358 //calculate size of bounds of rotated rectangle
359 double widthBoundsRotatedRect = originalWidth * std::fabs( cosAngle ) + originalHeight * std::fabs( sinAngle );
360 double heightBoundsRotatedRect = originalHeight * std::fabs( cosAngle ) + originalWidth * std::fabs( sinAngle );
361
362 //compare ratio of rotated rect with bounds rect and calculate scaling of rotated
363 //rect to fit within bounds
364 double ratioBoundsRotatedRect = widthBoundsRotatedRect / heightBoundsRotatedRect;
365 double rectScale = ratioBoundsRotatedRect > ratioBoundsRect ? boundsWidth / widthBoundsRotatedRect : boundsHeight / heightBoundsRotatedRect;
366 double rectScaledWidth = rectScale * originalWidth;
367 double rectScaledHeight = rectScale * originalHeight;
368
369 //now calculate offset so that rotated rectangle is centered within bounds
370 //first calculate min x and y coordinates
371 double currentCornerX = 0;
372 double minX = 0;
373 currentCornerX += rectScaledWidth * cosAngle;
374 minX = minX < currentCornerX ? minX : currentCornerX;
375 currentCornerX += rectScaledHeight * sinAngle;
376 minX = minX < currentCornerX ? minX : currentCornerX;
377 currentCornerX -= rectScaledWidth * cosAngle;
378 minX = minX < currentCornerX ? minX : currentCornerX;
379
380 double currentCornerY = 0;
381 double minY = 0;
382 currentCornerY -= rectScaledWidth * sinAngle;
383 minY = minY < currentCornerY ? minY : currentCornerY;
384 currentCornerY += rectScaledHeight * cosAngle;
385 minY = minY < currentCornerY ? minY : currentCornerY;
386 currentCornerY += rectScaledWidth * sinAngle;
387 minY = minY < currentCornerY ? minY : currentCornerY;
388
389 //now calculate offset position of rotated rectangle
390 double offsetX = ratioBoundsRotatedRect > ratioBoundsRect ? 0 : ( boundsWidth - rectScale * widthBoundsRotatedRect ) / 2.0;
391 offsetX += std::fabs( minX );
392 double offsetY = ratioBoundsRotatedRect > ratioBoundsRect ? ( boundsHeight - rectScale * heightBoundsRotatedRect ) / 2.0 : 0;
393 offsetY += std::fabs( minY );
394
395 return QRectF( offsetX, offsetY, rectScaledWidth, rectScaledHeight );
396}
397
399{
400 QString s = string.trimmed();
401 if ( s.compare( "Portrait"_L1, Qt::CaseInsensitive ) == 0 )
402 {
403 ok = true;
405 }
406 else if ( s.compare( "Landscape"_L1, Qt::CaseInsensitive ) == 0 )
407 {
408 ok = true;
410 }
411 ok = false;
412 return QgsLayoutItemPage::Landscape; // default to landscape
413}
414
415double QgsLayoutUtils::scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style )
416{
417 Q_UNUSED( style )
418 return 1;
419}
420
421double QgsLayoutUtils::scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style, QPainter *painter )
422{
423 Q_UNUSED( style );
424 return QStyleOptionGraphicsItem::levelOfDetailFromTransform( painter->worldTransform() );
425}
426
428{
429 // Maybe it's a layer id?
430 if ( QgsMapLayer *ml = project->mapLayer( string ) )
431 return ml;
432
433 // Still nothing? Check for layer name
434 if ( QgsMapLayer *ml = project->mapLayersByName( string ).value( 0 ) )
435 return ml;
436
437 // Still nothing? Check for layer name, case-insensitive
438 const auto layers = project->mapLayers();
439 for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
440 {
441 if ( it.value()->name().compare( string, Qt::CaseInsensitive ) == 0 )
442 return it.value();
443 }
444
445 return nullptr;
446}
447
448// nextNiceNumber(4573.23, d) = 5000 (d=1) -> 4600 (d=10) -> 4580 (d=100) -> 4574 (d=1000) -> etc
449inline double nextNiceNumber( double a, double d = 1 )
450{
451 double s = std::pow( 10.0, std::floor( std::log10( a ) ) ) / d;
452 return std::ceil( a / s ) * s;
453}
454
455// prevNiceNumber(4573.23, d) = 4000 (d=1) -> 4500 (d=10) -> 4570 (d=100) -> 4573 (d=1000) -> etc
456inline double prevNiceNumber( double a, double d = 1 )
457{
458 double s = std::pow( 10.0, std::floor( std::log10( a ) ) ) / d;
459 return std::floor( a / s ) * s;
460}
461
462double QgsLayoutUtils::calculatePrettySize( const double minimumSize, const double maximumSize )
463{
464 if ( maximumSize < minimumSize )
465 {
466 return 0;
467 }
468 else
469 {
470 // Start with coarsest "nice" number closest to minimumSize resp
471 // maximumSize, then proceed to finer numbers as long as neither
472 // lowerNiceUnitsPerSeg nor upperNiceUnitsPerSeg are in
473 // [minimumSize, maximumSize]
474 double lowerNiceUnitsPerSeg = nextNiceNumber( minimumSize );
475 double upperNiceUnitsPerSeg = prevNiceNumber( maximumSize );
476
477 double d = 1;
478 while ( lowerNiceUnitsPerSeg > maximumSize && upperNiceUnitsPerSeg < minimumSize )
479 {
480 d *= 10;
481 lowerNiceUnitsPerSeg = nextNiceNumber( minimumSize, d );
482 upperNiceUnitsPerSeg = prevNiceNumber( maximumSize, d );
483 }
484
485 // Pick size from {lowerNiceUnitsPerSeg, upperNiceUnitsPerSeg}, use the larger if possible
486 return upperNiceUnitsPerSeg < minimumSize ? lowerNiceUnitsPerSeg : upperNiceUnitsPerSeg;
487 }
488}
489
491{
493 return false; // not a clipping provider, so shortcut out
494
495 // maps
496 QList< QgsLayoutItemMap * > maps;
497 item->layout()->layoutItems( maps );
498 for ( QgsLayoutItemMap *map : std::as_const( maps ) )
499 {
500 if ( map->itemClippingSettings()->isActive() && map->itemClippingSettings()->sourceItem() == item )
501 return true;
502 }
503
504 // pictures
505 QList< QgsLayoutItemPicture * > pictures;
506 item->layout()->layoutItems( pictures );
507 for ( QgsLayoutItemPicture *picture : std::as_const( pictures ) )
508 {
509 if ( picture->clipToItem() && picture->clippingItem() == item )
510 return true;
511 }
512
513 return false;
514}
515
516double QgsLayoutUtils::pointsToMM( const double pointSize )
517{
518 //conversion to mm based on 1 point = 1/72 inch
519 return ( pointSize * 0.3527 );
520}
521
522double QgsLayoutUtils::mmToPoints( const double mmSize )
523{
524 //conversion to points based on 1 point = 1/72 inch
525 return ( mmSize / 0.3527 );
526}
527
528QVector< double > QgsLayoutUtils::predefinedScales( const QgsLayout *layout )
529{
530 QgsProject *lProject = layout ? layout->project() : nullptr;
531 QVector< double > mapScales;
532 if ( lProject )
533 mapScales = lProject->viewSettings()->mapScales();
534
535 bool hasProjectScales( lProject ? lProject->viewSettings()->useProjectScales() : false );
536 if ( !hasProjectScales || mapScales.isEmpty() )
537 {
538 // default to global map tool scales
539 QgsSettings settings;
540 const QStringList scales = QgsSettingsRegistryCore::settingsMapScales->value();
541 for ( const QString &scale : scales )
542 {
543 QStringList parts( scale.split( ':' ) );
544 if ( parts.size() == 2 )
545 {
546 mapScales.push_back( parts[1].toDouble() );
547 }
548 }
549 }
550
551 return mapScales;
552}
@ Millimeters
Millimeters.
Definition qgis.h:5460
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.
A layout item subclass that displays SVG files or raster format images (jpg, png, ....
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:51
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:121
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:83
Contains configuration for rendering maps.
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:113
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.
static const QgsSettingsEntryStringList * settingsMapScales
Stores settings for use within QGIS.
Definition qgssettings.h:68
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:7077
double prevNiceNumber(double a, double d=1)
#define M_DEG2RAD
double nextNiceNumber(double a, double d=1)