|
| 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 | + |
| 23 | +/** |
| 24 | + * \ingroup core |
| 25 | + * \class QgsMargins |
| 26 | + * \brief The QgsMargins class defines the four margins of a rectangle. |
| 27 | + * |
| 28 | + * QgsMargins defines a set of four margins; left, top, right and bottom, that describe the size of the borders surrounding a rectangle. |
| 29 | + * |
| 30 | + * The isNull() function returns true only if all margins are set to zero. |
| 31 | + * \note Added in QGIS 3.0 |
| 32 | + */ |
| 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 | + |
| 37 | +class CORE_EXPORT QgsMargins |
| 38 | +{ |
| 39 | + public: |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructs a margins object with all margins set to 0. |
| 43 | + */ |
| 44 | + QgsMargins() = default; |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructs margins with the given \a left, \a top, \a right, \a bottom |
| 48 | + * @see setLeft() |
| 49 | + * @see setRight() |
| 50 | + * @see setTop() |
| 51 | + * @see setBottom() |
| 52 | + */ |
| 53 | + QgsMargins( double left, double top, double right, double bottom ) |
| 54 | + : mLeft( left ) |
| 55 | + , mTop( top ) |
| 56 | + , mRight( right ) |
| 57 | + , mBottom( bottom ) |
| 58 | + {} |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns \c true if all margins are is 0; otherwise returns false. |
| 62 | + */ |
| 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 | + |
| 68 | + /** |
| 69 | + * Returns the left margin. |
| 70 | + * @see setLeft() |
| 71 | + */ |
| 72 | + double left() const { return mLeft; } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns the top margin. |
| 76 | + * @see setTop() |
| 77 | + */ |
| 78 | + double top() const { return mTop; } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the right margin. |
| 82 | + * @see setRight() |
| 83 | + */ |
| 84 | + double right() const { return mRight; } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns the bottom margin. |
| 88 | + * @see setBottom() |
| 89 | + */ |
| 90 | + double bottom() const { return mBottom; } |
| 91 | + |
| 92 | + /** |
| 93 | + * Sets the left margin to \a left. |
| 94 | + * @see left() |
| 95 | + */ |
| 96 | + void setLeft( double left ) { mLeft = left; } |
| 97 | + |
| 98 | + /** |
| 99 | + * Sets the top margin to \a top. |
| 100 | + * @see top() |
| 101 | + */ |
| 102 | + void setTop( double top ) { mTop = top; } |
| 103 | + |
| 104 | + /** |
| 105 | + * Sets the right margin to \a right. |
| 106 | + * @see right() |
| 107 | + */ |
| 108 | + void setRight( double right ) { mRight = right; } |
| 109 | + |
| 110 | + /** |
| 111 | + * Sets the bottom margin to \a bottom. |
| 112 | + * @see bottom() |
| 113 | + */ |
| 114 | + void setBottom( double bottom ) { mBottom = bottom; } |
| 115 | + |
| 116 | + /** |
| 117 | + * Add each component of \a margins to the respective component of this object |
| 118 | + * and returns a reference to it. |
| 119 | + */ |
| 120 | + inline QgsMargins &operator+=( const QgsMargins &margins ); |
| 121 | + |
| 122 | + /** |
| 123 | + * Subtract each component of \a margins from the respective component of this object |
| 124 | + * and returns a reference to it. |
| 125 | + */ |
| 126 | + inline QgsMargins &operator-=( const QgsMargins &margins ); |
| 127 | + |
| 128 | + /** |
| 129 | + * Adds the \a addend to each component of this object and returns a reference to it. |
| 130 | + */ |
| 131 | + inline QgsMargins &operator+=( double addend ); |
| 132 | + |
| 133 | + /** |
| 134 | + * Subtracts the \a subtrahend from each component of this object |
| 135 | + * and returns a reference to it. |
| 136 | + */ |
| 137 | + inline QgsMargins &operator-=( double subtrahend ); |
| 138 | + |
| 139 | + /** |
| 140 | + * Multiplies each component of this object by \a factor |
| 141 | + * and returns a reference to it. |
| 142 | + */ |
| 143 | + inline QgsMargins &operator*=( double factor ); |
| 144 | + |
| 145 | + /** |
| 146 | + * Multiplies each component of this object by \a factor |
| 147 | + * and returns a reference to it. |
| 148 | + */ |
| 149 | + inline QgsMargins &operator/=( double divisor ); |
| 150 | + |
| 151 | + /** |
| 152 | + * Returns the margins encoded to a string. |
| 153 | + * @see fromString() |
| 154 | + */ |
| 155 | + QString toString() const; |
| 156 | + |
| 157 | + /** |
| 158 | + * Returns a QgsMargins object decoded from a string, or a null QgsMargins |
| 159 | + * if the string could not be interpreted as margins. |
| 160 | + * @see toString() |
| 161 | + */ |
| 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 | +/** |
| 172 | + * Returns \c true if \a lhs and \a rhs are equal; otherwise returns \c false. |
| 173 | + */ |
| 174 | +inline bool operator==( const QgsMargins &lhs, const QgsMargins &rhs ) |
| 175 | +{ |
| 176 | + return qgsDoubleNear( lhs.left(), rhs.left() ) |
| 177 | + && qgsDoubleNear( lhs.top(), rhs.top() ) |
| 178 | + && qgsDoubleNear( lhs.right(), rhs.right() ) |
| 179 | + && qgsDoubleNear( lhs.bottom(), rhs.bottom() ); |
| 180 | +} |
| 181 | + |
| 182 | +/** |
| 183 | + * Returns \c true if \a lhs and \a rhs are different; otherwise returns \c false. |
| 184 | + */ |
| 185 | +inline bool operator!=( const QgsMargins &lhs, const QgsMargins &rhs ) |
| 186 | +{ |
| 187 | + return !operator==( lhs, rhs ); |
| 188 | +} |
| 189 | + |
| 190 | +/** |
| 191 | + * Returns a QgsMargins object that is the sum of the given margins, \a m1 |
| 192 | + * and \a m2; each component is added separately. |
| 193 | + */ |
| 194 | +inline QgsMargins operator+( const QgsMargins &m1, const QgsMargins &m2 ) |
| 195 | +{ |
| 196 | + return QgsMargins( m1.left() + m2.left(), m1.top() + m2.top(), |
| 197 | + m1.right() + m2.right(), m1.bottom() + m2.bottom() ); |
| 198 | +} |
| 199 | + |
| 200 | +/** |
| 201 | + * Returns a QgsMargins object that is formed by subtracting \a m2 from |
| 202 | + * \a m1; each component is subtracted separately. |
| 203 | + */ |
| 204 | +inline QgsMargins operator-( const QgsMargins &m1, const QgsMargins &m2 ) |
| 205 | +{ |
| 206 | + return QgsMargins( m1.left() - m2.left(), m1.top() - m2.top(), |
| 207 | + m1.right() - m2.right(), m1.bottom() - m2.bottom() ); |
| 208 | +} |
| 209 | + |
| 210 | +/** |
| 211 | + * Returns a QgsMargins object that is formed by adding \a rhs to \a lhs. |
| 212 | + */ |
| 213 | +inline QgsMargins operator+( const QgsMargins &lhs, double rhs ) |
| 214 | +{ |
| 215 | + return QgsMargins( lhs.left() + rhs, lhs.top() + rhs, |
| 216 | + lhs.right() + rhs, lhs.bottom() + rhs ); |
| 217 | +} |
| 218 | + |
| 219 | +/** |
| 220 | + * Returns a QgsMargins object that is formed by adding \a lhs to \a rhs. |
| 221 | + */ |
| 222 | +inline QgsMargins operator+( double lhs, const QgsMargins &rhs ) |
| 223 | +{ |
| 224 | + return QgsMargins( rhs.left() + lhs, rhs.top() + lhs, |
| 225 | + rhs.right() + lhs, rhs.bottom() + lhs ); |
| 226 | +} |
| 227 | + |
| 228 | +/** |
| 229 | + * Returns a QgsMargins object that is formed by subtracting \a rhs from \a lhs. |
| 230 | + */ |
| 231 | +inline QgsMargins operator-( const QgsMargins &lhs, double rhs ) |
| 232 | +{ |
| 233 | + return QgsMargins( lhs.left() - rhs, lhs.top() - rhs, |
| 234 | + lhs.right() - rhs, lhs.bottom() - rhs ); |
| 235 | +} |
| 236 | + |
| 237 | +/** |
| 238 | + * Returns a QgsMargins object that is formed by multiplying each component |
| 239 | + * of the given \a margins by \a factor. |
| 240 | + */ |
| 241 | +inline QgsMargins operator*( const QgsMargins &margins, double factor ) |
| 242 | +{ |
| 243 | + return QgsMargins( margins.left() * factor, margins.top() * factor, |
| 244 | + margins.right() * factor, margins.bottom() * factor ); |
| 245 | +} |
| 246 | + |
| 247 | +/** |
| 248 | + * Returns a QgsMargins object that is formed by multiplying each component |
| 249 | + * of the given \a margins by \a factor. |
| 250 | + */ |
| 251 | +inline QgsMargins operator*( double factor, const QgsMargins &margins ) |
| 252 | +{ |
| 253 | + return QgsMargins( margins.left() * factor, margins.top() * factor, |
| 254 | + margins.right() * factor, margins.bottom() * factor ); |
| 255 | +} |
| 256 | + |
| 257 | +/** |
| 258 | + * Returns a QgsMargins object that is formed by dividing the components of |
| 259 | + * the given \a margins by the given \a divisor. |
| 260 | + */ |
| 261 | +inline QgsMargins operator/( const QgsMargins &margins, double divisor ) |
| 262 | +{ |
| 263 | + return QgsMargins( margins.left() / divisor, margins.top() / divisor, |
| 264 | + margins.right() / divisor, margins.bottom() / divisor ); |
| 265 | +} |
| 266 | + |
| 267 | +inline QgsMargins& QgsMargins::operator+=( const QgsMargins & margins ) |
| 268 | +{ |
| 269 | + return *this = *this + margins; |
| 270 | +} |
| 271 | + |
| 272 | +inline QgsMargins& QgsMargins::operator-=( const QgsMargins & margins ) |
| 273 | +{ |
| 274 | + return *this = *this - margins; |
| 275 | +} |
| 276 | + |
| 277 | +inline QgsMargins& QgsMargins::operator+=( double addend ) |
| 278 | +{ |
| 279 | + mLeft += addend; |
| 280 | + mTop += addend; |
| 281 | + mRight += addend; |
| 282 | + mBottom += addend; |
| 283 | + return *this; |
| 284 | +} |
| 285 | + |
| 286 | +inline QgsMargins& QgsMargins::operator-=( double subtrahend ) |
| 287 | +{ |
| 288 | + mLeft -= subtrahend; |
| 289 | + mTop -= subtrahend; |
| 290 | + mRight -= subtrahend; |
| 291 | + mBottom -= subtrahend; |
| 292 | + return *this; |
| 293 | +} |
| 294 | + |
| 295 | +inline QgsMargins& QgsMargins::operator*=( double factor ) |
| 296 | +{ |
| 297 | + return *this = *this * factor; |
| 298 | +} |
| 299 | + |
| 300 | +inline QgsMargins& QgsMargins::operator/=( double divisor ) |
| 301 | +{ |
| 302 | + return *this = *this / divisor; |
| 303 | +} |
| 304 | + |
| 305 | +/** |
| 306 | + * Returns a QgsMargins object that is formed from all components of \a margins. |
| 307 | + */ |
| 308 | +inline QgsMargins operator+( const QgsMargins &margins ) |
| 309 | +{ |
| 310 | + return margins; |
| 311 | +} |
| 312 | + |
| 313 | +/** |
| 314 | + * Returns a QgsMargins object that is formed by negating all components of \a margins. |
| 315 | + */ |
| 316 | +inline QgsMargins operator-( const QgsMargins &margins ) |
| 317 | +{ |
| 318 | + return QgsMargins( -margins.left(), -margins.top(), -margins.right(), -margins.bottom() ); |
| 319 | +} |
| 320 | + |
| 321 | +Q_DECLARE_TYPEINFO( QgsMargins, Q_MOVABLE_TYPE ); |
| 322 | + |
| 323 | +#endif // QGSMARGINS_H |
0 commit comments