QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
20#include "qgis.h"
21#include "qgsunittypes.h"
22
23#include <QStringList>
24
26 : mWidth( width )
27 , mHeight( height )
28 , mUnits( units )
29{
30#ifdef QGISDEBUG
31 Q_ASSERT_X( !std::isnan( width ) && !std::isnan( height ), "QgsLayoutSize", "Layout size with NaN dimensions created" );
32#endif
33}
34
36 : mWidth( size.width() )
37 , mHeight( size.height() )
38 , mUnits( units )
39{
40}
41
43 : mUnits( units )
44{
45
46}
47
49{
50 return qgsDoubleNear( mWidth, 0 ) && qgsDoubleNear( mHeight, 0 );
51}
52
54{
55 return QSizeF( mWidth, mHeight );
56}
57
59{
60 return QStringLiteral( "%1,%2,%3" ).arg( mWidth ).arg( mHeight ).arg( QgsUnitTypes::encodeUnit( mUnits ) );
61}
62
64{
65 QStringList parts = string.split( ',' );
66 if ( parts.count() != 3 )
67 {
68 return QgsLayoutSize();
69 }
70
71 const double width = parts[0].toDouble();
72 const double height = parts[1].toDouble();
73 if ( std::isnan( width ) || std::isnan( height ) )
74 return QgsLayoutSize();
75
77}
78
79bool QgsLayoutSize::operator==( const QgsLayoutSize &other ) const
80{
81 return other.units() == mUnits && qgsDoubleNear( other.width(), mWidth ) && qgsDoubleNear( other.height(), mHeight );
82}
83
84bool QgsLayoutSize::operator!=( const QgsLayoutSize &other ) const
85{
86 return ( ! operator==( other ) );
87}
88
90{
91 return QgsLayoutSize( mWidth * v, mHeight * v, mUnits );
92}
93
95{
96 *this = *this * v;
97 return *this;
98}
99
101{
102 return QgsLayoutSize( mWidth / v, mHeight / v, mUnits );
103}
104
106{
107 *this = *this / v;
108 return *this;
109}
LayoutUnit
Layout measurement units.
Definition qgis.h:5203
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:6607