Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add isInfinite function to QgsIntRange/QgsDoubleRange
  • Loading branch information
nyalldawson committed Nov 25, 2020
1 parent e6c8a14 commit 5ae0453
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 4 deletions.
94 changes: 92 additions & 2 deletions python/core/auto_generated/qgsrange.sip.in
Expand Up @@ -120,12 +120,102 @@ Returns ``True`` if this range overlaps another range.
};


typedef QgsRange< double > QgsDoubleRange;

typedef QgsRange<double> QgsRangedoubleBase;

class QgsDoubleRange : QgsRangedoubleBase
{
%Docstring
QgsRange which stores a range of double values.

.. seealso:: :py:class:`QgsIntRange`

.. seealso:: :py:class:`QgsDateRange`

.. seealso:: :py:class:`QgsDateTimeRange`

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsrange.h"
typedef QgsRange<double> QgsRangedoubleBase;
%End
public:


QgsDoubleRange( double lower,
double upper,
bool includeLower = true, bool includeUpper = true );
%Docstring
Constructor for QgsDoubleRange. The ``lower`` and ``upper`` bounds are specified,
and optionally whether or not these bounds are included in the range.
%End

QgsDoubleRange();
%Docstring
Constructor for QgsDoubleRange containing an infinite range (see :py:func:`~QgsDoubleRange.isInfinite`).

.. versionadded:: 3.18
%End

bool isInfinite() const;
%Docstring
Returns ``True`` if the range consists of all possible values.

.. versionadded:: 3.18
%End

};



typedef QgsRange< int > QgsIntRange;
typedef QgsRange<int> QgsRangeintBase;

class QgsIntRange : QgsRangeintBase
{
%Docstring
QgsRange which stores a range of integer values.

.. seealso:: :py:class:`QgsDoubleRange`

.. seealso:: :py:class:`QgsDateRange`

.. seealso:: :py:class:`QgsDateTimeRange`

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsrange.h"
typedef QgsRange<int> QgsRangeintBase;
%End
public:


QgsIntRange( int lower,
int upper,
bool includeLower = true, bool includeUpper = true );
%Docstring
Constructor for QgsIntRange. The ``lower`` and ``upper`` bounds are specified,
and optionally whether or not these bounds are included in the range.
%End

QgsIntRange();
%Docstring
Constructor for QgsIntRange containing an infinite range (see :py:func:`~QgsIntRange.isInfinite`).

.. versionadded:: 3.18
%End

bool isInfinite() const;
%Docstring
Returns ``True`` if the range consists of all possible values.

.. versionadded:: 3.18
%End

};


template <T>
Expand Down
102 changes: 100 additions & 2 deletions src/core/qgsrange.h
Expand Up @@ -184,9 +184,57 @@ class QgsRange
* \see QgsDateTimeRange
* \since QGIS 3.0
*/
typedef QgsRange< double > QgsDoubleRange;
class CORE_EXPORT QgsDoubleRange : public QgsRange< double >
{
public:

#ifndef SIP_RUN

/**
* Constructor for QgsDoubleRange. The \a lower and \a upper bounds are specified,
* and optionally whether or not these bounds are included in the range.
*
* The default values for \a lower and \a upper construct an infinite range (see isInfinite()).
*
* \since QGIS 3.18
*/
QgsDoubleRange( double lower = std::numeric_limits< double >::lowest(),
double upper = std::numeric_limits< double >::max(),
bool includeLower = true, bool includeUpper = true )
: QgsRange( lower, upper, includeLower, includeUpper )
{}
#else

/**
* Constructor for QgsDoubleRange. The \a lower and \a upper bounds are specified,
* and optionally whether or not these bounds are included in the range.
*/
QgsDoubleRange( double lower,
double upper,
bool includeLower = true, bool includeUpper = true )
: QgsRange( lower, upper, includeLower, includeUpper )
{}

/**
* Constructor for QgsDoubleRange containing an infinite range (see isInfinite()).
*
* \since QGIS 3.18
*/
QgsDoubleRange()
: QgsRange( std::numeric_limits< double >::lowest(), std::numeric_limits< double >::max(), true, true )
{}
#endif

/**
* Returns TRUE if the range consists of all possible values.
* \since QGIS 3.18
*/
bool isInfinite() const
{
return lower() == std::numeric_limits< double >::lowest() && upper() == std::numeric_limits< double >::max();
}

};


