QGIS API Documentation  2.2.0-Valmiera
 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 "qgsvectorlayer.h"
18 #include "qgsexpression.h"
19 #include "qgslogger.h"
20 
21 // Qt includes
22 #include <QPoint>
23 #include <QToolTip>
24 #include <QSettings>
25 
26 #include "qgsmaptip.h"
27 
29 {
30  // init the visible flag
31  mMapTipVisible = false;
32 }
33 
35 {
36 
37 }
38 
40  QgsPoint & theMapPosition,
41  QPoint & thePixelPosition,
42  QgsMapCanvas *thepMapCanvas )
43 {
44  // Do the search using the active layer and the preferred label
45  // field for the layer. The label field must be defined in the layer configuration
46  // file/database. The code required to do this is similar to identify, except
47  // we only want the first qualifying feature and we will only display the
48  // field defined as the label field in the layer configuration file/database.
49  //
50  // TODO: Define the label (display) field for each map layer in the map configuration file/database
51 
52  // Show the maptip on the canvas
53  QString myTipText = fetchFeature( thepLayer, theMapPosition, thepMapCanvas );
54  mMapTipVisible = !myTipText.isEmpty();
55 
56  if ( mMapTipVisible )
57  {
58  QToolTip::showText( thepMapCanvas->mapToGlobal( thePixelPosition ), myTipText, thepMapCanvas );
59  // store the point so we can use it to clear the maptip later
60  mLastPosition = thePixelPosition;
61  }
62 }
63 
64 void QgsMapTip::clear( QgsMapCanvas *mpMapCanvas )
65 {
66  if ( !mMapTipVisible )
67  return;
68 
69  // set the maptip to blank
70  QToolTip::showText( mpMapCanvas->mapToGlobal( mLastPosition ), "", mpMapCanvas );
71  // reset the visible flag
72  mMapTipVisible = false;
73 }
74 
75 QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint &mapPosition, QgsMapCanvas *mpMapCanvas )
76 {
77  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
78  if ( !vlayer )
79  return "";
80 
81  // Get the setting for the search radius from user preferences, if it exists
82  QSettings settings;
83  double identifyValue = settings.value( "/Map/identifyRadius", QGis::DEFAULT_IDENTIFY_RADIUS ).toDouble();
84 
85  // create the search rectangle
86  double searchRadius = mpMapCanvas->extent().width() * ( identifyValue / 100.0 );
87 
88  QgsRectangle r;
89  r.setXMinimum( mapPosition.x() - searchRadius );
90  r.setYMinimum( mapPosition.y() - searchRadius );
91  r.setXMaximum( mapPosition.x() + searchRadius );
92  r.setYMaximum( mapPosition.y() + searchRadius );
93 
94  r = mpMapCanvas->mapRenderer()->mapToLayerCoordinates( layer, r );
95 
96  QgsFeature feature;
97 
98  if ( !vlayer->getFeatures( QgsFeatureRequest().setFilterRect( r ).setFlags( QgsFeatureRequest::ExactIntersect ) ).nextFeature( feature ) )
99  return "";
100 
101  int idx = vlayer->fieldNameIndex( vlayer->displayField() );
102  if ( idx < 0 )
103  return QgsExpression::replaceExpressionText( vlayer->displayField(), &feature, vlayer );
104  else
105  return feature.attribute( idx ).toString();
106 }
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:45
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:164
Use exact geometry intersection (slower) instead of bounding boxes.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
static const double DEFAULT_IDENTIFY_RADIUS
Definition: qgis.h:267
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:114
QgsPoint mapToLayerCoordinates(QgsMapLayer *theLayer, QgsPoint point)
transform point coordinates from output CRS to layer's CRS
virtual ~QgsMapTip()
Destructor.
Definition: qgsmaptip.cpp:34
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:99
double x() const
Definition: qgspoint.h:110
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:169
void showMapTip(QgsMapLayer *thepLayer, QgsPoint &theMapPosition, QPoint &thePixelPosition, QgsMapCanvas *mpMapCanvas)
Show a maptip at a given point on the map canvas.
Definition: qgsmaptip.cpp:39
QString fetchFeature(QgsMapLayer *thepLayer, QgsPoint &theMapPosition, QgsMapCanvas *thepMapCanvas)
Definition: qgsmaptip.cpp:75
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:64
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:231
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:174
QgsMapRenderer * mapRenderer()
double y() const
Definition: qgspoint.h:118
QgsRectangle extent() const
Returns the current zoom exent of the map canvas.
QgsMapTip()
Default constructor.
Definition: qgsmaptip.cpp:28
bool nextFeature(QgsFeature &f)
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:199
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:159
QPoint mLastPosition
Definition: qgsmaptip.h:68