Skip to content
Permalink
Browse files

New class QgsMargins for storing margins (left/right/top/bottom)

Basically a direct port of QMarginF, but forced to always use
double values, and with added toString()/fromString() methods.
  • Loading branch information
nyalldawson committed Jan 30, 2017
1 parent 7a2be42 commit a7be9968b810cb7b1984f5806afe748393fb192f
@@ -93,6 +93,7 @@
%Include qgsmapsettings.sip
%Include qgsmaptopixel.sip
%Include qgsmapunitscale.sip
%Include qgsmargins.sip
%Include qgsmessagelog.sip
%Include qgsmessageoutput.sip
%Include qgsmimedatautils.sip
@@ -0,0 +1,47 @@
class QgsMargins
{
%TypeHeaderCode
#include <qgsmargins.h>
%End

public:

QgsMargins();
QgsMargins( double left, double top, double right, double bottom );

bool isNull() const;

double left() const;
double top() const;
double right() const;
double bottom() const;

void setLeft( double left );
void setTop( double top );
void setRight( double right );
void setBottom( double bottom );

QgsMargins &operator+=( const QgsMargins &margins );
QgsMargins &operator-=( const QgsMargins &margins );
QgsMargins &operator+=( double addend );
QgsMargins &operator-=( double subtrahend );
QgsMargins &operator*=( double factor );
QgsMargins &operator/=( double divisor );

QString toString() const;
static QgsMargins fromString( const QString& string );
};

bool operator==( const QgsMargins &lhs, const QgsMargins &rhs );
bool operator!=( const QgsMargins &lhs, const QgsMargins &rhs );
QgsMargins operator+( const QgsMargins &m1, const QgsMargins &m2 );
QgsMargins operator-( const QgsMargins &m1, const QgsMargins &m2 );
QgsMargins operator+( const QgsMargins &lhs, double rhs );
QgsMargins operator+( double lhs, const QgsMargins &rhs );
QgsMargins operator-( const QgsMargins &lhs, double rhs );
QgsMargins operator*( const QgsMargins &margins, double factor );
QgsMargins operator*( double factor, const QgsMargins &margins );
QgsMargins operator/( const QgsMargins &margins, double divisor );
QgsMargins operator+( const QgsMargins &margins );
QgsMargins operator-( const QgsMargins &margins );

@@ -170,6 +170,7 @@ SET(QGIS_CORE_SRCS
qgsmaptopixel.cpp
qgsmaptopixelgeometrysimplifier.cpp
qgsmapunitscale.cpp
qgsmargins.cpp
qgsmessagelog.cpp
qgsmessageoutput.cpp
qgsmimedatautils.cpp
@@ -714,6 +715,7 @@ SET(QGIS_CORE_HDRS
qgsmaptopixel.h
qgsmaptopixelgeometrysimplifier.h
qgsmapunitscale.h
qgsmargins.h
qgsmimedatautils.h
qgsmultirenderchecker.h
qgsobjectcustomproperties.h
@@ -0,0 +1,37 @@
/***************************************************************************
qgsmargins.cpp
--------------
Date : January 2017
Copyright : (C) 2017 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsmargins.h"

QString QgsMargins::toString() const
{
if ( isNull() )
return QString();
else
return QStringLiteral( "%1,%2,%3,%4" ).arg( qgsDoubleToString( mLeft ), qgsDoubleToString( mTop ),
qgsDoubleToString( mRight ), qgsDoubleToString( mBottom ) );
}

QgsMargins QgsMargins::fromString( const QString& string )
{
QStringList margins = string.split( ',' );
if ( margins.count() != 4 )
return QgsMargins();

return QgsMargins( margins.at( 0 ).toDouble(),
margins.at( 1 ).toDouble(),
margins.at( 2 ).toDouble(),
margins.at( 3 ).toDouble() );
}

0 comments on commit a7be996

Please sign in to comment.
You can’t perform that action at this time.