16 #include <QMouseEvent>
35 , mAllowMultipleReturn( true )
36 , mExecWithSingleResult( false )
37 , mShowFeatureActions( false )
38 , mResultsIfExternalAction( false )
39 , mMaxLayerDisplay( 10 )
40 , mMaxFeatureDisplay( 10 )
41 , mDefaultActionName( tr(
"Identify" ) )
55 QgsDebugMsg( QStringLiteral(
"invalid value for number of layers displayed." ) );
65 QgsDebugMsg( QStringLiteral(
"invalid value for number of layers displayed." ) );
71 QList<QgsMapToolIdentify::IdentifyResult>
QgsIdentifyMenu::exec(
const QList<QgsMapToolIdentify::IdentifyResult> &idResults, QPoint pos )
74 mLayerIdResults.clear();
76 QList<QgsMapToolIdentify::IdentifyResult> returnResults = QList<QgsMapToolIdentify::IdentifyResult>();
78 if ( idResults.isEmpty() )
82 if ( idResults.count() == 1 && !mExecWithSingleResult )
84 returnResults << idResults[0];
89 const auto constIdResults = idResults;
93 if ( mLayerIdResults.contains( layer ) )
95 mLayerIdResults[layer].append( result );
99 mLayerIdResults.insert( layer, QList<QgsMapToolIdentify::IdentifyResult>() << result );
104 const bool singleLayer = mLayerIdResults.count() == 1;
106 QMapIterator< QgsMapLayer *, QList<QgsMapToolIdentify::IdentifyResult> > it( mLayerIdResults );
107 while ( it.hasNext() )
109 if ( mMaxLayerDisplay != 0 && count > mMaxLayerDisplay )
114 switch ( layer->
type() )
118 addRasterLayer( layer );
126 addVectorLayer( vl, it.value(), singleLayer );
144 if ( !singleLayer && mAllowMultipleReturn && idResults.count() > 1 )
147 QAction *allAction =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionIdentify.svg" ) ), tr(
"%1 All (%2)" ).arg( mDefaultActionName ).arg( idResults.count() ),
this );
148 allAction->setData( QVariant::fromValue<ActionData>(
ActionData(
nullptr ) ) );
149 connect( allAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
150 addAction( allAction );
154 QAction *selectedAction = QMenu::exec( pos );
156 returnResults = results( selectedAction, externalAction );
161 qDeleteAll( findChildren<QgsActionMenu *>() );
163 if ( externalAction && !mResultsIfExternalAction )
165 return QList<QgsMapToolIdentify::IdentifyResult>();
169 return returnResults;
176 QMenu::closeEvent( e );
179 void QgsIdentifyMenu::addRasterLayer(
QgsMapLayer *layer )
181 QAction *layerAction =
nullptr;
182 QMenu *layerMenu =
nullptr;
184 QList<QgsMapLayerAction *> separators = QList<QgsMapLayerAction *>();
186 const int nCustomActions = layerActions.count();
187 if ( nCustomActions )
189 separators.append( layerActions[0] );
191 if ( mShowFeatureActions )
194 if ( layerActions.count() > nCustomActions )
196 separators.append( layerActions[nCustomActions] );
201 if ( layerActions.isEmpty() )
203 layerAction =
new QAction( layer->
name(),
this );
207 layerMenu =
new QMenu( layer->
name(),
this );
208 layerAction = layerMenu->menuAction();
213 layerAction->setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
214 connect( layerAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
215 addAction( layerAction );
222 QAction *identifyFeatureAction =
new QAction( mDefaultActionName, layerMenu );
223 connect( identifyFeatureAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
224 identifyFeatureAction->setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
225 layerMenu->addAction( identifyFeatureAction );
228 const auto constLayerActions = layerActions;
231 QAction *action =
new QAction( mapLayerAction->icon(), mapLayerAction->text(), layerMenu );
232 action->setData( QVariant::fromValue<ActionData>( ActionData( layer,
true ) ) );
233 connect( action, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
234 connect( action, &QAction::triggered,
this, &QgsIdentifyMenu::triggerMapLayerAction );
235 layerMenu->addAction( action );
236 if ( separators.contains( mapLayerAction ) )
238 layerMenu->insertSeparator( action );
243 void QgsIdentifyMenu::addVectorLayer(
QgsVectorLayer *layer,
const QList<QgsMapToolIdentify::IdentifyResult> &results,
bool singleLayer )
245 QAction *layerAction =
nullptr;
246 QMenu *layerMenu =
nullptr;
252 QList<QgsMapLayerAction *> separators = QList<QgsMapLayerAction *>();
253 QList<QgsMapLayerAction *> layerActions = mCustomActionRegistry.mapLayerActions( layer, targets );
254 const int nCustomActions = layerActions.count();
255 if ( nCustomActions )
257 separators << layerActions[0];
259 if ( mShowFeatureActions )
263 if ( layerActions.count() > nCustomActions )
265 separators << layerActions[nCustomActions];
274 bool createMenu = results.count() > 1 || !layerActions.isEmpty();
281 if ( !createMenu && mShowFeatureActions )
285 createMenu = !featureActionMenu->actions().isEmpty();
286 delete featureActionMenu;
292 exp.prepare( &context );
293 context.setFeature( results[0].mFeature );
298 QString featureTitle = exp.evaluate( &context ).toString();
299 if ( featureTitle.isEmpty() )
300 featureTitle = QString::number( results[0].mFeature.id() );
301 layerAction =
new QAction( QStringLiteral(
"%1 (%2)" ).arg( layer->
name(), featureTitle ),
this );
313 if ( results.count() > 1 )
315 layerMenu =
new QMenu( layer->
name(),
this );
320 QString featureTitle = exp.evaluate( &context ).toString();
321 if ( featureTitle.isEmpty() )
322 featureTitle = QString::number( results[0].mFeature.id() );
323 layerMenu =
new QMenu( QStringLiteral(
"%1 (%2)" ).arg( layer->
name(), featureTitle ),
this );
325 layerAction = layerMenu->menuAction();
335 layerAction->setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
336 connect( layerAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
337 addAction( layerAction );
346 const auto constResults = results;
349 if ( mMaxFeatureDisplay != 0 && count > mMaxFeatureDisplay )
353 QAction *featureAction =
nullptr;
354 QMenu *featureMenu =
nullptr;
358 if ( mShowFeatureActions )
360 featureActionMenu =
new QgsActionMenu( layer, result.mFeature, QStringLiteral(
"Feature" ), layerMenu );
366 context.setFeature( result.mFeature );
367 QString featureTitle = exp.evaluate( &context ).toString();
368 if ( featureTitle.isEmpty() )
369 featureTitle = QString::number( result.mFeature.id() );
371 if ( customFeatureActions.isEmpty() && ( !featureActionMenu || featureActionMenu->actions().isEmpty() ) )
373 featureAction =
new QAction( featureTitle, layerMenu );
375 featureAction->setData( QVariant::fromValue<ActionData>( ActionData( layer, result.mFeature.id() ) ) );
376 connect( featureAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
377 layerMenu->addAction( featureAction );
379 else if ( results.count() == 1 )
384 featureMenu = layerMenu;
388 featureMenu =
new QMenu( featureTitle, layerMenu );
391 featureAction = featureMenu->menuAction();
393 featureAction->setData( QVariant::fromValue<ActionData>( ActionData( layer, result.mFeature.id() ) ) );
394 connect( featureAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
395 layerMenu->addAction( featureAction );
403 QAction *identifyFeatureAction =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionIdentify.svg" ) ), mDefaultActionName, featureMenu );
404 connect( identifyFeatureAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
405 identifyFeatureAction->setData( QVariant::fromValue<ActionData>( ActionData( layer, result.mFeature.id() ) ) );
406 featureMenu->addAction( identifyFeatureAction );
407 featureMenu->addSeparator();
410 const auto constCustomFeatureActions = customFeatureActions;
413 QAction *action =
new QAction( mapLayerAction->icon(), mapLayerAction->text(), featureMenu );
414 action->setData( QVariant::fromValue<ActionData>( ActionData( layer, result.mFeature.id(), mapLayerAction ) ) );
415 connect( action, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
416 connect( action, &QAction::triggered,
this, &QgsIdentifyMenu::triggerMapLayerAction );
417 featureMenu->addAction( action );
420 if ( featureActionMenu )
422 const auto constActions = featureActionMenu->actions();
423 for ( QAction *action : constActions )
425 connect( action, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
426 featureMenu->addAction( action );
434 if ( mAllowMultipleReturn && results.count() > 1 )
436 layerMenu->addSeparator();
437 QAction *allAction =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionIdentify.svg" ) ), tr(
"%1 All (%2)" ).arg( mDefaultActionName ).arg( results.count() ), layerMenu );
438 allAction->setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
439 connect( allAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
440 layerMenu->addAction( allAction );
444 const auto constLayerActions = layerActions;
447 QString title = mapLayerAction->text();
449 title.append( QStringLiteral(
" (%1)" ).arg( results.count() ) );
450 QAction *action =
new QAction( mapLayerAction->icon(), title, layerMenu );
451 action->setData( QVariant::fromValue<ActionData>( ActionData( layer, mapLayerAction ) ) );
452 connect( action, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
453 connect( action, &QAction::triggered,
this, &QgsIdentifyMenu::triggerMapLayerAction );
454 layerMenu->addAction( action );
455 if ( separators.contains( mapLayerAction ) )
457 layerMenu->insertSeparator( action );
462 void QgsIdentifyMenu::triggerMapLayerAction()
464 QAction *action = qobject_cast<QAction *>( sender() );
467 const QVariant varData = action->data();
468 if ( !varData.isValid() || !varData.canConvert<ActionData>() )
471 ActionData actData = action->data().value<ActionData>();
473 if ( actData.mIsValid && actData.mMapLayerAction )
478 actData.mMapLayerAction->triggerForLayer( actData.mLayer );
484 QList<QgsFeature> featureList;
485 const auto results { mLayerIdResults[actData.mLayer] };
488 featureList << result.mFeature;
490 actData.mMapLayerAction->triggerForFeatures( actData.mLayer, featureList );
496 const auto results { mLayerIdResults[actData.mLayer] };
499 if ( result.mFeature.id() == actData.mFeatureId )
501 actData.mMapLayerAction->triggerForFeature( actData.mLayer, result.mFeature );
505 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve feature for action %1" ).arg( action->text() ) );
511 QList<QgsMapToolIdentify::IdentifyResult> QgsIdentifyMenu::results( QAction *action,
bool &externalAction )
513 QList<QgsMapToolIdentify::IdentifyResult> idResults = QList<QgsMapToolIdentify::IdentifyResult>();
515 externalAction =
false;
518 bool hasData =
false;
523 const QVariant varData = action->data();
524 if ( !varData.isValid() )
526 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve results from menu entry (invalid data)" ) );
530 if ( varData.canConvert<ActionData>() )
532 actData = action->data().value<ActionData>();
533 if ( actData.mIsValid )
535 externalAction = actData.mIsExternalAction;
545 externalAction =
true;
553 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve results from menu entry (no data found)" ) );
558 if ( actData.mAllResults )
561 QMapIterator< QgsMapLayer *, QList<QgsMapToolIdentify::IdentifyResult> > it( mLayerIdResults );
562 while ( it.hasNext() )
565 idResults << it.value();
570 if ( !mLayerIdResults.contains( actData.mLayer ) )
572 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve results from menu entry (layer not found)" ) );
578 return mLayerIdResults[actData.mLayer];
583 const auto results {mLayerIdResults[actData.mLayer]};
586 if ( res.mFeature.id() == actData.mFeatureId )
594 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve results from menu entry (don't know what happened')" ) );
598 void QgsIdentifyMenu::handleMenuHover()
605 QAction *senderAction = qobject_cast<QAction *>( sender() );
610 const QList<QgsMapToolIdentify::IdentifyResult> idResults = results( senderAction, externalAction );
612 const auto constIdResults = idResults;
615 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( result.mLayer );
621 mRubberBands.append( hl );
622 connect( vl, &QObject::destroyed,
this, &QgsIdentifyMenu::layerDestroyed );
635 color.setAlpha( alpha );
641 void QgsIdentifyMenu::deleteRubberBands()
643 QList<QgsHighlight *>::const_iterator it = mRubberBands.constBegin();
644 for ( ; it != mRubberBands.constEnd(); ++it )
646 mRubberBands.clear();
649 void QgsIdentifyMenu::layerDestroyed()
651 QList<QgsHighlight *>::iterator it = mRubberBands.begin();
652 while ( it != mRubberBands.end() )
654 if ( ( *it )->layer() == sender() )
657 it = mRubberBands.erase( it );
668 mCustomActionRegistry.clear();
674 mExpressionContextScope = scope;
679 return mExpressionContextScope;
static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/stroke minimum width in mm.
static const double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
@ IdentifyMode
Identify the feature.
Single scope for storing variables and functions for use within a QgsExpressionContext.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
static QgsMapLayerActionRegistry * mapLayerActionRegistry()
Returns the global map layer action registry, used for registering map layer actions.
A class for highlight features on the map.
void setBuffer(double buffer)
Set line / stroke buffer in millimeters.
void setMinWidth(double width)
Set minimum line / stroke width in millimeters.
void setFillColor(const QColor &fillColor)
Fill color for the highlight.
void setColor(const QColor &color)
Set line/stroke to color, polygon fill to color with alpha = 63.
static QIcon iconForWkbType(QgsWkbTypes::Type type)
Returns the icon for a vector layer whose geometry type is provided.
Map canvas is a class for displaying all GIS data types on a canvas.
QList< QgsMapLayerAction * > mapLayerActions(QgsMapLayer *layer, QgsMapLayerAction::Targets targets=QgsMapLayerAction::AllActions)
Returns the map layer actions which can run on the specified layer.
An action which can run on map layers The class can be used in two manners:
Base class for all map layer types.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
QString displayExpression
@ PointCloudLayer
Point cloud layer. Added in QGIS 3.18.
@ MeshLayer
Mesh layer. Added in QGIS 3.2.
@ VectorLayer
Vector layer.
@ RasterLayer
Raster layer.
@ GroupLayer
Composite group layer. Added in QGIS 3.24.
@ VectorTileLayer
Vector tile layer. Added in QGIS 3.14.
@ AnnotationLayer
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ PluginLayer
Plugin based layer.