-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a FeaturesProxyModel to conditionally sort features list an…
…d value relations (#3543) * Introduce a FeaturesProxyModel to conditionally sort features list and value relations MMFeaturesListPage is sorted according to the layer's QgsAttributeTableConfig MMFormValueRelationEditor is sorted by value when the widget has the OrderByValue config set * Address review comments * Invalidate proxy model when disabling sorting Remove unused private variables Don't try to disconnect the first time model is set * Add tests * do initialization bits once * call setupSorting() on the model only once
- Loading branch information
Showing
13 changed files
with
355 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "featuresproxymodel.h" | ||
|
||
FeaturesProxyModel::FeaturesProxyModel( QObject *parent ) : QSortFilterProxyModel( parent ) | ||
{ | ||
setSortRole( FeaturesModel::SortValue ); | ||
setSortCaseSensitivity( Qt::CaseInsensitive ); | ||
} | ||
|
||
void FeaturesProxyModel::updateSorting() | ||
{ | ||
// don't sort if there is no sort expression for the layer | ||
if ( mModel->sortingEnabled() ) | ||
{ | ||
sort( 0, mModel->sortOrder() ); | ||
} | ||
else | ||
{ | ||
invalidate(); | ||
} | ||
} | ||
|
||
FeaturesModel *FeaturesProxyModel::featuresSourceModel() const | ||
{ | ||
return mModel; | ||
} | ||
|
||
void FeaturesProxyModel::setFeaturesSourceModel( FeaturesModel *sourceModel ) | ||
{ | ||
if ( mModel == sourceModel ) | ||
return; | ||
|
||
if ( mModel ) | ||
disconnect( mModel, nullptr, this, nullptr ); | ||
|
||
mModel = sourceModel; | ||
setSourceModel( mModel ); | ||
mModel->setupSorting(); | ||
connect( mModel, &FeaturesModel::fetchingResultsChanged, this, [ = ]( bool pending ) { if ( !pending ) updateSorting(); } ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef FEATURESPROXYMODEL_H | ||
#define FEATURESPROXYMODEL_H | ||
|
||
#include <QObject> | ||
#include <QSortFilterProxyModel> | ||
|
||
#include "featuresmodel.h" | ||
|
||
/** | ||
* \brief The FeaturesProxyModel class used as a proxy sort model for the \see FeaturesModel class. | ||
* | ||
* FeaturesProxyModel is a QML type with required property of FeatureSourceModel. Without source model, this model does nothing (is not initialized). | ||
* After setting source model, this model starts sorting if sortingEnabled() returns true for the source model. | ||
*/ | ||
class FeaturesProxyModel : public QSortFilterProxyModel | ||
{ | ||
Q_OBJECT | ||
|
||
Q_PROPERTY( FeaturesModel *featuresSourceModel READ featuresSourceModel WRITE setFeaturesSourceModel ) | ||
|
||
public: | ||
explicit FeaturesProxyModel( QObject *parent = nullptr ); | ||
~FeaturesProxyModel() override {}; | ||
|
||
FeaturesModel *featuresSourceModel() const; | ||
|
||
public slots: | ||
void setFeaturesSourceModel( FeaturesModel *sourceModel ); | ||
|
||
private: | ||
void updateSorting(); | ||
|
||
FeaturesModel *mModel = nullptr; // not owned by this, needs to be set in order to proxy model to work | ||
|
||
friend class TestModels; | ||
}; | ||
|
||
#endif // FEATURESPROXYMODEL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.