16 #include <QMouseEvent> 26 CustomActionRegistry::CustomActionRegistry(
QObject* parent )
35 , mAllowMultipleReturn( true )
36 , mExecWithSingleResult( false )
37 , mShowFeatureActions( false )
38 , mResultsIfExternalAction( false )
39 , mMaxLayerDisplay( 10 )
40 , mMaxFeatureDisplay( 10 )
41 , mDefaultActionName(
tr(
"Identify" ) )
42 , mCustomActionRegistry( CustomActionRegistry::instance() )
54 if ( maxLayerDisplay < 0 )
56 QgsDebugMsg(
"invalid value for number of layers displayed." );
64 if ( maxFeatureDisplay < 0 )
66 QgsDebugMsg(
"invalid value for number of layers displayed." );
75 mLayerIdResults.
clear();
83 if ( idResults.
count() == 1 && !mExecWithSingleResult )
85 returnResults << idResults[0];
93 if ( mLayerIdResults.
contains( layer ) )
95 mLayerIdResults[layer].append( result );
104 bool singleLayer = mLayerIdResults.
count() == 1;
109 if ( mMaxLayerDisplay != 0 && count > mMaxLayerDisplay )
116 addRasterLayer( layer );
123 addVectorLayer( vl, it.
value(), singleLayer );
128 if ( !singleLayer && mAllowMultipleReturn && idResults.
count() > 1 )
133 connect( allAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
140 returnResults = results( selectedAction, externalAction );
145 qDeleteAll( findChildren<QgsActionMenu*>() );
147 if ( externalAction && !mResultsIfExternalAction )
153 return returnResults;
163 void QgsIdentifyMenu::addRasterLayer(
QgsMapLayer* layer )
166 QMenu* layerMenu =
nullptr;
170 int nCustomActions = layerActions.
count();
171 if ( nCustomActions )
173 separators.
append( layerActions[0] );
175 if ( mShowFeatureActions )
178 if ( layerActions.
count() > nCustomActions )
180 separators.
append( layerActions[nCustomActions] );
191 layerMenu =
new QMenu( layer->
name(), this );
198 connect( layerAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
206 QAction* identifyFeatureAction =
new QAction( mDefaultActionName, layerMenu );
207 connect( identifyFeatureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
208 identifyFeatureAction->
setData( QVariant::fromValue<ActionData>(
ActionData( layer ) ) );
209 layerMenu->
addAction( identifyFeatureAction );
216 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
217 connect( action, SIGNAL(
triggered() ),
this, SLOT( triggerMapLayerAction() ) );
219 if ( separators.
contains( mapLayerAction ) )
228 QAction* layerAction =
nullptr;
229 QMenu* layerMenu =
nullptr;
237 int nCustomActions = layerActions.
count();
238 if ( nCustomActions )
240 separators << layerActions[0];
242 if ( mShowFeatureActions )
246 if ( layerActions.
count() > nCustomActions )
248 separators << layerActions[nCustomActions];
257 bool createMenu = results.
count() > 1 || !layerActions.
isEmpty();
264 if ( !createMenu && mShowFeatureActions )
267 createMenu = !featureActionMenu->
actions().isEmpty();
268 delete featureActionMenu;
278 featureTitle =
QString(
"%1" ).
arg( results[0].mFeature.id() );
279 layerAction =
new QAction(
QString(
"%1 (%2)" ).arg( layer->
name(), featureTitle ),
this );
291 if ( results.
count() > 1 )
293 layerMenu =
new QMenu( layer->
name(), this );
300 featureTitle =
QString(
"%1" ).
arg( results[0].mFeature.id() );
301 layerMenu =
new QMenu(
QString(
"%1 (%2)" ).arg( layer->
name(), featureTitle ),
this );
328 connect( layerAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
340 if ( mMaxFeatureDisplay != 0 && count > mMaxFeatureDisplay )
344 QAction* featureAction =
nullptr;
345 QMenu* featureMenu =
nullptr;
349 if ( mShowFeatureActions )
360 if ( customFeatureActions.
isEmpty() && ( !featureActionMenu || featureActionMenu->
actions().isEmpty() ) )
362 featureAction =
new QAction( featureTitle, layerMenu );
365 connect( featureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
368 else if ( results.
count() == 1 )
373 featureMenu = layerMenu;
377 featureMenu =
new QMenu( featureTitle, layerMenu );
383 connect( featureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
393 connect( identifyFeatureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
395 featureMenu->
addAction( identifyFeatureAction );
403 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
404 connect( action, SIGNAL(
triggered() ),
this, SLOT( triggerMapLayerAction() ) );
408 if ( featureActionMenu )
412 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
421 if ( mAllowMultipleReturn && results.
count() > 1 )
426 connect( allAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
437 action->
setData( QVariant::fromValue<ActionData>(
ActionData( layer, mapLayerAction ) ) );
438 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
439 connect( action, SIGNAL(
triggered() ),
this, SLOT( triggerMapLayerAction() ) );
441 if ( separators.
contains( mapLayerAction ) )
448 void QgsIdentifyMenu::triggerMapLayerAction()
489 QgsDebugMsg(
QString(
"Identify menu: could not retrieve feature for action %1" ).arg( action->
text() ) );
499 externalAction =
false;
502 bool hasData =
false;
510 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (invalid data)" );
529 externalAction =
true;
537 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (no data found)" );
549 idResults << it.
value();
556 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (layer not found)" );
562 return mLayerIdResults[actData.
mLayer];
577 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (don't know what happened')" );
581 void QgsIdentifyMenu::handleMenuHover()
612 mRubberBands.
append( hl );
617 void QgsIdentifyMenu::deleteRubberBands()
620 for ( ; it != mRubberBands.
constEnd(); ++it )
622 mRubberBands.
clear();
625 void QgsIdentifyMenu::layerDestroyed()
628 while ( it != mRubberBands.
end() )
630 if (( *it )->layer() ==
sender() )
633 it = mRubberBands.
erase( it );
644 mCustomActionRegistry.clear();
650 mExpressionContextScope = scope;
655 return mExpressionContextScope;
bool canConvert(Type t) const
static double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Base class for all map layer types.
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
QString & append(QChar ch)
bool contains(const Key &key) const
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
void setIcon(const QIcon &icon)
void setFillColor(const QColor &fillColor)
Set polygons fill color.
iterator erase(iterator pos)
const QgsGeometry * constGeometry() const
Gets a const pointer to the geometry object associated with this feature.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QgsMapLayer::LayerType type() const
Get the type of the layer.
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
void setBuffer(double buffer)
Set line / outline buffer in millimeters.
int count(const T &value) const
void append(const T &value)
A class for highlight features on the map.
QGis::GeometryType geometryType() const
Returns point, line or polygon.
Single scope for storing variables and functions for use within a QgsExpressionContext.
This class tracks map layer actions.
const Targets & targets() const
Return availibity of action.
QString displayField() const
Returns the primary display field name used in the identify results dialog.
void setData(const QVariant &userData)
QgsFeatureId id() const
Get the feature ID for this feature.
void triggerForFeatures(QgsMapLayer *layer, const QList< QgsFeature > &featureList)
Triggers the action with the specified layer and list of feature.
bool contains(const T &value) const
static QgsMapLayerActionRegistry * instance()
Returns the instance pointer, creating the object on the first call.
void setColor(const QColor &color)
Set line/outline to color, polygon fill to color with alpha = 63.
QList< QgsMapLayerAction * > mapLayerActions(QgsMapLayer *layer, const QgsMapLayerAction::Targets &targets=QgsMapLayerAction::AllActions)
Returns the map layer actions which can run on the specified layer.
QString name
Read property of QString layerName.
iterator insert(const Key &key, const T &value)
const_iterator constEnd() const
const_iterator constBegin() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
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.
static double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/outline minimum width in mm.
int count(const Key &key) const
An action which can run on map layers.
void destroyed(QObject *obj)
void setMinWidth(double width)
Set minimum line / outline width in millimeters.