Skip to content

Commit

Permalink
Add QgsLayoutUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 18, 2017
1 parent 6fd0698 commit c345613
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 2 deletions.
5 changes: 3 additions & 2 deletions python/core/core_auto.sip
Expand Up @@ -153,12 +153,13 @@
%Include composer/qgscomposermultiframecommand.sip
%Include composer/qgscomposertexttable.sip
%Include composer/qgspaperitem.sip
%Include layout/qgslayout.sip
%Include layout/qgslayoutcontext.sip
%Include layout/qgslayoutmeasurement.sip
%Include layout/qgslayoutmeasurementconverter.sip
%Include layout/qgspagesizeregistry.sip
%Include layout/qgslayoutpoint.sip
%Include layout/qgslayoutsize.sip
%Include layout/qgslayoututils.sip
%Include metadata/qgslayermetadata.sip
%Include metadata/qgslayermetadatavalidator.sip
%Include processing/qgsprocessing.sip
Expand Down Expand Up @@ -378,7 +379,7 @@
%Include gps/qgsgpsdetector.sip
%Include gps/qgsnmeaconnection.sip
%Include gps/qgsgpsdconnection.sip
%Include layout/qgslayoutcontext.sip
%Include layout/qgslayout.sip
%Include layout/qgslayoutitem.sip
%Include layout/qgslayoutitemregistry.sip
%Include layout/qgslayoutobject.sip
Expand Down
39 changes: 39 additions & 0 deletions python/core/layout/qgslayoututils.sip
@@ -0,0 +1,39 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/layout/qgslayoututils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



