Skip to content
Permalink
Browse files

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

  • Loading branch information
nyalldawson committed May 12, 2016
1 parent 77fc122 commit 79f3d4221bb75f0c89b7043c92b705d199211b44
@@ -78,6 +78,8 @@ class QgsGradientStop

//! Gradient color at stop
QColor color;

bool operator==( const QgsGradientStop& other ) const;
};

//! List of gradient stops
@@ -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
@@ -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()
@@ -26,6 +26,14 @@
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 @@ def testQgsVectorGradientRampV2(self):
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)

0 comments on commit 79f3d42

Please sign in to comment.
You can’t perform that action at this time.