Skip to content

Commit 79f3d42

Browse files
committedMay 12, 2016
Add equality operator for gradients, ensure stops are always in order
1 parent 77fc122 commit 79f3d42

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed
 

‎python/core/symbology-ng/qgsvectorcolorrampv2.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class QgsGradientStop
7878

7979
//! Gradient color at stop
8080
QColor color;
81+
82+
bool operator==( const QgsGradientStop& other ) const;
8183
};
8284

8385
//! List of gradient stops

‎src/core/symbology-ng/qgsvectorcolorrampv2.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,19 @@ void QgsVectorGradientColorRampV2::convertToDiscrete( bool discrete )
235235
mDiscrete = discrete;
236236
}
237237

238+
bool stopLessThan( const QgsGradientStop &s1, const QgsGradientStop &s2 )
239+
{
240+
return s1.offset < s2.offset;
241+
}
242+
243+
void QgsVectorGradientColorRampV2::setStops( const QgsGradientStopsList &stops )
244+
{
245+
mStops = stops;
246+
247+
//sort stops by offset
248+
qSort( mStops.begin(), mStops.end(), stopLessThan );
249+
}
250+
238251
void QgsVectorGradientColorRampV2::addStopsToGradient( QGradient* gradient, double alpha )
239252
{
240253
//copy color ramp stops to a QGradient

‎src/core/symbology-ng/qgsvectorcolorrampv2.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ class CORE_EXPORT QgsGradientStop
8181
double offset;
8282
//! Gradient color at stop
8383
QColor color;
84+
85+
bool operator==( const QgsGradientStop& other ) const
86+
{
87+
return other.color == color && qgsDoubleNear( other.offset, offset );
88+
}
8489
};
8590

8691
//! List of gradient stops
@@ -170,10 +175,12 @@ class CORE_EXPORT QgsVectorGradientColorRampV2 : public QgsVectorColorRampV2
170175
void convertToDiscrete( bool discrete );
171176

172177
/** Sets the list of intermediate gradient stops for the ramp.
173-
* @param stops list of stops. Any existing color stops will be replaced
178+
* @param stops list of stops. Any existing color stops will be replaced. The stop
179+
* list will be automatically reordered so that stops are listed in ascending offset
180+
* order.
174181
* @see stops()
175182
*/
176-
void setStops( const QgsGradientStopsList& stops ) { mStops = stops; }
183+
void setStops( const QgsGradientStopsList& stops );
177184

178185
/** Returns the list of intermediate gradient stops for the ramp.
179186
* @see setStops()

‎tests/src/python/test_qgsvectorcolorramp.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
class PyQgsVectorColorRamp(unittest.TestCase):
2727

2828
def testQgsVectorGradientRampV2(self):
29+
# test QgsGradientStop
30+
stop = QgsGradientStop(0.9, QColor(200, 150, 100))
31+
self.assertEqual(stop.offset, 0.9)
32+
self.assertEqual(stop.color, QColor(200, 150, 100))
33+
self.assertEqual(QgsGradientStop(0.1, QColor(180, 20, 30)), QgsGradientStop(0.1, QColor(180, 20, 30)))
34+
self.assertNotEqual(QgsGradientStop(0.1, QColor(180, 20, 30)), QgsGradientStop(0.2, QColor(180, 20, 30)))
35+
self.assertNotEqual(QgsGradientStop(0.1, QColor(180, 20, 30)), QgsGradientStop(0.1, QColor(180, 40, 30)))
36+
2937
# test gradient with only start/end color
3038
r = QgsVectorGradientColorRampV2(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200))
3139
self.assertEqual(r.type(), 'gradient')
@@ -135,6 +143,23 @@ def testQgsVectorGradientRampV2(self):
135143
self.assertEqual(g.stops()[2], (0.9, QColor(40, 60, 100, 127)))
136144
self.assertEqual(g.stops()[3], (1.0, QColor(0, 200, 0, 127)))
137145

146+
# test that stops are ordered when setting them
147+
# first add some out-of-order stops
148+
r.setStops([QgsGradientStop(0.4, QColor(100, 100, 40)),
149+
QgsGradientStop(0.2, QColor(200, 200, 80)),
150+
QgsGradientStop(0.8, QColor(50, 20, 10)),
151+
QgsGradientStop(0.6, QColor(10, 10, 4))])
152+
s = r.stops()
153+
self.assertEqual(len(s), 4)
154+
self.assertEqual(s[0].offset, 0.2)
155+
self.assertEqual(s[0].color, QColor(200, 200, 80))
156+
self.assertEqual(s[1].offset, 0.4)
157+
self.assertEqual(s[1].color, QColor(100, 100, 40))
158+
self.assertEqual(s[2].offset, 0.6)
159+
self.assertEqual(s[2].color, QColor(10, 10, 4))
160+
self.assertEqual(s[3].offset, 0.8)
161+
self.assertEqual(s[3].color, QColor(50, 20, 10))
162+
138163
def testQgsVectorRandomColorRampV2(self):
139164
# test random color ramp
140165
r = QgsVectorRandomColorRampV2(5)

0 commit comments

Comments
 (0)
Please sign in to comment.