Skip to content

Commit

Permalink
Add methods for converting QgsPoint to and from QPointF and QPoint
Browse files Browse the repository at this point in the history
Also add some more basic unit tests for QgsPoint
  • Loading branch information
nyalldawson committed Nov 6, 2014
1 parent a987525 commit b5c9df0
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
20 changes: 19 additions & 1 deletion python/core/qgspoint.sip
Expand Up @@ -23,6 +23,18 @@ class QgsPoint
*/
QgsPoint( double x, double y );

/*! Create a point from a QPointF
* @param point QPointF source
* @note added in QGIS 2.7
*/
QgsPoint( const QPointF& point );

/*! Create a point from a QPoint
* @param point QPoint source
* @note added in QGIS 2.7
*/
QgsPoint( const QPoint& point );

~QgsPoint();

/*! Sets the x value of the point
Expand All @@ -48,6 +60,12 @@ class QgsPoint
*/
double y() const;

/** Converts a point to a QPointF
* @returns QPointF with same x and y values
* @note added in QGIS 2.7
*/
QPointF toQPointF() const;

//! String representation of the point (x,y)
QString toString() const;

Expand Down Expand Up @@ -95,7 +113,7 @@ class QgsPoint
@note added in QGIS 1.5*/
double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint /Out/, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;

/**Calculates azimut between this point and other one (clockwise in degree, starting from north)
/**Calculates azimuth between this point and other one (clockwise in degree, starting from north)
@note: this function has been added in version 1.7*/
double azimuth( const QgsPoint& other );

Expand Down
5 changes: 5 additions & 0 deletions src/core/qgspoint.cpp
Expand Up @@ -118,6 +118,11 @@ QgsPoint::QgsPoint( const QgsPoint& p )
m_y = p.y();
}

QPointF QgsPoint::toQPointF() const
{
return QPointF( m_x, m_y );
}

QString QgsPoint::toString() const
{
QString rep;
Expand Down
24 changes: 23 additions & 1 deletion src/core/qgspoint.h
Expand Up @@ -78,6 +78,22 @@ class CORE_EXPORT QgsPoint
: m_x( x ), m_y( y )
{}

/*! Create a point from a QPointF
* @param point QPointF source
* @note added in QGIS 2.7
*/
QgsPoint( const QPointF& point )
: m_x( point.x() ), m_y( point.y() )
{}

/*! Create a point from a QPoint
* @param point QPoint source
* @note added in QGIS 2.7
*/
QgsPoint( const QPoint& point )
: m_x( point.x() ), m_y( point.y() )
{}

~QgsPoint()
{}

Expand Down Expand Up @@ -120,6 +136,12 @@ class CORE_EXPORT QgsPoint
return m_y;
}

/** Converts a point to a QPointF
* @returns QPointF with same x and y values
* @note added in QGIS 2.7
*/
QPointF toQPointF() const;

//! String representation of the point (x,y)
QString toString() const;

Expand Down Expand Up @@ -164,7 +186,7 @@ class CORE_EXPORT QgsPoint
/**Returns the minimum distance between this point and a segment */
double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;

/**Calculates azimut between this point and other one (clockwise in degree, starting from north) */
/**Calculates azimuth between this point and other one (clockwise in degree, starting from north) */
double azimuth( const QgsPoint& other );

//! equality operator
Expand Down
58 changes: 58 additions & 0 deletions tests/src/core/testqgspoint.cpp
Expand Up @@ -36,6 +36,10 @@ class TestQgsPoint: public QObject
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 equality();
void gettersSetters();
void constructors();
void toQPointF();
void toString();
void toDegreesMinutesSeconds();
void toDegreesMinutesSecondsNoSuffix();
Expand Down Expand Up @@ -71,6 +75,60 @@ void TestQgsPoint::cleanup()
// will be called after every testfunction.
}

void TestQgsPoint::equality()
{
QgsPoint point1( 5.0, 9.0 );
QgsPoint point2( 5.0, 9.0 );
QCOMPARE( point1, point2 );
QgsPoint point3( 5.0, 6.0 );
QVERIFY( !( point3 == point1 ) );
QVERIFY( point3 != point1 );
QgsPoint point4( 8.0, 9.0 );
QVERIFY( !( point4 == point1 ) );
QVERIFY( point4 != point1 );
QVERIFY( !( point4 == point3 ) );
QVERIFY( point4 != point3 );
}

void TestQgsPoint::gettersSetters()
{
QgsPoint point;
point.setX( 1.0 );
QCOMPARE( point.x(), 1.0 );
point.setY( 2.0 );
QCOMPARE( point.y(), 2.0 );
point.set( 3.0, 4.0 );
QCOMPARE( point.x(), 3.0 );
QCOMPARE( point.y(), 4.0 );
}

void TestQgsPoint::constructors()
{
QgsPoint point1 = QgsPoint( 20.0, -20.0 );
QCOMPARE( point1.x(), 20.0 );
QCOMPARE( point1.y(), -20.0 );
QgsPoint point2( point1 );
QCOMPARE( point2, point1 );

QPointF sourceQPointF( 20.0, -20.0 );
QgsPoint fromQPointF( sourceQPointF );
QCOMPARE( fromQPointF.x(), 20.0 );
QCOMPARE( fromQPointF.y(), -20.0 );

QPointF sourceQPoint( 20, -20 );
QgsPoint fromQPoint( sourceQPoint );
QCOMPARE( fromQPoint.x(), 20.0 );
QCOMPARE( fromQPoint.y(), -20.0 );
}

void TestQgsPoint::toQPointF()
{
QgsPoint point( 20.0, -20.0 );
QPointF result = point.toQPointF();
QCOMPARE( result.x(), 20.0 );
QCOMPARE( result.y(), -20.0 );
}

void TestQgsPoint::initTestCase()
{
//
Expand Down

0 comments on commit b5c9df0

Please sign in to comment.