Skip to content

Commit 31c11ed

Browse files
committed
make QgsCptCityColorRampV2 subclass of QgsVectorGradientColorRampV2 and adjust both accordingly (changed stops map to list)
1 parent 724f190 commit 31c11ed

File tree

7 files changed

+132
-158
lines changed

7 files changed

+132
-158
lines changed

python/core/symbology-ng/qgsvectorcolorrampv2.sip

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,25 @@ class QgsVectorColorRampV2
2626

2727
};
2828

29+
struct QgsGradientStop
30+
{
31+
double offset;
32+
QColor color;
33+
QgsGradientStop( double o, const QColor& c );
34+
};
35+
36+
typedef QList<QgsGradientStop> QgsGradientStopsList;
37+
2938
class QgsVectorGradientColorRampV2 : QgsVectorColorRampV2
3039
{
3140
%TypeHeaderCode
3241
#include <qgsvectorcolorrampv2.h>
3342
%End
3443
public:
3544
QgsVectorGradientColorRampV2( QColor color1 = DEFAULT_GRADIENT_COLOR1,
36-
QColor color2 = DEFAULT_GRADIENT_COLOR2 );
45+
QColor color2 = QColor(0,255,0),
46+
bool discrete = false,
47+
QgsGradientStopsList stops = QgsGradientStopsList() );
3748

3849
static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() ) /Factory/;
3950

@@ -51,10 +62,11 @@ class QgsVectorGradientColorRampV2 : QgsVectorColorRampV2
5162
void setColor1( QColor color );
5263
void setColor2( QColor color );
5364

54-
typedef QMap<double, QColor> StopsMap;
65+
bool isDiscrete() const;
66+
void setDiscrete( bool discrete );
5567

56-
void setStops( const StopsMap& stops );
57-
const StopsMap& stops() const;
68+
void setStops( const QgsGradientStopsList& stops );
69+
const QgsGradientStopsList& stops() const;
5870
};
5971

6072
class QgsVectorRandomColorRampV2 : QgsVectorColorRampV2

src/core/symbology-ng/qgscptcityarchive.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -730,16 +730,18 @@ void QgsCptCityColorRampItem::init( )
730730
if ( variantList.isEmpty() )
731731
{
732732
int count = mRamp.count();
733-
QgsCptCityColorRampV2::GradientType type = mRamp.gradientType();
734-
if ( type == QgsCptCityColorRampV2::Discrete )
733+
if ( mRamp.isDiscrete() )
735734
count--;
736735
mInfo = QString::number( count ) + " " + tr( "colors" ) + " - ";
737-
if ( type == QgsCptCityColorRampV2::Continuous )
738-
mInfo += tr( "continuous" );
739-
else if ( type == QgsCptCityColorRampV2::ContinuousMulti )
740-
mInfo += tr( "continuous (multi)" );
741-
else if ( type == QgsCptCityColorRampV2::Discrete )
736+
if ( mRamp.isDiscrete() )
742737
mInfo += tr( "discrete" );
738+
else
739+
{
740+
if ( !mRamp.hasMultiStops() )
741+
mInfo += tr( "continuous" );
742+
else
743+
mInfo += tr( "continuous (multi)" );
744+
}
743745
mShortInfo = QFileInfo( mName ).fileName();
744746
}
745747
else

src/core/symbology-ng/qgsvectorcolorrampv2.cpp

Lines changed: 69 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,23 @@
2424
#include <stdlib.h> // for random()
2525
#include <QTime>
2626

27-
QgsVectorGradientColorRampV2::QgsVectorGradientColorRampV2( QColor color1, QColor color2 )
28-
: mColor1( color1 ), mColor2( color2 )
27+
//////////////
28+
29+
static QColor _interpolate( QColor c1, QColor c2, double value )
30+
{
31+
int r = ( int )( c1.red() + value * ( c2.red() - c1.red() ) );
32+
int g = ( int )( c1.green() + value * ( c2.green() - c1.green() ) );
33+
int b = ( int )( c1.blue() + value * ( c2.blue() - c1.blue() ) );
34+
int a = ( int )( c1.alpha() + value * ( c2.alpha() - c1.alpha() ) );
35+
36+
return QColor::fromRgb( r, g, b, a );
37+
}
38+
39+
//////////////
40+
41+
QgsVectorGradientColorRampV2::QgsVectorGradientColorRampV2( QColor color1, QColor color2,
42+
bool discrete, QgsGradientStopsList stops )
43+
: mColor1( color1 ), mColor2( color2 ), mDiscrete( discrete ), mStops( stops )
2944
{
3045
}
3146

