Skip to content
Permalink
Browse files

Optionally pass vector layer pointer to rendercontext

  • Loading branch information
marco
marco committed Oct 30, 2011
1 parent 3a403cc commit bb21724dc5cea6c7c34d861975b7332891413747
@@ -72,7 +72,7 @@ void QgsSingleSymbolRendererV2::startRender( QgsRenderContext& context, const Qg
mRotationFieldIdx = mRotationField.isEmpty() ? -1 : vlayer->fieldNameIndex( mRotationField );
mSizeScaleFieldIdx = mSizeScaleField.isEmpty() ? -1 : vlayer->fieldNameIndex( mSizeScaleField );

mSymbol->startRender( context );
mSymbol->startRender( context, vlayer );

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

mTempSymbol->startRender( context );
mTempSymbol->startRender( context, vlayer );

if ( mSymbol->type() == QgsSymbolV2::Marker )
{
@@ -134,9 +134,10 @@ bool QgsSymbolV2::changeSymbolLayer( int index, QgsSymbolLayerV2* layer )
}


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

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

}
@@ -17,6 +17,7 @@ class QPolygonF;
class QgsFeature;
class QgsSymbolLayerV2;
class QgsRenderContext;
class QgsVectorLayer;

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


void startRender( QgsRenderContext& context );
void startRender( QgsRenderContext& context, const QgsVectorLayer* layer = 0 );
void stopRender( QgsRenderContext& context );

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

void setLayer( const QgsVectorLayer* layer ) { mLayer = layer; }
const QgsVectorLayer* layer() const { return mLayer; }

// Color used for selections
static QColor selectionColor();

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


@@ -16,9 +16,10 @@
***************************************************************************/

#include "qgsvectorfieldsymbollayer.h"
#include "qgsvectorlayer.h"

QgsVectorFieldSymbolLayer::QgsVectorFieldSymbolLayer(): mXAttribute( -1 ), mYAttribute( -1 ), mScale( 1.0 ),
mVectorFieldType( Cartesian ), mAngleOrientation( ClockwiseFromNorth ), mAngleUnits( Degrees )
QgsVectorFieldSymbolLayer::QgsVectorFieldSymbolLayer(): mXAttribute( "" ), mYAttribute( "" ), mScale( 1.0 ),
mVectorFieldType( Cartesian ), mAngleOrientation( ClockwiseFromNorth ), mAngleUnits( Degrees ), mXIndex( -1 ), mYIndex( -1 )
{
setSubSymbol( new QgsLineSymbolV2() );
}
@@ -32,11 +33,11 @@ QgsSymbolLayerV2* QgsVectorFieldSymbolLayer::create( const QgsStringMap& propert
QgsVectorFieldSymbolLayer* symbolLayer = new QgsVectorFieldSymbolLayer();
if ( properties.contains( "x_attribute" ) )
{
symbolLayer->setXAttribute( properties["x_attribute"].toInt() );
symbolLayer->setXAttribute( properties["x_attribute"] );
}
if ( properties.contains( "y_attribute" ) )
{
symbolLayer->setYAttribute( properties["y_attribute"].toInt() );
symbolLayer->setYAttribute( properties["y_attribute"] );
}
if ( properties.contains( "scale" ) )
{
@@ -69,7 +70,52 @@ bool QgsVectorFieldSymbolLayer::setSubSymbol( QgsSymbolV2* symbol )

void QgsVectorFieldSymbolLayer::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context )
{
//soon...
if ( !mLineSymbol )
{
return;
}

const QgsFeature* f = context.feature();
if ( !f )
{
//preview
QPolygonF line;
line << QPointF( 0, 50 );
line << QPointF( 100, 50 );
mLineSymbol->renderPolyline( line, 0, context.renderContext() );
}

double xComponent = 0;
double yComponent = 0;

double xVal = 0;
if ( mXIndex != -1 )
{
xVal = f->attributeMap()[mXIndex].toDouble();
}
double yVal = 0;
if ( mYIndex != -1 )
{
yVal = f->attributeMap()[mYIndex].toDouble();
}

switch ( mVectorFieldType )
{
case Cartesian:
xComponent = context.outputLineWidth( xVal );
yComponent = context.outputLineWidth( yVal );
break;
default:
break;
}

xComponent *= mScale;
yComponent *= mScale;

QPolygonF line;
line << point;
line << QPointF( point.x() + xComponent, point.y() - yComponent );
mLineSymbol->renderPolyline( line, f, context.renderContext() );
}

void QgsVectorFieldSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
@@ -78,6 +124,18 @@ void QgsVectorFieldSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
{
mLineSymbol->startRender( context.renderContext() );
}

const QgsVectorLayer* layer = context.layer();
if ( layer )
{
mXIndex = layer->fieldNameIndex( mXAttribute );
mYIndex = layer->fieldNameIndex( mYAttribute );
}
else
{
mXIndex = -1;
mYIndex = -1;
}
}

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

