Skip to content

Commit 64b7def

Browse files
committedAug 17, 2016
Make QgsVectorLayer uniqueValues/min/maxValue consider edits
Previously these methods would inconsistently handle the edit buffer, eg uniqueValues would consider changed attributes but not added features. Now uniqueValues, minimumValue and maximumValue all consider both added features and changed attribute values when performing their calculation. The most noticable effect of this fix is that the unique values widget now correctly shows values for features which have been added but not yet committed to the provider. (cherry-picked from 50c3592)
1 parent e37ffd0 commit 64b7def

File tree

3 files changed

+119
-10
lines changed

3 files changed

+119
-10
lines changed
 

‎python/core/qgsvectorlayer.sip

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,17 +1317,35 @@ class QgsVectorLayer : QgsMapLayer
13171317
/** Caches joined attributes if required (and not already done) */
13181318
void createJoinCaches();
13191319

1320-
/** Returns unique values for column
1320+
/** Calculates a list of unique values contained within an attribute in the layer. Note that
1321+
* in some circumstances when unsaved changes are present for the layer then the returned list
1322+
* may contain outdated values (for instance when the attribute value in a saved feature has
1323+
* been changed inside the edit buffer then the previous saved value will be included in the
1324+
* returned list).
13211325
* @param index column index for attribute
13221326
* @param uniqueValues out: result list
1323-
* @param limit maximum number of values to return (-1 if unlimited)
1327+
* @param limit maximum number of values to return (or -1 if unlimited)
1328+
* @see minimumValue()
1329+
* @see maximumValue()
13241330
*/
13251331
void uniqueValues( int index, QList<QVariant> &uniqueValues /Out/, int limit = -1 );
13261332

1327-
/** Returns minimum value for an attribute column or invalid variant in case of error */
1333+
/** Returns the minimum value for an attribute column or an invalid variant in case of error.
1334+
* Note that in some circumstances when unsaved changes are present for the layer then the
1335+
* returned value may be outdated (for instance when the attribute value in a saved feature has
1336+
* been changed inside the edit buffer then the previous saved value may be returned as the minimum).
1337+
* @see maximumValue()
1338+
* @see uniqueValues()
1339+
*/
13281340
QVariant minimumValue( int index );
13291341

1330-
/** Returns maximum value for an attribute column or invalid variant in case of error */
1342+
/** Returns the maximum value for an attribute column or an invalid variant in case of error.
1343+
* Note that in some circumstances when unsaved changes are present for the layer then the
1344+
* returned value may be outdated (for instance when the attribute value in a saved feature has
1345+
* been changed inside the edit buffer then the previous saved value may be returned as the maximum).
1346+
* @see minimumValue()
1347+
* @see uniqueValues()
1348+
*/
13311349
QVariant maximumValue( int index );
13321350

