QGIS API Documentation  3.6.0-Noosa (5873452)
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 
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 }
static QgsLayoutSize decodeSize(const QString &string)
Decodes a size from a string.
bool operator!=(const QgsLayoutSize &other) const
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:265
QgsLayoutSize operator/(double v) const
Divides the width and height by a scalar value.
QSizeF toQSizeF() const
Converts the layout size to a QSizeF.
static Q_INVOKABLE QgsUnitTypes::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
QgsLayoutSize(double width, double height, QgsUnitTypes::LayoutUnit units=QgsUnitTypes::LayoutMillimeters)
Constructor for QgsLayoutSize.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the size.
bool operator==(const QgsLayoutSize &other) const
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
QgsLayoutSize operator*(double v) const
Multiplies the width and height by a scalar value.
bool isEmpty() const
Tests whether the size is empty, ie both its width and height are zero.
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:125
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:40
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:90
QgsLayoutSize operator/=(double v)
Divides the width and height by a scalar value.
QString encodeSize() const
Encodes the layout size to a string.
QgsLayoutSize operator*=(double v)
Multiplies the width and height by a scalar value.
double width() const
Returns the width of the size.
Definition: qgslayoutsize.h:76