QgsSymbolLayerV2* QgsVectorFieldSymbolLayer::clone() const
{
return QgsVectorFieldSymbolLayer::create( properties() );
QgsSymbolLayerV2* clonedLayer = QgsVectorFieldSymbolLayer::create( properties() );
if ( mLineSymbol )
{
clonedLayer->setSubSymbol( mLineSymbol->clone() );
}
return clonedLayer;
}

QgsStringMap QgsVectorFieldSymbolLayer::properties() const
{
QgsStringMap properties;
properties["x_attribute"] = QString::number( mXAttribute );
properties["y_attribute"] = QString::number( mYAttribute );
properties["x_attribute"] = mXAttribute;
properties["y_attribute"] = mYAttribute;
properties["scale"] = QString::number( mScale );
properties["vector_field_type"] = QString::number( mVectorFieldType );
properties["angle_orientation"] = QString::number( mAngleOrientation );
properties["angle_units"] = QString::number( mAngleUnits );
return properties;
}

void QgsVectorFieldSymbolLayer::drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size )
{
if ( mLineSymbol )
{
mLineSymbol->drawPreviewIcon( context.renderContext().painter(), size );
}
}

QSet<QString> QgsVectorFieldSymbolLayer::usedAttributes() const
{
QSet<QString> attributes;
if ( !mXAttribute.isEmpty() )
{
attributes.insert( mXAttribute );
}
if ( !mYAttribute.isEmpty() )
{
attributes.insert( mYAttribute );
}
return attributes;
}
@@ -60,11 +60,15 @@ class QgsVectorFieldSymbolLayer: public QgsMarkerSymbolLayerV2
QgsSymbolLayerV2* clone() const;
QgsStringMap properties() const;

void drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size );

QSet<QString> usedAttributes() const;

//setters and getters
void setXAttribute( int attribute ) { mXAttribute = attribute; }
int xAttribute() const { return mXAttribute; }
void setYAttribute( int attribute ) { mYAttribute = attribute; }
int yAttribute() const { return mYAttribute; }
void setXAttribute( const QString& attribute ) { mXAttribute = attribute; }
QString xAttribute() const { return mXAttribute; }
void setYAttribute( const QString& attribute ) { mYAttribute = attribute; }
QString yAttribute() const { return mYAttribute; }
void setScale( double s ) { mScale = s; }
double scale() const { return mScale; }
void setVectorFieldType( VectorFieldType type ) { mVectorFieldType = type; }
@@ -75,14 +79,18 @@ class QgsVectorFieldSymbolLayer: public QgsMarkerSymbolLayerV2
AngleUnits angleUnits() const { return mAngleUnits; }

private:
int mXAttribute;
int mYAttribute;
QString mXAttribute;
QString mYAttribute;
double mScale;
VectorFieldType mVectorFieldType;
AngleOrientation mAngleOrientation;
AngleUnits mAngleUnits;

QgsLineSymbolV2* mLineSymbol;

//Attribute indices are resolved in startRender method
int mXIndex;
int mYIndex;
};

#endif // QGSVECTORFIELDSYMBOLLAYER_H

0 comments on commit bb21724

Please sign in to comment.
You can’t perform that action at this time.