Skip to content

Commit bb21724

Browse files
author
marco
committedOct 30, 2011
Optionally pass vector layer pointer to rendercontext
1 parent 3a403cc commit bb21724

6 files changed

+146
-48
lines changed
 

‎src/core/symbology-ng/qgssinglesymbolrendererv2.cpp

+2-2
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

+3-2
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

+6-1
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

+93-8
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

+14-6
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

‎src/gui/symbology-ng/qgsvectorfieldsymbollayerwidget.cpp

+28-29
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66
QgsVectorFieldSymbolLayerWidget::QgsVectorFieldSymbolLayerWidget( const QgsVectorLayer* vl, QWidget* parent ): QgsSymbolLayerV2Widget( parent, vl ), mLayer( 0 )
77
{
88
setupUi( this );
9-
if( mVectorLayer )
9+
if ( mVectorLayer )
1010
{
1111
const QgsFieldMap& fm = mVectorLayer->pendingFields();
1212
QgsFieldMap::const_iterator fieldIt = fm.constBegin();
13-
mXAttributeComboBox->addItem( "", -1 );
14-
mYAttributeComboBox->addItem( "", -1 );
13+
mXAttributeComboBox->addItem( "" );
14+
mYAttributeComboBox->addItem( "" );
1515
for ( ; fieldIt != fm.constEnd(); ++fieldIt )
1616
{
1717
QString fieldName = fieldIt.value().name();
18-
int index = fieldIt.key();
19-
mXAttributeComboBox->addItem( fieldName, index );
20-
mYAttributeComboBox->addItem( fieldName, index );
18+
mXAttributeComboBox->addItem( fieldName );
19+
mYAttributeComboBox->addItem( fieldName );
2120
}
2221
}
2322
}
@@ -28,50 +27,50 @@ QgsVectorFieldSymbolLayerWidget::~QgsVectorFieldSymbolLayerWidget()
2827

