Skip to content

Commit c345613

Browse files
committed
Add QgsLayoutUtils
1 parent 6fd0698 commit c345613

File tree

8 files changed

+235
-2
lines changed

8 files changed

+235
-2
lines changed

python/core/core_auto.sip

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,13 @@
153153
%Include composer/qgscomposermultiframecommand.sip
154154
%Include composer/qgscomposertexttable.sip
155155
%Include composer/qgspaperitem.sip
156-
%Include layout/qgslayout.sip
156+
%Include layout/qgslayoutcontext.sip
157157
%Include layout/qgslayoutmeasurement.sip
158158
%Include layout/qgslayoutmeasurementconverter.sip
159159
%Include layout/qgspagesizeregistry.sip
160160
%Include layout/qgslayoutpoint.sip
161161
%Include layout/qgslayoutsize.sip
162+
%Include layout/qgslayoututils.sip
162163
%Include metadata/qgslayermetadata.sip
163164
%Include metadata/qgslayermetadatavalidator.sip
164165
%Include processing/qgsprocessing.sip
@@ -378,7 +379,7 @@
378379
%Include gps/qgsgpsdetector.sip
379380
%Include gps/qgsnmeaconnection.sip
380381
%Include gps/qgsgpsdconnection.sip
381-
%Include layout/qgslayoutcontext.sip
382+
%Include layout/qgslayout.sip
382383
%Include layout/qgslayoutitem.sip
383384
%Include layout/qgslayoutitemregistry.sip
384385
%Include layout/qgslayoutobject.sip

python/core/layout/qgslayoututils.sip

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/layout/qgslayoututils.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
class QgsLayoutUtils
12+
{
13+
%Docstring
14+
Utilities for layouts.
15+
.. versionadded:: 3.0
16+
%End
17+
18+
%TypeHeaderCode
19+
#include "qgslayoututils.h"
20+
%End
21+
public:
22+
23+
static double normalizedAngle( const double angle, const bool allowNegative = false );
24+
%Docstring
25+
Ensures that an ``angle`` (in degrees) is in the range 0 <= angle < 360.
26+
If ``allowNegative`` is true then angles between (-360, 360) are allowed. If false,
27+
angles are converted to positive angles in the range [0, 360).
28+
:rtype: float
29+
%End
30+
31+
};
32+
33+
/************************************************************************
34+
* This file has been generated automatically from *
35+
* *
36+
* src/core/layout/qgslayoututils.h *
37+
* *
38+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
39+
************************************************************************/

