Skip to content

Commit bb21724

Browse files
author
marco
committed
Optionally pass vector layer pointer to rendercontext
1 parent 3a403cc commit bb21724

File tree

6 files changed

+146
-48
lines changed

6 files changed

+146
-48
lines changed

src/core/symbology-ng/qgssinglesymbolrendererv2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void QgsSingleSymbolRendererV2::startRender( QgsRenderContext& context, const Qg
7272
mRotationFieldIdx = mRotationField.isEmpty() ? -1 : vlayer->fieldNameIndex( mRotationField );
7373
mSizeScaleFieldIdx = mSizeScaleField.isEmpty() ? -1 : vlayer->fieldNameIndex( mSizeScaleField );
7474

75-
mSymbol->startRender( context );
75+
mSymbol->startRender( context, vlayer );
7676

7777
if ( mRotationFieldIdx != -1 || mSizeScaleFieldIdx != -1 )
7878
{
@@ -86,7 +86,7 @@ void QgsSingleSymbolRendererV2::startRender( QgsRenderContext& context, const Qg
8686
hints |= QgsSymbolV2::DataDefinedSizeScale;
8787
mTempSymbol->setRenderHints( hints );
8888

89-
mTempSymbol->startRender( context );
89+
mTempSymbol->startRender( context, vlayer );
9090

9191
if ( mSymbol->type() == QgsSymbolV2::Marker )
9292
{

src/core/symbology-ng/qgssymbolv2.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@ bool QgsSymbolV2::changeSymbolLayer( int index, QgsSymbolLayerV2* layer )
134134
}
135135

136136

137-
void QgsSymbolV2::startRender( QgsRenderContext& context )
137+
void QgsSymbolV2::startRender( QgsRenderContext& context, const QgsVectorLayer* layer )
138138
{
139139
QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha, false, mRenderHints );
140+
symbolContext.setLayer( layer );
140141
for ( QgsSymbolLayerV2List::iterator it = mLayers.begin(); it != mLayers.end(); ++it )
141142
( *it )->startRender( symbolContext );
142143
}
@@ -282,7 +283,7 @@ QSet<QString> QgsSymbolV2::usedAttributes() const
282283
////////////////////
283284

284285
QgsSymbolV2RenderContext::QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u, qreal alpha, bool selected, int renderHints, const QgsFeature* f )
285-
: mRenderContext( c ), mOutputUnit( u ), mAlpha( alpha ), mSelected( selected ), mRenderHints( renderHints ), mFeature( f )
286+
: mRenderContext( c ), mOutputUnit( u ), mAlpha( alpha ), mSelected( selected ), mRenderHints( renderHints ), mFeature( f ), mLayer( 0 )
286287
{
287288

288289
}

src/core/symbology-ng/qgssymbolv2.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class QPolygonF;
1717
class QgsFeature;
1818
class QgsSymbolLayerV2;
1919
class QgsRenderContext;
20+
class QgsVectorLayer;
2021

2122
typedef QMap<QString, QString> QgsStringMap;
2223
typedef QList<QgsSymbolLayerV2*> QgsSymbolLayerV2List;
@@ -74,7 +75,7 @@ class CORE_EXPORT QgsSymbolV2
7475
bool changeSymbolLayer( int index, QgsSymbolLayerV2* layer );
7576

7677

77-
void startRender( QgsRenderContext& context );
78+
void startRender( QgsRenderContext& context, const QgsVectorLayer* layer = 0 );
7879
void stopRender( QgsRenderContext& context );
7980

8081
void setColor( const QColor& color );
@@ -150,6 +151,9 @@ class CORE_EXPORT QgsSymbolV2RenderContext
150151
void setFeature( const QgsFeature* f ) { mFeature = f; }
151152
const QgsFeature* feature() const { return mFeature; }
152153

154+
void setLayer( const QgsVectorLayer* layer ) { mLayer = layer; }
155+
const QgsVectorLayer* layer() const { return mLayer; }
156+
153157
// Color used for selections
154158
static QColor selectionColor();
155159

@@ -166,6 +170,7 @@ class CORE_EXPORT QgsSymbolV2RenderContext
166170
bool mSelected;
167171
int mRenderHints;
168172
const QgsFeature* mFeature; //current feature
173+
const QgsVectorLayer* mLayer; //current vectorlayer
169174
};
170175

