QGIS API Documentation 3.41.0-Master (3440c17df1d)
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_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#ifdef SIP_RUN
164 SIP_PYOBJECT __repr__();
165 % MethodCode
166 const QString str = QStringLiteral( "<QgsMargins: %1 %2 %3 %4>" ).arg( sipCpp->left() ).arg( sipCpp->top() ).arg( sipCpp->right() ).arg( sipCpp->bottom() );
167 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
168 % End
169#endif
170
171 private:
172 double mLeft = 0.0;
173 double mTop = 0.0;
174 double mRight = 0.0;
175 double mBottom = 0.0;
176};
177
178
182inline bool operator==( const QgsMargins &lhs, const QgsMargins &rhs )
183{
184 return qgsDoubleNear( lhs.left(), rhs.left() )
185 && qgsDoubleNear( lhs.top(), rhs.top() )
186 && qgsDoubleNear( lhs.right(), rhs.right() )
187 && qgsDoubleNear( lhs.bottom(), rhs.bottom() );
188}
189
193inline bool operator!=( const QgsMargins &lhs, const QgsMargins &rhs )
194{
195 return !operator==( lhs, rhs );
196}
197
202inline QgsMargins operator+( const QgsMargins &m1, const QgsMargins &m2 )
203{
204 return QgsMargins( m1.left() + m2.left(), m1.top() + m2.top(),
205 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(),
215 m1.right() - m2.right(), m1.bottom() - m2.bottom() );
216}
217
221inline QgsMargins operator+( const QgsMargins &lhs, double rhs )
222{
223 return QgsMargins( lhs.left() + rhs, lhs.top() + rhs,
224 lhs.right() + rhs, lhs.bottom() + rhs );
225}
226
230inline QgsMargins operator+( double lhs, const QgsMargins &rhs )
231{
232 return QgsMargins( rhs.left() + lhs, rhs.top() + lhs,
233 rhs.right() + lhs, rhs.bottom() + lhs );
234}
235
239inline QgsMargins operator-( const QgsMargins &lhs, double rhs )
240{
241 return QgsMargins( lhs.left() - rhs, lhs.top() - rhs,
242 lhs.right() - rhs, lhs.bottom() - rhs );
243}
244
249inline QgsMargins operator*( const QgsMargins &margins, double factor )
250{
251 return QgsMargins( margins.left() * factor, margins.top() * factor,
252 margins.right() * factor, margins.bottom() * factor );
253}
254
259inline QgsMargins operator*( double factor, const QgsMargins &margins )
260{
261 return QgsMargins( margins.left() * factor, margins.top() * factor,
262 margins.right() * factor, margins.bottom() * factor );
263}
264
269inline QgsMargins operator/( const QgsMargins &margins, double divisor )
270{
271 return QgsMargins( margins.left() / divisor, margins.top() / divisor,
272 margins.right() / divisor, margins.bottom() / divisor );
273}
274
276{
277 return *this = *this + margins;
278}
279
281{
282 return *this = *this - margins;
283}
284
286{
287 mLeft += addend;
288 mTop += addend;
289 mRight += addend;
290 mBottom += addend;
291 return *this;
292}
293
294inline QgsMargins &QgsMargins::operator-=( double subtrahend ) SIP_SKIP
295{
296 mLeft -= subtrahend;
297 mTop -= subtrahend;
298 mRight -= subtrahend;
299 mBottom -= subtrahend;
300 return *this;
301}
302
304{
305 return *this = *this * factor;
306}
307
309{
310 return *this = *this / divisor;
311}
312
316inline QgsMargins operator+( const QgsMargins &margins )
317{
318 return margins;
319}
320
324inline QgsMargins operator-( const QgsMargins &margins )
325{
326 return QgsMargins( -margins.left(), -margins.top(), -margins.right(), -margins.bottom() );
327}
328
330
331#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:303
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:308
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:275
QgsMargins & operator-=(const QgsMargins &margins)
Subtract each component of margins from the respective component of this object and returns a referen...
Definition qgsmargins.h:280
double left() const
Returns the left margin.
Definition qgsmargins.h:71
#define str(x)
Definition qgis.cpp:39
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
#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:249
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:269
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:202
bool operator!=(const QgsMargins &lhs, const QgsMargins &rhs)
Returns true if lhs and rhs are different; otherwise returns false.
Definition qgsmargins.h:193
bool operator==(const QgsMargins &lhs, const QgsMargins &rhs)
Returns true if lhs and rhs are equal; otherwise returns false.
Definition qgsmargins.h:182