QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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.h"
20#include "qgis_core.h"
21
22#include <QString>
23
33
34//This class was originally based off Qt's QgsMarginsF class
35//It was forked in order to always use double values, rather than qreal values.
36
37class CORE_EXPORT QgsMargins
38{
39 public:
40
44 QgsMargins() = default;
45
53 QgsMargins( double left, double top, double right, double bottom )
54 : mLeft( left )
55 , mTop( top )
56 , mRight( right )
57 , mBottom( bottom )
58 {}
59
63 bool isNull() const
64 {
65 return qgsDoubleNear( mLeft, 0.0 ) && qgsDoubleNear( mTop, 0.0 ) && qgsDoubleNear( mRight, 0.0 ) && qgsDoubleNear( mBottom, 0.0 );
66 }
67
72 double left() const { return mLeft; }
73
78 double top() const { return mTop; }
79
84 double right() const { return mRight; }
85
90 double bottom() const { return mBottom; }
91
96 void setLeft( double left ) { mLeft = left; }
97
102 void setTop( double top ) { mTop = top; }
103
108 void setRight( double right ) { mRight = right; }
109
114 void setBottom( double bottom ) { mBottom = bottom; }
115
120 inline QgsMargins &operator+=( const QgsMargins &margins );
121
126 inline QgsMargins &operator-=( const QgsMargins &margins );
127
131 inline QgsMargins &operator+=( double addend );
132
137 inline QgsMargins &operator-=( double subtrahend );
138
143 inline QgsMargins &operator*=( double factor );
144
149 inline QgsMargins &operator/=( double divisor );
150
155 QString toString() const;
156
162 static QgsMargins fromString( const QString &string );
163
164#ifdef SIP_RUN
165 SIP_PYOBJECT __repr__();
166 % MethodCode
167 const QString str = QStringLiteral( "<QgsMargins: %1 %2 %3 %4>" ).arg( sipCpp->left() ).arg( sipCpp->top() ).arg( sipCpp->right() ).arg( sipCpp->bottom() );
168 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
169 % End
170#endif
171
172 private:
173 double mLeft = 0.0;
174 double mTop = 0.0;
175 double mRight = 0.0;
176 double mBottom = 0.0;
177};
178
179
183inline bool operator==( const QgsMargins &lhs, const QgsMargins &rhs )
184{
185 return qgsDoubleNear( lhs.left(), rhs.left() )
186 && qgsDoubleNear( lhs.top(), rhs.top() )
187 && qgsDoubleNear( lhs.right(), rhs.right() )
188 && qgsDoubleNear( lhs.bottom(), rhs.bottom() );
189}
190
194inline bool operator!=( const QgsMargins &lhs, const QgsMargins &rhs )
195{
196 return !operator==( lhs, rhs );
197}
198
203inline QgsMargins operator+( const QgsMargins &m1, const QgsMargins &m2 )
204{
205 return QgsMargins( m1.left() + m2.left(), m1.top() + m2.top(),
206 m1.right() + m2.right(), m1.bottom() + m2.bottom() );
207}
208
213inline QgsMargins operator-( const QgsMargins &m1, const QgsMargins &m2 )
214{
215 return QgsMargins( m1.left() - m2.left(), m1.top() - m2.top(),
216 m1.right() - m2.right(), m1.bottom() - m2.bottom() );
217}
218
222inline QgsMargins operator+( const QgsMargins &lhs, double rhs )
223{
224 return QgsMargins( lhs.left() + rhs, lhs.top() + rhs,
225 lhs.right() + rhs, lhs.bottom() + rhs );
226}
227
231inline QgsMargins operator+( double lhs, const QgsMargins &rhs )
232{
233 return QgsMargins( rhs.left() + lhs, rhs.top() + lhs,
234 rhs.right() + lhs, rhs.bottom() + lhs );
235}
236
240inline QgsMargins operator-( const QgsMargins &lhs, double rhs )
241{
242 return QgsMargins( lhs.left() - rhs, lhs.top() - rhs,
243 lhs.right() - rhs, lhs.bottom() - rhs );
244}
245
250inline QgsMargins operator*( const QgsMargins &margins, double factor )
251{
252 return QgsMargins( margins.left() * factor, margins.top() * factor,
253 margins.right() * factor, margins.bottom() * factor );
254}
255
260inline QgsMargins operator*( double factor, const QgsMargins &margins )
261{
262 return QgsMargins( margins.left() * factor, margins.top() * factor,
263 margins.right() * factor, margins.bottom() * factor );
264}
265
270inline QgsMargins operator/( const QgsMargins &margins, double divisor )
271{
272 return QgsMargins( margins.left() / divisor, margins.top() / divisor,
273 margins.right() / divisor, margins.bottom() / divisor );
274}
275
277{
278 return *this = *this + margins;
279}
280
282{
283 return *this = *this - margins;
284}
285
287{
288 mLeft += addend;
289 mTop += addend;
290 mRight += addend;
291 mBottom += addend;
292 return *this;
293}
294
295inline QgsMargins &QgsMargins::operator-=( double subtrahend ) SIP_SKIP
296{
297 mLeft -= subtrahend;
298 mTop -= subtrahend;
299 mRight -= subtrahend;
300 mBottom -= subtrahend;
301 return *this;
302}
303
305{
306 return *this = *this * factor;
307}
308
310{
311 return *this = *this / divisor;
312}
313
317inline QgsMargins operator+( const QgsMargins &margins )
318{
319 return margins;
320}
321
325inline QgsMargins operator-( const QgsMargins &margins )
326{
327 return QgsMargins( -margins.left(), -margins.top(), -margins.right(), -margins.bottom() );
328}
329
331
332#endif // QGSMARGINS_H
Defines the four margins of a rectangle.
Definition qgsmargins.h:38
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:114
QgsMargins & operator*=(double factor)
Multiplies each component of this object by factor and returns a reference to it.
Definition qgsmargins.h:304
double top() const
Returns the top margin.
Definition qgsmargins.h:78
void setLeft(double left)
Sets the left margin to left.
Definition qgsmargins.h:96
bool isNull() const
Returns true if all margins are is 0; otherwise returns false.
Definition qgsmargins.h:63
double right() const
Returns the right margin.
Definition qgsmargins.h:84
double bottom() const
Returns the bottom margin.
Definition qgsmargins.h:90
QgsMargins & operator/=(double divisor)
Multiplies each component of this object by factor and returns a reference to it.
Definition qgsmargins.h:309
QgsMargins(double left, double top, double right, double bottom)
Constructs margins with the given left, top, right, bottom.
Definition qgsmargins.h:53
void setRight(double right)
Sets the right margin to right.
Definition qgsmargins.h:108
void setTop(double top)
Sets the top margin to top.
Definition qgsmargins.h:102
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:276
QgsMargins & operator-=(const QgsMargins &margins)
Subtract each component of margins from the respective component of this object and returns a referen...
Definition qgsmargins.h:281
double left() const
Returns the left margin.
Definition qgsmargins.h:72
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
#define SIP_SKIP
Definition qgis_sip.h:134
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:250
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:213
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:270
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:203
bool operator!=(const QgsMargins &lhs, const QgsMargins &rhs)
Returns true if lhs and rhs are different; otherwise returns false.
Definition qgsmargins.h:194
bool operator==(const QgsMargins &lhs, const QgsMargins &rhs)
Returns true if lhs and rhs are equal; otherwise returns false.
Definition qgsmargins.h:183