@@ -120,8 +120,8 @@ QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForValue( QVariant value )
120
120
QgsDebugMsg ( " attribute value not found: " + value.toString () );
121
121
return NULL ;
122
122
}
123
- else
124
- return *it;
123
+
124
+ return *it;
125
125
}
126
126
127
127
QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForFeature ( QgsFeature& feature )
@@ -134,8 +134,40 @@ QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForFeature( QgsFeature& featu
134
134
return NULL ;
135
135
}
136
136
137
- // find the right category
138
- return symbolForValue ( *ita );
137
+ // find the right symbol for the category
138
+ QgsSymbolV2* symbol = symbolForValue ( *ita );
139
+
140
+ if ( mRotationFieldIdx == -1 && mSizeScaleFieldIdx == -1 )
141
+ return symbol; // no data-defined rotation/scaling - just return the symbol
142
+
143
+ // find out rotation, size scale
144
+ double rotation = 0 ;
145
+ double sizeScale = 1 ;
146
+ if ( mRotationFieldIdx != -1 )
147
+ rotation = attrMap[mRotationFieldIdx ].toDouble ();
148
+ if ( mSizeScaleFieldIdx != -1 )
149
+ sizeScale = attrMap[mSizeScaleFieldIdx ].toDouble ();
150
+
151
+ // take a temporary symbol (or create it if doesn't exist)
152
+ QgsSymbolV2* tempSymbol = mTempSymbols [ita->toString ()];
153
+
154
+ // modify the temporary symbol and return it
155
+ if ( tempSymbol->type () == QgsSymbolV2::Marker )
156
+ {
157
+ QgsMarkerSymbolV2* markerSymbol = static_cast <QgsMarkerSymbolV2*>( tempSymbol );
158
+ if ( mRotationFieldIdx != -1 )
159
+ markerSymbol->setAngle ( rotation );
160
+ if ( mSizeScaleFieldIdx != -1 )
161
+ markerSymbol->setSize ( sizeScale * static_cast <QgsMarkerSymbolV2*>( symbol )->size () );
162
+ }
163
+ else if ( tempSymbol->type () == QgsSymbolV2::Line )
164
+ {
165
+ QgsLineSymbolV2* lineSymbol = static_cast <QgsLineSymbolV2*>( tempSymbol );
166
+ if ( mSizeScaleFieldIdx != -1 )
167
+ lineSymbol->setWidth ( sizeScale * static_cast <QgsLineSymbolV2*>( symbol )->width () );
168
+ }
169
+
170
+ return tempSymbol;
139
171
}
140
172
141
173
int QgsCategorizedSymbolRendererV2::categoryIndexForValue ( QVariant val )
@@ -206,22 +238,50 @@ void QgsCategorizedSymbolRendererV2::startRender( QgsRenderContext& context, con
206
238
// find out classification attribute index from name
207
239
mAttrNum = vlayer ? vlayer->fieldNameIndex ( mAttrName ) : -1 ;
208
240
241
+ mRotationFieldIdx = ( mRotationField .isEmpty () ? -1 : vlayer->fieldNameIndex ( mRotationField ) );
242
+ mSizeScaleFieldIdx = ( mSizeScaleField .isEmpty () ? -1 : vlayer->fieldNameIndex ( mSizeScaleField ) );
243
+
209
244
QgsCategoryList::iterator it = mCategories .begin ();
210
245
for ( ; it != mCategories .end (); ++it )
246
+ {
211
247
it->symbol ()->startRender ( context );
248
+
249
+ if ( mRotationFieldIdx != -1 || mSizeScaleFieldIdx != -1 )
250
+ {
251
+ QgsSymbolV2* tempSymbol = it->symbol ()->clone ();
252
+ tempSymbol->setRenderHints (( mRotationFieldIdx != -1 ? QgsSymbolV2::DataDefinedRotation : 0 ) |
253
+ ( mSizeScaleFieldIdx != -1 ? QgsSymbolV2::DataDefinedSizeScale : 0 ) );
254
+ tempSymbol->startRender ( context );
255
+ mTempSymbols [ it->value ().toString ()] = tempSymbol;
256
+ }
257
+ }
258
+
212
259
}
213
260
214
261
void QgsCategorizedSymbolRendererV2::stopRender ( QgsRenderContext& context )
215
262
{
216
263
QgsCategoryList::iterator it = mCategories .begin ();
217
264
for ( ; it != mCategories .end (); ++it )
218
265
it->symbol ()->stopRender ( context );
266
+
267
+ // cleanup mTempSymbols
268
+ QHash<QString, QgsSymbolV2*>::iterator it2 = mTempSymbols .begin ();
269
+ for ( ; it2 != mTempSymbols .end (); ++it2 )
270
+ {
271
+ it2.value ()->stopRender ( context );
272
+ delete it2.value ();
273
+ }
274
+ mTempSymbols .clear ();
219
275
}
220
276
221
277
QList<QString> QgsCategorizedSymbolRendererV2::usedAttributes ()
222
278
{
223
279
QList<QString> lst;
224
280
lst.append ( mAttrName );
281
+ if ( !mRotationField .isEmpty () )
282
+ lst.append ( mRotationField );
283
+ if ( !mSizeScaleField .isEmpty () )
284
+ lst.append ( mSizeScaleField );
225
285
return lst;
226
286
}
227
287
@@ -241,6 +301,8 @@ QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::clone()
241
301
if ( mSourceColorRamp )
242
302
r->setSourceColorRamp ( mSourceColorRamp ->clone () );
243
303
r->setUsingSymbolLevels ( usingSymbolLevels () );
304
+ r->setRotationField ( rotationField () );
305
+ r->setSizeScaleField ( sizeScaleField () );
244
306
return r;
245
307
}
246
308
@@ -308,6 +370,14 @@ QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::create( QDomElement& eleme
308
370
r->setSourceColorRamp ( QgsSymbolLayerV2Utils::loadColorRamp ( sourceColorRampElem ) );
309
371
}
310
372
373
+ QDomElement rotationElem = element.firstChildElement ( " rotation" );
374
+ if ( !rotationElem.isNull () )
375
+ r->setRotationField ( rotationElem.attribute ( " field" ) );
376
+
377
+ QDomElement sizeScaleElem = element.firstChildElement ( " sizescale" );
378
+ if ( !sizeScaleElem.isNull () )
379
+ r->setSizeScaleField ( sizeScaleElem.attribute ( " field" ) );
380
+
311
381
// TODO: symbol levels
312
382
return r;
313
383
}
@@ -360,6 +430,14 @@ QDomElement QgsCategorizedSymbolRendererV2::save( QDomDocument& doc )
360
430
rendererElem.appendChild ( colorRampElem );
361
431
}
362
432
433
+ QDomElement rotationElem = doc.createElement ( " rotation" );
434
+ rotationElem.setAttribute ( " field" , mRotationField );
435
+ rendererElem.appendChild ( rotationElem );
436
+
437
+ QDomElement sizeScaleElem = doc.createElement ( " sizescale" );
438
+ sizeScaleElem.setAttribute ( " field" , mSizeScaleField );
439
+ rendererElem.appendChild ( sizeScaleElem );
440
+
363
441
return rendererElem;
364
442
}
365
443
0 commit comments