QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgslayoututils.h
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoututils.h
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#ifndef QGSLAYOUTUTILS_H
18#define QGSLAYOUTUTILS_H
19
20#include "qgis_core.h"
21#include "qgslayoutitempage.h"
22
23#include <QColor>
24#include <QFont>
25
27class QgsLayout;
29class QPainter;
30class QRectF;
31class QStyleOptionGraphicsItem;
32
37class CORE_EXPORT QgsLayoutUtils
38{
39 public:
46 static void rotate( double angle, double &x, double &y );
47
53 static double normalizedAngle( double angle, bool allowNegative = false );
54
59 static double snappedAngle( double angle );
60
68 static QgsRenderContext createRenderContextForMap( QgsLayoutItemMap *map, QPainter *painter, double dpi = -1 );
69
78 static QgsRenderContext createRenderContextForLayout( QgsLayout *layout, QPainter *painter, double dpi = -1 );
79
88 static void relativeResizeRect( QRectF &rectToResize, const QRectF &boundsBefore, const QRectF &boundsAfter );
89
99 static double relativePosition( double position, double beforeMin, double beforeMax, double afterMin, double afterMax );
100
106 static QFont scaledFontPixelSize( const QFont &font );
107
115 static double fontAscentMM( const QFont &font );
116
124 static double fontDescentMM( const QFont &font );
125
134 static double fontHeightMM( const QFont &font );
135
144 static double fontHeightCharacterMM( const QFont &font, QChar character );
145
155 static double textWidthMM( const QFont &font, const QString &text );
156
166 static double textHeightMM( const QFont &font, const QString &text, double multiLineHeight = 1.0 );
167
175 static void drawText( QPainter *painter, QPointF position, const QString &text, const QFont &font, const QColor &color = QColor() );
176
189 static void drawText(
190 QPainter *painter,
191 const QRectF &rectangle,
192 const QString &text,
193 const QFont &font,
194 const QColor &color = QColor(),
195 Qt::AlignmentFlag halignment = Qt::AlignLeft,
196 Qt::AlignmentFlag valignment = Qt::AlignTop,
197 int flags = Qt::TextWordWrap
198 );
199
208 static QRectF largestRotatedRectWithinBounds( const QRectF &originalRect, const QRectF &boundsRect, double rotation );
209
215 static QgsLayoutItemPage::Orientation decodePaperOrientation( const QString &string, bool &ok );
216
222 Q_DECL_DEPRECATED static double scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style ) SIP_DEPRECATED;
223
229 static double scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style, QPainter *painter );
230
240 static QgsMapLayer *mapLayerFromString( const QString &string, QgsProject *project );
241
250 static double calculatePrettySize( double minimumSize, double maximumSize );
251
257 static bool itemIsAClippingSource( const QgsLayoutItem *item );
258
264 static QVector< double > predefinedScales( const QgsLayout *layout );
265
266 private:
268 static constexpr double FONT_WORKAROUND_SCALE = 10;
269
274 static double pointsToMM( double pointSize );
275
280 static double mmToPoints( double mmSize );
281
282 friend class TestQgsLayoutUtils;
283};
284
285#endif //QGSLAYOUTUTILS_H
Layout graphical items for displaying a map.
Orientation
Page orientation.
Base class for graphical items within a QgsLayout.
Utilities for layouts.
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.
friend class TestQgsLayoutUtils
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
Base class for all map layer types.
Definition qgsmaplayer.h:83
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:113
Contains information about the context of a rendering operation.
#define SIP_DEPRECATED
Definition qgis_sip.h:113