class QgsLayoutUtils
{
%Docstring
Utilities for layouts.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoututils.h"
%End
public:

static double normalizedAngle( const double angle, const bool allowNegative = false );
%Docstring
Ensures that an ``angle`` (in degrees) is in the range 0 <= angle < 360.
If ``allowNegative`` is true then angles between (-360, 360) are allowed. If false,
angles are converted to positive angles in the range [0, 360).
:rtype: float
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/layout/qgslayoututils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -356,6 +356,7 @@ SET(QGIS_CORE_SRCS
layout/qgslayoutmeasurement.cpp
layout/qgslayoutmeasurementconverter.cpp
layout/qgslayoutobject.cpp
layout/qgslayoututils.cpp
layout/qgspagesizeregistry.cpp
layout/qgslayoutpoint.cpp
layout/qgslayoutsize.cpp
Expand Down Expand Up @@ -923,6 +924,7 @@ SET(QGIS_CORE_HDRS
layout/qgspagesizeregistry.h
layout/qgslayoutpoint.h
layout/qgslayoutsize.h
layout/qgslayoututils.h

metadata/qgslayermetadata.h
metadata/qgslayermetadatavalidator.h
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgslayoutitem.cpp
Expand Up @@ -16,6 +16,7 @@

#include "qgslayoutitem.h"
#include "qgslayout.h"
#include "qgslayoututils.h"
#include <QPainter>

QgsLayoutItem::QgsLayoutItem( QgsLayout *layout )
Expand Down
33 changes: 33 additions & 0 deletions src/core/layout/qgslayoututils.cpp
@@ -0,0 +1,33 @@
/***************************************************************************
qgslayoututils.cpp
------------------
begin : July 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 "qgslayoututils.h"
#include <math.h>

double QgsLayoutUtils::normalizedAngle( const double angle, const bool allowNegative )
{
double clippedAngle = angle;
if ( clippedAngle >= 360.0 || clippedAngle <= -360.0 )
{
clippedAngle = fmod( clippedAngle, 360.0 );
}
if ( !allowNegative && clippedAngle < 0.0 )
{
clippedAngle += 360.0;
}
return clippedAngle;
}
40 changes: 40 additions & 0 deletions src/core/layout/qgslayoututils.h
@@ -0,0 +1,40 @@
/***************************************************************************
qgslayoututils.h
-------------------
begin : July 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. *
* *
***************************************************************************/
#ifndef QGSLAYOUTUTILS_H
#define QGSLAYOUTUTILS_H

#include "qgis_core.h"

/**
* \ingroup core
* Utilities for layouts.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsLayoutUtils
{
public:

/**
* Ensures that an \a angle (in degrees) is in the range 0 <= angle < 360.
* If \a allowNegative is true then angles between (-360, 360) are allowed. If false,
* angles are converted to positive angles in the range [0, 360).
*/
static double normalizedAngle( const double angle, const bool allowNegative = false );

};

#endif //QGSLAYOUTUTILS_H
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -131,6 +131,7 @@ SET(TESTS
testqgslayoutitem.cpp
testqgslayoutobject.cpp
testqgslayoutunits.cpp
testqgslayoututils.cpp
testqgslegendrenderer.cpp
testqgscentroidfillsymbol.cpp
testqgslinefillsymbol.cpp
Expand Down
116 changes: 116 additions & 0 deletions tests/src/core/testqgslayoututils.cpp
@@ -0,0 +1,116 @@
/***************************************************************************
testqgslayoututils.cpp
---------------------
begin : July 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 "qgslayout.h"
#include "qgstest.h"
#include "qgslayoututils.h"

class TestQgsLayoutUtils: public QObject
{
Q_OBJECT

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void normalizedAngle(); //test normalised angle function

private:
QString mReport;

};

void TestQgsLayoutUtils::initTestCase()
{
mReport = "<h1>Layout Utils Tests</h1>\n";
}

void TestQgsLayoutUtils::cleanupTestCase()
{
QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
}

void TestQgsLayoutUtils::init()
{

}

void TestQgsLayoutUtils::cleanup()
{

}

void TestQgsLayoutUtils::normalizedAngle()
{
QList< QPair< double, double > > testVals;
testVals << qMakePair( 0.0, 0.0 );
testVals << qMakePair( 90.0, 90.0 );
testVals << qMakePair( 180.0, 180.0 );
testVals << qMakePair( 270.0, 270.0 );
testVals << qMakePair( 360.0, 0.0 );
testVals << qMakePair( 390.0, 30.0 );
testVals << qMakePair( 720.0, 0.0 );
testVals << qMakePair( 730.0, 10.0 );
testVals << qMakePair( -10.0, 350.0 );
testVals << qMakePair( -360.0, 0.0 );
testVals << qMakePair( -370.0, 350.0 );
testVals << qMakePair( -760.0, 320.0 );

//test normalized angle helper function
QList< QPair< double, double > >::const_iterator it = testVals.constBegin();
for ( ; it != testVals.constEnd(); ++it )

{
double result = QgsLayoutUtils::normalizedAngle( ( *it ).first );
qDebug() << QString( "actual: %1 expected: %2" ).arg( result ).arg( ( *it ).second );
QVERIFY( qgsDoubleNear( result, ( *it ).second ) );

}

//test with allowing negative angles
QList< QPair< double, double > > negativeTestVals;
negativeTestVals << qMakePair( 0.0, 0.0 );
negativeTestVals << qMakePair( 90.0, 90.0 );
negativeTestVals << qMakePair( 360.0, 0.0 );
negativeTestVals << qMakePair( -10.0, -10.0 );
negativeTestVals << qMakePair( -359.0, -359.0 );
negativeTestVals << qMakePair( -360.0, 0.0 );
negativeTestVals << qMakePair( -361.0, -1.0 );
negativeTestVals << qMakePair( -370.0, -10.0 );
negativeTestVals << qMakePair( -760.0, -40.0 );
it = negativeTestVals.constBegin();
for ( ; it != negativeTestVals.constEnd(); ++it )

{
double result = QgsLayoutUtils::normalizedAngle( ( *it ).first, true );
qDebug() << QString( "actual: %1 expected: %2" ).arg( result ).arg( ( *it ).second );
QVERIFY( qgsDoubleNear( result, ( *it ).second ) );

}
}


QGSTEST_MAIN( TestQgsLayoutUtils )
#include "testqgslayoututils.moc"

0 comments on commit c345613

Please sign in to comment.