Skip to content

Commit

Permalink
Merge pull request #5192 from opengisch/comboFilter
Browse files Browse the repository at this point in the history
Value map combobox filter
  • Loading branch information
nirvn committed Apr 22, 2024
2 parents 26aad60 + e9d6732 commit 29a72a2
Show file tree
Hide file tree
Showing 10 changed files with 520 additions and 214 deletions.
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ set(QFIELD_CORE_SRCS
tracker.cpp
trackingmodel.cpp
valuemapmodel.cpp
valuemapmodelbase.cpp
vertexmodel.cpp
viewstatus.cpp)

Expand Down Expand Up @@ -215,6 +216,7 @@ set(QFIELD_CORE_HDRS
tracker.h
trackingmodel.h
valuemapmodel.h
valuemapmodelbase.h
vertexmodel.h
viewstatus.h
${CMAKE_CURRENT_BINARY_DIR}/qfield.h)
Expand Down
2 changes: 1 addition & 1 deletion src/core/attributeformmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class AttributeFormModel : public QSortFilterProxyModel
*/
Q_INVOKABLE QVariant attribute( const QString &name );

//! Forces the form to update the fields visibility and constraints
//! Applies feature model data such as attribute values, constraints, visibility to the attribute form model
Q_INVOKABLE void applyFeatureModel();

//! Applies default values linked to a parent feature
Expand Down
13 changes: 6 additions & 7 deletions src/core/attributeformmodelbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ class AttributeFormModelBase : public QStandardItemModel
{
Q_OBJECT

Q_PROPERTY( FeatureModel *featureModel READ featureModel WRITE setFeatureModel NOTIFY featureModelChanged )
Q_PROPERTY( bool hasTabs READ hasTabs WRITE setHasTabs NOTIFY hasTabsChanged )
Q_PROPERTY( bool constraintsHardValid READ constraintsHardValid NOTIFY constraintsHardValidChanged )
Q_PROPERTY( bool constraintsSoftValid READ constraintsSoftValid NOTIFY constraintsSoftValidChanged )

public:
explicit AttributeFormModelBase( QObject *parent = nullptr );

Expand All @@ -46,22 +41,26 @@ class AttributeFormModelBase : public QStandardItemModel
bool hasTabs() const;
void setHasTabs( bool hasTabs );

//! \copydoc AttributeFormModel::save
bool save();

//! \copydoc AttributeFormModel::create
bool create();

//! \copydoc AttributeFormModel::deleteFeature
bool deleteFeature();

bool constraintsHardValid() const;

bool constraintsSoftValid() const;

//! \copydoc AttributeFormModel::attribute
QVariant attribute( const QString &name );

//! Applies feature model data such as attribute values, constraints, visibility to the attribute form model
//! \copydoc AttributeFormModel::applyFeatureModel
void applyFeatureModel();

//! Applies default values linked to a parent feature
//! \copydoc AttributeFormModel::applyParentDefaultValues
void applyParentDefaultValues();

signals:
Expand Down
71 changes: 20 additions & 51 deletions src/core/multifeaturelistmodelbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,16 @@ class MultiFeatureListModelBase : public QAbstractItemModel
public:
explicit MultiFeatureListModelBase( QObject *parent = nullptr );

/**
* Resets the model to contain features found from a list of \a requests.
*/
//! \copydoc MultiFeatureListModel::setFeatures
void setFeatures( const QMap<QgsVectorLayer *, QgsFeatureRequest> requests );

/**
* Appends features from a list of \a results.
*/
//! \copydoc MultiFeatureListModel::appendFeatures
void appendFeatures( const QList<IdentifyTool::IdentifyResult> &results );

/**
* Resets the model to either an empty feature list or one that contains only the selected features.
* \param keepSelected if set to TRUE, selected features will be kept when resetting the model.
*/
//! \copydoc MultiFeatureListModel::clear
void clear( const bool keepSelected = false );

/**
* Empties the list of selected features.
*/
//! \copydoc MultiFeatureListModel::clearSelection
void clearSelection();

QHash<int, QByteArray> roleNames() const override;
Expand All @@ -68,80 +59,58 @@ class MultiFeatureListModelBase : public QAbstractItemModel
*/
virtual bool removeRows( int row, int count, const QModelIndex &parent ) override;

/**
* Returns the number of features in the model.
*/
//! \copydoc MultiFeatureListModel::count
int count() const;

/**
* Returns the number of selected features in the model.
*/
//! \copydoc MultiFeatureListModel::selectedCount
int selectedCount() const;

//! Returns TRUE if the selected features can have their attributes value changed
//! \copydoc MultiFeatureListModel::canEditAttributesSelection
bool canEditAttributesSelection();

//! Returns TRUE if the selected features can be merged
//! \copydoc MultiFeatureListModel::canMergeSelection
bool canMergeSelection();

//! Returns TRUE if the selected features can be deleted
//! \copydoc MultiFeatureListModel::canDeleteSelection
bool canDeleteSelection();

//! Returns TRUE if the selected features can be duplicated onto their associated layer
//! \copydoc MultiFeatureListModel::canDuplicateSelection
bool canDuplicateSelection();

//! Returns TRUE if the selected features' geometry can be moved
//! \copydoc MultiFeatureListModel::canMoveSelection
bool canMoveSelection();

/**
* Merges selected features by updating the first seleted feature's geometry
* to a combination (i.e. union) of geometries of all selected features.
*
* All but the first feature will then be removed from the vector layer containing
* the selected features.
*/
//! \copydoc MultiFeatureListModel::mergeSelection
bool mergeSelection();

/**
* Deletes a feature from a vector layer
*
* \param layer The layer from which a feature will be removed
* \param fid The id of the feature to remove
* \param selectionAction
*/
bool deleteFeature( QgsVectorLayer *layer, QgsFeatureId fid, bool selectionAction = false );

//! Deletes selected features
//! \copydoc MultiFeatureListModel::deleteSelection
bool deleteSelection();

/**
* Duplicates a feature on a given layer
*
* \param layer The layer within which the feature will be duplicated
* \param feature The feature to be duplicated
*/
//! \copydoc MultiFeatureListModel::duplicateFeature
bool duplicateFeature( QgsVectorLayer *layer, const QgsFeature &feature );

//! Duplicates selected features onto their associated layer
//! \copydoc MultiFeatureListModel::duplicateSelection
bool duplicateSelection();

//! Moves selected features along a given \a vector.
//! \copydoc MultiFeatureListModel::moveSelection
bool moveSelection( const double x, const double y );

/**
* Toggles the selection state of a given item.
* \param item the item's row number
*/
//! \copydoc MultiFeatureListModel::toggleSelectedItem
void toggleSelectedItem( int item );

/**
* Returns the list of currently selected features.
* \note the current implementation only allow for selected features from a single layer
*/
//! \copydoc MultiFeatureListModel::selectedFeatures
QList<QgsFeature> selectedFeatures() const;

/**
* Returns the layer containing the list of currently selected features.
*/
//! \copydoc MultiFeatureListModel::selectedLayer
QgsVectorLayer *selectedLayer() const;

signals:
Expand Down
106 changes: 18 additions & 88 deletions src/core/valuemapmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,114 +16,44 @@
* *
***************************************************************************/

#include "qgsvaluemapfieldformatter.h"
#include "valuemapmodel.h"

#include <QDebug>
#include "valuemapmodelbase.h"

ValueMapModel::ValueMapModel( QObject *parent )
: QAbstractListModel( parent )
{
}

QVariant ValueMapModel::map() const
{
return mConfiguredMap;
}

void ValueMapModel::setMap( const QVariant &map )
: QSortFilterProxyModel( parent )
, mSourceModel( new ValueMapModelBase( this ) )
{
mMap.clear();
// QGIS 3
const QVariantList list = map.toList();
if ( !list.empty() )
{
beginInsertRows( QModelIndex(), 0, list.size() );

for ( const QVariant &item : list )
{
const QVariantMap mapItem = item.toMap();

const QString key = mapItem.firstKey();
const QVariant value = mapItem.value( key );

mMap.append( qMakePair( value, key ) );
}
endInsertRows();
}
else // QGIS 2 compat
{
const QVariantMap valueMap = map.toMap();
if ( !valueMap.empty() )
{
beginInsertRows( QModelIndex(), 0, valueMap.size() );
setSourceModel( mSourceModel );

QMapIterator<QString, QVariant> i( valueMap );
while ( i.hasNext() )
{
i.next();
const QString key = i.key();
const QVariant value = i.value();
setFilterRole( ValueRole );

mMap.append( qMakePair( value, key ) );
}
endInsertRows();
}
}

mConfiguredMap = map;
emit mapChanged();
}

int ValueMapModel::rowCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent )
return mMap.size();
connect( mSourceModel, &ValueMapModelBase::mapChanged, this, &ValueMapModel::mapChanged );
}

