16
16
***************************************************************************/
17
17
18
18
#include " qgsvectorfieldsymbollayer.h"
19
+ #include " qgsvectorlayer.h"
19
20
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 )
22
23
{
23
24
setSubSymbol ( new QgsLineSymbolV2 () );
24
25
}
@@ -32,11 +33,11 @@ QgsSymbolLayerV2* QgsVectorFieldSymbolLayer::create( const QgsStringMap& propert
32
33
QgsVectorFieldSymbolLayer* symbolLayer = new QgsVectorFieldSymbolLayer ();
33
34
if ( properties.contains ( " x_attribute" ) )
34
35
{
35
- symbolLayer->setXAttribute ( properties[" x_attribute" ]. toInt () );
36
+ symbolLayer->setXAttribute ( properties[" x_attribute" ] );
36
37
}
37
38
if ( properties.contains ( " y_attribute" ) )
38
39
{
39
- symbolLayer->setYAttribute ( properties[" y_attribute" ]. toInt () );
40
+ symbolLayer->setYAttribute ( properties[" y_attribute" ] );
40
41
}
41
42
if ( properties.contains ( " scale" ) )
42
43
{
@@ -69,7 +70,52 @@ bool QgsVectorFieldSymbolLayer::setSubSymbol( QgsSymbolV2* symbol )
69
70
70
71
void QgsVectorFieldSymbolLayer::renderPoint ( const QPointF& point, QgsSymbolV2RenderContext& context )
71
72
{
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 () );
73
119
}
74
120
75
121
void QgsVectorFieldSymbolLayer::startRender ( QgsSymbolV2RenderContext& context )
@@ -78,6 +124,18 @@ void QgsVectorFieldSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
78
124
{
79
125
mLineSymbol ->startRender ( context.renderContext () );
80
126
}
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
+ }
81
139
}
82
140
83
141
void QgsVectorFieldSymbolLayer::stopRender ( QgsSymbolV2RenderContext& context )
@@ -90,17 +148,44 @@ void QgsVectorFieldSymbolLayer::stopRender( QgsSymbolV2RenderContext& context )
90
148
91
149
QgsSymbolLayerV2* QgsVectorFieldSymbolLayer::clone () const
92
150
{
93
- return QgsVectorFieldSymbolLayer::create ( properties () );
151
+ QgsSymbolLayerV2* clonedLayer = QgsVectorFieldSymbolLayer::create ( properties () );
152
+ if ( mLineSymbol )
153
+ {
154
+ clonedLayer->setSubSymbol ( mLineSymbol ->clone () );
155
+ }
156
+ return clonedLayer;
94
157
}
95
158
96
159
QgsStringMap QgsVectorFieldSymbolLayer::properties () const
97
160
{
98
161
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 ;
101
164
properties[" scale" ] = QString::number ( mScale );
102
165
properties[" vector_field_type" ] = QString::number ( mVectorFieldType );
103
166
properties[" angle_orientation" ] = QString::number ( mAngleOrientation );
104
167
properties[" angle_units" ] = QString::number ( mAngleUnits );
105
168
return properties;
106
169
}
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
+ }
0 commit comments