Skip to content

Commit

Permalink
[locator] add open form action, automatically open form for non geome…
Browse files Browse the repository at this point in the history
…tric layers

manual backport of #43462
  • Loading branch information
3nids authored and nyalldawson committed Jun 21, 2021
1 parent afffcc3 commit 6cbfce2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
56 changes: 48 additions & 8 deletions src/app/locator/qgsinbuiltlocatorfilters.cpp
Expand Up @@ -275,6 +275,8 @@ QStringList QgsActiveLayerFeaturesLocatorFilter::prepare( const QString &string,
if ( !layer )
return QStringList();

mLayerIsSpatial = layer->isSpatial();

mDispExpression = QgsExpression( layer->displayExpression() );
mContext.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );
mDispExpression.prepare( &mContext );
Expand Down Expand Up @@ -405,10 +407,14 @@ void QgsActiveLayerFeaturesLocatorFilter::fetchResults( const QString &string, c
{
{QStringLiteral( "type" ), QVariant::fromValue( ResultType::Feature )},
{QStringLiteral( "feature_id" ), f.id()},
{QStringLiteral( "layer_id" ), mLayerId}
{QStringLiteral( "layer_id" ), mLayerId},
{QStringLiteral( "layer_is_spatial" ), mLayerIsSpatial}
} );
result.icon = mLayerIcon;
result.score = static_cast< double >( searchString.length() ) / result.displayString.size();
if ( mLayerIsSpatial )
result.actions << QgsLocatorResult::ResultAction( OpenForm, tr( "Open form…" ) );

emit resultFetched( result );

featuresFound << f.id();
Expand Down Expand Up @@ -456,10 +462,14 @@ void QgsActiveLayerFeaturesLocatorFilter::fetchResults( const QString &string, c
{
{QStringLiteral( "type" ), QVariant::fromValue( ResultType::Feature )},
{QStringLiteral( "feature_id" ), f.id()},
{QStringLiteral( "layer_id" ), mLayerId}
{QStringLiteral( "layer_id" ), mLayerId},
{QStringLiteral( "layer_is_spatial" ), mLayerIsSpatial}
} );
result.icon = mLayerIcon;
result.score = static_cast< double >( searchString.length() ) / result.displayString.size();
if ( mLayerIsSpatial )
result.actions << QgsLocatorResult::ResultAction( OpenForm, tr( "Open form…" ) );

emit resultFetched( result );

featuresFound << f.id();
Expand All @@ -469,6 +479,11 @@ void QgsActiveLayerFeaturesLocatorFilter::fetchResults( const QString &string, c
}

void QgsActiveLayerFeaturesLocatorFilter::triggerResult( const QgsLocatorResult &result )
{
triggerResultFromAction( result, NoEntry );
}

