QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgslayoutsize.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutsize.cpp
3 -----------------
4 begin : June 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 "qgslayoutsize.h"
19#include "qgis.h"
20#include "qgsunittypes.h"
21#include <QStringList>
22
23QgsLayoutSize::QgsLayoutSize( const double width, const double height, const Qgis::LayoutUnit units )
24 : mWidth( width )
25 , mHeight( height )
26 , mUnits( units )
27{
28#ifdef QGISDEBUG
29 Q_ASSERT_X( !std::isnan( width ) && !std::isnan( height ), "QgsLayoutSize", "Layout size with NaN dimensions created" );
30#endif
31}
32
33QgsLayoutSize::QgsLayoutSize( const QSizeF size, const Qgis::LayoutUnit units )
34 : mWidth( size.width() )
35 , mHeight( size.height() )
36 , mUnits( units )
37{
38}
39
41 : mUnits( units )
42{
43
44}
45
47{
48 return qgsDoubleNear( mWidth, 0 ) && qgsDoubleNear( mHeight, 0 );
49}
50
52{
53 return QSizeF( mWidth, mHeight );
54}
55
57{
58 return QStringLiteral( "%1,%2,%3" ).arg( mWidth ).arg( mHeight ).arg( QgsUnitTypes::encodeUnit( mUnits ) );
59}
60
62{
63 QStringList parts = string.split( ',' );
64 if ( parts.count() != 3 )
65 {
66 return QgsLayoutSize();
67 }
68
69 const double width = parts[0].toDouble();
70 const double height = parts[1].toDouble();
71 if ( std::isnan( width ) || std::isnan( height ) )
72 return QgsLayoutSize();
73
75}
76
77bool QgsLayoutSize::operator==( const QgsLayoutSize &other ) const
78{
79 return other.units() == mUnits && qgsDoubleNear( other.width(), mWidth ) && qgsDoubleNear( other.height(), mHeight );
80}
81
82bool QgsLayoutSize::operator!=( const QgsLayoutSize &other ) const
83{
84 return ( ! operator==( other ) );
85}
86
88{
89 return QgsLayoutSize( mWidth * v, mHeight * v, mUnits );
90}
91
93{
94 *this = *this * v;
95 return *this;
96}
97
99{
100 return QgsLayoutSize( mWidth / v, mHeight / v, mUnits );
101}
102
104{
105 *this = *this / v;
106 return *this;
107}
LayoutUnit
Layout measurement units.
Definition qgis.h:4930
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
static QgsLayoutSize decodeSize(const QString &string)
Decodes a size from a string.
double height() const
Returns the height of the size.
bool operator==(const QgsLayoutSize &other) const
QgsLayoutSize operator/=(double v)
Divides the width and height by a scalar value.
Qgis::LayoutUnit units() const
Returns the units for the size.
double width() const
Returns the width of the size.
bool operator!=(const QgsLayoutSize &other) const
QgsLayoutSize operator/(double v) const
Divides the width and height by a scalar value.
QString encodeSize() const
Encodes the layout size to a string.
bool isEmpty() const
Tests whether the size is empty, ie both its width and height are zero.
QgsLayoutSize operator*=(double v)
Multiplies the width and height by a scalar value.
QgsLayoutSize(double width, double height, Qgis::LayoutUnit units=Qgis::LayoutUnit::Millimeters)
Constructor for QgsLayoutSize.
QSizeF toQSizeF() const
Converts the layout size to a QSizeF.
QgsLayoutSize operator*(double v) const
Multiplies the width and height by a scalar value.
static Q_INVOKABLE Qgis::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6066