QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
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 
37 class 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  private:
165  double mLeft = 0.0;
166  double mTop = 0.0;
167  double mRight = 0.0;
168  double mBottom = 0.0;
169 };
170 
171 
175 inline bool operator==( const QgsMargins &lhs, const QgsMargins &rhs )
176 {
177  return qgsDoubleNear( lhs.left(), rhs.left() )
178  && qgsDoubleNear( lhs.top(), rhs.top() )
179  && qgsDoubleNear( lhs.right(), rhs.right() )
180  && qgsDoubleNear( lhs.bottom(), rhs.bottom() );
181 }
182 
186 inline bool operator!=( const QgsMargins &lhs, const QgsMargins &rhs )
187 {
188  return !operator==( lhs, rhs );
189 }
190 
195 inline QgsMargins operator+( const QgsMargins &m1, const QgsMargins &m2 )
196 {
197  return QgsMargins( m1.left() + m2.left(), m1.top() + m2.top(),
198  m1.right() + m2.right(), m1.bottom() + m2.bottom() );
199 }
200 
205 inline 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 
214 inline QgsMargins operator+( const QgsMargins &lhs, double rhs )
215 {
216  return QgsMargins( lhs.left() + rhs, lhs.top() + rhs,
217  lhs.right() + rhs, lhs.bottom() + rhs );
218 }
219 
223 inline QgsMargins operator+( double lhs, const QgsMargins &rhs )
224 {
225  return QgsMargins( rhs.left() + lhs, rhs.top() + lhs,
226  rhs.right() + lhs, rhs.bottom() + lhs );
227 }
228 
232 inline QgsMargins operator-( const QgsMargins &lhs, double rhs )
233 {
234  return QgsMargins( lhs.left() - rhs, lhs.top() - rhs,
235  lhs.right() - rhs, lhs.bottom() - rhs );
236 }
237 
242 inline QgsMargins operator*( const QgsMargins &margins, double factor )
243 {
244  return QgsMargins( margins.left() * factor, margins.top() * factor,
245  margins.right() * factor, margins.bottom() * factor );
246 }
247 
252 inline QgsMargins operator*( double factor, const QgsMargins &margins )
253 {
254  return QgsMargins( margins.left() * factor, margins.top() * factor,
255  margins.right() * factor, margins.bottom() * factor );
256 }
257 
262 inline QgsMargins operator/( const QgsMargins &margins, double divisor )
263 {
264  return QgsMargins( margins.left() / divisor, margins.top() / divisor,
265  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 
287 inline 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 
301 inline QgsMargins &QgsMargins::operator/=( double divisor ) SIP_SKIP
302 {
303  return *this = *this / divisor;
304 }
305 
309 inline QgsMargins operator+( const QgsMargins &margins )
310 {
311  return margins;
312 }
313 
317 inline QgsMargins operator-( const QgsMargins &margins )
318 {
319  return QgsMargins( -margins.left(), -margins.top(), -margins.right(), -margins.bottom() );
320 }
321 
322 Q_DECLARE_TYPEINFO( QgsMargins, Q_MOVABLE_TYPE );
323 
324 #endif // QGSMARGINS_H
The QgsMargins class 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:296
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:301
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: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: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:1246
#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:242
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:205
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:262
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:195
bool operator!=(const QgsMargins &lhs, const QgsMargins &rhs)
Returns true if lhs and rhs are different; otherwise returns false.
Definition: qgsmargins.h:186
bool operator==(const QgsMargins &lhs, const QgsMargins &rhs)
Returns true if lhs and rhs are equal; otherwise returns false.
Definition: qgsmargins.h:175