Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize feature model sorting #3638

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ set(MM_SRCS
compass.cpp
featurelayerpair.cpp
featuresmodel.cpp
featuresproxymodel.cpp
fieldsmodel.cpp
guidelinecontroller.cpp
identifykit.cpp
Expand Down Expand Up @@ -136,7 +135,6 @@ set(MM_HDRS
enumhelper.h
featurelayerpair.h
featuresmodel.h
featuresproxymodel.h
fieldsmodel.h
guidelinecontroller.h
identifykit.h
Expand Down
42 changes: 14 additions & 28 deletions app/featuresmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ QVariant FeaturesModel::data( const QModelIndex &index, int role ) const
case LayerName: return pair.layer() ? pair.layer()->name() : QString();
case LayerIcon: return pair.layer() ? InputUtils::loadIconFromLayer( pair.layer() ) : QString();
case Qt::DisplayRole: return featureTitle( pair );
case SortValue: return sortValue( pair );
}

return QVariant();
Expand Down Expand Up @@ -178,15 +177,6 @@ QVariant FeaturesModel::featureTitle( const FeatureLayerPair &featurePair ) cons
return title;
}

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 );
return result;
}

QString FeaturesModel::searchResultPair( const FeatureLayerPair &pair ) const
{
if ( mSearchExpression.isEmpty() )
Expand Down Expand Up @@ -261,6 +251,20 @@ void FeaturesModel::setupFeatureRequest( QgsFeatureRequest &request )
request.setFilterExpression( buildSearchExpression() );
}

if ( mUseAttributeTableSortOrder && mLayer && !mLayer->attributeTableConfig().sortExpression().isEmpty() )
{
// get a context with global, project and layer scopes
// QGIS docs are not very clear, but this context is also used for evaluation of the request's 'order by' expressions too
QgsExpressionContext context = mLayer->createExpressionContext();
request.setExpressionContext( context );
request.setOrderBy( QgsFeatureRequest::OrderBy(
{
QgsFeatureRequest::OrderByClause(
mLayer->attributeTableConfig().sortExpression(),
mLayer->attributeTableConfig().sortOrder() == Qt::AscendingOrder )
} ) );
}

request.setLimit( FEATURES_LIMIT );
}

Expand Down Expand Up @@ -290,7 +294,6 @@ QHash<int, QByteArray> FeaturesModel::roleNames() const
roleNames[SearchResult] = QStringLiteral( "SearchResult" ).toLatin1();
roleNames[LayerName] = QStringLiteral( "LayerName" ).toLatin1();
roleNames[LayerIcon] = QStringLiteral( "LayerIcon" ).toLatin1();
roleNames[SortValue] = QStringLiteral( "SortValue" ).toLatin1();
return roleNames;
}

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

mLayer = newLayer;
setupSorting();
emit layerChanged( mLayer );

if ( mLayer )
Expand All @@ -384,19 +386,3 @@ QgsVectorLayer *FeaturesModel::layer() const
{
return mLayer;
}

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

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

Qt::SortOrder FeaturesModel::sortOrder() const
{
return mSortOrder;
}
22 changes: 6 additions & 16 deletions app/featuresmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class FeaturesModel : public QAbstractListModel
// Name of the property is intentionally `count` so that it matches ListModel's count property
Q_PROPERTY( int count READ count NOTIFY countChanged )

// Returns if the model should be sorted according to the layer's attribute table configuration sort order
Q_PROPERTY( bool useAttributeTableSortOrder MEMBER mUseAttributeTableSortOrder )

public:

enum ModelRoles
Expand All @@ -64,7 +67,6 @@ class FeaturesModel : public QAbstractListModel
SearchResult, // pair of attribute and its value by which the feature was found, empty if search expression is empty
LayerName,
LayerIcon,
SortValue,
};
Q_ENUM( ModelRoles );

Expand Down Expand Up @@ -118,15 +120,6 @@ class FeaturesModel : public QAbstractListModel

int layerFeaturesCount() const;

//! Populates the sort expression and sort order for the model
virtual void setupSorting();

