Skip to content
Permalink
Browse files
use a struct for the locator result user data (#44977)
  • Loading branch information
3nids committed Sep 7, 2021
1 parent f7cc194 commit 1532c05c33fd2583b78e519f5e01115ba961bbfa
@@ -28,7 +28,6 @@

#include <QSpinBox>


QgsAllLayersFeaturesLocatorFilter::QgsAllLayersFeaturesLocatorFilter( QObject *parent )
: QgsLocatorFilter( parent )
{
@@ -121,7 +120,7 @@ void QgsAllLayersFeaturesLocatorFilter::fetchResults( const QString &string, con

result.displayString = preparedLayer->expression.evaluate( &( preparedLayer->context ) ).toString();

result.userData = QVariantList() << f.id() << preparedLayer->layerId << preparedLayer->layerIsSpatial;
result.userData = ResultData( f.id(), preparedLayer->layerId, preparedLayer->layerIsSpatial ).toVariant();
foundFeatureIds << f.id();
result.icon = preparedLayer->layerIcon;
result.score = static_cast< double >( string.length() ) / result.displayString.size();
@@ -155,7 +154,7 @@ void QgsAllLayersFeaturesLocatorFilter::fetchResults( const QString &string, con

result.displayString = preparedLayer->expression.evaluate( &( preparedLayer->context ) ).toString();

result.userData = QVariantList() << f.id() << preparedLayer->layerId << preparedLayer->layerIsSpatial;
result.userData = ResultData( f.id(), preparedLayer->layerId, preparedLayer->layerIsSpatial ).toVariant();
result.icon = preparedLayer->layerIcon;
result.score = static_cast< double >( string.length() ) / result.displayString.size();

@@ -180,10 +179,10 @@ void QgsAllLayersFeaturesLocatorFilter::triggerResult( const QgsLocatorResult &r

void QgsAllLayersFeaturesLocatorFilter::triggerResultFromAction( const QgsLocatorResult &result, const int actionId )
{
QVariantList dataList = result.userData.toList();
QgsFeatureId fid = dataList.at( 0 ).toLongLong();
QString layerId = dataList.at( 1 ).toString();
bool layerIsSpatial = dataList.at( 2 ).toBool();
ResultData data = ResultData::fromVariant( result.userData );
QgsFeatureId fid = data.id();
QString layerId = data.layerId();
bool layerIsSpatial = data.layerIsSpatial();
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
if ( !layer )
return;
@@ -62,12 +62,33 @@ class APP_EXPORT QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
QString layerName;
QString layerId;
QIcon layerIcon;
bool layerIsSpatial;
bool layerIsSpatial = true;
};

class ResultData
{
public:
ResultData &operator=( const ResultData & ) = default;
ResultData( QgsFeatureId id, const QString &layerId, bool layerIsSpatial )
: mId( id ), mLayerId( layerId ), mLayerIsSpatial( layerIsSpatial ) {}

QgsFeatureId id() const {return mId;}
QString layerId() const {return mLayerId;}
bool layerIsSpatial() const {return mLayerIsSpatial;}

QVariant toVariant() const {return QVariantList() << mId << mLayerId << mLayerIsSpatial;}
static ResultData fromVariant( QVariant const &value ) {QList dataList = value.toList(); return ResultData( dataList.at( 0 ).toLongLong(), dataList.at( 1 ).toString(), dataList.at( 2 ).toBool() );}

private:
QgsFeatureId mId;
QString mLayerId;
bool mLayerIsSpatial = true;
};

int mMaxResultsPerLayer = 8;
int mMaxTotalResults = 15;
QList<std::shared_ptr<PreparedLayer>> mPreparedLayers;
};


#endif // QGSALLLAYERSFEATURESLOCATORFILTERS_H

0 comments on commit 1532c05

Please sign in to comment.