Skip to content
Permalink
Browse files

[FEATURE]: Add a color ramp class which generates random colors on-th…

…e-fly
  • Loading branch information
mhugent committed Dec 3, 2013
1 parent b232e74 commit 4260a239afa1a409097a61ea0a291bd241d0ec76
@@ -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();
}

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

@@ -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
@@ -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", "" );
@@ -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 );
@@ -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 ) );

0 comments on commit 4260a23

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