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 )
113 addRasterLayer( layer );
120 addVectorLayer( vl, it.value(), singleLayer );
125 if ( !singleLayer && mAllowMultipleReturn && idResults.count() > 1 )
128 QAction *allAction =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionIdentify.svg" ) ), tr(
"%1 All (%2)" ).arg( mDefaultActionName ).arg( idResults.count() ),
this );
129 allAction->setData( QVariant::fromValue<ActionData>(
ActionData(
nullptr ) ) );
130 connect( allAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
131 addAction( allAction );
135 QAction *selectedAction = QMenu::exec( pos );
137 returnResults = results( selectedAction, externalAction );
142 qDeleteAll( findChildren<QgsActionMenu *>() );
144 if ( externalAction && !mResultsIfExternalAction )
146 return QList<QgsMapToolIdentify::IdentifyResult>();
150 return returnResults;
157 QMenu::closeEvent( e );
160 void QgsIdentifyMenu::addRasterLayer(
QgsMapLayer *layer )
162 QAction *layerAction =
nullptr;
163 QMenu *layerMenu =
nullptr;
165 QList<QgsMapLayerAction *> separators = QList<QgsMapLayerAction *>();
167 int nCustomActions = layerActions.count();
168 if ( nCustomActions )
170 separators.append( layerActions[0] );
172 if ( mShowFeatureActions )
175 if ( layerActions.count() > nCustomActions )
177 separators.append( layerActions[nCustomActions] );
182 if ( layerActions.isEmpty() )
184 layerAction =
new QAction( layer->
name(), this );
188 layerMenu =
new QMenu( layer->
name(), this );
189 layerAction = layerMenu->menuAction();
194 layerAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer ) ) );
195 connect( layerAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
196 addAction( layerAction );
203 QAction *identifyFeatureAction =
new QAction( mDefaultActionName, layerMenu );
204 connect( identifyFeatureAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
205 identifyFeatureAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer ) ) );
206 layerMenu->addAction( identifyFeatureAction );
211 QAction *action =
new QAction( mapLayerAction->icon(), mapLayerAction->text(), layerMenu );
212 action->setData( QVariant::fromValue<ActionData>(
ActionData( layer,
true ) ) );
213 connect( action, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
214 connect( action, &QAction::triggered,
this, &QgsIdentifyMenu::triggerMapLayerAction );
215 layerMenu->addAction( action );
216 if ( separators.contains( mapLayerAction ) )
218 layerMenu->insertSeparator( action );
223 void QgsIdentifyMenu::addVectorLayer(
QgsVectorLayer *layer,
const QList<QgsMapToolIdentify::IdentifyResult> &results,
bool singleLayer )
225 QAction *layerAction =
nullptr;
226 QMenu *layerMenu =
nullptr;
232 QList<QgsMapLayerAction *> separators = QList<QgsMapLayerAction *>();
233 QList<QgsMapLayerAction *> layerActions = mCustomActionRegistry.mapLayerActions( layer, targets );
234 int nCustomActions = layerActions.count();
235 if ( nCustomActions )
237 separators << layerActions[0];
239 if ( mShowFeatureActions )
243 if ( layerActions.count() > nCustomActions )
245 separators << layerActions[nCustomActions];
254 bool createMenu = results.count() > 1 || !layerActions.isEmpty();
261 if ( !createMenu && mShowFeatureActions )
265 createMenu = !featureActionMenu->actions().isEmpty();
266 delete featureActionMenu;
274 QString featureTitle = results[0].mFeature.attribute( layer->
displayField() ).toString();
275 if ( featureTitle.isEmpty() )
276 featureTitle = QStringLiteral(
"%1" ).arg( results[0].mFeature.id() );
277 layerAction =
new QAction( QStringLiteral(
"%1 (%2)" ).arg( layer->
name(), featureTitle ),
this );
289 if ( results.count() > 1 )
291 layerMenu =
new QMenu( layer->
name(), this );
296 QString featureTitle = results[0].mFeature.attribute( layer->
displayField() ).toString();
297 if ( featureTitle.isEmpty() )
298 featureTitle = QStringLiteral(
"%1" ).arg( results[0].mFeature.id() );
299 layerMenu =
new QMenu( QStringLiteral(
"%1 (%2)" ).arg( layer->
name(), featureTitle ),
this );
301 layerAction = layerMenu->menuAction();
325 layerAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer ) ) );
326 connect( layerAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
327 addAction( layerAction );
338 if ( mMaxFeatureDisplay != 0 && count > mMaxFeatureDisplay )
342 QAction *featureAction =
nullptr;
343 QMenu *featureMenu =
nullptr;
347 if ( mShowFeatureActions )
349 featureActionMenu =
new QgsActionMenu( layer, result.
mFeature, QStringLiteral(
"Feature" ), layerMenu );
356 if ( featureTitle.isEmpty() )
357 featureTitle = QStringLiteral(
"%1" ).arg( result.
mFeature.
id() );
359 if ( customFeatureActions.isEmpty() && ( !featureActionMenu || featureActionMenu->actions().isEmpty() ) )
361 featureAction =
new QAction( featureTitle, layerMenu );
363 featureAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer, result.
mFeature.
id() ) ) );
364 connect( featureAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
365 layerMenu->addAction( featureAction );
367 else if ( results.count() == 1 )
372 featureMenu = layerMenu;
376 featureMenu =
new QMenu( featureTitle, layerMenu );
379 featureAction = featureMenu->menuAction();
381 featureAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer, result.
mFeature.
id() ) ) );
382 connect( featureAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
383 layerMenu->addAction( featureAction );
391 QAction *identifyFeatureAction =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionIdentify.svg" ) ), mDefaultActionName, featureMenu );
392 connect( identifyFeatureAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
393 identifyFeatureAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer, result.
mFeature.
id() ) ) );
394 featureMenu->addAction( identifyFeatureAction );
395 featureMenu->addSeparator();
400 QAction *action =
new QAction( mapLayerAction->icon(), mapLayerAction->text(), featureMenu );
401 action->setData( QVariant::fromValue<ActionData>(
ActionData( layer, result.
mFeature.
id(), mapLayerAction ) ) );
402 connect( action, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
403 connect( action, &QAction::triggered,
this, &QgsIdentifyMenu::triggerMapLayerAction );
404 featureMenu->addAction( action );
407 if ( featureActionMenu )
409 Q_FOREACH ( QAction *action, featureActionMenu->actions() )
411 connect( action, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
412 featureMenu->addAction( action );
420 if ( mAllowMultipleReturn && results.count() > 1 )
422 layerMenu->addSeparator();
423 QAction *allAction =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionIdentify.svg" ) ), tr(
"%1 All (%2)" ).arg( mDefaultActionName ).arg( results.count() ), layerMenu );
424 allAction->setData( QVariant::fromValue<ActionData>(
ActionData( layer ) ) );
425 connect( allAction, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
426 layerMenu->addAction( allAction );
432 QString title = mapLayerAction->text();
434 title.append( QStringLiteral(
" (%1)" ).arg( results.count() ) );
435 QAction *action =
new QAction( mapLayerAction->icon(), title, layerMenu );
436 action->setData( QVariant::fromValue<ActionData>(
ActionData( layer, mapLayerAction ) ) );
437 connect( action, &QAction::hovered,
this, &QgsIdentifyMenu::handleMenuHover );
438 connect( action, &QAction::triggered,
this, &QgsIdentifyMenu::triggerMapLayerAction );
439 layerMenu->addAction( action );
440 if ( separators.contains( mapLayerAction ) )
442 layerMenu->insertSeparator( action );
447 void QgsIdentifyMenu::triggerMapLayerAction()
449 QAction *action = qobject_cast<QAction *>( sender() );
452 QVariant varData = action->data();
453 if ( !varData.isValid() || !varData.canConvert<
ActionData>() )
469 QList<QgsFeature> featureList;
488 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve feature for action %1" ).arg( action->text() ) );
494 QList<QgsMapToolIdentify::IdentifyResult> QgsIdentifyMenu::results( QAction *action,
bool &externalAction )
496 QList<QgsMapToolIdentify::IdentifyResult> idResults = QList<QgsMapToolIdentify::IdentifyResult>();
498 externalAction =
false;
501 bool hasData =
false;
506 QVariant varData = action->data();
507 if ( !varData.isValid() )
509 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve results from menu entry (invalid data)" ) );
528 externalAction =
true;
536 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve results from menu entry (no data found)" ) );
544 QMapIterator< QgsMapLayer *, QList<QgsMapToolIdentify::IdentifyResult> > it( mLayerIdResults );
545 while ( it.hasNext() )
548 idResults << it.value();
553 if ( !mLayerIdResults.contains( actData.
mLayer ) )
555 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve results from menu entry (layer not found)" ) );
561 return mLayerIdResults[actData.
mLayer];
576 QgsDebugMsg( QStringLiteral(
"Identify menu: could not retrieve results from menu entry (don't know what happened')" ) );
580 void QgsIdentifyMenu::handleMenuHover()
587 QAction *senderAction = qobject_cast<QAction *>( sender() );
592 QList<QgsMapToolIdentify::IdentifyResult> idResults = results( senderAction, externalAction );
607 color.setAlpha( alpha );
611 mRubberBands.append( hl );
612 connect( vl, &QObject::destroyed,
this, &QgsIdentifyMenu::layerDestroyed );
616 void QgsIdentifyMenu::deleteRubberBands()
618 QList<QgsHighlight *>::const_iterator it = mRubberBands.constBegin();
619 for ( ; it != mRubberBands.constEnd(); ++it )
621 mRubberBands.clear();
624 void QgsIdentifyMenu::layerDestroyed()
626 QList<QgsHighlight *>::iterator it = mRubberBands.begin();
627 while ( it != mRubberBands.end() )
629 if ( ( *it )->layer() == sender() )
632 it = mRubberBands.erase( it );
643 mCustomActionRegistry.clear();
649 mExpressionContextScope = scope;
654 return mExpressionContextScope;
Base class for all map layer types.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
This class is a composition of two QSettings instances:
void setFillColor(const QColor &fillColor)
Fill color for the highlight.
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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...
QString displayField() const
This is a shorthand for accessing the displayExpression if it is a simple field.
Map canvas is a class for displaying all GIS data types on a canvas.
void setBuffer(double buffer)
Set line / stroke buffer in millimeters.
QList< QgsMapLayerAction * > mapLayerActions(QgsMapLayer *layer, QgsMapLayerAction::Targets targets=QgsMapLayerAction::AllActions)
Returns the map layer actions which can run on the specified layer.
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.
void triggerForFeatures(QgsMapLayer *layer, const QList< QgsFeature > &featureList)
Triggers the action with the specified layer and list of feature.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
void setColor(const QColor &color)
Set line/stroke to color, polygon fill to color with alpha = 63.
static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/stroke minimum width in mm.
QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
static const double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Represents a vector layer which manages a vector based data sets.
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.