Quantum GIS API Documentation
1.7.4
|
00001 /*************************************************************************** 00002 qgsmaptips.cpp - Query a layer and show a maptip on the canvas 00003 --------------------- 00004 begin : October 2007 00005 copyright : (C) 2007 by Gary Sherman 00006 email : sherman @ mrcc dot com 00007 *************************************************************************** 00008 * * 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; either version 2 of the License, or * 00012 * (at your option) any later version. * 00013 * * 00014 ***************************************************************************/ 00015 // QGIS includes 00016 #include <qgsmapcanvas.h> 00017 #include <qgsmaplayer.h> 00018 #include <qgsvectordataprovider.h> 00019 #include <qgsvectorlayer.h> 00020 #include <qgsfield.h> 00021 00022 // Qt includes 00023 #include <QPoint> 00024 #include <QToolTip> 00025 #include <QSettings> 00026 00027 #include "qgsmaptip.h" 00028 00029 QgsMapTip::QgsMapTip() 00030 { 00031 // init the visible flag 00032 mMapTipVisible = false; 00033 } 00034 00035 QgsMapTip::~QgsMapTip() 00036 { 00037 00038 } 00039 00040 void QgsMapTip::showMapTip( QgsMapLayer * thepLayer, 00041 QgsPoint & theMapPosition, 00042 QPoint & thePixelPosition, 00043 QgsMapCanvas *thepMapCanvas ) 00044 { 00045 // Do the search using the active layer and the preferred label 00046 // field for the layer. The label field must be defined in the layer configuration 00047 // file/database. The code required to do this is similar to identify, except 00048 // we only want the first qualifying feature and we will only display the 00049 // field defined as the label field in the layer configuration file/database. 00050 // 00051 // TODO: Define the label (display) field for each map layer in the map configuration file/database 00052 00053 // Show the maptip on the canvas 00054 QString myTipText = fetchFeature( thepLayer, theMapPosition, thepMapCanvas ); 00055 if ( myTipText.length() > 0 ) 00056 { 00057 mMapTipVisible = true; 00058 QToolTip::showText( thepMapCanvas->mapToGlobal( thePixelPosition ), myTipText, thepMapCanvas ); 00059 // store the point so we can use it to clear the maptip later 00060 mLastPosition = thePixelPosition; 00061 } 00062 else 00063 { 00064 mMapTipVisible = false; 00065 } 00066 00067 } 00068 00069 void QgsMapTip::clear( QgsMapCanvas *mpMapCanvas ) 00070 { 00071 if ( mMapTipVisible ) 00072 { 00073 // set the maptip to blank 00074 QToolTip::showText( mpMapCanvas->mapToGlobal( mLastPosition ), "", mpMapCanvas ); 00075 // reset the visible flag 00076 mMapTipVisible = false; 00077 } 00078 } 00079 00080 QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint & mapPosition, QgsMapCanvas *mpMapCanvas ) 00081 { 00082 // Default return value 00083 QString maptipText = ""; 00084 // Protection just in case we get passed a null layer 00085 if ( layer ) 00086 { 00087 // Get the setting for the search radius from user preferences, if it exists 00088 QSettings settings; 00089 double identifyValue = settings.value( "/Map/identifyRadius", QGis::DEFAULT_IDENTIFY_RADIUS ).toDouble(); 00090 00091 // create the search rectangle 00092 double searchRadius = mpMapCanvas->extent().width() * ( identifyValue / 100.0 ); 00093 QgsRectangle r; 00094 r.setXMinimum( mapPosition.x() - searchRadius ); 00095 r.setXMaximum( mapPosition.x() + searchRadius ); 00096 r.setYMinimum( mapPosition.y() - searchRadius ); 00097 r.setYMaximum( mapPosition.y() + searchRadius ); 00098 00099 // Get the data provider 00100 QgsVectorDataProvider* dataProvider = qobject_cast<QgsVectorLayer *>( layer )->dataProvider(); 00101 // Fetch the attribute list for the layer 00102 QgsAttributeList allAttributes = dataProvider->attributeIndexes(); 00103 // Select all attributes within the search radius 00104 dataProvider->select( allAttributes, r, true, true ); 00105 // Feature to hold the results of the fetch 00106 QgsFeature feature; 00107 // Get the field list for the layer 00108 const QgsFieldMap& fields = dataProvider->fields(); 00109 // Get the label (display) field for the layer 00110 QString fieldIndex = qobject_cast<QgsVectorLayer *>( layer )->displayField(); 00111 if ( dataProvider->nextFeature( feature ) ) 00112 { 00113 // if we get a feature, pull out the display field and set the maptip text to its value 00114 QgsAttributeMap attributes = feature.attributeMap(); 00115 for ( QgsAttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it ) 00116 { 00117 00118 if ( fields[it.key()].name() == fieldIndex ) 00119 { 00120 maptipText = it->toString(); 00121 00122 } 00123 00124 } 00125 } 00126 } 00127 // return the map tip 00128 return maptipText; 00129 } 00130