Skip to content

Commit ebd300f

Browse files
committed
Add support for new symbology for rotate point tool
1 parent 6878bf1 commit ebd300f

7 files changed

+89
-35
lines changed

python/core/symbology-ng-core.sip

+7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ public:
9898
//! set type and size of editing vertex markers for subsequent rendering
9999
void setVertexMarkerAppearance( int type, int size );
100100

101+
//! return rotation field name (or empty string if not set or not supported by renderer)
102+
//! @note added in 1.9
103+
virtual QString rotationField() const;
104+
//! sets rotation field of renderer (if supported by the renderer)
105+
//! @note added in 1.9
106+
virtual void setRotationField( QString fieldName );
107+
101108
protected:
102109
QgsFeatureRendererV2(QString type);
103110

src/app/qgsmaptoolrotatepointsymbols.cpp

+67-30
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include "qgsmapcanvas.h"
1919
#include "qgspointrotationitem.h"
2020
#include "qgsrenderer.h"
21+
#include "qgsrendererv2.h"
2122
#include "qgssymbol.h"
23+
#include "qgssymbolv2.h"
2224
#include "qgsvectorlayer.h"
2325
#include <QGraphicsPixmapItem>
2426
#include <QMessageBox>
@@ -222,37 +224,54 @@ void QgsMapToolRotatePointSymbols::canvasReleaseEvent( QMouseEvent *e )
222224
mCanvas->refresh();
223225
}
224226

225-
int QgsMapToolRotatePointSymbols::layerRotationAttributes( const QgsVectorLayer* vl, QList<int>& attList )
227+
int QgsMapToolRotatePointSymbols::layerRotationAttributes( QgsVectorLayer* vl, QList<int>& attList )
226228
{
227229
attList.clear();
228230
if ( !vl )
229231
{
230232
return 1;
231233
}
232234

233-
//get renderer
235+
//old symbology
234236
const QgsRenderer* layerRenderer = vl->renderer();
235-
if ( !layerRenderer )
237+
if ( layerRenderer )
236238
{
237-
return 2;
238-
}
239+
//get renderer symbols
240+
const QList<QgsSymbol*> rendererSymbols = layerRenderer->symbols();
241+
int currentRotationAttribute;
239242

240-
//get renderer symbols
241-
const QList<QgsSymbol*> rendererSymbols = layerRenderer->symbols();
242-
int currentRotationAttribute;
243+
QList<QgsSymbol*>::const_iterator symbolIt = rendererSymbols.constBegin();
244+
for ( ; symbolIt != rendererSymbols.constEnd(); ++symbolIt )
245+
{
246+
currentRotationAttribute = ( *symbolIt )->rotationClassificationField();
247+
if ( currentRotationAttribute >= 0 )
248+
{
249+
attList.push_back( currentRotationAttribute );
250+
}
251+
}
252+
return 0;
253+
}
243254

244-
QList<QgsSymbol*>::const_iterator symbolIt = rendererSymbols.constBegin();
245-
for ( ; symbolIt != rendererSymbols.constEnd(); ++symbolIt )
255+
//new symbology
256+
const QgsFeatureRendererV2* symbologyNgRenderer = vl->rendererV2();
257+
if ( symbologyNgRenderer )
246258
{
247-
currentRotationAttribute = ( *symbolIt )->rotationClassificationField();
248-
if ( currentRotationAttribute >= 0 )
259+
//rotation field is supported for QgsSingleSymbolRendererV2, QgsCategorizedRendererV2, QgsUniqueCategorizedRendererV2
260+
QString rotationFieldName = symbologyNgRenderer->rotationField();
261+
262+
if ( !rotationFieldName.isEmpty() )
249263
{
250-
attList.push_back( currentRotationAttribute );
264+
attList.push_back( vl->fieldNameIndex( rotationFieldName ) );
251265
}
266+
return 0;
252267
}
253-
return 0;
268+
269+
//no renderer
270+
return 2;
254271
}
255272

273+
274+
256275
double QgsMapToolRotatePointSymbols::calculateAzimut( const QPoint& mousePos )
257276
{
258277
int dx = mousePos.x() - mSnappedPoint.x();
@@ -267,10 +286,20 @@ void QgsMapToolRotatePointSymbols::createPixmapItem( QgsFeature& f )
267286
return;
268287
}
269288

270-
if ( mActiveLayer && mActiveLayer->renderer() )
289+
//get reference to current render context
290+
QgsMapRenderer* mapRenderer = mCanvas->mapRenderer();
291+
if ( !mapRenderer )
292+
{
293+
return;
294+
}
295+
296+
//get the image that is used for that symbol, but without point rotation
297+
QImage pointImage;
298+
QgsRenderer* r = 0;
299+
QgsFeatureRendererV2* rv2 = 0;
300+
301+
if ( mActiveLayer && mActiveLayer->renderer() ) //old symbology
271302
{
272-
//get the image that is used for that symbol, but without point rotation
273-
QImage pointImage;
274303
//copy renderer
275304
QgsRenderer* r = mActiveLayer->renderer()->clone();
276305

@@ -282,14 +311,6 @@ void QgsMapToolRotatePointSymbols::createPixmapItem( QgsFeature& f )
282311
( *it )->setRotationClassificationField( -1 );
283312
}
284313

