Add methods for converting QgsPoint to and from QPointF and QPoint

Also add some more basic unit tests for QgsPoint
This commit is contained in:
Nyall Dawson 2014-11-06 17:29:05 +11:00
parent a987525ac9
commit b5c9df0e61
4 changed files with 105 additions and 2 deletions

View File

@ -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
@ -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;
@ -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 );

View File

@ -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;

View File

@ -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()
{}
@ -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;
@ -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

View File

@ -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();
@ -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()
{
//