Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 <qgsmaplayer.h>
18 #include <qgsvectordataprovider.h>
19 #include <qgsvectorlayer.h>
20 #include <qgsfield.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  if ( myTipText.length() > 0 )
56  {
57  mMapTipVisible = true;
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  else
63  {
64  mMapTipVisible = false;
65  }
66 
67 }
68 
69 void QgsMapTip::clear( QgsMapCanvas *mpMapCanvas )
70 {
71  if ( mMapTipVisible )
72  {
73  // set the maptip to blank
74  QToolTip::showText( mpMapCanvas->mapToGlobal( mLastPosition ), "", mpMapCanvas );
75  // reset the visible flag
76  mMapTipVisible = false;
77  }
78 }
79 
80 QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint & mapPosition, QgsMapCanvas *mpMapCanvas )
81 {
82  // Default return value
83  QString maptipText = "";
84  // Protection just in case we get passed a null layer
85  if ( layer )
86  {
87  // Get the setting for the search radius from user preferences, if it exists
88  QSettings settings;
89  double identifyValue = settings.value( "/Map/identifyRadius", QGis::DEFAULT_IDENTIFY_RADIUS ).toDouble();
90 
91  // create the search rectangle
92  double searchRadius = mpMapCanvas->extent().width() * ( identifyValue / 100.0 );
93  QgsRectangle r;
94  r.setXMinimum( mapPosition.x() - searchRadius );
95  r.setXMaximum( mapPosition.x() + searchRadius );
96  r.setYMinimum( mapPosition.y() - searchRadius );
97  r.setYMaximum( mapPosition.y() + searchRadius );
98 
99  // Get the data provider
100  QgsVectorDataProvider* dataProvider = qobject_cast<QgsVectorLayer *>( layer )->dataProvider();
101  // Fetch the attribute list for the layer
102  QgsAttributeList allAttributes = dataProvider->attributeIndexes();
103  // Select all attributes within the search radius
104  dataProvider->select( allAttributes, r, true, true );
105  // Feature to hold the results of the fetch
106  QgsFeature feature;
107  // Get the field list for the layer
108  const QgsFieldMap& fields = dataProvider->fields();
109  // Get the label (display) field for the layer
110  QString fieldIndex = qobject_cast<QgsVectorLayer *>( layer )->displayField();
111  if ( dataProvider->nextFeature( feature ) )
112  {
113  // if we get a feature, pull out the display field and set the maptip text to its value
114  QgsAttributeMap attributes = feature.attributeMap();
115  for ( QgsAttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
116  {
117 
118  if ( fields[it.key()].name() == fieldIndex )
119  {
120  maptipText = it->toString();
121 
122  }
123 
124  }
125  }
126  }
127  // return the map tip
128  return maptipText;
129 }
130