void QgsActiveLayerFeaturesLocatorFilter::triggerResultFromAction( const QgsLocatorResult &result, const int actionId )
{
QVariantMap data = result.userData.value<QVariantMap>();
switch ( data.value( QStringLiteral( "type" ) ).value<ResultType>() )
Expand All @@ -479,8 +494,29 @@ void QgsActiveLayerFeaturesLocatorFilter::triggerResult( const QgsLocatorResult
if ( layer )
{
QgsFeatureId fid = data.value( QStringLiteral( "feature_id" ) ).value<QgsFeatureId>();
QgisApp::instance()->mapCanvas()->zoomToFeatureIds( layer, QgsFeatureIds() << fid );
QgisApp::instance()->mapCanvas()->flashFeatureIds( layer, QgsFeatureIds() << fid );
if ( actionId == OpenForm || !data.value( QStringLiteral( "layer_is_spatial" ), true ).toBool() )
{
QgsFeature f;
QgsFeatureRequest request;
request.setFilterFid( fid );
bool fetched = layer->getFeatures( request ).nextFeature( f );
if ( !fetched )
return;
QgsFeatureAction action( tr( "Attributes changed" ), f, layer, QString(), -1, QgisApp::instance() );
if ( layer->isEditable() )
{
action.editFeature( false );
}
else
{
action.viewFeatureForm();
}
}
else
{
QgisApp::instance()->mapCanvas()->zoomToFeatureIds( layer, QgsFeatureIds() << fid );
QgisApp::instance()->mapCanvas()->flashFeatureIds( layer, QgsFeatureIds() << fid );
}
}
break;
}
Expand Down Expand Up @@ -580,6 +616,7 @@ QStringList QgsAllLayersFeaturesLocatorFilter::prepare( const QString &string, c
preparedLayer->request = req;
preparedLayer->exactMatchRequest = exactMatchRequest;
preparedLayer->layerIcon = QgsMapLayerModel::iconForLayer( layer );
preparedLayer->layerIsSpatial = layer->isSpatial();

mPreparedLayers.append( preparedLayer );
}
Expand Down Expand Up @@ -613,12 +650,13 @@ void QgsAllLayersFeaturesLocatorFilter::fetchResults( const QString &string, con

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

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

result.actions << QgsLocatorResult::ResultAction( OpenForm, tr( "Open form…" ) );
if ( preparedLayer->layerIsSpatial )
result.actions << QgsLocatorResult::ResultAction( OpenForm, tr( "Open form…" ) );
emit resultFetched( result );

foundInCurrentLayer++;
Expand Down Expand Up @@ -651,7 +689,8 @@ void QgsAllLayersFeaturesLocatorFilter::fetchResults( const QString &string, con
result.icon = preparedLayer->layerIcon;
result.score = static_cast< double >( string.length() ) / result.displayString.size();

result.actions << QgsLocatorResult::ResultAction( OpenForm, tr( "Open form…" ) );
if ( preparedLayer->layerIsSpatial )
result.actions << QgsLocatorResult::ResultAction( OpenForm, tr( "Open form…" ) );
emit resultFetched( result );

foundInCurrentLayer++;
Expand All @@ -674,11 +713,12 @@ void QgsAllLayersFeaturesLocatorFilter::triggerResultFromAction( const QgsLocato
QVariantList dataList = result.userData.toList();
QgsFeatureId fid = dataList.at( 0 ).toLongLong();
QString layerId = dataList.at( 1 ).toString();
bool layerIsSpatial = dataList.at( 2 ).toBool();
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
if ( !layer )
return;

if ( actionId == OpenForm )
if ( actionId == OpenForm || !layerIsSpatial )
{
QgsFeature f;
QgsFeatureRequest request;
Expand Down
10 changes: 8 additions & 2 deletions src/app/locator/qgsinbuiltlocatorfilters.h
Expand Up @@ -105,6 +105,7 @@ class APP_EXPORT QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter
QStringList prepare( const QString &string, const QgsLocatorContext &context ) override;
void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
void triggerResult( const QgsLocatorResult &result ) override;
void triggerResultFromAction( const QgsLocatorResult &result, const int actionId ) override;
bool hasConfigWidget() const override {return true;}
void openConfigWidget( QWidget *parent ) override;

Expand All @@ -116,6 +117,11 @@ class APP_EXPORT QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter
Q_ENUM( ResultType )

private:
enum ContextMenuEntry
{
NoEntry,
OpenForm
};

/**
* Returns the field restriction if defined (starting with @)
Expand All @@ -128,6 +134,7 @@ class APP_EXPORT QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter
QgsFeatureIterator mDisplayTitleIterator;
QgsFeatureIterator mFieldIterator;
QString mLayerId;
bool mLayerIsSpatial = false;
QIcon mLayerIcon;
QStringList mAttributeAliases;
QStringList mFieldsCompletion;
Expand Down Expand Up @@ -158,6 +165,7 @@ class APP_EXPORT QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
QString layerName;
QString layerId;
QIcon layerIcon;
bool layerIsSpatial;
};

QgsAllLayersFeaturesLocatorFilter( QObject *parent = nullptr );
Expand Down Expand Up @@ -260,5 +268,3 @@ class APP_EXPORT QgsGotoLocatorFilter : public QgsLocatorFilter
};

#endif // QGSINBUILTLOCATORFILTERS_H


0 comments on commit 6cbfce2

Please sign in to comment.