QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgslayoutpoint.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutpoint.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 "qgslayoutpoint.h"
19 #include "qgis.h"
20 
21 #include <QStringList>
22 
23 QgsLayoutPoint::QgsLayoutPoint( const double x, const double y, const QgsUnitTypes::LayoutUnit units )
24  : mX( x )
25  , mY( y )
26  , mUnits( units )
27 {
28 
29 }
30 
32  : mX( point.x() )
33  , mY( point.y() )
34  , mUnits( units )
35 {
36 
37 }
38 
40  : mUnits( units )
41 {
42 
43 }
44 
46 {
47  return qgsDoubleNear( mX, 0 ) && qgsDoubleNear( mY, 0 );
48 }
49 
51 {
52  return QPointF( mX, mY );
53 }
54 
56 {
57  return QStringLiteral( "%1,%2,%3" ).arg( mX ).arg( mY ).arg( QgsUnitTypes::encodeUnit( mUnits ) );
58 }
59 
61 {
62  QStringList parts = string.split( ',' );
63  if ( parts.count() != 3 )
64  {
65  return QgsLayoutPoint();
66  }
67  return QgsLayoutPoint( parts[0].toDouble(), parts[1].toDouble(), QgsUnitTypes::decodeLayoutUnit( parts[2] ) );
68 }
69 
70 bool QgsLayoutPoint::operator==( const QgsLayoutPoint &other ) const
71 {
72  return other.units() == mUnits && qgsDoubleNear( other.x(), mX ) && qgsDoubleNear( other.y(), mY );
73 }
74 
75 bool QgsLayoutPoint::operator!=( const QgsLayoutPoint &other ) const
76 {
77  return ( ! operator==( other ) );
78 }
79 
81 {
82  return QgsLayoutPoint( mX * v, mY * v, mUnits );
83 }
84 
86 {
87  *this = *this * v;
88  return *this;
89 }
90 
92 {
93  return QgsLayoutPoint( mX / v, mY / v, mUnits );
94 }
95 
97 {
98  *this = *this / v;
99  return *this;
100 }
QString encodePoint() const
Encodes the layout point to a string.
QgsLayoutPoint operator/(double v) const
Divides the x and y by a scalar value.
QgsLayoutPoint operator*(double v) const
Multiplies the x and y 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:265
bool isNull() const
Tests whether the position is null, ie both its x and y coordinates are zero.
bool operator==(const QgsLayoutPoint &other) const
This class provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the point.
static Q_INVOKABLE QgsUnitTypes::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
double y() const
Returns y coordinate of point.
double x() const
Returns x coordinate of point.
bool operator!=(const QgsLayoutPoint &other) const
QgsLayoutPoint operator/=(double v)
Divides the x and y by a scalar value.
QPointF toQPointF() const
Converts the layout point to a QPointF.
QgsLayoutPoint(double x, double y, QgsUnitTypes::LayoutUnit units=QgsUnitTypes::LayoutMillimeters)
Constructor for QgsLayoutPoint.
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
QgsLayoutPoint operator*=(double v)
Multiplies the x and y by a scalar value.
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:125
static QgsLayoutPoint decodePoint(const QString &string)
Decodes a point from a string.