QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 <QStringList>
21 
22 QgsLayoutSize::QgsLayoutSize( const double width, const double height, const QgsUnitTypes::LayoutUnit units )
23  : mWidth( width )
24  , mHeight( height )
25  , mUnits( units )
26 {
27 }
28 
29 QgsLayoutSize::QgsLayoutSize( const QSizeF size, const QgsUnitTypes::LayoutUnit units )
30  : mWidth( size.width() )
31  , mHeight( size.height() )
32  , mUnits( units )
33 {
34 }
35 
37  : mUnits( units )
38 {
39 
40 }
41 
43 {
44  return qgsDoubleNear( mWidth, 0 ) && qgsDoubleNear( mHeight, 0 );
45 }
46 
48 {
49  return QSizeF( mWidth, mHeight );
50 }
51 
53 {
54  return QStringLiteral( "%1,%2,%3" ).arg( mWidth ).arg( mHeight ).arg( QgsUnitTypes::encodeUnit( mUnits ) );
55 }
56 
57 QgsLayoutSize QgsLayoutSize::decodeSize( const QString &string )
58 {
59  QStringList parts = string.split( ',' );
60  if ( parts.count() != 3 )
61  {
62  return QgsLayoutSize();
63  }
64  return QgsLayoutSize( parts[0].toDouble(), parts[1].toDouble(), QgsUnitTypes::decodeLayoutUnit( parts[2] ) );
65 
66 }
67 
68 bool QgsLayoutSize::operator==( const QgsLayoutSize &other ) const
69 {
70  return other.units() == mUnits && qgsDoubleNear( other.width(), mWidth ) && qgsDoubleNear( other.height(), mHeight );
71 }
72 
73 bool QgsLayoutSize::operator!=( const QgsLayoutSize &other ) const
74 {
75  return ( ! operator==( other ) );
76 }
77 
78 QgsLayoutSize QgsLayoutSize::operator*( const double v ) const
79 {
80  return QgsLayoutSize( mWidth * v, mHeight * v, mUnits );
81 }
82 
84 {
85  *this = *this * v;
86  return *this;
87 }
88 
89 QgsLayoutSize QgsLayoutSize::operator/( const double v ) const
90 {
91  return QgsLayoutSize( mWidth / v, mHeight / v, mUnits );
92 }
93 
95 {
96  *this = *this / v;
97  return *this;
98 }
QgsLayoutSize::width
double width() const
Returns the width of the size.
Definition: qgslayoutsize.h:76
QgsLayoutSize::units
QgsUnitTypes::LayoutUnit units() const
Returns the units for the size.
Definition: qgslayoutsize.h:103
QgsUnitTypes::LayoutUnit
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:180
QgsLayoutSize::isEmpty
bool isEmpty() const
Tests whether the size is empty, ie both its width and height are zero.
Definition: qgslayoutsize.cpp:42
qgis.h
QgsLayoutSize::decodeSize
static QgsLayoutSize decodeSize(const QString &string)
Decodes a size from a string.
Definition: qgslayoutsize.cpp:57
QgsLayoutSize::operator==
bool operator==(const QgsLayoutSize &other) const
Definition: qgslayoutsize.cpp:68
qgslayoutsize.h
QgsLayoutSize::encodeSize
QString encodeSize() const
Encodes the layout size to a string.
Definition: qgslayoutsize.cpp:52
QgsUnitTypes::encodeUnit
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
Definition: qgsunittypes.cpp:122
QgsLayoutSize::operator/=
QgsLayoutSize operator/=(double v)
Divides the width and height by a scalar value.
Definition: qgslayoutsize.cpp:94
QgsLayoutSize::toQSizeF
QSizeF toQSizeF() const
Converts the layout size to a QSizeF.
Definition: qgslayoutsize.cpp:47
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
QgsLayoutSize::QgsLayoutSize
QgsLayoutSize(double width, double height, QgsUnitTypes::LayoutUnit units=QgsUnitTypes::LayoutMillimeters)
Constructor for QgsLayoutSize.
Definition: qgslayoutsize.cpp:22
QgsLayoutSize::operator*
QgsLayoutSize operator*(double v) const
Multiplies the width and height by a scalar value.
Definition: qgslayoutsize.cpp:78
QgsLayoutSize::operator/
QgsLayoutSize operator/(double v) const
Divides the width and height by a scalar value.
Definition: qgslayoutsize.cpp:89
QgsLayoutSize::height
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:90
QgsLayoutSize
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:40
QgsLayoutSize::operator*=
QgsLayoutSize operator*=(double v)
Multiplies the width and height by a scalar value.
Definition: qgslayoutsize.cpp:83
QgsUnitTypes::decodeLayoutUnit
static Q_INVOKABLE QgsUnitTypes::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
Definition: qgsunittypes.cpp:2995
QgsLayoutSize::operator!=
bool operator!=(const QgsLayoutSize &other) const
Definition: qgslayoutsize.cpp:73