Skip to content

Commit

Permalink
optimize feature model sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
uclaros committed Oct 4, 2024
1 parent acb2374 commit 76bc2fd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
13 changes: 7 additions & 6 deletions app/featuresmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,8 @@ QVariant FeaturesModel::featureTitle( const FeatureLayerPair &featurePair ) cons

QVariant FeaturesModel::sortValue( const FeatureLayerPair &featurePair ) const
{
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( featurePair.layer() ) );
context.setFeature( featurePair.feature() );
QgsExpression expr( mSortExpression );
QVariant result = expr.evaluate( &context );
mExpressionContext.setFeature( featurePair.feature() );
QVariant result = mSortExpression.evaluate( &mExpressionContext );
return result;
}

Expand Down Expand Up @@ -363,6 +361,7 @@ void FeaturesModel::setLayer( QgsVectorLayer *newLayer )
}

mLayer = newLayer;
mExpressionContext = mLayer->createExpressionContext();
setupSorting();
emit layerChanged( mLayer );

Expand All @@ -387,13 +386,15 @@ QgsVectorLayer *FeaturesModel::layer() const

void FeaturesModel::setupSorting()
{
mSortExpression = mLayer ? mLayer->attributeTableConfig().sortExpression() : QString();
mSortExpressionString = mLayer ? mLayer->attributeTableConfig().sortExpression() : QString();
mSortOrder = mLayer ? mLayer->attributeTableConfig().sortOrder() : Qt::AscendingOrder;
mSortExpression = QgsExpression( mSortExpressionString );
mSortExpression.prepare( &mExpressionContext );
}

bool FeaturesModel::sortingEnabled() const
{
return !mSortExpression.isEmpty();
return !mSortExpressionString.isEmpty();
}

Qt::SortOrder FeaturesModel::sortOrder() const
Expand Down
4 changes: 3 additions & 1 deletion app/featuresmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ class FeaturesModel : public QAbstractListModel

virtual QVariant featureTitle( const FeatureLayerPair &featurePair ) const;

QString mSortExpression;
QString mSortExpressionString;
mutable QgsExpression mSortExpression;
mutable QgsExpressionContext mExpressionContext;
Qt::SortOrder mSortOrder = Qt::AscendingOrder;

private slots:
Expand Down
4 changes: 3 additions & 1 deletion app/valuerelationfeaturesmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,7 @@ void ValueRelationFeaturesModel::setConfig( const QVariantMap &newConfig )
void ValueRelationFeaturesModel::setupSorting()
{
const bool orderByValue = mConfig.value( QStringLiteral( "OrderByValue" ) ).toBool();
mSortExpression = orderByValue ? mTitleField : QString();
mSortExpressionString = orderByValue ? mTitleField : QString();
mSortExpression = QgsExpression( mSortExpressionString );
mSortExpression.prepare( &mExpressionContext );
}

1 comment on commit 76bc2fd

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 24.10.667011 just submitted!

Please sign in to comment.