//! Returns true if there is a sort expression set for the model
bool sortingEnabled() const;

//! Returns the order in witch the model should be sorted
Qt::SortOrder sortOrder() const;

signals:

void featuresLimitChanged( int featuresLimit );
Expand All @@ -149,9 +142,6 @@ class FeaturesModel : public QAbstractListModel

virtual QVariant featureTitle( const FeatureLayerPair &featurePair ) const;

QString mSortExpression;
Qt::SortOrder mSortOrder = Qt::AscendingOrder;

private slots:
void onFutureFinished();

Expand All @@ -164,9 +154,6 @@ class FeaturesModel : public QAbstractListModel
//! Returns found attribute and its value from search expression for feature
QString searchResultPair( const FeatureLayerPair &feat ) const;

//! Evaluates the sort expression and returns the value used for this feature when sorting the model
QVariant sortValue( const FeatureLayerPair &featurePair ) const;

const int FEATURES_LIMIT = 10000; //!< Number of maximum features loaded from layer

FeatureLayerPairs mFeatures;
Expand All @@ -176,6 +163,9 @@ class FeaturesModel : public QAbstractListModel
QAtomicInt mNextSearchId = 0;
QFutureWatcher<QgsFeatureList> mSearchResultWatcher;
bool mFetchingResults = false;
bool mUseAttributeTableSortOrder = false;

friend class TestModels;
};

#endif // FEATURESMODEL_H
48 changes: 0 additions & 48 deletions app/featuresproxymodel.cpp

This file was deleted.

47 changes: 0 additions & 47 deletions app/featuresproxymodel.h

This file was deleted.

2 changes: 0 additions & 2 deletions app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
#include "position/positionkit.h"
#include "scalebarkit.h"
#include "featuresmodel.h"
#include "featuresproxymodel.h"
#include "relationfeaturesmodel.h"
#include "relationreferencefeaturesmodel.h"
#include "fieldvalidator.h"
Expand Down Expand Up @@ -327,7 +326,6 @@ void initDeclarative()
qmlRegisterType< MapThemesModel >( "mm", 1, 0, "MapThemesModel" );
qmlRegisterType< GuidelineController >( "mm", 1, 0, "GuidelineController" );
qmlRegisterType< FeaturesModel >( "mm", 1, 0, "FeaturesModel" );
qmlRegisterType< FeaturesProxyModel >( "mm", 1, 0, "FeaturesProxyModel" );
qmlRegisterType< RelationFeaturesModel >( "mm", 1, 0, "RelationFeaturesModel" );
qmlRegisterType< ValueRelationFeaturesModel >( "mm", 1, 0, "ValueRelationFeaturesModel" );
qmlRegisterType< RelationReferenceFeaturesModel >( "mm", 1, 0, "RelationReferenceFeaturesModel" );
Expand Down
12 changes: 4 additions & 8 deletions app/qml/form/editors/MMFormValueRelationEditor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,11 @@ MMFormComboboxBaseEditor {
valueRole: "FeatureId"
textRole: "FeatureTitle"

list.model: MM.FeaturesProxyModel {
id: vrDropdownProxyModel
list.model: MM.ValueRelationFeaturesModel {
id: vrDropdownModel

featuresSourceModel: MM.ValueRelationFeaturesModel {
id: vrDropdownModel

config: root._fieldConfig
pair: root._fieldFeatureLayerPair
}
config: root._fieldConfig
pair: root._fieldFeatureLayerPair
}

onSearchTextChanged: ( searchText ) => vrDropdownModel.searchExpression = searchText
Expand Down
11 changes: 4 additions & 7 deletions app/qml/layers/MMFeaturesListPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,11 @@ MMComponents.MMPage {
topMargin: __style.spacing20
}

model: MM.FeaturesProxyModel {
id: featuresProxyModel
model: MM.FeaturesModel {
id: featuresModel

featuresSourceModel: MM.FeaturesModel {
id: featuresModel

layer: root.selectedLayer
}
useAttributeTableSortOrder: true
layer: root.selectedLayer
}

clip: true
Expand Down
Loading
Loading