@@ -46,13 +46,15 @@ QgsVectorGradientColorRampV2::QgsVectorGradientColorRampV2( QColor color1, QColo
46
46
47
47
QgsVectorColorRampV2* QgsVectorGradientColorRampV2::create ( const QgsStringMap& props )
48
48
{
49
+ // color1 and color2
49
50
QColor color1 = DEFAULT_GRADIENT_COLOR1;
50
51
QColor color2 = DEFAULT_GRADIENT_COLOR2;
51
52
if ( props.contains ( " color1" ) )
52
53
color1 = QgsSymbolLayerV2Utils::decodeColor ( props[" color1" ] );
53
54
if ( props.contains ( " color2" ) )
54
55
color2 = QgsSymbolLayerV2Utils::decodeColor ( props[" color2" ] );
55
56
57
+ // stops
56
58
QgsGradientStopsList stops;
57
59
if ( props.contains ( " stops" ) )
58
60
{
@@ -66,14 +68,26 @@ QgsVectorColorRampV2* QgsVectorGradientColorRampV2::create( const QgsStringMap&
66
68
stops.append ( QgsGradientStop ( stop.left ( i ).toDouble (), c ) );
67
69
}
68
70
}
71
+
72
+ // discrete vs. continuous
69
73
bool discrete = false ;
70
74
if ( props.contains ( " discrete" ) )
71
75
{
72
76
if ( props[" discrete" ] == " 1" )
73
77
discrete = true ;
74
78
}
75
79
80
+ // search for information keys starting with "info_"
81
+ QgsStringMap info;
82
+ for ( QgsStringMap::const_iterator it = props.constBegin ();
83
+ it != props.constEnd (); ++it )
84
+ {
85
+ if ( it.key ().startsWith ( " info_" ) )
86
+ info[ it.key ().mid ( 5 )] = it.value ();
87
+ }
88
+
76
89
QgsVectorGradientColorRampV2* r = new QgsVectorGradientColorRampV2 ( color1, color2, discrete, stops );
90
+ r->setInfo ( info );
77
91
return r;
78
92
}
79
93
@@ -118,6 +132,7 @@ QgsVectorColorRampV2* QgsVectorGradientColorRampV2::clone() const
118
132
{
119
133
QgsVectorGradientColorRampV2* r = new QgsVectorGradientColorRampV2 ( mColor1 , mColor2 ,
120
134
mDiscrete , mStops );
135
+ r->setInfo ( mInfo );
121
136
return r;
122
137
}
123
138
@@ -135,10 +150,58 @@ QgsStringMap QgsVectorGradientColorRampV2::properties() const
135
150
}
136
151
map[" stops" ] = lst.join ( " :" );
137
152
}
153
+
138
154
map[" discrete" ] = mDiscrete ? " 1" : " 0" ;
155
+
156
+ for ( QgsStringMap::const_iterator it = mInfo .constBegin ();
157
+ it != mInfo .constEnd (); ++it )
158
+ {
159
+ map[" info_" + it.key ()] = it.value ();
160
+ }
161
+
139
162
return map;
140
163
}
164
+ void QgsVectorGradientColorRampV2::convertToDiscrete ( bool discrete )
165
+ {
166
+ if ( discrete == mDiscrete )
167
+ return ;
141
168
169
+ // if going to/from Discrete, re-arrange stops
170
+ // this will only work when stops are equally-spaced
171
+ QgsGradientStopsList newStops;
172
+ if ( discrete )
173
+ {
174
+ // re-arrange stops offset
175
+ int numStops = mStops .count () + 2 ;
176
+ int i = 1 ;
177
+ for ( QgsGradientStopsList::const_iterator it = mStops .begin ();
178
+ it != mStops .end (); ++it )
179
+ {
180
+ newStops.append ( QgsGradientStop (( double ) i / numStops, it->color ) );
181
+ if ( i == numStops - 1 )
182
+ break ;
183
+ i++;
184
+ }
185
+ // replicate last color
186
+ newStops.append ( QgsGradientStop (( double ) i / numStops, mColor2 ) );
187
+ }
188
+ else
189
+ {
190
+ // re-arrange stops offset, remove duplicate last color
191
+ int numStops = mStops .count () + 2 ;
192
+ int i = 1 ;
193
+ for ( QgsGradientStopsList::const_iterator it = mStops .begin ();
194
+ it != mStops .end (); ++it )
195
+ {
196
+ newStops.append ( QgsGradientStop (( double ) i / ( numStops - 2 ), it->color ) );
197
+ if ( i == numStops - 3 )
198
+ break ;
199
+ i++;
200
+ }
201
+ }
202
+ mStops = newStops;
203
+ mDiscrete = discrete;
204
+ }
142
205
143
206
// ////////////
144
207
@@ -337,6 +400,20 @@ void QgsCptCityColorRampV2::copy( const QgsCptCityColorRampV2* other )
337
400
mFileLoaded = other->mFileLoaded ;
338
401
}
339
402
403
+ QgsVectorGradientColorRampV2* QgsCptCityColorRampV2::cloneGradientRamp () const
404
+ {
405
+ QgsVectorGradientColorRampV2* ramp =
406
+ new QgsVectorGradientColorRampV2 ( mColor1 , mColor2 , mDiscrete , mStops );
407
+ // add author and copyright information
408
+ // TODO also add COPYING.xml file/link?
409
+ QgsStringMap info = copyingInfo ();
410
+ info[" source" ] = " cpt-city archive - " + mSchemeName + mVariantName + " .svg" ;
411
+ info[" license/file" ] = copyingFileName ();
412
+ ramp->setInfo ( info );
413
+ return ramp;
414
+ }
415
+
416
+
340
417
QgsStringMap QgsCptCityColorRampV2::properties () const
341
418
{
342
419
QgsStringMap map;
@@ -368,7 +445,7 @@ QString QgsCptCityColorRampV2::descFileName() const
368
445
QgsCptCityArchive::defaultBaseDir () );
369
446
}
370
447
371
- QMap< QString, QString > QgsCptCityColorRampV2::copyingInfo ( ) const
448
+ QgsStringMap QgsCptCityColorRampV2::copyingInfo ( ) const
372
449
{
373
450
return QgsCptCityArchive::copyingInfo ( copyingFileName () );
374
451
}
@@ -425,6 +502,7 @@ bool QgsCptCityColorRampV2::loadFile()
425
502
++it;
426
503
}
427
504
505
+ // fill all stops
428
506
it = prev = colorMap.constBegin ();
429
507
while ( it != colorMap.constEnd () )
430
508
{
@@ -447,6 +525,12 @@ bool QgsCptCityColorRampV2::loadFile()
447
525
++it;
448
526
}
449
527
528
+ // remove first and last items (mColor1 and mColor2)
529
+ if ( ! mStops .isEmpty () && mStops .first ().offset == 0.0 )
530
+ mColor1 = mStops .takeFirst ().color ;
531
+ if ( ! mStops .isEmpty () && mStops .last ().offset == 1.0 )
532
+ mColor2 = mStops .takeLast ().color ;
533
+
450
534
mFileLoaded = true ;
451
535
return true ;
452
536
}
0 commit comments