Skip to content

Commit

Permalink
Revert #3543 and use QgsFeatureRequest::setOrderBy instead
Browse files Browse the repository at this point in the history
  • Loading branch information
uclaros committed Oct 7, 2024
1 parent 76bc2fd commit bc03521
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 249 deletions.
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
43 changes: 14 additions & 29 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,13 +177,6 @@ QVariant FeaturesModel::featureTitle( const FeatureLayerPair &featurePair ) cons
return title;
}

QVariant FeaturesModel::sortValue( const FeatureLayerPair &featurePair ) const
{
mExpressionContext.setFeature( featurePair.feature() );
QVariant result = mSortExpression.evaluate( &mExpressionContext );
return result;
}

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

if ( 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 @@ -288,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 @@ -361,8 +366,6 @@ void FeaturesModel::setLayer( QgsVectorLayer *newLayer )
}

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

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

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

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

Qt::SortOrder FeaturesModel::sortOrder() const
{
return mSortOrder;
}
18 changes: 0 additions & 18 deletions app/featuresmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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 +117,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,11 +139,6 @@ class FeaturesModel : public QAbstractListModel

virtual QVariant featureTitle( const FeatureLayerPair &featurePair ) const;

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

private slots:
void onFutureFinished();

Expand All @@ -166,9 +151,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 Down
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
10 changes: 3 additions & 7 deletions app/qml/layers/MMFeaturesListPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,10 @@ MMComponents.MMPage {
topMargin: __style.spacing20
}

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

featuresSourceModel: MM.FeaturesModel {
id: featuresModel

layer: root.selectedLayer
}
layer: root.selectedLayer
}

clip: true
Expand Down
Loading

0 comments on commit bc03521

Please sign in to comment.