QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsmargins.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsmargins.h
3 ------------
4 Date : January 2017
5 Copyright : (C) 2017 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSMARGINS_H
17#define QGSMARGINS_H
18
19#include "qgis_core.h"
20#include "qgis.h"
21#include <QString>
22
33//This class was originally based off Qt's QgsMarginsF class
34//It was forked in order to always use double values, rather than qreal values.
35
36class CORE_EXPORT QgsMargins
37{
38 public:
39
43 QgsMargins() = default;
44
52 QgsMargins( double left, double top, double right, double bottom )
53 : mLeft( left )
54 , mTop( top )
55 , mRight( right )
56 , mBottom( bottom )
57 {}
58
62 bool isNull() const
63 {
64 return qgsDoubleNear( mLeft, 0.0 ) && qgsDoubleNear( mTop, 0.0 ) && qgsDoubleNear( mRight, 0.0 ) && qgsDoubleNear( mBottom, 0.0 );
65 }
66
71 double left() const { return mLeft; }
72
77 double top() const { return mTop; }
78
83 double right() const { return mRight; }
84
89 double bottom() const { return mBottom; }
90
95 void setLeft( double left ) { mLeft = left; }
96
101 void setTop( double top ) { mTop = top; }
102
107 void setRight( double right ) { mRight = right; }
108
113 void setBottom( double bottom ) { mBottom = bottom; }
114
119 inline QgsMargins &operator+=( const QgsMargins &margins );
120
125 inline QgsMargins &operator-=( const QgsMargins &margins );
126
130 inline QgsMargins &operator+=( double addend );
131
136 inline QgsMargins &operator-=( double subtrahend );
137
142 inline QgsMargins &operator*=( double factor );
143
148 inline QgsMargins &operator/=( double divisor );
149
154 QString toString() const;
155
161 static QgsMargins fromString( const QString &string );
162
163 private:
164 double mLeft = 0.0;
165 double mTop = 0.0;
166 double mRight = 0.0;
167 double mBottom = 0.0;
168};
169
170
174inline bool operator==( const QgsMargins &lhs, const QgsMargins &rhs )
175{
176 return qgsDoubleNear( lhs.left(), rhs.left() )
177 && qgsDoubleNear( lhs.top(), rhs.top() )
178 && qgsDoubleNear( lhs.right(), rhs.right() )
179 && qgsDoubleNear( lhs.bottom(), rhs.bottom() );
180}
181
185inline bool operator!=( const QgsMargins &lhs, const QgsMargins &rhs )
186{
187 return !operator==( lhs, rhs );
188}
189
194inline QgsMargins operator+( const QgsMargins &m1, const QgsMargins &m2 )
195{
196 return QgsMargins( m1.left() + m2.left(), m1.top() + m2.top(),
197 m1.right() + m2.right(), m1.bottom() + m2.bottom() );
198}
199
204inline QgsMargins operator-( const QgsMargins &m1, const QgsMargins &m2 )
205{
206 return QgsMargins( m1.left() - m2.left(), m1.top() - m2.top(),
207 m1.right() - m2.right(), m1.bottom() - m2.bottom() );
208}
209
213inline QgsMargins operator+( const QgsMargins &lhs, double rhs )
214{
215 return QgsMargins( lhs.left() + rhs, lhs.top() + rhs,
216 lhs.right() + rhs, lhs.bottom() + rhs );
217}
218
222inline QgsMargins operator+( double lhs, const QgsMargins &rhs )
223{
224 return QgsMargins( rhs.left() + lhs, rhs.top() + lhs,
225 rhs.right() + lhs, rhs.bottom() + lhs );
226}
227
231inline QgsMargins operator-( const QgsMargins &lhs, double rhs )
232{
233 return QgsMargins( lhs.left() - rhs, lhs.top() - rhs,
234 lhs.right() - rhs, lhs.bottom() - rhs );
235}
236
241inline QgsMargins operator*( const QgsMargins &margins, double factor )
242{
243 return QgsMargins( margins.left() * factor, margins.top() * factor,
244 margins.right() * factor, margins.bottom() * factor );
245}
246
251inline QgsMargins operator*( double factor, const QgsMargins &margins )
252{
253 return QgsMargins( margins.left() * factor, margins.top() * factor,
254 margins.right() * factor, margins.bottom() * factor );
255}
256
261inline QgsMargins operator/( const QgsMargins &margins, double divisor )
262{
263 return QgsMargins( margins.left() / divisor, margins.top() / divisor,
264 margins.right() / divisor, margins.bottom() / divisor );
265}
266
268{
269 return *this = *this + margins;
270}
271
273{
274 return *this = *this - margins;
275}
276
278{
279 mLeft += addend;
280 mTop += addend;
281 mRight += addend;
282 mBottom += addend;
283 return *this;
284}
285
286inline QgsMargins &QgsMargins::operator-=( double subtrahend ) SIP_SKIP
287{
288 mLeft -= subtrahend;
289 mTop -= subtrahend;
290 mRight -= subtrahend;
291 mBottom -= subtrahend;
292 return *this;
293}
294
296{
297 return *this = *this * factor;
298}
299
301{
302 return *this = *this / divisor;
303}
304
308inline QgsMargins operator+( const QgsMargins &margins )
309{
310 return margins;
311}
312
316inline QgsMargins operator-( const QgsMargins &margins )
317{
318 return QgsMargins( -margins.left(), -margins.top(), -margins.right(), -margins.bottom() );
319}
320
322
323#endif // QGSMARGINS_H
The QgsMargins class defines the four margins of a rectangle.
Definition: qgsmargins.h:37
QgsMargins()=default
Constructs a margins object with all margins set to 0.
void setBottom(double bottom)
Sets the bottom margin to bottom.
Definition: qgsmargins.h:113
QgsMargins & operator*=(double factor)
Multiplies each component of this object by factor and returns a reference to it.
Definition: qgsmargins.h:295
double top() const
Returns the top margin.
Definition: qgsmargins.h:77
void setLeft(double left)
Sets the left margin to left.
Definition: qgsmargins.h:95
bool isNull() const
Returns true if all margins are is 0; otherwise returns false.
Definition: qgsmargins.h:62
double right() const
Returns the right margin.
Definition: qgsmargins.h:83
double bottom() const
Returns the bottom margin.
Definition: qgsmargins.h:89
QgsMargins & operator/=(double divisor)
Multiplies each component of this object by factor and returns a reference to it.
Definition: qgsmargins.h:300
QgsMargins(double left, double top, double right, double bottom)
Constructs margins with the given left, top, right, bottom.
Definition: qgsmargins.h:52
void setRight(double right)
Sets the right margin to right.
Definition: qgsmargins.h:107
void setTop(double top)
Sets the top margin to top.
Definition: qgsmargins.h:101
QgsMargins & operator+=(const QgsMargins &margins)
Add each component of margins to the respective component of this object and returns a reference to i...
Definition: qgsmargins.h:267
QgsMargins & operator-=(const QgsMargins &margins)
Subtract each component of margins from the respective component of this object and returns a referen...
Definition: qgsmargins.h:272
double left() const
Returns the left margin.
Definition: qgsmargins.h:71
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:5207
#define SIP_SKIP
Definition: qgis_sip.h:126
Q_DECLARE_TYPEINFO(QgsMargins, Q_MOVABLE_TYPE)
QgsMargins operator*(const QgsMargins &margins, double factor)
Returns a QgsMargins object that is formed by multiplying each component of the given margins by fact...
Definition: qgsmargins.h:241
QgsMargins operator-(const QgsMargins &m1, const QgsMargins &m2)
Returns a QgsMargins object that is formed by subtracting m2 from m1; each component is subtracted se...
Definition: qgsmargins.h:204
QgsMargins operator/(const QgsMargins &margins, double divisor)
Returns a QgsMargins object that is formed by dividing the components of the given margins by the giv...
Definition: qgsmargins.h:261
QgsMargins operator+(const QgsMargins &m1, const QgsMargins &m2)
Returns a QgsMargins object that is the sum of the given margins, m1 and m2; each component is added ...
Definition: qgsmargins.h:194
bool operator!=(const QgsMargins &lhs, const QgsMargins &rhs)
Returns true if lhs and rhs are different; otherwise returns false.
Definition: qgsmargins.h:185
bool operator==(const QgsMargins &lhs, const QgsMargins &rhs)
Returns true if lhs and rhs are equal; otherwise returns false.
Definition: qgsmargins.h:174