QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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#include "qgsunittypes.h"
21
22#include <QStringList>
23
24QgsLayoutPoint::QgsLayoutPoint( const double x, const double y, const Qgis::LayoutUnit units )
25 : mX( x )
26 , mY( y )
27 , mUnits( units )
28{
29#ifdef QGISDEBUG
30 Q_ASSERT_X( !std::isnan( mX ) && !std::isnan( mY ), "QgsLayoutPoint", "Layout point with NaN coordinates created" );
31#endif
32}
33
34QgsLayoutPoint::QgsLayoutPoint( const QPointF point, const Qgis::LayoutUnit units )
35 : mX( point.x() )
36 , mY( point.y() )
37 , mUnits( units )
38{
39
40}
41
43 : mUnits( units )
44{
45
46}
47
49{
50 return qgsDoubleNear( mX, 0 ) && qgsDoubleNear( mY, 0 );
51}
52
54{
55 return QPointF( mX, mY );
56}
57
59{
60 return QStringLiteral( "%1,%2,%3" ).arg( mX ).arg( mY ).arg( QgsUnitTypes::encodeUnit( mUnits ) );
61}
62
64{
65 QStringList parts = string.split( ',' );
66 if ( parts.count() != 3 )
67 {
68 return QgsLayoutPoint();
69 }
70 const double x = parts[0].toDouble();
71 const double y = parts[1].toDouble();
72
73 // don't restore corrupted coordinates from xml. This can happen when eg a broken item size causes a nan position,
74 // which breaks the layout size calculation and results in nan or massive x/y values. Restoring these leads to a broken
75 // layout which cannot be interacted with.
76 if ( std::isnan( x ) || std::isnan( y ) || x > 9.99998e+06 || y > 9.99998e+06 )
77 return QgsLayoutPoint();
78
79 return QgsLayoutPoint( x, y, QgsUnitTypes::decodeLayoutUnit( parts[2] ) );
80}
81
83{
84 return other.units() == mUnits && qgsDoubleNear( other.x(), mX ) && qgsDoubleNear( other.y(), mY );
85}
86
88{
89 return ( ! operator==( other ) );
90}
91
93{
94 return QgsLayoutPoint( mX * v, mY * v, mUnits );
95}
96
98{
99 *this = *this * v;
100 return *this;
101}
102
104{
105 return QgsLayoutPoint( mX / v, mY / v, mUnits );
106}
107
109{
110 *this = *this / v;
111 return *this;
112}
LayoutUnit
Layout measurement units.
Definition qgis.h:4867
This class provides a method of storing points, consisting of an x and y coordinate,...
QgsLayoutPoint operator/=(double v)
Divides the x and y by a scalar value.
double x() const
Returns x coordinate of point.
QPointF toQPointF() const
Converts the layout point to a QPointF.
QString encodePoint() const
Encodes the layout point to a string.
QgsLayoutPoint operator/(double v) const
Divides the x and y by a scalar value.
static QgsLayoutPoint decodePoint(const QString &string)
Decodes a point from a string.
bool isNull() const
Tests whether the position is null, ie both its x and y coordinates are zero.
double y() const
Returns y coordinate of point.
QgsLayoutPoint(double x, double y, Qgis::LayoutUnit units=Qgis::LayoutUnit::Millimeters)
Constructor for QgsLayoutPoint.
QgsLayoutPoint operator*=(double v)
Multiplies the x and y by a scalar value.
bool operator==(const QgsLayoutPoint &other) const
Qgis::LayoutUnit units() const
Returns the units for the point.
bool operator!=(const QgsLayoutPoint &other) const
QgsLayoutPoint operator*(double v) const
Multiplies the x and y 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:5958