Skip to content

Commit

Permalink
[FEATURE]: Add a color ramp class which generates random colors on-th…
Browse files Browse the repository at this point in the history
…e-fly
  • Loading branch information
mhugent committed Dec 3, 2013
1 parent b232e74 commit 4260a23
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/core/symbology-ng/qgsvectorcolorrampv2.cpp
Expand Up @@ -319,6 +319,52 @@ void QgsVectorRandomColorRampV2::updateColors()
}
}

/////////////

QgsRandomColorsV2::QgsRandomColorsV2()
{
srand( QTime::currentTime().msec() );
}

QgsRandomColorsV2::~QgsRandomColorsV2()
{

}

int QgsRandomColorsV2::count() const
{
return INT_MAX;
}

double QgsRandomColorsV2::value( int index ) const
{
Q_UNUSED( index );
return 0.0;
}

QColor QgsRandomColorsV2::color( double value ) const
{
Q_UNUSED( value );
int r = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
int g = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
int b = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
return QColor( r, g, b );
}

QString QgsRandomColorsV2::type() const
{
return "randomcolors";
}

QgsVectorColorRampV2* QgsRandomColorsV2::clone() const
{
return new QgsRandomColorsV2();
}

QgsStringMap QgsRandomColorsV2::properties() const
{
return QgsStringMap();
}

////////////

Expand Down
19 changes: 19 additions & 0 deletions src/core/symbology-ng/qgsvectorcolorrampv2.h
Expand Up @@ -155,6 +155,25 @@ class CORE_EXPORT QgsVectorRandomColorRampV2 : public QgsVectorColorRampV2
QList<QColor> mColors;
};

class CORE_EXPORT QgsRandomColorsV2: public QgsVectorColorRampV2
{
public:
QgsRandomColorsV2();
~QgsRandomColorsV2();

int count() const;

double value( int index ) const;

QColor color( double value ) const;

QString type() const;

QgsVectorColorRampV2* clone() const;

QgsStringMap properties() const;
};


#define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
#define DEFAULT_COLORBREWER_COLORS 5
Expand Down
5 changes: 5 additions & 0 deletions src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp
Expand Up @@ -371,6 +371,11 @@ QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsV
populateColumns();

cboCategorizedColorRamp->populate( mStyle );
int randomIndex = cboCategorizedColorRamp->findText( tr( "Random colors" ) );
if ( randomIndex != -1 )
{
cboCategorizedColorRamp->setCurrentIndex( randomIndex );
}

// set project default color ramp
QString defaultColorRamp = QgsProject::instance()->readEntry( "DefaultStyles", "/ColorRamp", "" );
Expand Down
8 changes: 7 additions & 1 deletion src/gui/symbology-ng/qgscolorrampcombobox.cpp
Expand Up @@ -54,14 +54,20 @@ void QgsColorRampComboBox::populate( QgsStyleV2* style )
delete ramp;
}

addItem( tr( "Random colors" ) );
addItem( tr( "New color ramp..." ) );
connect( this, SIGNAL( activated( int ) ), SLOT( colorRampChanged( int ) ) );
}

QgsVectorColorRampV2* QgsColorRampComboBox::currentColorRamp()
{
QString rampName = currentText();
if ( rampName == "[source]" && mSourceColorRamp )

if ( rampName == tr( "Random colors" ) )
{
return new QgsRandomColorsV2();
}
else if ( rampName == "[source]" && mSourceColorRamp )
return mSourceColorRamp->clone();
else
return mStyle->colorRamp( rampName );
Expand Down
Expand Up @@ -635,7 +635,6 @@ QgsRangeList QgsGraduatedSymbolRendererV2Widget::selectedRanges()
QModelIndexList selectedRows = viewGraduated->selectionModel()->selectedRows();
QModelIndexList::const_iterator sIt = selectedRows.constBegin();

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

0 comments on commit 4260a23

Please sign in to comment.