QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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}
29
30QgsLayoutSize::QgsLayoutSize( const QSizeF size, const Qgis::LayoutUnit units )
31 : mWidth( size.width() )
32 , mHeight( size.height() )
33 , mUnits( units )
34{
35}
36
38 : mUnits( units )
39{
40
41}
42
44{
45 return qgsDoubleNear( mWidth, 0 ) && qgsDoubleNear( mHeight, 0 );
46}
47
49{
50 return QSizeF( mWidth, mHeight );
51}
52
54{
55 return QStringLiteral( "%1,%2,%3" ).arg( mWidth ).arg( mHeight ).arg( QgsUnitTypes::encodeUnit( mUnits ) );
56}
57
59{
60 QStringList parts = string.split( ',' );
61 if ( parts.count() != 3 )
62 {
63 return QgsLayoutSize();
64 }
65 return QgsLayoutSize( parts[0].toDouble(), parts[1].toDouble(), QgsUnitTypes::decodeLayoutUnit( parts[2] ) );
66
67}
68
69bool QgsLayoutSize::operator==( const QgsLayoutSize &other ) const
70{
71 return other.units() == mUnits && qgsDoubleNear( other.width(), mWidth ) && qgsDoubleNear( other.height(), mHeight );
72}
73
74bool QgsLayoutSize::operator!=( const QgsLayoutSize &other ) const
75{
76 return ( ! operator==( other ) );
77}
78
80{
81 return QgsLayoutSize( mWidth * v, mHeight * v, mUnits );
82}
83
85{
86 *this = *this * v;
87 return *this;
88}
89
91{
92 return QgsLayoutSize( mWidth / v, mHeight / v, mUnits );
93}
94
96{
97 *this = *this / v;
98 return *this;
99}
LayoutUnit
Layout measurement units.
Definition: qgis.h:4275
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:40
static QgsLayoutSize decodeSize(const QString &string)
Decodes a size from a string.
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:89
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.
Definition: qgslayoutsize.h:75
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:5207