Add equality operator for gradients, ensure stops are always in order

This commit is contained in:
Nyall Dawson 2016-05-12 20:55:27 +10:00
parent 77fc122bed
commit 79f3d4221b
4 changed files with 49 additions and 2 deletions

View File

@ -78,6 +78,8 @@ class QgsGradientStop
//! Gradient color at stop
QColor color;
bool operator==( const QgsGradientStop& other ) const;
};
//! List of gradient stops

View File

@ -235,6 +235,19 @@ void QgsVectorGradientColorRampV2::convertToDiscrete( bool discrete )
mDiscrete = discrete;
}
bool stopLessThan( const QgsGradientStop &s1, const QgsGradientStop &s2 )
{
return s1.offset < s2.offset;
}
void QgsVectorGradientColorRampV2::setStops( const QgsGradientStopsList &stops )
{
mStops = stops;
//sort stops by offset
qSort( mStops.begin(), mStops.end(), stopLessThan );
}
void QgsVectorGradientColorRampV2::addStopsToGradient( QGradient* gradient, double alpha )
{
//copy color ramp stops to a QGradient

View File

@ -81,6 +81,11 @@ class CORE_EXPORT QgsGradientStop
double offset;
//! Gradient color at stop
QColor color;
bool operator==( const QgsGradientStop& other ) const
{
return other.color == color && qgsDoubleNear( other.offset, offset );
}
};
//! List of gradient stops
@ -170,10 +175,12 @@ class CORE_EXPORT QgsVectorGradientColorRampV2 : public QgsVectorColorRampV2
void convertToDiscrete( bool discrete );
/** Sets the list of intermediate gradient stops for the ramp.
* @param stops list of stops. Any existing color stops will be replaced
* @param stops list of stops. Any existing color stops will be replaced. The stop
* list will be automatically reordered so that stops are listed in ascending offset
* order.
* @see stops()
*/
void setStops( const QgsGradientStopsList& stops ) { mStops = stops; }
void setStops( const QgsGradientStopsList& stops );
/** Returns the list of intermediate gradient stops for the ramp.
* @see setStops()

View File

@ -26,6 +26,14 @@ from qgis.testing import unittest
class PyQgsVectorColorRamp(unittest.TestCase):
def testQgsVectorGradientRampV2(self):
# test QgsGradientStop
stop = QgsGradientStop(0.9, QColor(200, 150, 100))
self.assertEqual(stop.offset, 0.9)
self.assertEqual(stop.color, QColor(200, 150, 100))
self.assertEqual(QgsGradientStop(0.1, QColor(180, 20, 30)), QgsGradientStop(0.1, QColor(180, 20, 30)))
self.assertNotEqual(QgsGradientStop(0.1, QColor(180, 20, 30)), QgsGradientStop(0.2, QColor(180, 20, 30)))
self.assertNotEqual(QgsGradientStop(0.1, QColor(180, 20, 30)), QgsGradientStop(0.1, QColor(180, 40, 30)))
# test gradient with only start/end color
r = QgsVectorGradientColorRampV2(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200))
self.assertEqual(r.type(), 'gradient')
@ -135,6 +143,23 @@ class PyQgsVectorColorRamp(unittest.TestCase):
self.assertEqual(g.stops()[2], (0.9, QColor(40, 60, 100, 127)))
self.assertEqual(g.stops()[3], (1.0, QColor(0, 200, 0, 127)))
# test that stops are ordered when setting them
# first add some out-of-order stops
r.setStops([QgsGradientStop(0.4, QColor(100, 100, 40)),
QgsGradientStop(0.2, QColor(200, 200, 80)),
QgsGradientStop(0.8, QColor(50, 20, 10)),
QgsGradientStop(0.6, QColor(10, 10, 4))])
s = r.stops()
self.assertEqual(len(s), 4)
self.assertEqual(s[0].offset, 0.2)
self.assertEqual(s[0].color, QColor(200, 200, 80))
self.assertEqual(s[1].offset, 0.4)
self.assertEqual(s[1].color, QColor(100, 100, 40))
self.assertEqual(s[2].offset, 0.6)
self.assertEqual(s[2].color, QColor(10, 10, 4))
self.assertEqual(s[3].offset, 0.8)
self.assertEqual(s[3].color, QColor(50, 20, 10))
def testQgsVectorRandomColorRampV2(self):
# test random color ramp
r = QgsVectorRandomColorRampV2(5)