QVariant ValueMapModel::data( const QModelIndex &index, int role ) const
QVariant ValueMapModel::map() const
{
if ( role == KeyRole )
return mMap.at( index.row() ).first;
else
return mMap.at( index.row() ).second;
return mSourceModel->map();
}

QHash<int, QByteArray> ValueMapModel::roleNames() const
void ValueMapModel::setMap( const QVariant &map )
{
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();

roles[KeyRole] = "key";
roles[ValueRole] = "value";

return roles;
mSourceModel->setMap( map );
}

int ValueMapModel::keyToIndex( const QVariant &key ) const
{
int i = 0;
for ( const auto &item : mMap )
{
if ( item.first.toString() == key.toString() )
{
return i;
}
++i;
}
return -1;
return mSourceModel->keyToIndex( key );
}

QVariant ValueMapModel::keyForValue( const QString &value ) const
{
QVariant result;

auto match = std::find_if( mMap.begin(), mMap.end(), [&value]( const QPair<QVariant, QString> &x ) { return x.second == value; } );

if ( match != mMap.end() )
result = match->first;
return mSourceModel->keyForValue( value );
}

if ( result == QgsValueMapFieldFormatter::NULL_VALUE )
result = QVariant();
bool ValueMapModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
{
QModelIndex index = sourceModel()->index( sourceRow, 0, sourceParent );

return result;
QVariant data = sourceModel()->data( index, ValueRole );
return data.toString().contains( filterRegularExpression() );
}

1 comment on commit 29a72a2

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please sign in to comment.