16 #include <QMouseEvent>
24 QgsIdentifyMenu::CustomActionRegistry::CustomActionRegistry(
QObject* parent )
33 , mAllowMultipleReturn( true )
34 , mExecWithSingleResult( false )
35 , mShowFeatureActions( false )
36 , mResultsIfExternalAction( false )
37 , mMaxLayerDisplay( 10 )
38 , mMaxFeatureDisplay( 10 )
39 , mDefaultActionName(
tr(
"Identify" ) )
40 , mCustomActionRegistry( CustomActionRegistry::instance() )
52 if ( maxLayerDisplay < 0 )
54 QgsDebugMsg(
"invalid value for number of layers displayed." );
62 if ( maxFeatureDisplay < 0 )
64 QgsDebugMsg(
"invalid value for number of layers displayed." );
73 mLayerIdResults.
clear();
77 if ( idResults.
count() == 0 )
81 if ( idResults.
count() == 1 && !mExecWithSingleResult )
83 returnResults << idResults[0];
91 if ( mLayerIdResults.
contains( layer ) )
93 mLayerIdResults[layer].append( result );
102 bool singleLayer = mLayerIdResults.
count() == 1;
107 if ( mMaxLayerDisplay != 0 && count > mMaxLayerDisplay )
114 addRasterLayer( layer );
121 addVectorLayer( vl, it.
value(), singleLayer );
126 if ( !singleLayer && mAllowMultipleReturn && idResults.
count() > 1 )
131 connect( allAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
138 returnResults = results( selectedAction, externalAction );
143 qDeleteAll( findChildren<QgsActionMenu*>() );
145 if ( externalAction && !mResultsIfExternalAction )
151 return returnResults;
161 void QgsIdentifyMenu::addRasterLayer(
QgsMapLayer* layer )
164 QMenu* layerMenu = 0;
168 int nCustomActions = layerActions.
count();
169 if ( nCustomActions )
171 separators.
append( layerActions[0] );
173 if ( mShowFeatureActions )
176 if ( layerActions.
count() > nCustomActions )
178 separators.
append( layerActions[nCustomActions] );
183 if ( !layerActions.
count() )
189 layerMenu =
new QMenu( layer->
name(), this );
195 layerAction->setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
196 connect( layerAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
204 QAction* identifyFeatureAction =
new QAction( mDefaultActionName, layerMenu );
205 connect( identifyFeatureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
206 identifyFeatureAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
207 layerMenu->
addAction( identifyFeatureAction );
213 action->
setData( QVariant::fromValue<ActionData>( ActionData( layer,
true ) ) );
214 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
215 connect( action, SIGNAL(
triggered() ),
this, SLOT( triggerMapLayerAction() ) );
217 if ( separators.
contains( mapLayerAction ) )
227 QMenu* layerMenu = 0;
235 int nCustomActions = layerActions.
count();
236 if ( nCustomActions )
238 separators << layerActions[0];
240 if ( mShowFeatureActions )
244 if ( layerActions.
count() > nCustomActions )
246 separators << layerActions[nCustomActions];
255 bool createMenu = results.
count() > 1 || layerActions.
count() > 0;
262 if ( !createMenu && mShowFeatureActions )
265 createMenu = featureActionMenu->
actions().count() > 0;
266 delete featureActionMenu;
286 layerMenu =
new QMenu( layer->
name(), this );
311 layerAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
312 connect( layerAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
324 if ( mMaxFeatureDisplay != 0 && count > mMaxFeatureDisplay )
329 QMenu* featureMenu = 0;
333 if ( mShowFeatureActions )
343 if ( !customFeatureActions.
count() && ( !featureActionMenu || !featureActionMenu->
actions().count() ) )
345 featureAction =
new QAction( featureTitle, layerMenu );
347 featureAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer, result.
mFeature.
id() ) ) );
348 connect( featureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
351 else if ( results.
count() == 1 )
356 featureMenu = layerMenu;
360 featureMenu =
new QMenu( featureTitle, layerMenu );
365 featureAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer, result.
mFeature.
id() ) ) );
366 connect( featureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
376 connect( identifyFeatureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
377 identifyFeatureAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer, result.
mFeature.
id() ) ) );
378 featureMenu->
addAction( identifyFeatureAction );
385 action->
setData( QVariant::fromValue<ActionData>( ActionData( layer, result.
mFeature.
id(), mapLayerAction ) ) );
386 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
387 connect( action, SIGNAL(
triggered() ),
this, SLOT( triggerMapLayerAction() ) );
391 if ( featureActionMenu )
395 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
404 if ( mAllowMultipleReturn && results.
count() > 1 )
408 allAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
409 connect( allAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
420 action->
setData( QVariant::fromValue<ActionData>( ActionData( layer, mapLayerAction ) ) );
421 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
422 connect( action, SIGNAL(
triggered() ),
this, SLOT( triggerMapLayerAction() ) );
424 if ( separators.
contains( mapLayerAction ) )
431 void QgsIdentifyMenu::triggerMapLayerAction()
440 ActionData actData = action->
data().
value<ActionData>();
442 if ( actData.mIsValid && actData.mMapLayerAction )
447 actData.mMapLayerAction->triggerForLayer( actData.mLayer );
458 actData.mMapLayerAction->triggerForFeatures( actData.mLayer, featureList );
466 if ( result.
mFeature.
id() == actData.mFeatureId )
468 actData.mMapLayerAction->triggerForFeature( actData.mLayer,
new QgsFeature( result.
mFeature ) );
472 QgsDebugMsg(
QString(
"Identify menu: could not retrieve feature for action %1" ).arg( action->
text() ) );
482 externalAction =
false;
485 bool hasData =
false;
493 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (invalid data)" );
499 actData = action->
data().
value<ActionData>();
500 if ( actData.mIsValid )
502 externalAction = actData.mIsExternalAction;
512 externalAction =
true;
520 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (no data found)" );
525 if ( actData.mAllResults )
529 while ( it.hasNext() )
532 idResults << it.
value();
537 if ( !mLayerIdResults.
contains( actData.mLayer ) )
539 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (layer not found)" );
545 return mLayerIdResults[actData.mLayer];
552 if ( res.
mFeature.
id() == actData.mFeatureId )
560 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (don't know what happened')" );
564 void QgsIdentifyMenu::handleMenuHover()
595 mRubberBands.
append( hl );
600 void QgsIdentifyMenu::deleteRubberBands()
603 for ( ; it != mRubberBands.
constEnd(); ++it )
605 mRubberBands.
clear();
608 void QgsIdentifyMenu::layerDestroyed()
611 while ( it != mRubberBands.
end() )
613 if (( *it )->layer() ==
sender() )
616 it = mRubberBands.
erase( it );
627 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
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
void setFillColor(const QColor &fillColor)
Set polygons fill color.
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.
const QString & name() const
Get the display name of the layer.
int count(const T &value) const
void append(const T &value)
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.
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.
char * toString(const T &value)
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)
void setMinWidth(double width)
Set minimum line / outline width in millimeters.