16
16
#include " qgsfeaturefiltermodel.h"
17
17
#include " qgsfeaturefiltermodel_p.h"
18
18
19
+ #include " qgsconditionalstyle.h"
20
+
19
21
QgsFeatureFilterModel::QgsFeatureFilterModel ( QObject *parent )
20
22
: QAbstractItemModel( parent )
21
23
{
@@ -42,21 +44,22 @@ void QgsFeatureFilterModel::setSourceLayer( QgsVectorLayer *sourceLayer )
42
44
return ;
43
45
44
46
mSourceLayer = sourceLayer;
47
+ mExpressionContext = sourceLayer->createExpressionContext ();
45
48
reload ();
46
49
emit sourceLayerChanged ();
47
50
}
48
51
49
52
QString QgsFeatureFilterModel::displayExpression () const
50
53
{
51
- return mDisplayExpression ;
54
+ return mDisplayExpression . expression () ;
52
55
}
53
56
54
57
void QgsFeatureFilterModel::setDisplayExpression ( const QString &displayExpression )
55
58
{
56
- if ( mDisplayExpression == displayExpression )
59
+ if ( mDisplayExpression . expression () == displayExpression )
57
60
return ;
58
61
59
- mDisplayExpression = displayExpression;
62
+ mDisplayExpression = QgsExpression ( displayExpression ) ;
60
63
reload ();
61
64
emit displayExpressionChanged ();
62
65
}
@@ -136,24 +139,51 @@ QVariant QgsFeatureFilterModel::data( const QModelIndex &index, int role ) const
136
139
case IdentifierValueRole:
137
140
return mEntries .value ( index .row () ).identifierValue ;
138
141
139
- case Qt::ForegroundRole:
142
+ case Qt::BackgroundColorRole:
143
+ FALLTHROUGH;
144
+ case Qt::TextColorRole:
145
+ FALLTHROUGH;
146
+ case Qt::FontRole:
147
+ {
140
148
if ( mEntries .value ( index .row () ).identifierValue .isNull () )
141
149
{
142
- return QBrush ( QColor ( Qt::gray ) );
150
+ // Representation for NULL value
151
+ if ( role == Qt::TextColorRole )
152
+ {
153
+ return QBrush ( QColor ( Qt::gray ) );
154
+ }
155
+ else if ( role == Qt::FontRole )
156
+ {
157
+ QFont font = QFont ();
158
+ if ( index .row () == mExtraIdentifierValueIndex )
159
+ font.setBold ( true );
160
+
161
+ if ( mEntries .value ( index .row () ).identifierValue .isNull () )
162
+ {
163
+ font.setItalic ( true );
164
+ }
165
+ return font;
166
+ }
143
167
}
144
- break ;
145
-
146
- case Qt::FontRole:
147
- QFont font = QFont ();
148
- if ( index .row () == mExtraIdentifierValueIndex )
149
- font.setBold ( true );
150
-
151
- if ( mEntries .value ( index .row () ).identifierValue .isNull () )
168
+ else
152
169
{
153
- font.setItalic ( true );
170
+ // Respect conditional style
171
+ const QgsConditionalStyle style = featureStyle ( mEntries .value ( index .row () ).feature );
172
+
173
+ if ( style.isValid () )
174
+ {
175
+ if ( role == Qt::BackgroundColorRole && style.validBackgroundColor () )
176
+ return style.backgroundColor ();
177
+ if ( role == Qt::TextColorRole && style.validTextColor () )
178
+ return style.textColor ();
179
+ if ( role == Qt::DecorationRole )
180
+ return style.icon ();
181
+ if ( role == Qt::FontRole )
182
+ return style.font ();
183
+ }
154
184
}
155
- return font;
156
185
break ;
186
+ }
157
187
}
158
188
159
189
return QVariant ();
@@ -193,7 +223,7 @@ void QgsFeatureFilterModel::updateCompleter()
193
223
std::sort ( entries.begin (), entries.end (), []( const Entry & a, const Entry & b ) { return a.value .localeAwareCompare ( b.value ) < 0 ; } );
194
224
195
225
if ( mAllowNull )
196
- entries.prepend ( Entry ( QVariant ( QVariant::Int ), tr ( " NULL" ) ) );
226
+ entries.prepend ( Entry ( QVariant ( QVariant::Int ), tr ( " NULL" ), QgsFeature () ) );
197
227
198
228
const int newEntriesSize = entries.size ();
199
229
@@ -340,6 +370,31 @@ void QgsFeatureFilterModel::scheduledReload()
340
370
emit isLoadingChanged ();
341
371
}
342
372
373
+ QSet<QString> QgsFeatureFilterModel::requestedAttributes () const
374
+ {
375
+ QSet<QString> requestedAttrs;
376
+
377
+ const auto rowStyles = mSourceLayer ->conditionalStyles ()->rowStyles ();
378
+
379
+ for ( const QgsConditionalStyle &style : rowStyles )
380
+ {
381
+ QgsExpression exp ( style.rule () );
382
+ requestedAttrs += exp .referencedColumns ();
383
+ }
384
+
385
+ if ( mDisplayExpression .isField () )
386
+ {
387
+ QString fieldName = *mDisplayExpression .referencedColumns ().constBegin ();
388
+ Q_FOREACH ( const QgsConditionalStyle &style, mSourceLayer ->conditionalStyles ()->fieldStyles ( fieldName ) )
389
+ {
390
+ QgsExpression exp ( style.rule () );
391
+ requestedAttrs += exp .referencedColumns ();
392
+ }
393
+ }
394
+
395
+ return requestedAttrs;
396
+ }
397
+
343
398
void QgsFeatureFilterModel::setExtraIdentifierValueIndex ( int index )
344
399
{
345
400
if ( mExtraIdentifierValueIndex == index )
@@ -376,16 +431,49 @@ void QgsFeatureFilterModel::setExtraIdentifierValueUnguarded( const QVariant &ex
376
431
{
377
432
beginInsertRows ( QModelIndex (), 0 , 0 );
378
433
if ( extraIdentifierValue.isNull () )
379
- mEntries .prepend ( Entry ( QVariant ( QVariant::Int ), QStringLiteral ( " %1" ).arg ( tr ( " NULL" ) ) ) );
434
+ mEntries .prepend ( Entry ( QVariant ( QVariant::Int ), QStringLiteral ( " %1" ).arg ( tr ( " NULL" ) ), QgsFeature () ) );
380
435
else
381
- mEntries .prepend ( Entry ( extraIdentifierValue, QStringLiteral ( " (%1)" ).arg ( extraIdentifierValue.toString () ) ) );
436
+ mEntries .prepend ( Entry ( extraIdentifierValue, QStringLiteral ( " (%1)" ).arg ( extraIdentifierValue.toString () ), QgsFeature () ) );
382
437
endInsertRows ();
383
438
setExtraIdentifierValueIndex ( 0 );
384
439
385
440
reloadCurrentFeature ();
386
441
}
387
442
}
388
443
444
+ QgsConditionalStyle QgsFeatureFilterModel::featureStyle ( const QgsFeature &feature ) const
445
+ {
446
+ if ( !mSourceLayer )
447
+ return QgsConditionalStyle ();
448
+
449
+ QgsVectorLayer *layer = mSourceLayer ;
450
+ QgsFeatureId fid = feature.id ();
451
+ mExpressionContext .setFeature ( feature );
452
+ QgsConditionalStyle style;
453
+
454
+ if ( mEntryStylesMap .contains ( fid ) )
455
+ {
456
+ style = mEntryStylesMap .value ( fid );
457
+ }
458
+
459
+ auto styles = QgsConditionalStyle::matchingConditionalStyles ( layer->conditionalStyles ()->rowStyles (), QVariant (), mExpressionContext );
460
+
461
+ if ( mDisplayExpression .isField () )
462
+ {
463
+ // Style specific for this field
464
+ QString fieldName = *mDisplayExpression .referencedColumns ().constBegin ();
465
+ const auto allStyles = layer->conditionalStyles ()->fieldStyles ( fieldName );
466
+ const auto matchingFieldStyles = QgsConditionalStyle::matchingConditionalStyles ( allStyles, feature.attribute ( fieldName ), mExpressionContext );
467
+
468
+ styles += matchingFieldStyles;
469
+
470
+ style = QgsConditionalStyle::compressStyles ( styles );
471
+ mEntryStylesMap .insert ( fid, style );
472
+ }
473
+
474
+ return style;
475
+ }
476
+
389
477
bool QgsFeatureFilterModel::allowNull () const
390
478
{
391
479
return mAllowNull ;
0 commit comments