171176

src/core/symbology-ng/qgsvectorfieldsymbollayer.cpp

Lines changed: 93 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
***************************************************************************/
1717

1818
#include "qgsvectorfieldsymbollayer.h"
19+
#include "qgsvectorlayer.h"
1920

20-
QgsVectorFieldSymbolLayer::QgsVectorFieldSymbolLayer(): mXAttribute( -1 ), mYAttribute( -1 ), mScale( 1.0 ),
21-
mVectorFieldType( Cartesian ), mAngleOrientation( ClockwiseFromNorth ), mAngleUnits( Degrees )
21+
QgsVectorFieldSymbolLayer::QgsVectorFieldSymbolLayer(): mXAttribute( "" ), mYAttribute( "" ), mScale( 1.0 ),
22+
mVectorFieldType( Cartesian ), mAngleOrientation( ClockwiseFromNorth ), mAngleUnits( Degrees ), mXIndex( -1 ), mYIndex( -1 )
2223
{
2324
setSubSymbol( new QgsLineSymbolV2() );
2425
}
@@ -32,11 +33,11 @@ QgsSymbolLayerV2* QgsVectorFieldSymbolLayer::create( const QgsStringMap& propert
3233
QgsVectorFieldSymbolLayer* symbolLayer = new QgsVectorFieldSymbolLayer();
3334
if ( properties.contains( "x_attribute" ) )
3435
{
35-
symbolLayer->setXAttribute( properties["x_attribute"].toInt() );
36+
symbolLayer->setXAttribute( properties["x_attribute"] );
3637
}
3738
if ( properties.contains( "y_attribute" ) )
3839
{
39-
symbolLayer->setYAttribute( properties["y_attribute"].toInt() );
40+
symbolLayer->setYAttribute( properties["y_attribute"] );
4041
}
4142
if ( properties.contains( "scale" ) )
4243
{
@@ -69,7 +70,52 @@ bool QgsVectorFieldSymbolLayer::setSubSymbol( QgsSymbolV2* symbol )
6970

7071
void QgsVectorFieldSymbolLayer::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context )
7172
{
72-
//soon...
73+
if ( !mLineSymbol )
74+
{
75+
return;
76+
}
77+
78+
const QgsFeature* f = context.feature();
79+
if ( !f )
80+
{
81+
//preview
82+
QPolygonF line;
83+
line << QPointF( 0, 50 );
84+
line << QPointF( 100, 50 );
85+
mLineSymbol->renderPolyline( line, 0, context.renderContext() );
86+
}
87+
88+
double xComponent = 0;
89+
double yComponent = 0;
90+
91+
double xVal = 0;
92+
if ( mXIndex != -1 )
93+
{
94+
xVal = f->attributeMap()[mXIndex].toDouble();
95+
}
96+
double yVal = 0;
97+
if ( mYIndex != -1 )
98+
{
99+
yVal = f->attributeMap()[mYIndex].toDouble();
100+
}
101+
102+
switch ( mVectorFieldType )
103+
{
104+
case Cartesian:
105+
xComponent = context.outputLineWidth( xVal );
106+
yComponent = context.outputLineWidth( yVal );
107+
break;
108+
default:
109+
break;
110+
}
111+
112+
xComponent *= mScale;
113+
yComponent *= mScale;
114+
115+
QPolygonF line;
116+
line << point;
117+
line << QPointF( point.x() + xComponent, point.y() - yComponent );
118+
mLineSymbol->renderPolyline( line, f, context.renderContext() );
73119
}
74120

75121
void QgsVectorFieldSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
@@ -78,6 +124,18 @@ void QgsVectorFieldSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
78124
{
79125
mLineSymbol->startRender( context.renderContext() );
80126
}
127+
128+
const QgsVectorLayer* layer = context.layer();
129+
if ( layer )
130+
{
131+
mXIndex = layer->fieldNameIndex( mXAttribute );
132+
mYIndex = layer->fieldNameIndex( mYAttribute );
133+
}
134+
else
135+
{
136+
mXIndex = -1;
137+
mYIndex = -1;
138+
}
81139
}
82140

