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