285-
286-
//get reference to current render context
287-
QgsMapRenderer* mapRenderer = mCanvas->mapRenderer();
288-
if ( !mapRenderer )
289-
{
290-
delete r;
291-
return;
292-
}
293314
QgsRenderContext* renderContext = mCanvas->mapRenderer()->rendererContext(); //todo: check if pointers are not 0
294315
if ( !renderContext )
295316
{
@@ -298,16 +319,32 @@ void QgsMapToolRotatePointSymbols::createPixmapItem( QgsFeature& f )
298319
}
299320

300321
r->renderFeature( *renderContext, f, &pointImage, false );
301-
mRotationItem = new QgsPointRotationItem( mCanvas );
302-
mRotationItem->setSymbol( pointImage );
303-
delete r;
304322
}
323+
else if ( mActiveLayer && mActiveLayer->rendererV2() ) //symbology-ng
324+
{
325+
rv2 = mActiveLayer->rendererV2()->clone();
326+
rv2->setRotationField( "" );
327+
328+
QgsSymbolV2* symbolV2 = rv2->symbolForFeature( f );
329+
if ( symbolV2 )
330+
{
331+
pointImage = symbolV2->bigSymbolPreviewImage();
332+
}
333+
}
334+
335+
mRotationItem = new QgsPointRotationItem( mCanvas );
336+
mRotationItem->setSymbol( pointImage );
337+
delete r;
338+
delete rv2;
305339
}
306340

307341
void QgsMapToolRotatePointSymbols::setPixmapItemRotation( double rotation )
308342
{
309-
mRotationItem->setSymbolRotation( rotation );
310-
mRotationItem->update();
343+
if ( mRotationItem )
344+
{
345+
mRotationItem->setSymbolRotation( rotation );
346+
mRotationItem->update();
347+
}
311348
}
312349

313350
int QgsMapToolRotatePointSymbols::roundTo15Degrees( double n )

src/app/qgsmaptoolrotatepointsymbols.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class QgsMapToolRotatePointSymbols: public QgsMapToolEdit
6060
@param vl the point vector layer
6161
@param attList out: the list containing the rotation indices
6262
@return 0 in case of success*/
63-
static int layerRotationAttributes( const QgsVectorLayer* vl, QList<int>& attList );
63+
static int layerRotationAttributes( QgsVectorLayer* vl, QList<int>& attList );
6464
void drawArrow( double azimut ) const;
6565
/**Calculates the azimut between mousePos and mSnappedPoint*/
6666
double calculateAzimut( const QPoint& mousePos );

src/core/symbology-ng/qgscategorizedsymbolrendererv2.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
6262

6363
//! returns bitwise OR-ed capabilities of the renderer
6464
//! \note added in 2.0
65-
virtual int capabilities() { return SymbolLevels; }
65+
virtual int capabilities() { return SymbolLevels | RotationField; }
6666

6767
virtual QgsSymbolV2List symbols();
6868

src/core/symbology-ng/qgsgraduatedsymbolrendererv2.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
5757

5858
//! returns bitwise OR-ed capabilities of the renderer
5959
//! \note added in 2.0
60-
virtual int capabilities() { return SymbolLevels; }
60+
virtual int capabilities() { return SymbolLevels | RotationField; }
6161

6262
virtual QgsSymbolV2List symbols();
6363

src/core/symbology-ng/qgsrendererv2.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class CORE_EXPORT QgsFeatureRendererV2
8080

8181
enum Capabilities
8282
{
83-
SymbolLevels = 1 // rendering with symbol levels (i.e. implements symbols(), symbolForFeature())
83+
SymbolLevels = 1, // rendering with symbol levels (i.e. implements symbols(), symbolForFeature())
84+
RotationField = 1 << 1 // rotate symbols by attribute value
8485
};
8586

8687
//! returns bitwise OR-ed capabilities of the renderer
@@ -109,6 +110,15 @@ class CORE_EXPORT QgsFeatureRendererV2
109110
//! set type and size of editing vertex markers for subsequent rendering
110111
void setVertexMarkerAppearance( int type, int size );
111112

113+
//! return rotation field name (or empty string if not set or not supported by renderer)
114+
//! @note added in 1.9
115+
virtual QString rotationField() const { return ""; }
116+
//! sets rotation field of renderer (if supported by the renderer)
117+
//! @note added in 1.9
118+
virtual void setRotationField( QString fieldName ) { Q_UNUSED( fieldName ); }
119+
120+
121+
112122
protected:
113123
QgsFeatureRendererV2( QString type );
114124

src/core/symbology-ng/qgssinglesymbolrendererv2.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CORE_EXPORT QgsSingleSymbolRendererV2 : public QgsFeatureRendererV2
3838

3939
//! returns bitwise OR-ed capabilities of the renderer
4040
//! \note added in 2.0
41-
virtual int capabilities() { return SymbolLevels; }
41+
virtual int capabilities() { return SymbolLevels | RotationField; }
4242

4343
virtual QgsSymbolV2List symbols();
4444

0 commit comments

Comments
 (0)