83141
void QgsVectorFieldSymbolLayer::stopRender( QgsSymbolV2RenderContext& context )
@@ -90,17 +148,44 @@ void QgsVectorFieldSymbolLayer::stopRender( QgsSymbolV2RenderContext& context )
90148

91149
QgsSymbolLayerV2* QgsVectorFieldSymbolLayer::clone() const
92150
{
93-
return QgsVectorFieldSymbolLayer::create( properties() );
151+
QgsSymbolLayerV2* clonedLayer = QgsVectorFieldSymbolLayer::create( properties() );
152+
if ( mLineSymbol )
153+
{
154+
clonedLayer->setSubSymbol( mLineSymbol->clone() );
155+
}
156+
return clonedLayer;
94157
}
95158

96159
QgsStringMap QgsVectorFieldSymbolLayer::properties() const
97160
{
98161
QgsStringMap properties;
99-
properties["x_attribute"] = QString::number( mXAttribute );
100-
properties["y_attribute"] = QString::number( mYAttribute );
162+
properties["x_attribute"] = mXAttribute;
163+
properties["y_attribute"] = mYAttribute;
101164
properties["scale"] = QString::number( mScale );
102165
properties["vector_field_type"] = QString::number( mVectorFieldType );
103166
properties["angle_orientation"] = QString::number( mAngleOrientation );
104167
properties["angle_units"] = QString::number( mAngleUnits );
105168
return properties;
106169
}
170+
171+
void QgsVectorFieldSymbolLayer::drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size )
172+
{
173+
if ( mLineSymbol )
174+
{
175+
mLineSymbol->drawPreviewIcon( context.renderContext().painter(), size );
176+
}
177+
}
178+
179+
QSet<QString> QgsVectorFieldSymbolLayer::usedAttributes() const
180+
{
181+
QSet<QString> attributes;
182+
if ( !mXAttribute.isEmpty() )
183+
{
184+
attributes.insert( mXAttribute );
185+
}
186+
if ( !mYAttribute.isEmpty() )
187+
{
188+
attributes.insert( mYAttribute );
189+
}
190+
return attributes;
191+
}

src/core/symbology-ng/qgsvectorfieldsymbollayer.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ class QgsVectorFieldSymbolLayer: public QgsMarkerSymbolLayerV2
6060
QgsSymbolLayerV2* clone() const;
6161
QgsStringMap properties() const;
6262

63+
void drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size );
64+
65+
QSet<QString> usedAttributes() const;
66+
6367
//setters and getters
64-
void setXAttribute( int attribute ) { mXAttribute = attribute; }
65-
int xAttribute() const { return mXAttribute; }
66-
void setYAttribute( int attribute ) { mYAttribute = attribute; }
67-
int yAttribute() const { return mYAttribute; }
68+
void setXAttribute( const QString& attribute ) { mXAttribute = attribute; }
69+
QString xAttribute() const { return mXAttribute; }
70+
void setYAttribute( const QString& attribute ) { mYAttribute = attribute; }
71+
QString yAttribute() const { return mYAttribute; }
6872
void setScale( double s ) { mScale = s; }
6973
double scale() const { return mScale; }
7074
void setVectorFieldType( VectorFieldType type ) { mVectorFieldType = type; }
@@ -75,14 +79,18 @@ class QgsVectorFieldSymbolLayer: public QgsMarkerSymbolLayerV2
7579
AngleUnits angleUnits() const { return mAngleUnits; }
7680

7781
private:
78-
int mXAttribute;
79-
int mYAttribute;
82+
QString mXAttribute;
83+
QString mYAttribute;
8084
double mScale;
8185
VectorFieldType mVectorFieldType;
8286
AngleOrientation mAngleOrientation;
8387
AngleUnits mAngleUnits;
8488

8589
QgsLineSymbolV2* mLineSymbol;
90+
91+
//Attribute indices are resolved in startRender method
92+
int mXIndex;
93+
int mYIndex;
8694
};
8795

8896
#endif // QGSVECTORFIELDSYMBOLLAYER_H

0 commit comments

Comments
 (0)