QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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:
42
46 QgsMargins() = default;
47
55 QgsMargins( double left, double top, double right, double bottom )
56 : mLeft( left )
57 , mTop( top )
58 , mRight( right )
59 , mBottom( bottom )
60 {}
61
65 bool isNull() const
66 {
67 return qgsDoubleNear( mLeft, 0.0 ) && qgsDoubleNear( mTop, 0.0 ) && qgsDoubleNear( mRight, 0.0 ) && qgsDoubleNear( mBottom, 0.0 );
68 }
69
74 double left() const { return mLeft; }
75
80 double top() const { return mTop; }
81
86 double right() const { return mRight; }
87
92 double bottom() const { return mBottom; }
93
98 void setLeft( double left ) { mLeft = left; }
99
104 void setTop( double top ) { mTop = top; }
105
110 void setRight( double right ) { mRight = right; }
111
116 void setBottom( double bottom ) { mBottom = bottom; }
117
122 inline QgsMargins &operator+=( const QgsMargins &margins );
123
128 inline QgsMargins &operator-=( const QgsMargins &margins );
129
133 inline QgsMargins &operator+=( double addend );
134
139 inline QgsMargins &operator-=( double subtrahend );
140
145 inline QgsMargins &operator*=( double factor );
146
151 inline QgsMargins &operator/=( double divisor );
152
157 QString toString() const;
158
164 static QgsMargins fromString( const QString &string );
165
166#ifdef SIP_RUN
167 SIP_PYOBJECT __repr__();
168 % MethodCode
169 const QString str = u"<QgsMargins: %1 %2 %3 %4>"_s.arg( sipCpp->left() ).arg( sipCpp->top() ).arg( sipCpp->right() ).arg( sipCpp->bottom() );
170 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
171 % End
172#endif
173
174 private:
175 double mLeft = 0.0;
176 double mTop = 0.0;
177 double mRight = 0.0;
178 double mBottom = 0.0;
179};
180
181
185inline bool operator==( const QgsMargins &lhs, const QgsMargins &rhs )
186{
187 return qgsDoubleNear( lhs.left(), rhs.left() )
188 && qgsDoubleNear( lhs.top(), rhs.top() )
189 && qgsDoubleNear( lhs.right(), rhs.right() )
190 && qgsDoubleNear( lhs.bottom(), rhs.bottom() );
191}
192
196inline bool operator!=( const QgsMargins &lhs, const QgsMargins &rhs )
197{
198 return !operator==( lhs, rhs );
199}
200
205inline QgsMargins operator+( const QgsMargins &m1, const QgsMargins &m2 )
206{
207 return QgsMargins( m1.left() + m2.left(), m1.top() + m2.top(),
208 m1.right() + m2.right(), m1.bottom() + m2.bottom() );
209}
210
215inline QgsMargins operator-( const QgsMargins &m1, const QgsMargins &m2 )
216{
217 return QgsMargins( m1.left() - m2.left(), m1.top() - m2.top(),
218 m1.right() - m2.right(), m1.bottom() - m2.bottom() );
219}
220
224inline QgsMargins operator+( const QgsMargins &lhs, double rhs )
225{
226 return QgsMargins( lhs.left() + rhs, lhs.top() + rhs,
227 lhs.right() + rhs, lhs.bottom() + rhs );
228}
229
233inline QgsMargins operator+( double lhs, const QgsMargins &rhs )
234{
235 return QgsMargins( rhs.left() + lhs, rhs.top() + lhs,
236 rhs.right() + lhs, rhs.bottom() + lhs );
237}
238
242inline QgsMargins operator-( const QgsMargins &lhs, double rhs )
243{
244 return QgsMargins( lhs.left() - rhs, lhs.top() - rhs,
245 lhs.right() - rhs, lhs.bottom() - rhs );
246}
247
252inline QgsMargins operator*( const QgsMargins &margins, double factor )
253{
254 return QgsMargins( margins.left() * factor, margins.top() * factor,
255 margins.right() * factor, margins.bottom() * factor );
256}
257
262inline QgsMargins operator*( double factor, const QgsMargins &margins )
263{
264 return QgsMargins( margins.left() * factor, margins.top() * factor,
265 margins.right() * factor, margins.bottom() * factor );
266}
267
272inline QgsMargins operator/( const QgsMargins &margins, double divisor )
273{
274 return QgsMargins( margins.left() / divisor, margins.top() / divisor,
275 margins.right() / divisor, margins.bottom() / divisor );
276}
277
279{
280 return *this = *this + margins;
281}
282
284{
285 return *this = *this - margins;
286}
287
289{
290 mLeft += addend;
291 mTop += addend;
292 mRight += addend;
293 mBottom += addend;
294 return *this;
295}
296
297inline QgsMargins &QgsMargins::operator-=( double subtrahend ) SIP_SKIP
298{
299 mLeft -= subtrahend;
300 mTop -= subtrahend;
301 mRight -= subtrahend;
302 mBottom -= subtrahend;
303 return *this;
304}
305
307{
308 return *this = *this * factor;
309}
310
312{
313 return *this = *this / divisor;
314}
315
319inline QgsMargins operator+( const QgsMargins &margins )
320{
321 return margins;
322}
323
327inline QgsMargins operator-( const QgsMargins &margins )
328{
329 return QgsMargins( -margins.left(), -margins.top(), -margins.right(), -margins.bottom() );
330}
331
333
334#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:116
QgsMargins & operator*=(double factor)
Multiplies each component of this object by factor and returns a reference to it.
Definition qgsmargins.h:306
double top() const
Returns the top margin.
Definition qgsmargins.h:80
void setLeft(double left)
Sets the left margin to left.
Definition qgsmargins.h:98
bool isNull() const
Returns true if all margins are is 0; otherwise returns false.
Definition qgsmargins.h:65
double right() const
Returns the right margin.
Definition qgsmargins.h:86
double bottom() const
Returns the bottom margin.
Definition qgsmargins.h:92
QgsMargins & operator/=(double divisor)
Multiplies each component of this object by factor and returns a reference to it.
Definition qgsmargins.h:311
QgsMargins(double left, double top, double right, double bottom)
Constructs margins with the given left, top, right, bottom.
Definition qgsmargins.h:55
void setRight(double right)
Sets the right margin to right.
Definition qgsmargins.h:110
void setTop(double top)
Sets the top margin to top.
Definition qgsmargins.h:104
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:278
QgsMargins & operator-=(const QgsMargins &margins)
Subtract each component of margins from the respective component of this object and returns a referen...
Definition qgsmargins.h:283
double left() const
Returns the left margin.
Definition qgsmargins.h:74
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6924
#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:252
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:215
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:272
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:205
bool operator!=(const QgsMargins &lhs, const QgsMargins &rhs)
Returns true if lhs and rhs are different; otherwise returns false.
Definition qgsmargins.h:196
bool operator==(const QgsMargins &lhs, const QgsMargins &rhs)
Returns true if lhs and rhs are equal; otherwise returns false.
Definition qgsmargins.h:185