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;
276 featureTitle =
QString(
"%1" ).
arg( results[0].mFeature.id() );
277 layerAction =
new QAction(
QString(
"%1 (%2)" ).arg( layer->
name(), featureTitle ),
this );
289 if ( results.
count() > 1 )
291 layerMenu =
new QMenu( layer->
name(), this );
298 featureTitle =
QString(
"%1" ).
arg( results[0].mFeature.id() );
299 layerMenu =
new QMenu(
QString(
"%1 (%2)" ).arg( layer->
name(), featureTitle ),
this );
325 layerAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
326 connect( layerAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
338 if ( mMaxFeatureDisplay != 0 && count > mMaxFeatureDisplay )
343 QMenu* featureMenu = 0;
347 if ( mShowFeatureActions )
357 if ( !customFeatureActions.
count() && ( !featureActionMenu || !featureActionMenu->
actions().count() ) )
359 featureAction =
new QAction( featureTitle, layerMenu );
361 featureAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer, result.
mFeature.
id() ) ) );
362 connect( featureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
365 else if ( results.
count() == 1 )
370 featureMenu = layerMenu;
374 featureMenu =
new QMenu( featureTitle, layerMenu );
379 featureAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer, result.
mFeature.
id() ) ) );
380 connect( featureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
390 connect( identifyFeatureAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
391 identifyFeatureAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer, result.
mFeature.
id() ) ) );
392 featureMenu->
addAction( identifyFeatureAction );
399 action->
setData( QVariant::fromValue<ActionData>( ActionData( layer, result.
mFeature.
id(), mapLayerAction ) ) );
400 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
401 connect( action, SIGNAL(
triggered() ),
this, SLOT( triggerMapLayerAction() ) );
405 if ( featureActionMenu )
409 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
418 if ( mAllowMultipleReturn && results.
count() > 1 )
422 allAction->
setData( QVariant::fromValue<ActionData>( ActionData( layer ) ) );
423 connect( allAction, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
434 action->
setData( QVariant::fromValue<ActionData>( ActionData( layer, mapLayerAction ) ) );
435 connect( action, SIGNAL(
hovered() ),
this, SLOT( handleMenuHover() ) );
436 connect( action, SIGNAL(
triggered() ),
this, SLOT( triggerMapLayerAction() ) );
438 if ( separators.
contains( mapLayerAction ) )
445 void QgsIdentifyMenu::triggerMapLayerAction()
454 ActionData actData = action->
data().
value<ActionData>();
456 if ( actData.mIsValid && actData.mMapLayerAction )
461 actData.mMapLayerAction->triggerForLayer( actData.mLayer );
472 actData.mMapLayerAction->triggerForFeatures( actData.mLayer, featureList );
480 if ( result.
mFeature.
id() == actData.mFeatureId )
482 actData.mMapLayerAction->triggerForFeature( actData.mLayer,
new QgsFeature( result.
mFeature ) );
486 QgsDebugMsg(
QString(
"Identify menu: could not retrieve feature for action %1" ).arg( action->
text() ) );
496 externalAction =
false;
499 bool hasData =
false;
507 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (invalid data)" );
513 actData = action->
data().
value<ActionData>();
514 if ( actData.mIsValid )
516 externalAction = actData.mIsExternalAction;
526 externalAction =
true;
534 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (no data found)" );
539 if ( actData.mAllResults )
543 while ( it.hasNext() )
546 idResults << it.
value();
551 if ( !mLayerIdResults.
contains( actData.mLayer ) )
553 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (layer not found)" );
559 return mLayerIdResults[actData.mLayer];
566 if ( res.
mFeature.
id() == actData.mFeatureId )
574 QgsDebugMsg(
"Identify menu: could not retrieve results from menu entry (don't know what happened')" );
578 void QgsIdentifyMenu::handleMenuHover()
609 mRubberBands.
append( hl );
614 void QgsIdentifyMenu::deleteRubberBands()
617 for ( ; it != mRubberBands.
constEnd(); ++it )
619 mRubberBands.
clear();
622 void QgsIdentifyMenu::layerDestroyed()
625 while ( it != mRubberBands.
end() )
627 if (( *it )->layer() ==
sender() )
630 it = mRubberBands.
erase( it );
641 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.
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.
const QString & name() const
Get the display name of the layer.
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.