src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ SET(QGIS_CORE_SRCS
356356
layout/qgslayoutmeasurement.cpp
357357
layout/qgslayoutmeasurementconverter.cpp
358358
layout/qgslayoutobject.cpp
359+
layout/qgslayoututils.cpp
359360
layout/qgspagesizeregistry.cpp
360361
layout/qgslayoutpoint.cpp
361362
layout/qgslayoutsize.cpp
@@ -923,6 +924,7 @@ SET(QGIS_CORE_HDRS
923924
layout/qgspagesizeregistry.h
924925
layout/qgslayoutpoint.h
925926
layout/qgslayoutsize.h
927+
layout/qgslayoututils.h
926928

927929
metadata/qgslayermetadata.h
928930
metadata/qgslayermetadatavalidator.h

src/core/layout/qgslayoutitem.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "qgslayoutitem.h"
1818
#include "qgslayout.h"
19+
#include "qgslayoututils.h"
1920
#include <QPainter>
2021

2122
QgsLayoutItem::QgsLayoutItem( QgsLayout *layout )

src/core/layout/qgslayoututils.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/***************************************************************************
2+
qgslayoututils.cpp
3+
------------------
4+
begin : July 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgslayoututils.h"
19+
#include <math.h>
20+
21+
double QgsLayoutUtils::normalizedAngle( const double angle, const bool allowNegative )
22+
{
23+
double clippedAngle = angle;
24+
if ( clippedAngle >= 360.0 || clippedAngle <= -360.0 )
25+
{
26+
clippedAngle = fmod( clippedAngle, 360.0 );
27+
}
28+
if ( !allowNegative && clippedAngle < 0.0 )
29+
{
30+
clippedAngle += 360.0;
31+
}
32+
return clippedAngle;
33+
}

src/core/layout/qgslayoututils.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/***************************************************************************
2+
qgslayoututils.h
3+
-------------------
4+
begin : July 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
#ifndef QGSLAYOUTUTILS_H
18+
#define QGSLAYOUTUTILS_H
19+
20+
#include "qgis_core.h"
21+
22+
/**
23+
* \ingroup core
24+
* Utilities for layouts.
25+
* \since QGIS 3.0
26+
*/
27+
class CORE_EXPORT QgsLayoutUtils
28+
{
29+
public:
30+
31+
/**
32+
* Ensures that an \a angle (in degrees) is in the range 0 <= angle < 360.
33+
* If \a allowNegative is true then angles between (-360, 360) are allowed. If false,
34+
* angles are converted to positive angles in the range [0, 360).
35+
*/
36+
static double normalizedAngle( const double angle, const bool allowNegative = false );
37+
38+
};
39+
40+
#endif //QGSLAYOUTUTILS_H

tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ SET(TESTS
131131
testqgslayoutitem.cpp
132132
testqgslayoutobject.cpp
133133
testqgslayoutunits.cpp
134+
testqgslayoututils.cpp
134135
testqgslegendrenderer.cpp
135136
testqgscentroidfillsymbol.cpp
136137
testqgslinefillsymbol.cpp

tests/src/core/testqgslayoututils.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/***************************************************************************
2+
testqgslayoututils.cpp
3+
---------------------
4+
begin : July 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgslayout.h"
19+
#include "qgstest.h"
20+
#include "qgslayoututils.h"
21+
22+
class TestQgsLayoutUtils: public QObject
23+
{
24+
Q_OBJECT
25+
26+
private slots:
27+
void initTestCase();// will be called before the first testfunction is executed.
28+
void cleanupTestCase();// will be called after the last testfunction was executed.
29+
void init();// will be called before each testfunction is executed.
30+
void cleanup();// will be called after every testfunction.
31+
void normalizedAngle(); //test normalised angle function
32+
33+
private:
34+
QString mReport;
35+
36+
};
37+
38+
void TestQgsLayoutUtils::initTestCase()
39+
{
40+
mReport = "<h1>Layout Utils Tests</h1>\n";
41+
}
42+
43+
void TestQgsLayoutUtils::cleanupTestCase()
44+
{
45+
QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html";
46+
QFile myFile( myReportFile );
47+
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
48+
{
49+
QTextStream myQTextStream( &myFile );
50+
myQTextStream << mReport;
51+
myFile.close();
52+
}
53+
}
54+
55+
void TestQgsLayoutUtils::init()
56+
{
57+
58+
}
59+
60+
void TestQgsLayoutUtils::cleanup()
61+
{
62+
63+
}
64+
65+
void TestQgsLayoutUtils::normalizedAngle()
66+
{
67+
QList< QPair< double, double > > testVals;
68+
testVals << qMakePair( 0.0, 0.0 );
69+
testVals << qMakePair( 90.0, 90.0 );
70+
testVals << qMakePair( 180.0, 180.0 );
71+
testVals << qMakePair( 270.0, 270.0 );
72+
testVals << qMakePair( 360.0, 0.0 );
73+
testVals << qMakePair( 390.0, 30.0 );
74+
testVals << qMakePair( 720.0, 0.0 );
75+
testVals << qMakePair( 730.0, 10.0 );
76+
testVals << qMakePair( -10.0, 350.0 );
77+
testVals << qMakePair( -360.0, 0.0 );
78+
testVals << qMakePair( -370.0, 350.0 );
79+
testVals << qMakePair( -760.0, 320.0 );
80+
81+
//test normalized angle helper function
82+
QList< QPair< double, double > >::const_iterator it = testVals.constBegin();
83+
for ( ; it != testVals.constEnd(); ++it )
84+
85+
{
86+
double result = QgsLayoutUtils::normalizedAngle( ( *it ).first );
87+
qDebug() << QString( "actual: %1 expected: %2" ).arg( result ).arg( ( *it ).second );
88+
QVERIFY( qgsDoubleNear( result, ( *it ).second ) );
89+
90+
}
91+
92+
//test with allowing negative angles
93+
QList< QPair< double, double > > negativeTestVals;
94+
negativeTestVals << qMakePair( 0.0, 0.0 );
95+
negativeTestVals << qMakePair( 90.0, 90.0 );
96+
negativeTestVals << qMakePair( 360.0, 0.0 );
97+
negativeTestVals << qMakePair( -10.0, -10.0 );
98+
negativeTestVals << qMakePair( -359.0, -359.0 );
99+
negativeTestVals << qMakePair( -360.0, 0.0 );
100+
negativeTestVals << qMakePair( -361.0, -1.0 );
101+
negativeTestVals << qMakePair( -370.0, -10.0 );
102+
negativeTestVals << qMakePair( -760.0, -40.0 );
103+
it = negativeTestVals.constBegin();
104+
for ( ; it != negativeTestVals.constEnd(); ++it )
105+
106+
{
107+
double result = QgsLayoutUtils::normalizedAngle( ( *it ).first, true );
108+
qDebug() << QString( "actual: %1 expected: %2" ).arg( result ).arg( ( *it ).second );
109+
QVERIFY( qgsDoubleNear( result, ( *it ).second ) );
110+
111+
}
112+
}
113+
114+
115+
QGSTEST_MAIN( TestQgsLayoutUtils )
116+
#include "testqgslayoututils.moc"

0 commit comments

Comments
 (0)