QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
24using namespace Qt::StringLiterals;
25
35
36//This class was originally based off Qt's QgsMarginsF class
37//It was forked in order to always use double values, rather than qreal values.
38
39class CORE_EXPORT QgsMargins
40{
41 public:
45 QgsMargins() = default;
46
54 QgsMargins( double left, double top, double right, double bottom )
55 : mLeft( left )
56 , mTop( top )
57 , mRight( right )
58 , mBottom( bottom )
59 {}
60
64 bool isNull() const { return qgsDoubleNear( mLeft, 0.0 ) && qgsDoubleNear( mTop, 0.0 ) && qgsDoubleNear( mRight, 0.0 ) && qgsDoubleNear( mBottom, 0.0 ); }
65
70 double left() const { return mLeft; }
71
76 double top() const { return mTop; }
77
82 double right() const { return mRight; }
83
88 double bottom() const { return mBottom; }
89
94 void setLeft( double left ) { mLeft = left; }
95
100 void setTop( double top ) { mTop = top; }
101
106 void setRight( double right ) { mRight = right; }
107
112 void setBottom( double bottom ) { mBottom = bottom; }
113
118 inline QgsMargins &operator+=( const QgsMargins &margins );
119
124 inline QgsMargins &operator-=( const QgsMargins &margins );
125
129 inline QgsMargins &operator+=( double addend );
130
135 inline QgsMargins &operator-=( double subtrahend );
136
141 inline QgsMargins &operator*=( double factor );
142
147 inline QgsMargins &operator/=( double divisor );
148
153 QString toString() const;
154
160 static QgsMargins fromString( const QString &string );
161
162#ifdef SIP_RUN
163 // clang-format off
164 SIP_PYOBJECT __repr__();
165 % MethodCode
166 const QString str = u"<QgsMargins: %1 %2 %3 %4>"_s.arg( sipCpp->left() ).arg( sipCpp->top() ).arg( sipCpp->right() ).arg( sipCpp->bottom() );
167 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
168 % End
169// clang-format on
170#endif
171
172 // clang-format off
173 private:
174 // clang-format on
175 double mLeft
176 = 0.0;
177 double mTop = 0.0;
178 double mRight = 0.0;
179 double mBottom = 0.0;
180};
181
182
186inline bool operator==( const QgsMargins &lhs, const QgsMargins &rhs )
187{
188 return qgsDoubleNear( lhs.left(), rhs.left() ) && qgsDoubleNear( lhs.top(), rhs.top() ) && qgsDoubleNear( lhs.right(), rhs.right() ) && 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(), m1.right() + m2.right(), m1.bottom() + m2.bottom() );
206}
207
212inline QgsMargins operator-( const QgsMargins &m1, const QgsMargins &m2 )
213{
214 return QgsMargins( m1.left() - m2.left(), m1.top() - m2.top(), m1.right() - m2.right(), m1.bottom() - m2.bottom() );
215}
216
220inline QgsMargins operator+( const QgsMargins &lhs, double rhs )
221{
222 return QgsMargins( lhs.left() + rhs, lhs.top() + rhs, lhs.right() + rhs, lhs.bottom() + rhs );
223}
224
228inline QgsMargins operator+( double lhs, const QgsMargins &rhs )
229{
230 return QgsMargins( rhs.left() + lhs, rhs.top() + lhs, rhs.right() + lhs, rhs.bottom() + lhs );
231}
232
236inline QgsMargins operator-( const QgsMargins &lhs, double rhs )
237{
238 return QgsMargins( lhs.left() - rhs, lhs.top() - rhs, lhs.right() - rhs, lhs.bottom() - rhs );
239}
240
245inline QgsMargins operator*( const QgsMargins &margins, double factor )
246{
247 return QgsMargins( margins.left() * factor, margins.top() * factor, margins.right() * factor, margins.bottom() * factor );
248}
249
254inline QgsMargins operator*( double factor, const QgsMargins &margins )
255{
256 return QgsMargins( margins.left() * factor, margins.top() * factor, margins.right() * factor, margins.bottom() * factor );
257}
258
263inline QgsMargins operator/( const QgsMargins &margins, double divisor )
264{
265 return QgsMargins( margins.left() / divisor, margins.top() / divisor, margins.right() / divisor, margins.bottom() / divisor );
266}
267
269{
270 return *this = *this + margins;
271}
272
274{
275 return *this = *this - margins;
276}
277
279{
280 mLeft += addend;
281 mTop += addend;
282 mRight += addend;
283 mBottom += addend;
284 return *this;
285}
286
287inline QgsMargins &QgsMargins::operator-=( double subtrahend ) SIP_SKIP
288{
289 mLeft -= subtrahend;
290 mTop -= subtrahend;
291 mRight -= subtrahend;
292 mBottom -= subtrahend;
293 return *this;
294}
295
297{
298 return *this = *this * factor;
299}
300
302{
303 return *this = *this / divisor;
304}
305
309inline QgsMargins operator+( const QgsMargins &margins )
310{
311 return margins;
312}
313
317inline QgsMargins operator-( const QgsMargins &margins )
318{
319 return QgsMargins( -margins.left(), -margins.top(), -margins.right(), -margins.bottom() );
320}
321
323
324#endif // QGSMARGINS_H
Defines the four margins of a rectangle.
Definition qgsmargins.h:40
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:112
QgsMargins & operator*=(double factor)
Multiplies each component of this object by factor and returns a reference to it.
Definition qgsmargins.h:296
double top() const
Returns the top margin.
Definition qgsmargins.h:76
void setLeft(double left)
Sets the left margin to left.
Definition qgsmargins.h:94
bool isNull() const
Returns true if all margins are is 0; otherwise returns false.
Definition qgsmargins.h:64
double right() const
Returns the right margin.
Definition qgsmargins.h:82
double bottom() const
Returns the bottom margin.
Definition qgsmargins.h:88
QgsMargins & operator/=(double divisor)
Multiplies each component of this object by factor and returns a reference to it.
Definition qgsmargins.h:301
QgsMargins(double left, double top, double right, double bottom)
Constructs margins with the given left, top, right, bottom.
Definition qgsmargins.h:54
void setRight(double right)
Sets the right margin to right.
Definition qgsmargins.h:106
void setTop(double top)
Sets the top margin to top.
Definition qgsmargins.h:100
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:268
QgsMargins & operator-=(const QgsMargins &margins)
Subtract each component of margins from the respective component of this object and returns a referen...
Definition qgsmargins.h:273
double left() const
Returns the left margin.
Definition qgsmargins.h:70
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975
#define SIP_SKIP
Definition qgis_sip.h:133
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:245
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:212
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:263
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:186