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