QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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 <QString>
24#include <QStringList>
25
26using namespace Qt::StringLiterals;
27
29 : mWidth( width )
30 , mHeight( height )
31 , mUnits( units )
32{
33#ifdef QGISDEBUG
34 Q_ASSERT_X( !std::isnan( width ) && !std::isnan( height ), "QgsLayoutSize", "Layout size with NaN dimensions created" );
35#endif
36}
37
39 : mWidth( size.width() )
40 , mHeight( size.height() )
41 , mUnits( units )
42{
43}
44
46 : mUnits( units )
47{
48
49}
50
52{
53 return qgsDoubleNear( mWidth, 0 ) && qgsDoubleNear( mHeight, 0 );
54}
55
57{
58 return QSizeF( mWidth, mHeight );
59}
60
62{
63 return u"%1,%2,%3"_s.arg( mWidth ).arg( mHeight ).arg( QgsUnitTypes::encodeUnit( mUnits ) );
64}
65
67{
68 QStringList parts = string.split( ',' );
69 if ( parts.count() != 3 )
70 {
71 return QgsLayoutSize();
72 }
73
74 const double width = parts[0].toDouble();
75 const double height = parts[1].toDouble();
76 if ( std::isnan( width ) || std::isnan( height ) )
77 return QgsLayoutSize();
78
80}
81
82bool QgsLayoutSize::operator==( const QgsLayoutSize &other ) const
83{
84 return other.units() == mUnits && qgsDoubleNear( other.width(), mWidth ) && qgsDoubleNear( other.height(), mHeight );
85}
86
87bool QgsLayoutSize::operator!=( const QgsLayoutSize &other ) const
88{
89 return ( ! operator==( other ) );
90}
91
93{
94 return QgsLayoutSize( mWidth * v, mHeight * v, mUnits );
95}
96
98{
99 *this = *this * v;
100 return *this;
101}
102
104{
105 return QgsLayoutSize( mWidth / v, mHeight / v, mUnits );
106}
107
109{
110 *this = *this / v;
111 return *this;
112}
LayoutUnit
Layout measurement units.
Definition qgis.h:5299
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:6924