13331351
/** Calculates an aggregated value from the layer's features.

‎src/core/qgsvectorlayer.cpp

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,6 +3156,23 @@ void QgsVectorLayer::uniqueValues( int index, QList<QVariant> &uniqueValues, int
31563156
vals << v.toString();
31573157
}
31583158

3159+
QgsFeatureMap added = mEditBuffer->addedFeatures();
3160+
QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
3161+
while ( addedIt.hasNext() && ( limit < 0 || uniqueValues.count() < limit ) )
3162+
{
3163+
addedIt.next();
3164+
QVariant v = addedIt.value().attribute( index );
3165+
if ( v.isValid() )
3166+
{
3167+
QString vs = v.toString();
3168+
if ( !vals.contains( vs ) )
3169+
{
3170+
vals << vs;
3171+
uniqueValues << v;
3172+
}
3173+
}
3174+
}
3175+
31593176
QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
31603177
while ( it.hasNext() && ( limit < 0 || uniqueValues.count() < limit ) )
31613178
{
@@ -3234,7 +3251,35 @@ QVariant QgsVectorLayer::minimumValue( int index )
32343251
return QVariant();
32353252

32363253
case QgsFields::OriginProvider: //a provider field
3237-
return mDataProvider->minimumValue( index );
3254+
{
3255+
QVariant min = mDataProvider->minimumValue( index );
3256+
if ( mEditBuffer )
3257+
{
3258+
QgsFeatureMap added = mEditBuffer->addedFeatures();
3259+
QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
3260+
while ( addedIt.hasNext() )
3261+
{
3262+
addedIt.next();
3263+
QVariant v = addedIt.value().attribute( index );
3264+
if ( v.isValid() && qgsVariantLessThan( v, min ) )
3265+
{
3266+
min = v;
3267+
}
3268+
}
3269+
3270+
QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
3271+
while ( it.hasNext() )
3272+
{
3273+
it.next();
3274+
QVariant v = it.value().value( index );
3275+
if ( v.isValid() && qgsVariantLessThan( v, min ) )
3276+
{
3277+
min = v;
3278+
}
3279+
}
3280+
}
3281+
return min;
3282+
}
32383283

32393284
case QgsFields::OriginEdit:
32403285
{
@@ -3293,7 +3338,35 @@ QVariant QgsVectorLayer::maximumValue( int index )
32933338
return QVariant();
32943339

32953340
case QgsFields::OriginProvider: //a provider field
3296-
return mDataProvider->maximumValue( index );
3341+
{
3342+
QVariant min = mDataProvider->maximumValue( index );
3343+
if ( mEditBuffer )
3344+
{
3345+
QgsFeatureMap added = mEditBuffer->addedFeatures();
3346+
QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
3347+
while ( addedIt.hasNext() )
3348+
{
3349+
addedIt.next();
3350+
QVariant v = addedIt.value().attribute( index );
3351+
if ( v.isValid() && qgsVariantGreaterThan( v, min ) )
3352+
{
3353+
min = v;
3354+
}
3355+
}
3356+
3357+
QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
3358+
while ( it.hasNext() )
3359+
{
3360+
it.next();
3361+
QVariant v = it.value().value( index );
3362+
if ( v.isValid() && qgsVariantGreaterThan( v, min ) )
3363+
{
3364+
min = v;
3365+
}
3366+
}
3367+
}
3368+
return min;
3369+
}
32973370

32983371
case QgsFields::OriginEdit:
32993372
// the layer is editable, but in certain cases it can still be avoided going through all features

‎src/core/qgsvectorlayer.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,17 +1707,35 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
17071707
/** Caches joined attributes if required (and not already done) */
17081708
void createJoinCaches();
17091709

1710-
/** Returns unique values for column
1710+
/** Calculates a list of unique values contained within an attribute in the layer. Note that
1711+
* in some circumstances when unsaved changes are present for the layer then the returned list
1712+
* may contain outdated values (for instance when the attribute value in a saved feature has
1713+
* been changed inside the edit buffer then the previous saved value will be included in the
1714+
* returned list).
17111715
* @param index column index for attribute
17121716
* @param uniqueValues out: result list
1713-
* @param limit maximum number of values to return (-1 if unlimited)
1717+
* @param limit maximum number of values to return (or -1 if unlimited)
1718+
* @see minimumValue()
1719+
* @see maximumValue()
17141720
*/
17151721
void uniqueValues( int index, QList<QVariant> &uniqueValues, int limit = -1 );
17161722

1717-
/** Returns minimum value for an attribute column or invalid variant in case of error */
1723+
/** Returns the minimum value for an attribute column or an invalid variant in case of error.
1724+
* Note that in some circumstances when unsaved changes are present for the layer then the
1725+
* returned value may be outdated (for instance when the attribute value in a saved feature has
1726+
* been changed inside the edit buffer then the previous saved value may be returned as the minimum).
1727+
* @see maximumValue()
1728+
* @see uniqueValues()
1729+
*/
17181730
QVariant minimumValue( int index );
17191731

1720-
/** Returns maximum value for an attribute column or invalid variant in case of error */
1732+
/** Returns the maximum value for an attribute column or an invalid variant in case of error.
1733+
* Note that in some circumstances when unsaved changes are present for the layer then the
1734+
* returned value may be outdated (for instance when the attribute value in a saved feature has
1735+
* been changed inside the edit buffer then the previous saved value may be returned as the maximum).
1736+
* @see minimumValue()
1737+
* @see uniqueValues()
1738+
*/
17211739
QVariant maximumValue( int index );
17221740

17231741
/** Calculates an aggregated value from the layer's features.

0 commit comments

Comments
 (0)
Please sign in to comment.