QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsmaptip.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptips.cpp - Query a layer and show a maptip on the canvas
3  ---------------------
4  begin : October 2007
5  copyright : (C) 2007 by Gary Sherman
6  email : sherman @ mrcc dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 // QGIS includes
16 #include "qgsmapcanvas.h"
17 #include "qgsmaptool.h"
18 #include "qgsvectorlayer.h"
19 #include "qgsexpression.h"
20 #include "qgslogger.h"
21 
22 // Qt includes
23 #include <QPoint>
24 #include <QToolTip>
25 #include <QSettings>
26 
27 #include "qgsmaptip.h"
28 
30 {
31  // init the visible flag
32  mMapTipVisible = false;
33 }
34 
36 {
37 
38 }
39 
41  QgsPoint & theMapPosition,
42  QPoint & thePixelPosition,
43  QgsMapCanvas *thepMapCanvas )
44 {
45  // Do the search using the active layer and the preferred label
46  // field for the layer. The label field must be defined in the layer configuration
47  // file/database. The code required to do this is similar to identify, except
48  // we only want the first qualifying feature and we will only display the
49  // field defined as the label field in the layer configuration file/database.
50  //
51  // TODO: Define the label (display) field for each map layer in the map configuration file/database
52 
53  // Show the maptip on the canvas
54  QString myTipText = fetchFeature( thepLayer, theMapPosition, thepMapCanvas );
55  mMapTipVisible = !myTipText.isEmpty();
56 
57  if ( mMapTipVisible )
58  {
59  QToolTip::showText( thepMapCanvas->mapToGlobal( thePixelPosition ), myTipText, thepMapCanvas );
60  // store the point so we can use it to clear the maptip later
61  mLastPosition = thePixelPosition;
62  }
63 }
64 
65 void QgsMapTip::clear( QgsMapCanvas *mpMapCanvas )
66 {
67  if ( !mMapTipVisible )
68  return;
69 
70  // set the maptip to blank
71  QToolTip::showText( mpMapCanvas->mapToGlobal( mLastPosition ), "", mpMapCanvas );
72  // reset the visible flag
73  mMapTipVisible = false;
74 }
75 
76 QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint &mapPosition, QgsMapCanvas *mpMapCanvas )
77 {
78  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
79  if ( !vlayer )
80  return "";
81 
82  double searchRadius = QgsMapTool::searchRadiusMU( mpMapCanvas );
83 
84  QgsRectangle r;
85  r.setXMinimum( mapPosition.x() - searchRadius );
86  r.setYMinimum( mapPosition.y() - searchRadius );
87  r.setXMaximum( mapPosition.x() + searchRadius );
88  r.setYMaximum( mapPosition.y() + searchRadius );
89 
90  r = mpMapCanvas->mapSettings().mapToLayerCoordinates( layer, r );
91 
92  QgsFeature feature;
93 
94  if ( !vlayer->getFeatures( QgsFeatureRequest().setFilterRect( r ).setFlags( QgsFeatureRequest::ExactIntersect ) ).nextFeature( feature ) )
95  return "";
96 
97  int idx = vlayer->fieldNameIndex( vlayer->displayField() );
98  if ( idx < 0 )
99  return QgsExpression::replaceExpressionText( vlayer->displayField(), &feature, vlayer );
100  else
101  return feature.attribute( idx ).toString();
102 }
bool mMapTipVisible
Definition: qgsmaptip.h:65
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:48
static double searchRadiusMU(const QgsRenderContext &context)
Get search radius in map units for given context.
Definition: qgsmaptool.cpp:208
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:169
Use exact geometry intersection (slower) instead of bounding boxes.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
QgsPoint mapToLayerCoordinates(QgsMapLayer *theLayer, QgsPoint point) const
transform point coordinates from output CRS to layer's CRS
virtual ~QgsMapTip()
Destructor.
Definition: qgsmaptip.cpp:35
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:104
double x() const
Definition: qgspoint.h:110
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:174
void showMapTip(QgsMapLayer *thepLayer, QgsPoint &theMapPosition, QPoint &thePixelPosition, QgsMapCanvas *mpMapCanvas)
Show a maptip at a given point on the map canvas.
Definition: qgsmaptip.cpp:40
QString fetchFeature(QgsMapLayer *thepLayer, QgsPoint &theMapPosition, QgsMapCanvas *thepMapCanvas)
Definition: qgsmaptip.cpp:76
This class wraps a request for features to a vector layer (or directly its vector data provider)...
const QString displayField() const
Returns the primary display field name used in the identify results dialog.
A class to represent a point geometry.
Definition: qgspoint.h:63
void clear(QgsMapCanvas *mpMapCanvas)
Clear the current maptip if it exists.
Definition: qgsmaptip.cpp:65
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:230
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:179
double y() const
Definition: qgspoint.h:118
QgsMapTip()
Default constructor.
Definition: qgsmaptip.cpp:29
bool nextFeature(QgsFeature &f)
Represents a vector layer which manages a vector based data sets.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
static QString replaceExpressionText(const QString &action, const QgsFeature *feat, QgsVectorLayer *layer, const QMap< QString, QVariant > *substitutionMap=0)
This function currently replaces each expression between [% and %] in the string with the result of i...
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:164
QPoint mLastPosition
Definition: qgsmaptip.h:68