2928
void QgsVectorFieldSymbolLayerWidget::setSymbolLayer( QgsSymbolLayerV2* layer )
3029
{
31-
if( layer->layerType() != "VectorField" )
30+
if ( layer->layerType() != "VectorField" )
3231
{
3332
return;
3433
}
3534
mLayer = static_cast<QgsVectorFieldSymbolLayer*>( layer );
36-
if( !mLayer )
35+
if ( !mLayer )
3736
{
3837
return;
3938
}
4039

41-
mXAttributeComboBox->setCurrentIndex( mXAttributeComboBox->findData( mLayer->xAttribute() ) );
42-
mYAttributeComboBox->setCurrentIndex( mYAttributeComboBox->findData( mLayer->yAttribute() ) );
40+
mXAttributeComboBox->setCurrentIndex( mXAttributeComboBox->findText( mLayer->xAttribute() ) );
41+
mYAttributeComboBox->setCurrentIndex( mYAttributeComboBox->findText( mLayer->yAttribute() ) );
4342
mScaleSpinBox->setValue( mLayer->scale() );
4443

4544
QgsVectorFieldSymbolLayer::VectorFieldType type = mLayer->vectorFieldType();
46-
if( type == QgsVectorFieldSymbolLayer::Cartesian )
45+
if ( type == QgsVectorFieldSymbolLayer::Cartesian )
4746
{
4847
mCartesianRadioButton->setChecked( true );
4948
}
50-
else if( type == QgsVectorFieldSymbolLayer::Polar )
49+
else if ( type == QgsVectorFieldSymbolLayer::Polar )
5150
{
5251
mPolarRadioButton->setChecked( true );
5352
}
54-
else if( type == QgsVectorFieldSymbolLayer::Height )
53+
else if ( type == QgsVectorFieldSymbolLayer::Height )
5554
{
5655
mHeightRadioButton->setChecked( true );
5756
}
5857

5958
QgsVectorFieldSymbolLayer::AngleOrientation orientation = mLayer->angleOrientation();
60-
if( orientation == QgsVectorFieldSymbolLayer::ClockwiseFromNorth )
59+
if ( orientation == QgsVectorFieldSymbolLayer::ClockwiseFromNorth )
6160
{
6261
mClockwiseFromNorthRadioButton->setChecked( true );
6362
}
64-
else if( orientation == QgsVectorFieldSymbolLayer::CounterclockwiseFromEast )
63+
else if ( orientation == QgsVectorFieldSymbolLayer::CounterclockwiseFromEast )
6564
{
6665
mCounterclockwiseFromEastRadioButton->setChecked( true );
6766
}
6867

6968
QgsVectorFieldSymbolLayer::AngleUnits angleUnits = mLayer->angleUnits();
70-
if( angleUnits == QgsVectorFieldSymbolLayer::Degrees )
69+
if ( angleUnits == QgsVectorFieldSymbolLayer::Degrees )
7170
{
7271
mDegreesRadioButton->setChecked( true );
7372
}
74-
else if( angleUnits == QgsVectorFieldSymbolLayer::Radians )
73+
else if ( angleUnits == QgsVectorFieldSymbolLayer::Radians )
7574
{
7675
mRadiansRadioButton->setChecked( true );
7776
}
@@ -85,7 +84,7 @@ QgsSymbolLayerV2* QgsVectorFieldSymbolLayerWidget::symbolLayer()
8584

8685
void QgsVectorFieldSymbolLayerWidget::on_mScaleSpinBox_valueChanged( double d )
8786
{
88-
if( mLayer )
87+
if ( mLayer )
8988
{
9089
mLayer->setScale( d );
9190
emit changed();
@@ -94,18 +93,18 @@ void QgsVectorFieldSymbolLayerWidget::on_mScaleSpinBox_valueChanged( double d )
9493

9594
void QgsVectorFieldSymbolLayerWidget::on_mXAttributeComboBox_currentIndexChanged( int index )
9695
{
97-
if( mLayer )
96+
if ( mLayer )
9897
{
99-
mLayer->setXAttribute( mXAttributeComboBox->itemData( index ).toInt() );
98+
mLayer->setXAttribute( mXAttributeComboBox->itemText( index ) );
10099
emit changed();
101100
}
102101
}
103102

104103
void QgsVectorFieldSymbolLayerWidget::on_mYAttributeComboBox_currentIndexChanged( int index )
105104
{
106-
if( mLayer )
105+
if ( mLayer )
107106
{
108-
mLayer->setYAttribute( mYAttributeComboBox->itemData( index ).toInt() );
107+
mLayer->setYAttribute( mYAttributeComboBox->itemText( index ) );
109108
emit changed();
110109
}
111110
}
@@ -138,7 +137,7 @@ void QgsVectorFieldSymbolLayerWidget::updateMarkerIcon()
138137

139138
void QgsVectorFieldSymbolLayerWidget::on_mCartesianRadioButton_toggled( bool checked )
140139
{
141-
if( mLayer && checked )
140+
if ( mLayer && checked )
142141
{
143142
mLayer->setVectorFieldType( QgsVectorFieldSymbolLayer::Cartesian );
144143
emit changed();
@@ -147,7 +146,7 @@ void QgsVectorFieldSymbolLayerWidget::on_mCartesianRadioButton_toggled( bool che
147146

148147
void QgsVectorFieldSymbolLayerWidget::on_mPolarRadioButton_toggled( bool checked )
149148
{
150-
if( mLayer && checked )
149+
if ( mLayer && checked )
151150
{
152151
mLayer->setVectorFieldType( QgsVectorFieldSymbolLayer::Polar );
153152
emit changed();
@@ -156,7 +155,7 @@ void QgsVectorFieldSymbolLayerWidget::on_mPolarRadioButton_toggled( bool checked
156155

157156
void QgsVectorFieldSymbolLayerWidget::on_mHeightRadioButton_toggled( bool checked )
158157
{
159-
if( mLayer && checked )
158+
if ( mLayer && checked )
160159
{
161160
mLayer->setVectorFieldType( QgsVectorFieldSymbolLayer::Height );
162161
emit changed();
@@ -165,7 +164,7 @@ void QgsVectorFieldSymbolLayerWidget::on_mHeightRadioButton_toggled( bool checke
165164

166165
void QgsVectorFieldSymbolLayerWidget::on_mDegreesRadioButton_toggled( bool checked )
167166
{
168-
if( mLayer && checked )
167+
if ( mLayer && checked )
169168
{
170169
mLayer->setAngleUnits( QgsVectorFieldSymbolLayer::Degrees );
171170
emit changed();
@@ -174,7 +173,7 @@ void QgsVectorFieldSymbolLayerWidget::on_mDegreesRadioButton_toggled( bool check
174173

175174
void QgsVectorFieldSymbolLayerWidget::on_mRadiansRadioButton_toggled( bool checked )
176175
{
177-
if( mLayer && checked )
176+
if ( mLayer && checked )
178177
{
179178
mLayer->setAngleUnits( QgsVectorFieldSymbolLayer::Radians );
180179
emit changed();
@@ -183,7 +182,7 @@ void QgsVectorFieldSymbolLayerWidget::on_mRadiansRadioButton_toggled( bool check
183182

184183
void QgsVectorFieldSymbolLayerWidget::on_mClockwiseFromNorthRadioButton_toggled( bool checked )
185184
{
186-
if( mLayer && checked )
185+
if ( mLayer && checked )
187186
{
188187
mLayer->setAngleOrientation( QgsVectorFieldSymbolLayer::ClockwiseFromNorth );
189188
emit changed();
@@ -192,7 +191,7 @@ void QgsVectorFieldSymbolLayerWidget::on_mClockwiseFromNorthRadioButton_toggled(
192191

193192
void QgsVectorFieldSymbolLayerWidget::on_mCounterclockwiseFromEastRadioButton_toggled( bool checked )
194193
{
195-
if( mLayer && checked )
194+
if ( mLayer && checked )
196195
{
197196
mLayer->setAngleOrientation( QgsVectorFieldSymbolLayer::CounterclockwiseFromEast );
198197
emit changed();

0 commit comments

Comments
 (0)
Please sign in to comment.