/**
Expand All @@ -196,7 +244,57 @@ typedef QgsRange< double > QgsDoubleRange;
* \see QgsDateTimeRange
* \since QGIS 3.0
*/
typedef QgsRange< int > QgsIntRange;
class CORE_EXPORT QgsIntRange : public QgsRange< int >
{
public:

#ifndef SIP_RUN

/**
* Constructor for QgsIntRange. The \a lower and \a upper bounds are specified,
* and optionally whether or not these bounds are included in the range.
*
* The default values for \a lower and \a upper construct an infinite range (see isInfinite()).
*
* \since QGIS 3.18
*/
QgsIntRange( int lower = std::numeric_limits< int >::lowest(),
int upper = std::numeric_limits< int >::max(),
bool includeLower = true, bool includeUpper = true )
: QgsRange( lower, upper, includeLower, includeUpper )
{}
#else

/**
* Constructor for QgsIntRange. The \a lower and \a upper bounds are specified,
* and optionally whether or not these bounds are included in the range.
*/
QgsIntRange( int lower,
int upper,
bool includeLower = true, bool includeUpper = true )
: QgsRange( lower, upper, includeLower, includeUpper )
{}

/**
* Constructor for QgsIntRange containing an infinite range (see isInfinite()).
*
* \since QGIS 3.18
*/
QgsIntRange()
: QgsRange( std::numeric_limits< int >::lowest(), std::numeric_limits< int >::max(), true, true )
{}
#endif

/**
* Returns TRUE if the range consists of all possible values.
* \since QGIS 3.18
*/
bool isInfinite() const
{
return lower() == std::numeric_limits< int >::lowest() && upper() == std::numeric_limits< int >::max();
}

};


/**
Expand Down
33 changes: 33 additions & 0 deletions tests/src/python/test_qgsrange.py
Expand Up @@ -14,6 +14,7 @@

from qgis.testing import unittest
from qgis.core import (QgsIntRange,
QgsDoubleRange,
QgsDateRange)
from qgis.PyQt.QtCore import QDate

Expand All @@ -33,6 +34,14 @@ def testGetters(self):
self.assertFalse(range.includeLower())
self.assertFalse(range.includeUpper())

def testIsInfinite(self):
range = QgsIntRange()
self.assertTrue(range.isInfinite())
range2 = QgsIntRange(range.lower(), 5)
self.assertFalse(range2.isInfinite())
range2 = QgsIntRange(5, range.upper())
self.assertFalse(range2.isInfinite())

def testIsEmpty(self):
range = QgsIntRange(1, 1)
# should not be empty because 1 is included
Expand Down Expand Up @@ -188,6 +197,30 @@ def testOverlaps(self):
self.assertFalse(range.overlaps(QgsIntRange(11, 12)))


class TestQgsDoubleRange(unittest.TestCase):

def testGetters(self):
range = QgsDoubleRange(1.0, 11.0)
self.assertEqual(range.lower(), 1)
self.assertEqual(range.upper(), 11)
self.assertTrue(range.includeLower())
self.assertTrue(range.includeUpper())

range = QgsDoubleRange(-1.0, 3.0, False, False)
self.assertEqual(range.lower(), -1)
self.assertEqual(range.upper(), 3)
self.assertFalse(range.includeLower())
self.assertFalse(range.includeUpper())

def testIsInfinite(self):
range = QgsDoubleRange()
self.assertTrue(range.isInfinite())
range2 = QgsDoubleRange(range.lower(), 5)
self.assertFalse(range2.isInfinite())
range2 = QgsDoubleRange(5, range.upper())
self.assertFalse(range2.isInfinite())


class TestQgsDateRange(unittest.TestCase):

def testGetters(self):
Expand Down

0 comments on commit 5ae0453

Please sign in to comment.