QGIS API Documentation  3.2.0-Bonn (bc43194)
qgslayoutmeasurement.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutmeasurement.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 "qgslayoutmeasurement.h"
19 
20 #include <QStringList>
21 
23  : mLength( length )
24  , mUnits( units )
25 {
26 }
27 
29 {
30  return QStringLiteral( "%1,%2" ).arg( mLength ).arg( QgsUnitTypes::encodeUnit( mUnits ) );
31 }
32 
34 {
35  QStringList parts = string.split( ',' );
36  if ( parts.count() != 2 )
37  {
38  return QgsLayoutMeasurement( 0 );
39  }
40  return QgsLayoutMeasurement( parts[0].toDouble(), QgsUnitTypes::decodeLayoutUnit( parts[1] ) );
41 }
42 
44 {
45  return other.units() == mUnits && qgsDoubleNear( other.length(), mLength );
46 }
47 
49 {
50  return ( ! operator==( other ) );
51 }
52 
54 {
55  return QgsLayoutMeasurement( mLength + v, mUnits );
56 }
57 
59 {
60  *this = *this + v;
61  return *this;
62 }
63 
65 {
66  return QgsLayoutMeasurement( mLength - v, mUnits );
67 }
68 
70 {
71  *this = *this - v;
72  return *this;
73 }
74 
76 {
77  return QgsLayoutMeasurement( mLength * v, mUnits );
78 }
79 
81 {
82  *this = *this * v;
83  return *this;
84 }
85 
87 {
88  return QgsLayoutMeasurement( mLength / v, mUnits );
89 }
90 
92 {
93  *this = *this / v;
94  return *this;
95 }
QgsUnitTypes::LayoutUnit units() const
Returns the units for the measurement.
QgsLayoutMeasurement operator/(double v) const
Divides the measurement by a scalar value.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:251
bool operator==(const QgsLayoutMeasurement &other) const
QgsLayoutMeasurement operator*=(double v)
Multiplies the measurement by a scalar value.
QgsLayoutMeasurement operator-=(double v)
Subtracts a scalar value from the measurement.
QgsLayoutMeasurement(double length, QgsUnitTypes::LayoutUnit units=QgsUnitTypes::LayoutMillimeters)
Constructor for QgsLayoutMeasurement.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
static Q_INVOKABLE QgsUnitTypes::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
QgsLayoutMeasurement operator*(double v) const
Multiplies the measurement by a scalar value.
bool operator!=(const QgsLayoutMeasurement &other) const
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
QgsLayoutMeasurement operator+=(double v)
Adds a scalar value to the measurement.
static QgsLayoutMeasurement decodeMeasurement(const QString &string)
Decodes a measurement from a string.
double length() const
Returns the length of the measurement.
QgsLayoutMeasurement operator/=(double v)
Divides the measurement by a scalar value.
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:114
QgsLayoutMeasurement operator+(double v) const
Adds a scalar value to the measurement.
QString encodeMeasurement() const
Encodes the layout measurement to a string.
QgsLayoutMeasurement operator-(double v) const
Subtracts a scalar value from the measurement.