@@ -38,7 +53,7 @@ QgsVectorColorRampV2* QgsVectorGradientColorRampV2::create( const QgsStringMap&
3853
if ( props.contains( "color2" ) )
3954
color2 = QgsSymbolLayerV2Utils::decodeColor( props["color2"] );
4055

41-
StopsMap stops;
56+
QgsGradientStopsList stops;
4257
if ( props.contains( "stops" ) )
4358
{
4459
foreach ( QString stop, props["stops"].split( ':' ) )
@@ -48,48 +63,51 @@ QgsVectorColorRampV2* QgsVectorGradientColorRampV2::create( const QgsStringMap&
4863
continue;
4964

5065
QColor c = QgsSymbolLayerV2Utils::decodeColor( stop.mid( i + 1 ) );
51-
stops.insert( stop.left( i ).toDouble(), c );
66+
stops.append( QgsGradientStop( stop.left( i ).toDouble(), c ) );
5267
}
5368
}
69+
bool discrete = false;
70+
if ( props.contains( "discrete" ) )
71+
{
72+
if ( props["discrete"] == "1" )
73+
discrete = true;
74+
}
5475

55-
QgsVectorGradientColorRampV2* r = new QgsVectorGradientColorRampV2( color1, color2 );
56-
r->setStops( stops );
76+
QgsVectorGradientColorRampV2* r = new QgsVectorGradientColorRampV2( color1, color2, discrete, stops );
5777
return r;
5878
}
5979

