16
16
#include " qgsmarkersymbollayerv2.h"
17
17
#include " qgssymbollayerv2utils.h"
18
18
19
+ #include " qgsexpression.h"
19
20
#include " qgsrendercontext.h"
20
21
#include " qgsproject.h"
21
22
#include " qgssvgcache.h"
29
30
30
31
QgsSimpleFillSymbolLayerV2::QgsSimpleFillSymbolLayerV2 ( QColor color, Qt::BrushStyle style, QColor borderColor, Qt::PenStyle borderStyle, double borderWidth )
31
32
: mBrushStyle( style ), mBorderColor( borderColor ), mBorderStyle( borderStyle ), mBorderWidth( borderWidth ), mBorderWidthUnit( QgsSymbolV2::MM ),
32
- mOffsetUnit( QgsSymbolV2::MM )
33
+ mOffsetUnit( QgsSymbolV2::MM ), mColorExpression( 0 ), mColorBorderExpression( 0 ), mWidthBorderExpression( 0 )
33
34
{
34
35
mColor = color;
35
36
}
@@ -50,6 +51,124 @@ QgsSymbolV2::OutputUnit QgsSimpleFillSymbolLayerV2::outputUnit() const
50
51
return unit;
51
52
}
52
53
54
+ const QgsExpression* QgsSimpleFillSymbolLayerV2::dataDefinedProperty ( const QString& property ) const
55
+ {
56
+ if ( property == " color" )
57
+ {
58
+ return mColorExpression ;
59
+ }
60
+ else if ( property == " color_border" )
61
+ {
62
+ return mColorBorderExpression ;
63
+ }
64
+ else if ( property == " width_border" )
65
+ {
66
+ return mWidthBorderExpression ;
67
+ }
68
+ return 0 ;
69
+ }
70
+
71
+ QString QgsSimpleFillSymbolLayerV2::dataDefinedPropertyString ( const QString& property ) const
72
+ {
73
+ const QgsExpression* ex = dataDefinedProperty ( property );
74
+ return ex ? ex->dump () : QString ();
75
+ }
76
+
77
+ void QgsSimpleFillSymbolLayerV2::setDataDefinedProperty ( const QString& property, const QString& expressionString )
78
+ {
79
+ if ( property == " color" )
80
+ {
81
+ delete mColorExpression ; mColorExpression = new QgsExpression ( expressionString );
82
+ }
83
+ else if ( property == " color_border" )
84
+ {
85
+ delete mColorBorderExpression ; mColorBorderExpression = new QgsExpression ( expressionString );
86
+ }
87
+ else if ( property == " width_border" )
88
+ {
89
+ delete mWidthBorderExpression ; mWidthBorderExpression = new QgsExpression ( expressionString );
90
+ }
91
+ }
92
+
93
+ void QgsSimpleFillSymbolLayerV2::removeDataDefinedProperty ( const QString& property )
94
+ {
95
+ if ( property == " color" )
96
+ {
97
+ delete mColorExpression ; mColorExpression = 0 ;
98
+ }
99
+ else if ( property == " color_border" )
100
+ {
101
+ delete mColorBorderExpression ; mColorBorderExpression = 0 ;
102
+ }
103
+ else if ( property == " width_border" )
104
+ {
105
+ delete mWidthBorderExpression ; mWidthBorderExpression = 0 ;
106
+ }
107
+ }
108
+
109
+ void QgsSimpleFillSymbolLayerV2::removeDataDefinedProperties ()
110
+ {
111
+ delete mColorExpression ; mColorExpression = 0 ;
112
+ delete mColorBorderExpression ; mColorBorderExpression = 0 ;
113
+ delete mWidthBorderExpression ; mWidthBorderExpression = 0 ;
114
+ }
115
+
116
+ QSet<QString> QgsSimpleFillSymbolLayerV2::usedAttributes () const
117
+ {
118
+ QSet<QString> attributes;
119
+
120
+ // add data defined attributes
121
+ QStringList columns;
122
+ if ( mColorExpression )
123
+ columns.append ( mColorExpression ->referencedColumns () );
124
+ if ( mColorBorderExpression )
125
+ columns.append ( mColorBorderExpression ->referencedColumns () );
126
+ if ( mWidthBorderExpression )
127
+ columns.append ( mWidthBorderExpression ->referencedColumns () );
128
+
129
+ QStringList::const_iterator it = columns.constBegin ();
130
+ for ( ; it != columns.constEnd (); ++it )
131
+ {
132
+ attributes.insert ( *it );
133
+ }
134
+ return attributes;
135
+ }
136
+
137
+ void QgsSimpleFillSymbolLayerV2::prepareExpressions ( const QgsVectorLayer* vl )
138
+ {
139
+ if ( !vl )
140
+ {
141
+ return ;
142
+ }
143
+
144
+ const QgsFields& fields = vl->pendingFields ();
145
+ if ( mColorExpression )
146
+ mColorExpression ->prepare ( fields );
147
+ if ( mColorBorderExpression )
148
+ mColorBorderExpression ->prepare ( fields );
149
+ if ( mWidthBorderExpression )
150
+ mWidthBorderExpression ->prepare ( fields );
151
+ }
152
+
153
+ void QgsSimpleFillSymbolLayerV2::applyDataDefinedSymbology ( QgsSymbolV2RenderContext& context, QBrush& brush, QPen& pen, QPen& selPen )
154
+ {
155
+ if ( mColorExpression )
156
+ {
157
+ brush.setColor ( QColor ( mColorExpression ->evaluate ( const_cast <QgsFeature*>( context.feature () ) ).toString () ) );
158
+ }
159
+ if ( mColorBorderExpression )
160
+ {
161
+ pen.setColor ( QColor ( mColorBorderExpression ->evaluate ( const_cast <QgsFeature*>( context.feature () ) ).toString () ) );
162
+ }
163
+ if ( mWidthBorderExpression )
164
+ {
165
+ double width = mWidthBorderExpression ->evaluate ( const_cast <QgsFeature*>( context.feature () ) ).toDouble ();
166
+ width *= QgsSymbolLayerV2Utils::lineWidthScaleFactor ( context.renderContext (), mBorderWidthUnit );
167
+ pen.setWidthF ( width );
168
+ selPen.setWidthF ( width );
169
+ }
170
+ }
171
+
53
172
54
173
QgsSymbolLayerV2* QgsSimpleFillSymbolLayerV2::create ( const QgsStringMap& props )
55
174
{
@@ -79,6 +198,19 @@ QgsSymbolLayerV2* QgsSimpleFillSymbolLayerV2::create( const QgsStringMap& props
79
198
sl->setBorderWidthUnit ( QgsSymbolLayerV2Utils::decodeOutputUnit ( props[" border_width_unit" ] ) );
80
199
if ( props.contains ( " offset_unit" ) )
81
200
sl->setOffsetUnit ( QgsSymbolLayerV2Utils::decodeOutputUnit ( props[" offset_unit" ] ) );
201
+
202
+ if ( props.contains ( " color_expression" ) )
203
+ {
204
+ sl->setDataDefinedProperty ( " color" , props[" color_expression" ] );
205
+ }
206
+ if ( props.contains ( " color_border_expression" ) )
207
+ {
208
+ sl->setDataDefinedProperty ( " color_border" , props[" color_border_expression" ] );
209
+ }
210
+ if ( props.contains ( " width_border_expression" ) )
211
+ {
212
+ sl->setDataDefinedProperty ( " width_border" , props[" width_border_expression" ] );
213
+ }
82
214
return sl;
83
215
}
84
216
@@ -116,6 +248,7 @@ void QgsSimpleFillSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context
116
248
mSelPen = QPen ( selPenColor );
117
249
mPen .setStyle ( mBorderStyle );
118
250
mPen .setWidthF ( mBorderWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor ( context.renderContext (), mBorderWidthUnit ) );
251
+ prepareExpressions ( context.layer () );
119
252
}
120
253
121
254
void QgsSimpleFillSymbolLayerV2::stopRender ( QgsSymbolV2RenderContext& context )
@@ -131,6 +264,8 @@ void QgsSimpleFillSymbolLayerV2::renderPolygon( const QPolygonF& points, QList<Q
131
264
return ;
132
265
}
133
266
267
+ applyDataDefinedSymbology ( context, mBrush , mPen , mSelPen );
268
+
134
269
p->setBrush ( context.selected () ? mSelBrush : mBrush );
135
270
p->setPen ( mPen );
136
271
p->setPen ( context.selected () ? mSelPen : mPen );
@@ -162,6 +297,18 @@ QgsStringMap QgsSimpleFillSymbolLayerV2::properties() const
162
297
map[" border_width_unit" ] = QgsSymbolLayerV2Utils::encodeOutputUnit ( mBorderWidthUnit );
163
298
map[" offset" ] = QgsSymbolLayerV2Utils::encodePoint ( mOffset );
164
299
map[" offset_unit" ] = QgsSymbolLayerV2Utils::encodeOutputUnit ( mOffsetUnit );
300
+ if ( mColorExpression )
301
+ {
302
+ map[" color_expression" ] = mColorExpression ->dump ();
303
+ }
304
+ if ( mColorBorderExpression )
305
+ {
306
+ map[" color_border_expression" ] = mColorBorderExpression ->dump ();
307
+ }
308
+ if ( mWidthBorderExpression )
309
+ {
310
+ map[" width_border_expression" ] = mWidthBorderExpression ->dump ();
311
+ }
165
312
return map;
166
313
}
167
314
@@ -171,6 +318,19 @@ QgsSymbolLayerV2* QgsSimpleFillSymbolLayerV2::clone() const
171
318
sl->setOffset ( mOffset );
172
319
sl->setOffsetUnit ( mOffsetUnit );
173
320
sl->setBorderWidthUnit ( mBorderWidthUnit );
321
+
322
+ if ( mColorExpression )
323
+ {
324
+ sl->setDataDefinedProperty ( " color" , mColorExpression ->dump () );
325
+ }
326
+ if ( mColorBorderExpression )
327
+ {
328
+ sl->setDataDefinedProperty ( " color_border" , mColorBorderExpression ->dump () );
329
+ }
330
+ if ( mWidthBorderExpression )
331
+ {
332
+ sl->setDataDefinedProperty ( " width_border" , mWidthBorderExpression ->dump () );
333
+ }
174
334
return sl;
175
335
}
176
336
0 commit comments