QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
20#include "qgis.h"
21#include "qgsunittypes.h"
22
23#include <QStringList>
24
25QgsLayoutPoint::QgsLayoutPoint( const double x, const double y, const Qgis::LayoutUnit units )
26 : mX( x )
27 , mY( y )
28 , mUnits( units )
29{
30#ifdef QGISDEBUG
31 Q_ASSERT_X( !std::isnan( mX ) && !std::isnan( mY ), "QgsLayoutPoint", "Layout point with NaN coordinates created" );
32#endif
33}
34
36 : mX( point.x() )
37 , mY( point.y() )
38 , mUnits( units )
39{
40
41}
42
48
50{
51 return qgsDoubleNear( mX, 0 ) && qgsDoubleNear( mY, 0 );
52}
53
55{
56 return QPointF( mX, mY );
57}
58
60{
61 return QStringLiteral( "%1,%2,%3" ).arg( mX ).arg( mY ).arg( QgsUnitTypes::encodeUnit( mUnits ) );
62}
63
65{
66 QStringList parts = string.split( ',' );
67 if ( parts.count() != 3 )
68 {
69 return QgsLayoutPoint();
70 }
71 const double x = parts[0].toDouble();
72 const double y = parts[1].toDouble();
73
74 // don't restore corrupted coordinates from xml. This can happen when eg a broken item size causes a nan position,
75 // which breaks the layout size calculation and results in nan or massive x/y values. Restoring these leads to a broken
76 // layout which cannot be interacted with.
77 if ( std::isnan( x ) || std::isnan( y ) || x > 9.99998e+06 || y > 9.99998e+06 )
78 return QgsLayoutPoint();
79
80 return QgsLayoutPoint( x, y, QgsUnitTypes::decodeLayoutUnit( parts[2] ) );
81}
82
84{
85 return other.units() == mUnits && qgsDoubleNear( other.x(), mX ) && qgsDoubleNear( other.y(), mY );
86}
87
89{
90 return ( ! operator==( other ) );
91}
92
94{
95 return QgsLayoutPoint( mX * v, mY * v, mUnits );
96}
97
99{
100 *this = *this * v;
101 return *this;
102}
103
105{
106 return QgsLayoutPoint( mX / v, mY / v, mUnits );
107}
108
110{
111 *this = *this / v;
112 return *this;
113}
LayoutUnit
Layout measurement units.
Definition qgis.h:5203
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:6607