@@ -57,13 +57,8 @@ bool QgsMapToolRotatePointSymbols::layerIsRotatable( QgsMapLayer* ml )
57
57
return false ;
58
58
}
59
59
60
- // does it have a least one rotation attribute?
61
- QList<int > rotationAttributes;
62
- layerRotationAttributes ( vLayer, rotationAttributes );
63
- if ( rotationAttributes.size () < 1 )
64
- {
65
- return false ;
66
- }
60
+ // we consider all point layers as rotatable, as data defined rotation can be set on a per
61
+ // symbol/feature basis
67
62
return true ;
68
63
}
69
64
@@ -101,35 +96,71 @@ void QgsMapToolRotatePointSymbols::canvasPressEvent( QMouseEvent *e )
101
96
}
102
97
103
98
mFeatureNumber = m.featureId ();
99
+ mCurrentRotationAttributes .clear ();
100
+ mSnappedPoint = toCanvasCoordinates ( m.point () );
104
101
105
- // get list with renderer rotation attributes
106
- if ( layerRotationAttributes ( mActiveLayer , mCurrentRotationAttributes ) != 0 )
102
+ QgsFeature pointFeature;
103
+ if ( ! mActiveLayer -> getFeatures ( QgsFeatureRequest (). setFlags ( QgsFeatureRequest::NoGeometry ). setFilterFid ( mFeatureNumber ) ). nextFeature ( pointFeature ) )
107
104
{
108
105
return ;
109
106
}
110
107
111
- if ( mCurrentRotationAttributes . size () < 1 )
112
- {
113
- emit messageEmitted ( tr ( " The active point layer does not have a rotation attribute. " ), QgsMessageBar::CRITICAL );
108
+ // check whether selected feature has a rotatable symbol
109
+ QgsFeatureRendererV2* renderer = mActiveLayer -> rendererV2 ();
110
+ if ( !renderer )
114
111
return ;
115
- }
112
+ QgsRenderContext context = QgsRenderContext::fromMapSettings ( mCanvas ->mapSettings () );
113
+ renderer->startRender ( context, mActiveLayer ->fields () );
116
114
117
- mSnappedPoint = toCanvasCoordinates ( m.point () );
115
+ // find all rotation fields used by renderer for feature
116
+ QgsMarkerSymbolV2* markerSymbol = 0 ;
117
+ if ( renderer->capabilities () & QgsFeatureRendererV2::MoreSymbolsPerFeature )
118
+ {
119
+ // could be multiple symbols for this feature, so check them all
120
+ foreach ( QgsSymbolV2* s, renderer->originalSymbolsForFeature ( pointFeature ) )
121
+ {
122
+ if ( s && s->type () == QgsSymbolV2::Marker )
123
+ {
124
+ markerSymbol = static_cast < QgsMarkerSymbolV2* >( s );
125
+ QString rotationField = ( markerSymbol->dataDefinedAngle ().isActive () && !markerSymbol->dataDefinedAngle ().useExpression () ) ?
126
+ markerSymbol->dataDefinedAngle ().field () : QString ();
127
+ if ( !rotationField.isEmpty () )
128
+ {
129
+ int fieldIndex = mActiveLayer ->fields ().indexFromName ( rotationField );
130
+ if ( !mCurrentRotationAttributes .contains ( fieldIndex ) )
131
+ mCurrentRotationAttributes << fieldIndex;
132
+ }
133
+ }
134
+ }
135
+ }
136
+ else
137
+ {
138
+ QgsSymbolV2* s = renderer->originalSymbolForFeature ( pointFeature );
139
+ if ( s && s->type () == QgsSymbolV2::Marker )
140
+ {
141
+ markerSymbol = static_cast < QgsMarkerSymbolV2* >( s );
142
+ QString rotationField = ( markerSymbol->dataDefinedAngle ().isActive () && !markerSymbol->dataDefinedAngle ().useExpression () ) ?
143
+ markerSymbol->dataDefinedAngle ().field () : QString ();
144
+ if ( !rotationField.isEmpty () )
145
+ mCurrentRotationAttributes << mActiveLayer ->fields ().indexFromName ( rotationField );
146
+ }
147
+ }
118
148
119
- // find out initial arrow direction
120
- QgsFeature pointFeature;
121
- if ( !mActiveLayer ->getFeatures ( QgsFeatureRequest ().setFlags ( QgsFeatureRequest::NoGeometry ).setFilterFid ( mFeatureNumber ) ).nextFeature ( pointFeature ) )
149
+ if ( mCurrentRotationAttributes .isEmpty () )
122
150
{
151
+ emit messageEmitted ( tr ( " The selected point does not have a rotation attribute." ), QgsMessageBar::CRITICAL );
123
152
return ;
124
153
}
154
+
155
+ // find out initial arrow direction
125
156
QVariant attrVal = pointFeature.attribute ( mCurrentRotationAttributes .at ( 0 ) );
126
157
if ( !attrVal.isValid () )
127
158
{
128
159
return ;
129
160
}
130
161
131
162
mCurrentRotationFeature = attrVal.toDouble ();
132
- createPixmapItem ( pointFeature );
163
+ createPixmapItem ( markerSymbol );
133
164
if ( mRotationItem )
134
165
{
135
166
mRotationItem ->setPointLocation ( m.point () );
@@ -228,69 +259,30 @@ void QgsMapToolRotatePointSymbols::canvasReleaseEvent( QMouseEvent *e )
228
259
mCanvas ->refresh ();
229
260
}
230
261
231
- int QgsMapToolRotatePointSymbols::layerRotationAttributes ( QgsVectorLayer* vl, QList<int >& attList )
232
- {
233
- attList.clear ();
234
- if ( !vl )
235
- {
236
- return 1 ;
237
- }
238
-
239
- // new symbology
240
- const QgsFeatureRendererV2* symbologyNgRenderer = vl->rendererV2 ();
241
- if ( symbologyNgRenderer )
242
- {
243
- // TODO - replace this method with code which handles data defined rotation on the symbol level
244
- Q_NOWARN_DEPRECATED_PUSH
245
- QString rotationFieldName = symbologyNgRenderer->rotationField ();
246
- Q_NOWARN_DEPRECATED_POP
247
-
248
- if ( !rotationFieldName.isEmpty () )
249
- {
250
- attList.push_back ( vl->fieldNameIndex ( rotationFieldName ) );
251
- }
252
- return 0 ;
253
- }
254
-
255
- // no renderer
256
- return 2 ;
257
- }
258
-
259
-
260
-
261
262
double QgsMapToolRotatePointSymbols::calculateAzimut ( const QPoint& mousePos )
262
263
{
263
264
int dx = mousePos.x () - mSnappedPoint .x ();
264
265
int dy = mousePos.y () - mSnappedPoint .y ();
265
266
return 180 - atan2 (( double ) dx, ( double ) dy ) * 180.0 / M_PI;
266
267
}
267
268
268
- void QgsMapToolRotatePointSymbols::createPixmapItem ( QgsFeature& f )
269
+ void QgsMapToolRotatePointSymbols::createPixmapItem ( QgsMarkerSymbolV2* markerSymbol )
269
270
{
270
271
if ( !mCanvas )
271
272
{
272
273
return ;
273
274
}
274
275
275
- QgsRenderContext renderContext ( QgsRenderContext::fromMapSettings ( mCanvas ->mapSettings () ) );
276
-
277
276
// get the image that is used for that symbol, but without point rotation
278
277
QImage pointImage;
279
278
280
- if ( mActiveLayer && mActiveLayer -> rendererV2 () ) // symbology-ng
279
+ if ( markerSymbol )
281
280
{
282
- QgsFeatureRendererV2* rv2 = mActiveLayer ->rendererV2 ()->clone ();
283
- rv2->startRender ( renderContext, mActiveLayer ->fields () );
284
-
285
- QgsSymbolV2* symbolV2 = rv2->symbolForFeature ( f );
286
- if ( symbolV2 && symbolV2->type () == QgsSymbolV2::Marker )
287
- {
288
- QgsMarkerSymbolV2* markerSymbol = static_cast <QgsMarkerSymbolV2*>( symbolV2 );
289
- markerSymbol->setDataDefinedAngle ( QgsDataDefined () );
290
- pointImage = markerSymbol->bigSymbolPreviewImage ();
291
- }
292
- rv2->stopRender ( renderContext );
293
- delete rv2;
281
+ QgsSymbolV2* clone = markerSymbol->clone ();
282
+ QgsMarkerSymbolV2* markerClone = static_cast <QgsMarkerSymbolV2*>( clone );
283
+ markerClone->setDataDefinedAngle ( QgsDataDefined () );
284
+ pointImage = markerClone->bigSymbolPreviewImage ();
285
+ delete clone;
294
286
}
295
287
296
288
mRotationItem = new QgsPointRotationItem ( mCanvas );
0 commit comments