Skip to content

Commit 4260a23

Browse files
committed
[FEATURE]: Add a color ramp class which generates random colors on-the-fly
1 parent b232e74 commit 4260a23

5 files changed

+77
-2
lines changed

src/core/symbology-ng/qgsvectorcolorrampv2.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,52 @@ void QgsVectorRandomColorRampV2::updateColors()
319319
}
320320
}
321321

322+
/////////////
323+
324+
QgsRandomColorsV2::QgsRandomColorsV2()
325+
{
326+
srand( QTime::currentTime().msec() );
327+
}
328+
329+
QgsRandomColorsV2::~QgsRandomColorsV2()
330+
{
331+
332+
}
333+
334+
int QgsRandomColorsV2::count() const
335+
{
336+
return INT_MAX;
337+
}
338+
339+
double QgsRandomColorsV2::value( int index ) const
340+
{
341+
Q_UNUSED( index );
342+
return 0.0;
343+
}
344+
345+
QColor QgsRandomColorsV2::color( double value ) const
346+
{
347+
Q_UNUSED( value );
348+
int r = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
349+
int g = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
350+
int b = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
351+
return QColor( r, g, b );
352+
}
353+
354+
QString QgsRandomColorsV2::type() const
355+
{
356+
return "randomcolors";
357+
}
358+
359+
QgsVectorColorRampV2* QgsRandomColorsV2::clone() const
360+
{
361+
return new QgsRandomColorsV2();
362+
}
363+
364+
QgsStringMap QgsRandomColorsV2::properties() const
365+
{
366+
return QgsStringMap();
367+
}
322368

323369
////////////
324370

src/core/symbology-ng/qgsvectorcolorrampv2.h

+19
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ class CORE_EXPORT QgsVectorRandomColorRampV2 : public QgsVectorColorRampV2
155155
QList<QColor> mColors;
156156
};
157157

158+
class CORE_EXPORT QgsRandomColorsV2: public QgsVectorColorRampV2
159+
{
160+
public:
161+
QgsRandomColorsV2();
162+
~QgsRandomColorsV2();
163+
164+
int count() const;
165+
166+
double value( int index ) const;
167+
168+
QColor color( double value ) const;
169+
170+
QString type() const;
171+
172+
QgsVectorColorRampV2* clone() const;
173+
174+
QgsStringMap properties() const;
175+
};
176+
158177

159178
#define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
160179
#define DEFAULT_COLORBREWER_COLORS 5

src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsV
371371
populateColumns();
372372

373373
cboCategorizedColorRamp->populate( mStyle );
374+
int randomIndex = cboCategorizedColorRamp->findText( tr( "Random colors" ) );
375+
if ( randomIndex != -1 )
376+
{
377+
cboCategorizedColorRamp->setCurrentIndex( randomIndex );
378+
}
374379

375380
// set project default color ramp
376381
QString defaultColorRamp = QgsProject::instance()->readEntry( "DefaultStyles", "/ColorRamp", "" );

src/gui/symbology-ng/qgscolorrampcombobox.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ void QgsColorRampComboBox::populate( QgsStyleV2* style )
5454
delete ramp;
5555
}
5656

57+
addItem( tr( "Random colors" ) );
5758
addItem( tr( "New color ramp..." ) );
5859
connect( this, SIGNAL( activated( int ) ), SLOT( colorRampChanged( int ) ) );
5960
}
6061

6162
QgsVectorColorRampV2* QgsColorRampComboBox::currentColorRamp()
6263
{
6364
QString rampName = currentText();
64-
if ( rampName == "[source]" && mSourceColorRamp )
65+
66+
if ( rampName == tr( "Random colors" ) )
67+
{
68+
return new QgsRandomColorsV2();
69+
}
70+
else if ( rampName == "[source]" && mSourceColorRamp )
6571
return mSourceColorRamp->clone();
6672
else
6773
return mStyle->colorRamp( rampName );

src/gui/symbology-ng/qgsgraduatedsymbolrendererv2widget.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,6 @@ QgsRangeList QgsGraduatedSymbolRendererV2Widget::selectedRanges()
635635
QModelIndexList selectedRows = viewGraduated->selectionModel()->selectedRows();
636636
QModelIndexList::const_iterator sIt = selectedRows.constBegin();
637637

638-
const QgsRangeList& ranges = mRenderer->ranges();
639638
for ( ; sIt != selectedRows.constEnd(); ++sIt )
640639
{
641640
selectedRanges.append( mModel->rendererRange( *sIt ) );

0 commit comments

Comments
 (0)