|
QGIS API Documentation
master-6227475
|
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 "qgsvectorlayer.h" 00018 #include "qgsexpression.h" 00019 #include "qgslogger.h" 00020 00021 // Qt includes 00022 #include <QPoint> 00023 #include <QToolTip> 00024 #include <QSettings> 00025 00026 #include "qgsmaptip.h" 00027 00028 QgsMapTip::QgsMapTip() 00029 { 00030 // init the visible flag 00031 mMapTipVisible = false; 00032 } 00033 00034 QgsMapTip::~QgsMapTip() 00035 { 00036 00037 } 00038 00039 void QgsMapTip::showMapTip( QgsMapLayer *thepLayer, 00040 QgsPoint & theMapPosition, 00041 QPoint & thePixelPosition, 00042 QgsMapCanvas *thepMapCanvas ) 00043 { 00044 // Do the search using the active layer and the preferred label 00045 // field for the layer. The label field must be defined in the layer configuration 00046 // file/database. The code required to do this is similar to identify, except 00047 // we only want the first qualifying feature and we will only display the 00048 // field defined as the label field in the layer configuration file/database. 00049 // 00050 // TODO: Define the label (display) field for each map layer in the map configuration file/database 00051 00052 // Show the maptip on the canvas 00053 QString myTipText = fetchFeature( thepLayer, theMapPosition, thepMapCanvas ); 00054 mMapTipVisible = !myTipText.isEmpty(); 00055 00056 if ( mMapTipVisible ) 00057 { 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 } 00063 00064 void QgsMapTip::clear( QgsMapCanvas *mpMapCanvas ) 00065 { 00066 if ( !mMapTipVisible ) 00067 return; 00068 00069 // set the maptip to blank 00070 QToolTip::showText( mpMapCanvas->mapToGlobal( mLastPosition ), "", mpMapCanvas ); 00071 // reset the visible flag 00072 mMapTipVisible = false; 00073 } 00074 00075 QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint &mapPosition, QgsMapCanvas *mpMapCanvas ) 00076 { 00077 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer ); 00078 if ( !vlayer ) 00079 return ""; 00080 00081 // Get the setting for the search radius from user preferences, if it exists 00082 QSettings settings; 00083 double identifyValue = settings.value( "/Map/identifyRadius", QGis::DEFAULT_IDENTIFY_RADIUS ).toDouble(); 00084 00085 // create the search rectangle 00086 double searchRadius = mpMapCanvas->extent().width() * ( identifyValue / 100.0 ); 00087 00088 QgsRectangle r; 00089 r.setXMinimum( mapPosition.x() - searchRadius ); 00090 r.setYMinimum( mapPosition.y() - searchRadius ); 00091 r.setXMaximum( mapPosition.x() + searchRadius ); 00092 r.setYMaximum( mapPosition.y() + searchRadius ); 00093 00094 r = mpMapCanvas->mapRenderer()->mapToLayerCoordinates( layer, r ); 00095 00096 QgsFeature feature; 00097 00098 if ( !vlayer->getFeatures( QgsFeatureRequest().setFilterRect( r ).setFlags( QgsFeatureRequest::ExactIntersect ) ).nextFeature( feature ) ) 00099 return ""; 00100 00101 int idx = vlayer->fieldNameIndex( vlayer->displayField() ); 00102 if ( idx < 0 ) 00103 return QgsExpression::replaceExpressionText( vlayer->displayField(), feature, vlayer ); 00104 else 00105 return feature.attribute( idx ).toString(); 00106 }