QGIS API Documentation  2.8.2-Wien
 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 }