60-
static QColor _interpolate( QColor c1, QColor c2, double value )
61-
{
62-
int r = ( int )( c1.red() + value * ( c2.red() - c1.red() ) );
63-
int g = ( int )( c1.green() + value * ( c2.green() - c1.green() ) );
64-
int b = ( int )( c1.blue() + value * ( c2.blue() - c1.blue() ) );
65-
int a = ( int )( c1.alpha() + value * ( c2.alpha() - c1.alpha() ) );
66-
67-
return QColor::fromRgb( r, g, b, a );
68-
}
69-
7080
QColor QgsVectorGradientColorRampV2::color( double value ) const
7181
{
7282
if ( mStops.isEmpty() )
7383
{
84+
if ( mDiscrete )
85+
return mColor1;
7486
return _interpolate( mColor1, mColor2, value );
7587
}
7688
else
7789
{
78-
double lower = 0, upper;
90+
double lower = 0, upper = 0;
7991
QColor c1 = mColor1, c2;
80-
for ( StopsMap::const_iterator it = mStops.begin(); it != mStops.end(); ++it )
92+
for ( QgsGradientStopsList::const_iterator it = mStops.begin(); it != mStops.end(); ++it )
8193
{
82-
if ( it.key() >= value )
94+
if ( it->offset > value )
8395
{
84-
upper = it.key();
85-
c2 = it.value();
96+
if ( mDiscrete )
97+
return c1;
98+
99+
upper = it->offset;
100+
c2 = it->color;
86101

87102
return upper == lower ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
88103
}
89-
lower = it.key();
90-
c1 = it.value();
104+
lower = it->offset;
105+
c1 = it->color;
91106
}
92107

108+
if ( mDiscrete )
109+
return c1;
110+
93111
upper = 1;
94112
c2 = mColor2;
95113
return upper == lower ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
@@ -98,8 +116,8 @@ QColor QgsVectorGradientColorRampV2::color( double value ) const
98116

99117
QgsVectorColorRampV2* QgsVectorGradientColorRampV2::clone() const
100118
{
101-
QgsVectorGradientColorRampV2* r = new QgsVectorGradientColorRampV2( mColor1, mColor2 );
102-
r->setStops( mStops );
119+
QgsVectorGradientColorRampV2* r = new QgsVectorGradientColorRampV2( mColor1, mColor2,
120+
mDiscrete, mStops );
103121
return r;
104122
}
105123

@@ -111,15 +129,17 @@ QgsStringMap QgsVectorGradientColorRampV2::properties() const
111129
if ( !mStops.isEmpty() )
112130
{
113131
QStringList lst;
114-
for ( StopsMap::const_iterator it = mStops.begin(); it != mStops.end(); ++it )
132+
for ( QgsGradientStopsList::const_iterator it = mStops.begin(); it != mStops.end(); ++it )
115133
{
116-
lst.append( QString( "%1;%2" ).arg( it.key() ).arg( QgsSymbolLayerV2Utils::encodeColor( it.value() ) ) );
134+
lst.append( QString( "%1;%2" ).arg( it->offset ).arg( QgsSymbolLayerV2Utils::encodeColor( it->color ) ) );
117135
}
118136
map["stops"] = lst.join( ":" );
119137
}
138+
map["discrete"] = mDiscrete ? "1" : "0";
120139
return map;
121140
}
122141

142+
123143
//////////////
124144

125145

@@ -256,33 +276,12 @@ QgsStringMap QgsVectorColorBrewerColorRampV2::properties() const
256276

257277
////////////
258278

259-
/*
260-
TODO
261-
- return a suitable name (scheme_variant) ??
262-
- re-organize and rename colorramp classes and widgets
263-
*/
264-
265-
/*
266-
TODO load schemes
267-
- don't show empty dirs? (e.g. jjg/hatch)
268-
269-
- better grouping:
270-
- cl/fs2????
271-
- cw
272-
- dca
273-
- dirs with one scheme ( cl/ es/ ma/ occ/ wkp/
274-
jjg/neo10/env/green-crystal jjg/neo10/face/eyes/blue )
275-
276-
- fix rendering:
277-
- ibcao
278-
279-
*/
280279

281280
QgsCptCityColorRampV2::QgsCptCityColorRampV2( QString schemeName, QString variantName,
282281
bool doLoadFile )
283-
: mSchemeName( schemeName ), mVariantName( variantName ),
284-
mVariantList( QStringList() ), mFileLoaded( false ),
285-
mGradientType( Continuous )
282+
: QgsVectorGradientColorRampV2(),
283+
mSchemeName( schemeName ), mVariantName( variantName ),
284+
mVariantList( QStringList() ), mFileLoaded( false ), mMultiStops( false )
286285
{
287286
// TODO replace this with hard-coded data in the default case
288287
// don't load file if variant is missing
@@ -292,9 +291,9 @@ QgsCptCityColorRampV2::QgsCptCityColorRampV2( QString schemeName, QString varian
292291

293292
QgsCptCityColorRampV2::QgsCptCityColorRampV2( QString schemeName, QStringList variantList,
294293
QString variantName, bool doLoadFile )
295-
: mSchemeName( schemeName ), mVariantName( variantName ),
296-
mVariantList( variantList ), mFileLoaded( false ),
297-
mGradientType( Continuous )
294+
: QgsVectorGradientColorRampV2(),
295+
mSchemeName( schemeName ), mVariantName( variantName ),
296+
mVariantList( variantList ), mFileLoaded( false ), mMultiStops( false )
298297
{
299298
mVariantList = variantList;
300299

@@ -317,41 +316,6 @@ QgsVectorColorRampV2* QgsCptCityColorRampV2::create( const QgsStringMap& props )
317316
return new QgsCptCityColorRampV2( schemeName, variantName );
318317
}
319318

320-
QColor QgsCptCityColorRampV2::color( double value ) const
321-
{
322-
int numStops = mPalette.count();
323-
if ( mPalette.isEmpty() || value < 0 || value > 1 || numStops < 2 )
324-
return QColor( 255, 0, 0 ); // red color as a warning :)
325-
326-
double lower = 0, upper = 0;
327-
QColor c1, c2;
328-
c1 = mPalette[0].second;
329-
for ( int i = 0; i < numStops; i++ )
330-
{
331-
if ( mPalette[i].first > value )
332-
{
333-
if ( mGradientType == Discrete )
334-
return c1;
335-
336-
upper = mPalette[i].first;
337-
c2 = mPalette[i].second;
338-
339-
return upper == lower ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
340-
}
341-
342-
lower = mPalette[i].first;
343-
c1 = mPalette[i].second;
344-
}
345-
346-
if ( mGradientType == Discrete )
347-
return c1;
348-
349-
upper = 1;
350-
c2 = mPalette[ numStops - 1 ].second;
351-
352-
return upper == lower ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
353-
}
354-
355319
QgsVectorColorRampV2* QgsCptCityColorRampV2::clone() const
356320
{
357321
QgsCptCityColorRampV2* ramp = new QgsCptCityColorRampV2( "", "", false );
@@ -363,15 +327,14 @@ void QgsCptCityColorRampV2::copy( const QgsCptCityColorRampV2* other )
363327
{
364328
if ( ! other )
365329
return;
330+
mColor1 = other->color1();
331+
mColor2 = other->color2();
332+
mDiscrete = other->isDiscrete();
333+
mStops = other->stops();
366334
mSchemeName = other->mSchemeName;
367335
mVariantName = other->mVariantName;
368336
mVariantList = other->mVariantList;
369337
mFileLoaded = other->mFileLoaded;
370-
if ( other->mFileLoaded )
371-
{
372-
mGradientType = other->mGradientType;
373-
mPalette = other->mPalette;
374-
}
375338
}
376339

377340
QgsStringMap QgsCptCityColorRampV2::properties() const
@@ -434,12 +397,13 @@ bool QgsCptCityColorRampV2::loadFile()
434397

435398
// add colors to palette
436399
mFileLoaded = false;
437-
mPalette.clear();
400+
mStops.clear();
438401
QMap<double, QPair<QColor, QColor> >::const_iterator it, prev;
439402
// first detect if file is gradient is continuous or dicrete
440403
// discrete: stop contains 2 colors and first color is identical to previous second
441404
// multi: stop contains 2 colors and no relation with previous stop
442-
mGradientType = Continuous;
405+
mDiscrete = false;
406+
mMultiStops = false;
443407
it = prev = colorMap.constBegin();
444408
while ( it != colorMap.constEnd() )
445409
{
@@ -448,12 +412,12 @@ bool QgsCptCityColorRampV2::loadFile()
448412
{
449413
if ( it.value().first == prev.value().second )
450414
{
451-
mGradientType = Discrete;
415+
mDiscrete = true;
452416
break;
453417
}
454418
else
455419
{
456-
mGradientType = ContinuousMulti;
420+
mMultiStops = true;
457421
break;
458422
}
459423
}
@@ -464,17 +428,19 @@ bool QgsCptCityColorRampV2::loadFile()
464428
it = prev = colorMap.constBegin();
465429
while ( it != colorMap.constEnd() )
466430
{
467-
if ( mGradientType == Discrete )
431+
if ( mDiscrete )
468432
{
469-
mPalette << qMakePair( it.key(), it.value().second );
433+
// mPalette << qMakePair( it.key(), it.value().second );
434+
mStops.append( QgsGradientStop( it.key(), it.value().second ) );
470435
}
471436
else
472437
{
473-
mPalette << qMakePair( it.key(), it.value().first );
474-
if (( mGradientType == ContinuousMulti ) &&
438+
// mPalette << qMakePair( it.key(), it.value().first );
439+
mStops.append( QgsGradientStop( it.key(), it.value().first ) );
440+
if (( mMultiStops ) &&
475441
( it.key() != 0.0 && it.key() != 1.0 ) )
476442
{
477-
mPalette << qMakePair( it.key(), it.value().second );
443+
mStops.append( QgsGradientStop( it.key(), it.value().second ) );
478444
}
479445
}
480446
prev = it;

0 commit comments

Comments
 (0)