@@ -27,18 +27,18 @@ QgsFeatureListModel::QgsFeatureListModel( QgsAttributeTableFilterModel *sourceMo
27
27
, mInjectNull( false )
28
28
{
29
29
setSourceModel ( sourceModel );
30
- mExpression = new QgsExpression ( QLatin1String ( " " ) );
31
30
}
32
31
33
32
QgsFeatureListModel::~QgsFeatureListModel ()
34
33
{
35
- delete mExpression ;
36
34
}
37
35
38
36
void QgsFeatureListModel::setSourceModel ( QgsAttributeTableFilterModel *sourceModel )
39
37
{
40
38
QAbstractProxyModel::setSourceModel ( sourceModel );
39
+ mExpressionContext = sourceModel->layer ()->createExpressionContext ();
41
40
mFilterModel = sourceModel;
41
+
42
42
if ( mFilterModel )
43
43
{
44
44
// rewire (filter-)change events in the source model so this proxy reflects the changes
@@ -91,9 +91,8 @@ QVariant QgsFeatureListModel::data( const QModelIndex &index, int role ) const
91
91
92
92
mFilterModel ->layerCache ()->featureAtId ( idxToFid ( index ), feat );
93
93
94
- QgsExpressionContext context ( QgsExpressionContextUtils::globalProjectLayerScopes ( mFilterModel ->layer () ) );
95
- context.setFeature ( feat );
96
- return mExpression ->evaluate ( &context );
94
+ mExpressionContext .setFeature ( feat );
95
+ return mDisplayExpression .evaluate ( &mExpressionContext );
97
96
}
98
97
99
98
if ( role == FeatureInfoRole )
@@ -133,6 +132,57 @@ QVariant QgsFeatureListModel::data( const QModelIndex &index, int role ) const
133
132
return Qt::AlignLeft;
134
133
}
135
134
135
+
136
+ if ( role == Qt::BackgroundColorRole
137
+ || Qt::TextColorRole
138
+ || Qt::DecorationRole
139
+ || Qt::FontRole )
140
+ {
141
+ QgsVectorLayer *layer = mFilterModel ->layer ();
142
+ QgsFeature feat;
143
+ QgsFeatureId fid = idxToFid ( index );
144
+ mFilterModel ->layerCache ()->featureAtId ( fid, feat );
145
+ mExpressionContext .setFeature ( feat );
146
+ QList<QgsConditionalStyle> styles;
147
+
148
+ if ( mRowStylesMap .contains ( fid ) )
149
+ {
150
+ styles = mRowStylesMap .value ( fid );
151
+ }
152
+ else
153
+ {
154
+ styles = QgsConditionalStyle::matchingConditionalStyles ( layer->conditionalStyles ()->rowStyles (), QVariant (), mExpressionContext );
155
+ mRowStylesMap .insert ( fid, styles );
156
+ }
157
+
158
+ QgsConditionalStyle rowstyle = QgsConditionalStyle::compressStyles ( styles );
159
+
160
+ if ( mDisplayExpression .isField () )
161
+ {
162
+ QString fieldName = *mDisplayExpression .referencedColumns ().constBegin ();
163
+ styles = layer->conditionalStyles ()->fieldStyles ( fieldName );
164
+ styles = QgsConditionalStyle::matchingConditionalStyles ( styles, feat.attribute ( fieldName ), mExpressionContext );
165
+ }
166
+
167
+ styles.insert ( 0 , rowstyle );
168
+
169
+ QgsConditionalStyle style = QgsConditionalStyle::compressStyles ( styles );
170
+
171
+ if ( style.isValid () )
172
+ {
173
+ if ( role == Qt::BackgroundColorRole && style.validBackgroundColor () )
174
+ return style.backgroundColor ();
175
+ if ( role == Qt::TextColorRole && style.validTextColor () )
176
+ return style.textColor ();
177
+ if ( role == Qt::DecorationRole )
178
+ return style.icon ();
179
+ if ( role == Qt::FontRole )
180
+ return style.font ();
181
+ }
182
+
183
+ return QVariant ();
184
+ }
185
+
136
186
return sourceModel ()->data ( mapToSource ( index ), role );
137
187
}
138
188
@@ -170,21 +220,17 @@ QgsAttributeTableModel *QgsFeatureListModel::masterModel()
170
220
171
221
bool QgsFeatureListModel::setDisplayExpression ( const QString &expression )
172
222
{
173
- QgsExpression *exp = new QgsExpression ( expression );
174
-
175
- QgsExpressionContext context ( QgsExpressionContextUtils::globalProjectLayerScopes ( mFilterModel ->layer () ) );
223
+ QgsExpression exp = QgsExpression ( expression );
176
224
177
- exp -> prepare ( &context );
225
+ exp . prepare ( &mExpressionContext );
178
226
179
- if ( exp -> hasParserError () )
227
+ if ( exp . hasParserError () )
180
228
{
181
- mParserErrorString = exp ->parserErrorString ();
182
- delete exp ;
229
+ mParserErrorString = exp .parserErrorString ();
183
230
return false ;
184
231
}
185
232
186
- delete mExpression ;
187
- mExpression = exp ;
233
+ mDisplayExpression = exp ;
188
234
189
235
emit dataChanged ( index ( 0 , 0 ), index ( rowCount () - 1 , 0 ) );
190
236
return true ;
@@ -197,7 +243,7 @@ QString QgsFeatureListModel::parserErrorString()
197
243
198
244
QString QgsFeatureListModel::displayExpression () const
199
245
{
200
- return mExpression -> expression ();
246
+ return mDisplayExpression . expression ();
201
247
}
202
248
203
249
bool QgsFeatureListModel::featureByIndex ( const QModelIndex &index, QgsFeature &feat )
0 commit comments