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 }