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 );
197 layerAction->setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
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 );
215 action->
setData( QVariant::fromValue<ActionData>( ActionData( layer,
true ) ) );
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 );
327 layerAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
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 )
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, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
367 else if ( results.
count() == 1 )
372 featureMenu = layerMenu;
376 featureMenu =
new QMenu( featureTitle, layerMenu );
381 featureAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer, result.
mFeature.
id() ) ) );
382 connect( featureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
392 connect( identifyFeatureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
393 identifyFeatureAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer, result.
mFeature.
id() ) ) );
394 featureMenu->
addAction( identifyFeatureAction );
401 action->
setData( QVariant::fromValue<ActionData>( ActionData( layer, result.
mFeature.
id(), mapLayerAction ) ) );
402 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
403 connect( action, SIGNAL(
triggered() ),
this, SLOT( triggerMapLayerAction() ) );
407 if ( featureActionMenu )
411 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
420 if ( mAllowMultipleReturn && results.
count() > 1 )
424 allAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
425 connect( allAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
436 action->
setData( QVariant::fromValue<ActionData>( ActionData( layer, mapLayerAction ) ) );
437 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
438 connect( action, SIGNAL(
triggered() ),
this, SLOT( triggerMapLayerAction() ) );
440 if ( separators.
contains( mapLayerAction ) )
447 void QgsIdentifyMenu::triggerMapLayerAction()
456 ActionData actData = action->
data().
value<ActionData>();
458 if ( actData.mIsValid && actData.mMapLayerAction )
463 actData.mMapLayerAction->triggerForLayer( actData.mLayer );
474 actData.mMapLayerAction->triggerForFeatures( actData.mLayer, featureList );
482 if ( result.
mFeature.
id() == actData.mFeatureId )
484 actData.mMapLayerAction->triggerForFeature( actData.mLayer,
new QgsFeature( result.
mFeature ) );
488 QgsDebugMsg(
QString(
"Identify menu: could not retrieve feature for action %1" ).arg( action->
text() ) );
498 externalAction =
false;
501 bool hasData =
false;
509 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (invalid data)" );
515 actData = action->
data().
value<ActionData>();
516 if ( actData.mIsValid )
518 externalAction = actData.mIsExternalAction;
528 externalAction =
true;
536 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (no data found)" );
541 if ( actData.mAllResults )
545 while ( it.hasNext() )
548 idResults << it.
value();
553 if ( !mLayerIdResults.
contains( actData.mLayer ) )
555 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (layer not found)" );
561 return mLayerIdResults[actData.mLayer];
568 if ( res.
mFeature.
id() == actData.mFeatureId )
576 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (don't know what happened')" );
580 void QgsIdentifyMenu::handleMenuHover()
611 mRubberBands.
append( hl );
616 void QgsIdentifyMenu::deleteRubberBands()
619 for ( ; it != mRubberBands.
constEnd(); ++it )
621 mRubberBands.
clear();
624 void QgsIdentifyMenu::layerDestroyed()
627 while ( it != mRubberBands.
end() )
629 if (( *it )->layer() ==
sender() )
632 it = mRubberBands.
erase( it );
643 mCustomActionRegistry.clear();
QgsFeatureId id() const
Get the feature ID for this feature.
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)
QgsMapLayer::LayerType type() const
Get the type of the layer.
bool contains(const Key &key) const
QString name() const
Get the display name of the layer.
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
void setFillColor(const QColor &fillColor)
Set polygons fill color.
QList< QgsMapLayerAction * > mapLayerActions(QgsMapLayer *layer, const QgsMapLayerAction::Targets &targets=QgsMapLayerAction::AllActions)
Returns the map layer actions which can run on the specified layer.
iterator erase(iterator pos)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
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.
const QString displayField() const
Returns the primary display field name used in the identify results dialog.
QGis::GeometryType geometryType() const
Returns point, line or polygon.
This class tracks map layer actions.
const Targets & targets() const
Return availibity of action.
void setData(const QVariant &userData)
bool contains(const T &value) const
static QgsMapLayerActionRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
void setColor(const QColor &color)
Set line/outline to color, polygon fill to color with alpha = 63.
const QgsGeometry * constGeometry() const
Gets a const pointer to the geometry object associated with this feature.
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.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
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)
char * toString(const QLatin1String &string)
void setMinWidth(double width)
Set minimum line / outline width in millimeters.