16 #include <QMouseEvent> 33 , mAllowMultipleReturn( true )
34 , mExecWithSingleResult( false )
35 , mShowFeatureActions( false )
36 , mResultsIfExternalAction( false )
37 , mMaxLayerDisplay( 10 )
38 , mMaxFeatureDisplay( 10 )
39 , mDefaultActionName( tr(
"Identify" ) )
51 if ( maxLayerDisplay < 0 )
53 QgsDebugMsg( QStringLiteral(
"invalid value for number of layers displayed." ) );
61 if ( maxFeatureDisplay < 0 )
63 QgsDebugMsg( QStringLiteral(
"invalid value for number of layers displayed." ) );
69 QList<QgsMapToolIdentify::IdentifyResult>
QgsIdentifyMenu::exec(
const QList<QgsMapToolIdentify::IdentifyResult> &idResults, QPoint pos )
72 mLayerIdResults.clear();
74 QList<QgsMapToolIdentify::IdentifyResult> returnResults = QList<QgsMapToolIdentify::IdentifyResult>();
76 if ( idResults.isEmpty() )
80 if ( idResults.count() == 1 && !mExecWithSingleResult )
82 returnResults << idResults[0];
90 if ( mLayerIdResults.contains( layer ) )
92 mLayerIdResults[layer].append( result );
96 mLayerIdResults.insert( layer, QList<QgsMapToolIdentify::IdentifyResult>() << result );
101 bool singleLayer = mLayerIdResults.count() == 1;
103 QMapIterator< QgsMapLayer *, QList<QgsMapToolIdentify::IdentifyResult> > it( mLayerIdResults );
104 while ( it.hasNext() )
106 if ( mMaxLayerDisplay != 0 && count > mMaxLayerDisplay )
111 switch ( layer->
type() )
115 addRasterLayer( layer );
123 addVectorLayer( vl, it.value(), singleLayer );
134 if ( !singleLayer && mAllowMultipleReturn && idResults.count() > 1 )
137 QAction *allAction =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionIdentify.svg" ) ), tr(
"%1 All (%2)" ).arg( mDefaultActionName ).arg( idResults.count() ),
this );
138 allAction->setData( QVariant::fromValue<ActionData>(
ActionData(
nullptr ) ) );
139 connect( allAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
140 addAction( allAction );
144 QAction *selectedAction = QMenu::exec( pos );
146 returnResults = results( selectedAction, externalAction );
151 qDeleteAll( findChildren<QgsActionMenu *>() );
153 if ( externalAction && !mResultsIfExternalAction )
155 return QList<QgsMapToolIdentify::IdentifyResult>();
159 return returnResults;
166 QMenu::closeEvent( e );
169 void QgsIdentifyMenu::addRasterLayer(
QgsMapLayer *layer )
171 QAction *layerAction =
nullptr;
172 QMenu *layerMenu =
nullptr;
174 QList<QgsMapLayerAction *> separators = QList<QgsMapLayerAction *>();
176 int nCustomActions = layerActions.count();
177 if ( nCustomActions )
179 separators.append( layerActions[0] );
181 if ( mShowFeatureActions )
184 if ( layerActions.count() > nCustomActions )
186 separators.append( layerActions[nCustomActions] );
191 if ( layerActions.isEmpty() )
193 layerAction =
new QAction( layer->
name(), this );
197 layerMenu =
new QMenu( layer->
name(), this );
198 layerAction = layerMenu->menuAction();
203 layerAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer ) ) );
204 connect( layerAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
205 addAction( layerAction );
212 QAction *identifyFeatureAction =
new QAction( mDefaultActionName, layerMenu );
213 connect( identifyFeatureAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
214 identifyFeatureAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer ) ) );
215 layerMenu->addAction( identifyFeatureAction );
220 QAction *action =
new QAction( mapLayerAction->icon(), mapLayerAction->text(), layerMenu );
221 action->setData( QVariant::fromValue<ActionData>(
ActionData( layer,
true ) ) );
222 connect( action, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
223 connect( action, &QAction::triggered,
this, &QgsIdentifyMenu::triggerMapLayerAction );
224 layerMenu->addAction( action );
225 if ( separators.contains( mapLayerAction ) )
227 layerMenu->insertSeparator( action );
232 void QgsIdentifyMenu::addVectorLayer(
QgsVectorLayer *layer,
const QList<QgsMapToolIdentify::IdentifyResult> &results,
bool singleLayer )
234 QAction *layerAction =
nullptr;
235 QMenu *layerMenu =
nullptr;
241 QList<QgsMapLayerAction *> separators = QList<QgsMapLayerAction *>();
242 QList<QgsMapLayerAction *> layerActions = mCustomActionRegistry.mapLayerActions( layer, targets );
243 int nCustomActions = layerActions.count();
244 if ( nCustomActions )
246 separators << layerActions[0];
248 if ( mShowFeatureActions )
252 if ( layerActions.count() > nCustomActions )
254 separators << layerActions[nCustomActions];
263 bool createMenu = results.count() > 1 || !layerActions.isEmpty();
270 if ( !createMenu && mShowFeatureActions )
274 createMenu = !featureActionMenu->actions().isEmpty();
275 delete featureActionMenu;
283 QString featureTitle = results[0].mFeature.attribute( layer->
displayField() ).toString();
284 if ( featureTitle.isEmpty() )
285 featureTitle = QStringLiteral(
"%1" ).arg( results[0].mFeature.id() );
286 layerAction =
new QAction( QStringLiteral(
"%1 (%2)" ).arg( layer->
name(), featureTitle ),
this );
298 if ( results.count() > 1 )
300 layerMenu =
new QMenu( layer->
name(), this );
305 QString featureTitle = results[0].mFeature.attribute( layer->
displayField() ).toString();
306 if ( featureTitle.isEmpty() )
307 featureTitle = QStringLiteral(
"%1" ).arg( results[0].mFeature.id() );
308 layerMenu =
new QMenu( QStringLiteral(
"%1 (%2)" ).arg( layer->
name(), featureTitle ),
this );
310 layerAction = layerMenu->menuAction();
334 layerAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer ) ) );
335 connect( layerAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
336 addAction( layerAction );
347 if ( mMaxFeatureDisplay != 0 && count > mMaxFeatureDisplay )
351 QAction *featureAction =
nullptr;
352 QMenu *featureMenu =
nullptr;
356 if ( mShowFeatureActions )
358 featureActionMenu =
new QgsActionMenu( layer, result.
mFeature, QStringLiteral(
"Feature" ), layerMenu );
365 if ( featureTitle.isEmpty() )
366 featureTitle = QStringLiteral(
"%1" ).arg( result.
mFeature.
id() );
368 if ( customFeatureActions.isEmpty() && ( !featureActionMenu || featureActionMenu->actions().isEmpty() ) )
370 featureAction =
new QAction( featureTitle, layerMenu );
372 featureAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer, result.
mFeature.
id() ) ) );
373 connect( featureAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
374 layerMenu->addAction( featureAction );
376 else if ( results.count() == 1 )
381 featureMenu = layerMenu;
385 featureMenu =
new QMenu( featureTitle, layerMenu );
388 featureAction = featureMenu->menuAction();
390 featureAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer, result.
mFeature.
id() ) ) );
391 connect( featureAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
392 layerMenu->addAction( featureAction );
400 QAction *identifyFeatureAction =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionIdentify.svg" ) ), mDefaultActionName, featureMenu );
401 connect( identifyFeatureAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
402 identifyFeatureAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer, result.
mFeature.
id() ) ) );
403 featureMenu->addAction( identifyFeatureAction );
404 featureMenu->addSeparator();
409 QAction *action =
new QAction( mapLayerAction->icon(), mapLayerAction->text(), featureMenu );
410 action->setData( QVariant::fromValue<ActionData>(
ActionData( layer, result.
mFeature.
id(), mapLayerAction ) ) );
411 connect( action, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
412 connect( action, &QAction::triggered,
this, &QgsIdentifyMenu::triggerMapLayerAction );
413 featureMenu->addAction( action );
416 if ( featureActionMenu )
418 Q_FOREACH ( QAction *action, featureActionMenu->actions() )
420 connect( action, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
421 featureMenu->addAction( action );
429 if ( mAllowMultipleReturn && results.count() > 1 )
431 layerMenu->addSeparator();
432 QAction *allAction =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionIdentify.svg" ) ), tr(
"%1 All (%2)" ).arg( mDefaultActionName ).arg( results.count() ), layerMenu );
433 allAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer ) ) );
434 connect( allAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
435 layerMenu->addAction( allAction );
441 QString title = mapLayerAction->text();
443 title.append( QStringLiteral(
" (%1)" ).arg( results.count() ) );
444 QAction *action =
new QAction( mapLayerAction->icon(), title, layerMenu );
445 action->setData( QVariant::fromValue<ActionData>(
ActionData( layer, mapLayerAction ) ) );
446 connect( action, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
447 connect( action, &QAction::triggered,
this, &QgsIdentifyMenu::triggerMapLayerAction );
448 layerMenu->addAction( action );
449 if ( separators.contains( mapLayerAction ) )
451 layerMenu->insertSeparator( action );
456 void QgsIdentifyMenu::triggerMapLayerAction()
458 QAction *action = qobject_cast<QAction *>( sender() );
461 QVariant varData = action->data();
462 if ( !varData.isValid() || !varData.canConvert<
ActionData>() )
478 QList<QgsFeature> featureList;
497 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve feature for action %1" ).arg( action->text() ) );
503 QList<QgsMapToolIdentify::IdentifyResult> QgsIdentifyMenu::results( QAction *action,
bool &externalAction )
505 QList<QgsMapToolIdentify::IdentifyResult> idResults = QList<QgsMapToolIdentify::IdentifyResult>();
507 externalAction =
false;
510 bool hasData =
false;
515 QVariant varData = action->data();
516 if ( !varData.isValid() )
518 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve results from menu entry (invalid data)" ) );
537 externalAction =
true;
545 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve results from menu entry (no data found)" ) );
553 QMapIterator< QgsMapLayer *, QList<QgsMapToolIdentify::IdentifyResult> > it( mLayerIdResults );
554 while ( it.hasNext() )
557 idResults << it.value();
562 if ( !mLayerIdResults.contains( actData.
mLayer ) )
564 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve results from menu entry (layer not found)" ) );
570 return mLayerIdResults[actData.
mLayer];
585 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve results from menu entry (don't know what happened')" ) );
589 void QgsIdentifyMenu::handleMenuHover()
596 QAction *senderAction = qobject_cast<QAction *>( sender() );
601 QList<QgsMapToolIdentify::IdentifyResult> idResults = results( senderAction, externalAction );
616 color.setAlpha( alpha );
620 mRubberBands.append( hl );
621 connect( vl, &QObject::destroyed,
this, &QgsIdentifyMenu::layerDestroyed );
625 void QgsIdentifyMenu::deleteRubberBands()
627 QList<QgsHighlight *>::const_iterator it = mRubberBands.constBegin();
628 for ( ; it != mRubberBands.constEnd(); ++it )
630 mRubberBands.clear();
633 void QgsIdentifyMenu::layerDestroyed()
635 QList<QgsHighlight *>::iterator it = mRubberBands.begin();
636 while ( it != mRubberBands.end() )
638 if ( ( *it )->layer() == sender() )
641 it = mRubberBands.erase( it );
652 mCustomActionRegistry.clear();
658 mExpressionContextScope = scope;
663 return mExpressionContextScope;
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.
void setFillColor(const QColor &fillColor)
Fill color for the highlight.
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QgsMapLayer::LayerType type() const
Returns the type of the layer.
Map canvas is a class for displaying all GIS data types on a canvas.
void setBuffer(double buffer)
Set line / stroke buffer in millimeters.
A class for highlight features on the map.
Single scope for storing variables and functions for use within a QgsExpressionContext.
const Targets & targets() const
Returns availibity of action.
QString displayField() const
This is a shorthand for accessing the displayExpression if it is a simple field.
void triggerForFeatures(QgsMapLayer *layer, const QList< QgsFeature > &featureList)
Triggers the action with the specified layer and list of feature.
void setColor(const QColor &color)
Set line/stroke to color, polygon fill to color with alpha = 63.
QList< QgsMapLayerAction * > mapLayerActions(QgsMapLayer *layer, QgsMapLayerAction::Targets targets=QgsMapLayerAction::AllActions)
Returns the map layer actions which can run on the specified layer.
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.
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
static QgsMapLayerActionRegistry * mapLayerActionRegistry()
Returns the global map layer action registry, used for registering map layer actions.
void triggerForLayer(QgsMapLayer *layer)
Triggers the action with the specified layer.
void triggerForFeature(QgsMapLayer *layer, const QgsFeature *feature)
Triggers the action with the specified layer and feature.
An action which can run on map layers.
void setMinWidth(double width)
Set minimum line / stroke width in millimeters.